Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
11caa3ae
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
11caa3ae
编写于
6月 19, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
6月 19, 2020
浏览文件
操作
浏览文件
下载
差异文件
!2340 fix random_crop_resize_2
Merge pull request !2340 from panfengfeng/fix_random_crop_resize_2
上级
91c856e5
25827a86
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
140 addition
and
31 deletion
+140
-31
example/resnet50_imagenet2012/dataset.py
example/resnet50_imagenet2012/dataset.py
+1
-1
example/resnet50_imagenet2012_THOR/dataset_imagenet.py
example/resnet50_imagenet2012_THOR/dataset_imagenet.py
+1
-1
mindspore/ccsrc/dataset/kernels/image/random_crop_and_resize_op.cc
.../ccsrc/dataset/kernels/image/random_crop_and_resize_op.cc
+33
-21
mindspore/ccsrc/dataset/kernels/image/random_crop_and_resize_op.h
...e/ccsrc/dataset/kernels/image/random_crop_and_resize_op.h
+2
-0
tests/ut/cpp/dataset/random_crop_and_resize_op_test.cc
tests/ut/cpp/dataset/random_crop_and_resize_op_test.cc
+48
-5
tests/ut/data/dataset/golden/random_crop_and_resize_01_c_result.npz
...ata/dataset/golden/random_crop_and_resize_01_c_result.npz
+0
-0
tests/ut/data/dataset/golden/random_crop_and_resize_01_py_result.npz
...ta/dataset/golden/random_crop_and_resize_01_py_result.npz
+0
-0
tests/ut/data/dataset/golden/random_crop_and_resize_02_c_result.npz
...ata/dataset/golden/random_crop_and_resize_02_c_result.npz
+0
-0
tests/ut/data/dataset/golden/random_crop_and_resize_03_c_result.npz
...ata/dataset/golden/random_crop_and_resize_03_c_result.npz
+0
-0
tests/ut/python/dataset/test_random_crop_and_resize.py
tests/ut/python/dataset/test_random_crop_and_resize.py
+55
-3
未找到文件。
example/resnet50_imagenet2012/dataset.py
浏览文件 @
11caa3ae
...
...
@@ -65,7 +65,7 @@ def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32, target="
else
:
trans
=
[
C
.
Decode
(),
C
.
Resize
(
(
256
,
256
)
),
C
.
Resize
(
256
),
C
.
CenterCrop
(
image_size
),
C
.
Normalize
(
mean
=
mean
,
std
=
std
),
C
.
HWC2CHW
()
...
...
example/resnet50_imagenet2012_THOR/dataset_imagenet.py
浏览文件 @
11caa3ae
...
...
@@ -57,7 +57,7 @@ def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32):
else
:
transform_img
=
[
V_C
.
Decode
(),
V_C
.
Resize
(
(
256
,
256
)
),
V_C
.
Resize
(
256
),
V_C
.
CenterCrop
(
image_size
),
V_C
.
Normalize
(
mean
=
mean
,
std
=
std
),
V_C
.
HWC2CHW
()
...
...
mindspore/ccsrc/dataset/kernels/image/random_crop_and_resize_op.cc
浏览文件 @
11caa3ae
...
...
@@ -35,8 +35,10 @@ RandomCropAndResizeOp::RandomCropAndResizeOp(int32_t target_height, int32_t targ
:
target_height_
(
target_height
),
target_width_
(
target_width
),
rnd_scale_
(
scale_lb
,
scale_ub
),
rnd_aspect_
(
aspect_lb
,
aspect_ub
),
rnd_aspect_
(
log
(
aspect_lb
),
log
(
aspect_ub
)
),
interpolation_
(
interpolation
),
aspect_lb_
(
aspect_lb
),
aspect_ub_
(
aspect_ub
),
max_iter_
(
max_iter
)
{
rnd_
.
seed
(
GetSeed
());
}
...
...
@@ -63,34 +65,44 @@ Status RandomCropAndResizeOp::OutputShape(const std::vector<TensorShape> &inputs
if
(
!
outputs
.
empty
())
return
Status
::
OK
();
return
Status
(
StatusCode
::
kUnexpectedError
,
"Input has a wrong shape"
);
}
Status
RandomCropAndResizeOp
::
GetCropBox
(
int
h_in
,
int
w_in
,
int
*
x
,
int
*
y
,
int
*
crop_height
,
int
*
crop_width
)
{
double
scale
,
aspect
;
*
crop_width
=
w_in
;
*
crop_height
=
h_in
;
bool
crop_success
=
false
;
CHECK_FAIL_RETURN_UNEXPECTED
(
w_in
!=
0
,
"Width is 0"
);
CHECK_FAIL_RETURN_UNEXPECTED
(
h_in
!=
0
,
"Height is 0"
);
CHECK_FAIL_RETURN_UNEXPECTED
(
aspect_lb_
>
0
,
"Aspect lower bound must be greater than zero"
);
for
(
int32_t
i
=
0
;
i
<
max_iter_
;
i
++
)
{
scale
=
rnd_scale_
(
rnd_
);
aspect
=
rnd_aspect_
(
rnd_
);
*
crop_width
=
static_cast
<
int32_t
>
(
std
::
round
(
std
::
sqrt
(
h_in
*
w_in
*
scale
/
aspect
)));
*
crop_height
=
static_cast
<
int32_t
>
(
std
::
round
(
*
crop_width
*
aspect
));
double
const
sample_scale
=
rnd_scale_
(
rnd_
);
// In case of non-symmetrical aspect ratios, use uniform distribution on a logarithmic sample_scale.
// Note rnd_aspect_ is already a random distribution of the input aspect ratio in logarithmic sample_scale.
double
const
sample_aspect
=
exp
(
rnd_aspect_
(
rnd_
));
*
crop_width
=
static_cast
<
int32_t
>
(
std
::
round
(
std
::
sqrt
(
h_in
*
w_in
*
sample_scale
*
sample_aspect
)));
*
crop_height
=
static_cast
<
int32_t
>
(
std
::
round
(
*
crop_width
/
sample_aspect
));
if
(
*
crop_width
<=
w_in
&&
*
crop_height
<=
h_in
)
{
crop_success
=
true
;
break
;
std
::
uniform_int_distribution
<>
rd_x
(
0
,
w_in
-
*
crop_width
);
std
::
uniform_int_distribution
<>
rd_y
(
0
,
h_in
-
*
crop_height
);
*
x
=
rd_x
(
rnd_
);
*
y
=
rd_y
(
rnd_
);
return
Status
::
OK
();
}
}
if
(
!
crop_success
)
{
CHECK_FAIL_RETURN_UNEXPECTED
(
w_in
!=
0
,
"Width is 0"
);
aspect
=
static_cast
<
double
>
(
h_in
)
/
w_in
;
scale
=
rnd_scale_
(
rnd_
);
*
crop_width
=
static_cast
<
int32_t
>
(
std
::
round
(
std
::
sqrt
(
h_in
*
w_in
*
scale
/
aspect
)));
*
crop_height
=
static_cast
<
int32_t
>
(
std
::
round
(
*
crop_width
*
aspect
));
*
crop_height
=
(
*
crop_height
>
h_in
)
?
h_in
:
*
crop_height
;
*
crop_width
=
(
*
crop_width
>
w_in
)
?
w_in
:
*
crop_width
;
double
const
img_aspect
=
static_cast
<
double
>
(
w_in
)
/
h_in
;
if
(
img_aspect
<
aspect_lb_
)
{
*
crop_width
=
w_in
;
*
crop_height
=
static_cast
<
int32_t
>
(
std
::
round
(
*
crop_width
/
static_cast
<
double
>
(
aspect_lb_
)));
}
else
{
if
(
img_aspect
>
aspect_ub_
)
{
*
crop_height
=
h_in
;
*
crop_width
=
static_cast
<
int32_t
>
(
std
::
round
(
*
crop_height
*
static_cast
<
double
>
(
aspect_ub_
)));
}
else
{
*
crop_width
=
w_in
;
*
crop_height
=
h_in
;
}
}
std
::
uniform_int_distribution
<>
rd_x
(
0
,
w_in
-
*
crop_width
);
std
::
uniform_int_distribution
<>
rd_y
(
0
,
h_in
-
*
crop_height
);
*
x
=
rd_x
(
rnd_
);
*
y
=
rd_y
(
rnd_
);
*
x
=
static_cast
<
int32_t
>
(
std
::
round
((
w_in
-
*
crop_width
)
/
2.0
));
*
y
=
static_cast
<
int32_t
>
(
std
::
round
((
h_in
-
*
crop_height
)
/
2.0
));
return
Status
::
OK
();
}
}
// namespace dataset
...
...
mindspore/ccsrc/dataset/kernels/image/random_crop_and_resize_op.h
浏览文件 @
11caa3ae
...
...
@@ -60,6 +60,8 @@ class RandomCropAndResizeOp : public TensorOp {
std
::
mt19937
rnd_
;
InterpolationMode
interpolation_
;
int32_t
max_iter_
;
double
aspect_lb_
;
double
aspect_ub_
;
};
}
// namespace dataset
}
// namespace mindspore
...
...
tests/ut/cpp/dataset/random_crop_and_resize_op_test.cc
浏览文件 @
11caa3ae
...
...
@@ -28,17 +28,38 @@ class MindDataTestRandomCropAndResizeOp : public UT::CVOP::CVOpCommon {
public:
MindDataTestRandomCropAndResizeOp
()
:
CVOpCommon
()
{}
};
TEST_F
(
MindDataTestRandomCropAndResizeOp
,
TestOpSimpleTest1
)
{
MS_LOG
(
INFO
)
<<
" starting RandomCropAndResizeOp simple test"
;
TensorShape
s_in
=
input_tensor_
->
shape
();
std
::
shared_ptr
<
Tensor
>
output_tensor
;
int
h_out
=
1024
;
int
w_out
=
2048
;
float
aspect_lb
=
2
;
float
aspect_ub
=
2.5
;
float
scale_lb
=
0.2
;
float
scale_ub
=
2.0
;
TEST_F
(
MindDataTestRandomCropAndResizeOp
,
TestOpSimpleTest
)
{
TensorShape
s_out
({
h_out
,
w_out
,
s_in
[
2
]});
auto
op
=
std
::
make_unique
<
RandomCropAndResizeOp
>
(
h_out
,
w_out
,
scale_lb
,
scale_ub
,
aspect_lb
,
aspect_ub
);
Status
s
;
for
(
auto
i
=
0
;
i
<
100
;
i
++
)
{
s
=
op
->
Compute
(
input_tensor_
,
&
output_tensor
);
EXPECT_TRUE
(
s
.
IsOk
());
}
MS_LOG
(
INFO
)
<<
"RandomCropAndResizeOp simple test finished"
;
}
TEST_F
(
MindDataTestRandomCropAndResizeOp
,
TestOpSimpleTest2
)
{
MS_LOG
(
INFO
)
<<
" starting RandomCropAndResizeOp simple test"
;
TensorShape
s_in
=
input_tensor_
->
shape
();
std
::
shared_ptr
<
Tensor
>
output_tensor
;
int
h_out
=
1024
;
int
w_out
=
2048
;
float
aspect_lb
=
0.2
;
float
aspect_ub
=
5
;
float
scale_lb
=
0.
0001
;
float
scale_ub
=
1
.0
;
float
aspect_lb
=
1
;
float
aspect_ub
=
1.
5
;
float
scale_lb
=
0.
2
;
float
scale_ub
=
2
.0
;
TensorShape
s_out
({
h_out
,
w_out
,
s_in
[
2
]});
...
...
@@ -51,3 +72,25 @@ TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest) {
MS_LOG
(
INFO
)
<<
"RandomCropAndResizeOp simple test finished"
;
}
TEST_F
(
MindDataTestRandomCropAndResizeOp
,
TestOpSimpleTest3
)
{
MS_LOG
(
INFO
)
<<
" starting RandomCropAndResizeOp simple test"
;
TensorShape
s_in
=
input_tensor_
->
shape
();
std
::
shared_ptr
<
Tensor
>
output_tensor
;
int
h_out
=
1024
;
int
w_out
=
2048
;
float
aspect_lb
=
0.2
;
float
aspect_ub
=
3
;
float
scale_lb
=
0.2
;
float
scale_ub
=
2.0
;
TensorShape
s_out
({
h_out
,
w_out
,
s_in
[
2
]});
auto
op
=
std
::
make_unique
<
RandomCropAndResizeOp
>
(
h_out
,
w_out
,
scale_lb
,
scale_ub
,
aspect_lb
,
aspect_ub
);
Status
s
;
for
(
auto
i
=
0
;
i
<
100
;
i
++
)
{
s
=
op
->
Compute
(
input_tensor_
,
&
output_tensor
);
EXPECT_TRUE
(
s
.
IsOk
());
}
MS_LOG
(
INFO
)
<<
"RandomCropAndResizeOp simple test finished"
;
}
\ No newline at end of file
tests/ut/data/dataset/golden/random_crop_and_resize_01_c_result.npz
浏览文件 @
11caa3ae
无法预览此类型文件
tests/ut/data/dataset/golden/random_crop_and_resize_01_py_result.npz
浏览文件 @
11caa3ae
无法预览此类型文件
tests/ut/data/dataset/golden/random_crop_and_resize_02_c_result.npz
浏览文件 @
11caa3ae
无法预览此类型文件
tests/ut/data/dataset/golden/random_crop_and_resize_03_c_result.npz
浏览文件 @
11caa3ae
无法预览此类型文件
tests/ut/python/dataset/test_random_crop_and_resize.py
浏览文件 @
11caa3ae
...
...
@@ -39,7 +39,8 @@ def test_random_crop_and_resize_op(plot=False):
# First dataset
data1
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
decode_op
=
c_vision
.
Decode
()
random_crop_and_resize_op
=
c_vision
.
RandomResizedCrop
((
256
,
512
),
(
1
,
1
),
(
0.5
,
0.5
))
# With these inputs we expect the code to crop the whole image
random_crop_and_resize_op
=
c_vision
.
RandomResizedCrop
((
256
,
512
),
(
2
,
2
),
(
1
,
3
))
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
decode_op
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
random_crop_and_resize_op
)
...
...
@@ -63,6 +64,49 @@ def test_random_crop_and_resize_op(plot=False):
if
plot
:
visualize
(
original_images
,
crop_and_resize_images
)
def
test_random_crop_and_resize_op_py
(
plot
=
False
):
"""
Test RandomCropAndResize op in py transforms
"""
logger
.
info
(
"test_random_crop_and_resize_op_py"
)
# First dataset
data1
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
# With these inputs we expect the code to crop the whole image
transforms1
=
[
py_vision
.
Decode
(),
py_vision
.
RandomResizedCrop
((
256
,
512
),
(
2
,
2
),
(
1
,
3
)),
py_vision
.
ToTensor
()
]
transform1
=
py_vision
.
ComposeOp
(
transforms1
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
transform1
())
# Second dataset
# Second dataset for comparison
data2
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
transforms2
=
[
py_vision
.
Decode
(),
py_vision
.
ToTensor
()
]
transform2
=
py_vision
.
ComposeOp
(
transforms2
)
data2
=
data2
.
map
(
input_columns
=
[
"image"
],
operations
=
transform2
())
num_iter
=
0
crop_and_resize_images
=
[]
original_images
=
[]
for
item1
,
item2
in
zip
(
data1
.
create_dict_iterator
(),
data2
.
create_dict_iterator
()):
crop_and_resize
=
(
item1
[
"image"
].
transpose
(
1
,
2
,
0
)
*
255
).
astype
(
np
.
uint8
)
original
=
(
item2
[
"image"
].
transpose
(
1
,
2
,
0
)
*
255
).
astype
(
np
.
uint8
)
original
=
cv2
.
resize
(
original
,
(
512
,
256
))
mse
=
diff_mse
(
crop_and_resize
,
original
)
# Due to rounding error the mse for Python is not exactly 0
assert
mse
<=
0.05
logger
.
info
(
"random_crop_and_resize_op_{}, mse: {}"
.
format
(
num_iter
+
1
,
mse
))
num_iter
+=
1
crop_and_resize_images
.
append
(
crop_and_resize
)
original_images
.
append
(
original
)
if
plot
:
visualize
(
original_images
,
crop_and_resize_images
)
def
test_random_crop_and_resize_01
():
"""
Test RandomCropAndResize with md5 check, expected to pass
...
...
@@ -74,7 +118,7 @@ def test_random_crop_and_resize_01():
# First dataset
data1
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
decode_op
=
c_vision
.
Decode
()
random_crop_and_resize_op
=
c_vision
.
RandomResizedCrop
((
256
,
512
),
(
0.5
,
1
),
(
0.5
,
1
))
random_crop_and_resize_op
=
c_vision
.
RandomResizedCrop
((
256
,
512
),
(
0.5
,
0.5
),
(
1
,
1
))
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
decode_op
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
random_crop_and_resize_op
)
...
...
@@ -82,7 +126,7 @@ def test_random_crop_and_resize_01():
data2
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
transforms
=
[
py_vision
.
Decode
(),
py_vision
.
RandomResizedCrop
((
256
,
512
),
(
0.5
,
1
),
(
0.5
,
1
)),
py_vision
.
RandomResizedCrop
((
256
,
512
),
(
0.5
,
0.5
),
(
1
,
1
)),
py_vision
.
ToTensor
()
]
transform
=
py_vision
.
ComposeOp
(
transforms
)
...
...
@@ -93,6 +137,7 @@ def test_random_crop_and_resize_01():
save_and_check_md5
(
data1
,
filename1
,
generate_golden
=
GENERATE_GOLDEN
)
save_and_check_md5
(
data2
,
filename2
,
generate_golden
=
GENERATE_GOLDEN
)
def
test_random_crop_and_resize_02
():
"""
Test RandomCropAndResize with md5 check:Image interpolation mode is Inter.NEAREST,
...
...
@@ -124,6 +169,7 @@ def test_random_crop_and_resize_02():
save_and_check_md5
(
data1
,
filename1
,
generate_golden
=
GENERATE_GOLDEN
)
save_and_check_md5
(
data2
,
filename2
,
generate_golden
=
GENERATE_GOLDEN
)
def
test_random_crop_and_resize_03
():
"""
Test RandomCropAndResize with md5 check: max_attempts is 1, expected to pass
...
...
@@ -154,6 +200,7 @@ def test_random_crop_and_resize_03():
save_and_check_md5
(
data1
,
filename1
,
generate_golden
=
GENERATE_GOLDEN
)
save_and_check_md5
(
data2
,
filename2
,
generate_golden
=
GENERATE_GOLDEN
)
def
test_random_crop_and_resize_04_c
():
"""
Test RandomCropAndResize with c_tranforms: invalid range of scale (max<min),
...
...
@@ -179,6 +226,7 @@ def test_random_crop_and_resize_04_c():
logger
.
info
(
"Got an exception in DE: {}"
.
format
(
str
(
e
)))
assert
"Input range is not valid"
in
str
(
e
)
def
test_random_crop_and_resize_04_py
():
"""
Test RandomCropAndResize with py_transforms: invalid range of scale (max<min),
...
...
@@ -207,6 +255,7 @@ def test_random_crop_and_resize_04_py():
logger
.
info
(
"Got an exception in DE: {}"
.
format
(
str
(
e
)))
assert
"Input range is not valid"
in
str
(
e
)
def
test_random_crop_and_resize_05_c
():
"""
Test RandomCropAndResize with c_transforms: invalid range of ratio (max<min),
...
...
@@ -232,6 +281,7 @@ def test_random_crop_and_resize_05_c():
logger
.
info
(
"Got an exception in DE: {}"
.
format
(
str
(
e
)))
assert
"Input range is not valid"
in
str
(
e
)
def
test_random_crop_and_resize_05_py
():
"""
Test RandomCropAndResize with py_transforms: invalid range of ratio (max<min),
...
...
@@ -260,6 +310,7 @@ def test_random_crop_and_resize_05_py():
logger
.
info
(
"Got an exception in DE: {}"
.
format
(
str
(
e
)))
assert
"Input range is not valid"
in
str
(
e
)
def
test_random_crop_and_resize_comp
(
plot
=
False
):
"""
Test RandomCropAndResize and compare between python and c image augmentation
...
...
@@ -293,6 +344,7 @@ def test_random_crop_and_resize_comp(plot=False):
if
plot
:
visualize
(
image_c_cropped
,
image_py_cropped
)
if
__name__
==
"__main__"
:
test_random_crop_and_resize_op
(
True
)
test_random_crop_and_resize_01
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录