Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
正统之独孤求败
mindspore
提交
0f0548f2
M
mindspore
项目概览
正统之独孤求败
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
0
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0f0548f2
编写于
5月 06, 2020
作者:
E
eric
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added test case for grayscale support
上级
fb4b16a5
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
144 addition
and
55 deletion
+144
-55
mindspore/ccsrc/dataset/kernels/image/image_utils.cc
mindspore/ccsrc/dataset/kernels/image/image_utils.cc
+8
-3
tests/ut/python/dataset/test_center_crop.py
tests/ut/python/dataset/test_center_crop.py
+34
-1
tests/ut/python/dataset/test_pad.py
tests/ut/python/dataset/test_pad.py
+51
-27
tests/ut/python/dataset/test_random_color_adjust.py
tests/ut/python/dataset/test_random_color_adjust.py
+51
-24
未找到文件。
mindspore/ccsrc/dataset/kernels/image/image_utils.cc
浏览文件 @
0f0548f2
...
...
@@ -370,6 +370,11 @@ Status HwcToChw(std::shared_ptr<Tensor> input, std::shared_ptr<Tensor> *output)
if
(
!
input_cv
->
mat
().
data
)
{
RETURN_STATUS_UNEXPECTED
(
"Could not convert to CV Tensor"
);
}
if
(
input_cv
->
Rank
()
==
2
)
{
// If input tensor is 2D, we assume we have hw dimensions
*
output
=
input
;
return
Status
::
OK
();
}
if
(
input_cv
->
shape
().
Size
()
!=
3
&&
input_cv
->
shape
()[
2
]
!=
3
)
{
RETURN_STATUS_UNEXPECTED
(
"The shape is incorrect: number of channels is not equal 3"
);
}
...
...
@@ -395,9 +400,6 @@ Status HwcToChw(std::shared_ptr<Tensor> input, std::shared_ptr<Tensor> *output)
Status
SwapRedAndBlue
(
std
::
shared_ptr
<
Tensor
>
input
,
std
::
shared_ptr
<
Tensor
>
*
output
)
{
try
{
std
::
shared_ptr
<
CVTensor
>
input_cv
=
CVTensor
::
AsCVTensor
(
std
::
move
(
input
));
if
(
!
input_cv
->
mat
().
data
)
{
RETURN_STATUS_UNEXPECTED
(
"Could not convert to CV Tensor"
);
}
if
(
input_cv
->
shape
().
Size
()
!=
3
&&
input_cv
->
shape
()[
2
]
!=
3
)
{
RETURN_STATUS_UNEXPECTED
(
"The shape is incorrect: number of channels is not equal 3"
);
}
...
...
@@ -714,7 +716,10 @@ Status Pad(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output
}
std
::
shared_ptr
<
CVTensor
>
output_cv
=
std
::
make_shared
<
CVTensor
>
(
out_image
);
RETURN_UNEXPECTED_IF_NULL
(
output_cv
);
// pad the dimension if shape information is only 2 dimensional, this is grayscale
if
(
input_cv
->
Rank
()
==
3
&&
input_cv
->
shape
()[
2
]
==
1
&&
output_cv
->
Rank
()
==
2
)
output_cv
->
ExpandDim
(
2
);
*
output
=
std
::
static_pointer_cast
<
Tensor
>
(
output_cv
);
return
Status
::
OK
();
}
catch
(
const
cv
::
Exception
&
e
)
{
RETURN_STATUS_UNEXPECTED
(
"Unexpected error in pad"
);
...
...
tests/ut/python/dataset/test_center_crop.py
浏览文件 @
0f0548f2
...
...
@@ -108,9 +108,42 @@ def test_center_crop_comp(height=375, width=375, plot=False):
visualize
(
image
,
image_cropped
)
def
test_crop_grayscale
(
height
=
375
,
width
=
375
):
"""
Test that centercrop works with pad and grayscale images
"""
def
channel_swap
(
image
):
"""
Py func hack for our pytransforms to work with c transforms
"""
return
(
image
.
transpose
(
1
,
2
,
0
)
*
255
).
astype
(
np
.
uint8
)
transforms
=
[
py_vision
.
Decode
(),
py_vision
.
Grayscale
(
1
),
py_vision
.
ToTensor
(),
(
lambda
image
:
channel_swap
(
image
))
]
transform
=
py_vision
.
ComposeOp
(
transforms
)
data1
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
transform
())
# if input is grayscale, the output dimensions should be single channel
crop_gray
=
vision
.
CenterCrop
([
height
,
width
])
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
crop_gray
)
for
item1
in
data1
.
create_dict_iterator
():
c_image
=
item1
[
"image"
]
# check that the image is grayscale
assert
(
len
(
c_image
.
shape
)
==
3
and
c_image
.
shape
[
2
]
==
1
)
if
__name__
==
"__main__"
:
test_center_crop_op
(
600
,
600
)
test_center_crop_op
(
300
,
600
)
test_center_crop_op
(
600
,
300
)
test_center_crop_md5
(
600
,
600
)
test_center_crop_md5
()
test_center_crop_comp
()
test_crop_grayscale
()
tests/ut/python/dataset/test_pad.py
浏览文件 @
0f0548f2
...
...
@@ -22,34 +22,11 @@ import numpy as np
import
mindspore.dataset
as
ds
import
mindspore.dataset.transforms.vision.py_transforms
as
py_vision
from
mindspore
import
log
as
logger
from
util
import
diff_mse
DATA_DIR
=
[
"../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"
]
SCHEMA_DIR
=
"../data/dataset/test_tf_file_3_images/datasetSchema.json"
def
visualize
(
first
,
mse
,
second
):
"""
visualizes the image using DE op and enCV
"""
plt
.
subplot
(
141
)
plt
.
imshow
(
first
)
plt
.
title
(
"c transformed image"
)
plt
.
subplot
(
142
)
plt
.
imshow
(
second
)
plt
.
title
(
"py random_color_jitter image"
)
plt
.
subplot
(
143
)
plt
.
imshow
(
first
-
second
)
plt
.
title
(
"Difference image, mse : {}"
.
format
(
mse
))
plt
.
show
()
def
diff_mse
(
in1
,
in2
):
mse
=
(
np
.
square
(
in1
.
astype
(
float
)
/
255
-
in2
.
astype
(
float
)
/
255
)).
mean
()
return
mse
*
100
def
test_pad_op
():
"""
Test Pad op
...
...
@@ -77,9 +54,7 @@ def test_pad_op():
data2
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
data2
=
data2
.
map
(
input_columns
=
[
"image"
],
operations
=
transform
())
num_iter
=
0
for
item1
,
item2
in
zip
(
data1
.
create_dict_iterator
(),
data2
.
create_dict_iterator
()):
num_iter
+=
1
c_image
=
item1
[
"image"
]
py_image
=
(
item2
[
"image"
].
transpose
(
1
,
2
,
0
)
*
255
).
astype
(
np
.
uint8
)
...
...
@@ -89,11 +64,60 @@ def test_pad_op():
logger
.
info
(
"dtype of c_image: {}"
.
format
(
c_image
.
dtype
))
logger
.
info
(
"dtype of py_image: {}"
.
format
(
py_image
.
dtype
))
diff
=
c_image
-
py_image
mse
=
diff_mse
(
c_image
,
py_image
)
logger
.
info
(
"mse is {}"
.
format
(
mse
))
assert
mse
<
0.01
def
test_pad_grayscale
():
"""
Tests that the pad works for grayscale images
"""
def
channel_swap
(
image
):
"""
Py func hack for our pytransforms to work with c transforms
"""
return
(
image
.
transpose
(
1
,
2
,
0
)
*
255
).
astype
(
np
.
uint8
)
transforms
=
[
py_vision
.
Decode
(),
py_vision
.
Grayscale
(
1
),
py_vision
.
ToTensor
(),
(
lambda
image
:
channel_swap
(
image
))
]
transform
=
py_vision
.
ComposeOp
(
transforms
)
data1
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
transform
())
# if input is grayscale, the output dimensions should be single channel
pad_gray
=
c_vision
.
Pad
(
100
,
fill_value
=
(
20
,
20
,
20
))
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
pad_gray
)
dataset_shape_1
=
[]
for
item1
in
data1
.
create_dict_iterator
():
c_image
=
item1
[
"image"
]
dataset_shape_1
.
append
(
c_image
.
shape
)
# Dataset for comparison
data2
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
decode_op
=
c_vision
.
Decode
()
# we use the same padding logic
ctrans
=
[
decode_op
,
pad_gray
]
dataset_shape_2
=
[]
data2
=
data2
.
map
(
input_columns
=
[
"image"
],
operations
=
ctrans
)
for
item2
in
data2
.
create_dict_iterator
():
c_image
=
item2
[
"image"
]
dataset_shape_2
.
append
(
c_image
.
shape
)
for
shape1
,
shape2
in
zip
(
dataset_shape_1
,
dataset_shape_2
):
# validate that the first two dimensions are the same
# we have a little inconsistency here because the third dimension is 1 after py_vision.Grayscale
assert
(
shape1
[
0
:
1
]
==
shape2
[
0
:
1
])
if
__name__
==
"__main__"
:
test_pad_op
()
test_pad_grayscale
()
tests/ut/python/dataset/test_random_color_adjust.py
浏览文件 @
0f0548f2
...
...
@@ -22,6 +22,7 @@ import numpy as np
import
mindspore.dataset
as
ds
import
mindspore.dataset.transforms.vision.py_transforms
as
py_vision
from
mindspore
import
log
as
logger
from
util
import
diff_mse
DATA_DIR
=
[
"../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"
]
SCHEMA_DIR
=
"../data/dataset/test_tf_file_3_images/datasetSchema.json"
...
...
@@ -29,7 +30,7 @@ SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
def
visualize
(
first
,
mse
,
second
):
"""
visualizes the image using DE op and enCV
visualizes the image using DE op and
Op
enCV
"""
plt
.
subplot
(
141
)
plt
.
imshow
(
first
)
...
...
@@ -45,12 +46,7 @@ def visualize(first, mse, second):
plt
.
show
()
def
diff_mse
(
in1
,
in2
):
mse
=
(
np
.
square
(
in1
.
astype
(
float
)
/
255
-
in2
.
astype
(
float
)
/
255
)).
mean
()
return
mse
*
100
def
test_random_color_adjust_op_brightness
():
def
test_random_color_adjust_op_brightness
(
plot
=
False
):
"""
Test RandomColorAdjust op
"""
...
...
@@ -92,15 +88,16 @@ def test_random_color_adjust_op_brightness():
mse
=
diff_mse
(
c_image
,
py_image
)
logger
.
info
(
"mse is {}"
.
format
(
mse
))
logger
.
info
(
"random_rotation_op_{}, mse: {}"
.
format
(
num_iter
+
1
,
mse
))
assert
mse
<
0.01
# logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse))
# if mse != 0:
# logger.info("mse is: {}".format(mse))
# Uncomment below line if you want to visualize images
#
visualize(c_image, mse, py_image)
if
plot
:
visualize
(
c_image
,
mse
,
py_image
)
def
test_random_color_adjust_op_contrast
():
def
test_random_color_adjust_op_contrast
(
plot
=
False
):
"""
Test RandomColorAdjust op
"""
...
...
@@ -139,11 +136,10 @@ def test_random_color_adjust_op_contrast():
logger
.
info
(
"dtype of c_image: {}"
.
format
(
c_image
.
dtype
))
logger
.
info
(
"dtype of py_image: {}"
.
format
(
py_image
.
dtype
))
diff
=
c_image
-
py_image
logger
.
info
(
"contrast difference c is : {}"
.
format
(
c_image
[
0
][
0
]))
logger
.
info
(
"contrast difference py is : {}"
.
format
(
py_image
[
0
][
0
]))
diff
=
c_image
-
py_image
logger
.
info
(
"contrast difference is : {}"
.
format
(
diff
[
0
][
0
]))
# mse = (np.sum(np.power(diff, 2))) / (c_image.shape[0] * c_image.shape[1])
mse
=
diff_mse
(
c_image
,
py_image
)
...
...
@@ -152,11 +148,11 @@ def test_random_color_adjust_op_contrast():
# logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse))
# if mse != 0:
# logger.info("mse is: {}".format(mse))
# Uncomment below line if you want to visualize images
#
visualize(c_image, mse, py_image)
if
plot
:
visualize
(
c_image
,
mse
,
py_image
)
def
test_random_color_adjust_op_saturation
():
def
test_random_color_adjust_op_saturation
(
plot
=
False
):
"""
Test RandomColorAdjust op
"""
...
...
@@ -197,19 +193,17 @@ def test_random_color_adjust_op_saturation():
logger
.
info
(
"dtype of c_image: {}"
.
format
(
c_image
.
dtype
))
logger
.
info
(
"dtype of py_image: {}"
.
format
(
py_image
.
dtype
))
diff
=
c_image
-
py_image
mse
=
diff_mse
(
c_image
,
py_image
)
logger
.
info
(
"mse is {}"
.
format
(
mse
))
assert
mse
<
0.01
# logger.info("random_rotation_op_{}, mse: {}".format(num_iter + 1, mse))
# if mse != 0:
# logger.info("mse is: {}".format(mse))
# Uncomment below line if you want to visualize images
#
visualize(c_image, mse, py_image)
if
plot
:
visualize
(
c_image
,
mse
,
py_image
)
def
test_random_color_adjust_op_hue
():
def
test_random_color_adjust_op_hue
(
plot
=
False
):
"""
Test RandomColorAdjust op
"""
...
...
@@ -251,13 +245,45 @@ def test_random_color_adjust_op_hue():
logger
.
info
(
"dtype of py_image: {}"
.
format
(
py_image
.
dtype
))
# logger.info("dtype of img: {}".format(img.dtype))
diff
=
c_image
-
py_image
# mse = (np.sum(np.power(diff, 2))) / (c_image.shape[0] * c_image.shape[1])
mse
=
diff_mse
(
c_image
,
py_image
)
logger
.
info
(
"mse is {}"
.
format
(
mse
))
assert
mse
<
0.01
# Uncomment below line if you want to visualize images
# visualize(c_image, mse, py_image)
if
plot
:
visualize
(
c_image
,
mse
,
py_image
)
def
test_random_color_adjust_grayscale
():
"""
Tests that the random color adjust works for grayscale images
"""
def
channel_swap
(
image
):
"""
Py func hack for our pytransforms to work with c transforms
"""
return
(
image
.
transpose
(
1
,
2
,
0
)
*
255
).
astype
(
np
.
uint8
)
transforms
=
[
py_vision
.
Decode
(),
py_vision
.
Grayscale
(
1
),
py_vision
.
ToTensor
(),
(
lambda
image
:
channel_swap
(
image
))
]
transform
=
py_vision
.
ComposeOp
(
transforms
)
data1
=
ds
.
TFRecordDataset
(
DATA_DIR
,
SCHEMA_DIR
,
columns_list
=
[
"image"
],
shuffle
=
False
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
transform
())
# if input is grayscale, the output dimensions should be single channel, the following should fail
random_adjust_op
=
c_vision
.
RandomColorAdjust
((
1
,
1
),
(
1
,
1
),
(
1
,
1
),
(
0.2
,
0.2
))
try
:
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
random_adjust_op
)
dataset_shape_1
=
[]
for
item1
in
data1
.
create_dict_iterator
():
c_image
=
item1
[
"image"
]
dataset_shape_1
.
append
(
c_image
.
shape
)
except
BaseException
as
e
:
logger
.
info
(
"Got an exception in DE: {}"
.
format
(
str
(
e
)))
if
__name__
==
"__main__"
:
...
...
@@ -265,3 +291,4 @@ if __name__ == "__main__":
test_random_color_adjust_op_contrast
()
test_random_color_adjust_op_saturation
()
test_random_color_adjust_op_hue
()
test_random_color_adjust_grayscale
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录