Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
正统之独孤求败
mindspore
提交
35c3a637
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,发现更多精彩内容 >>
提交
35c3a637
编写于
7月 14, 2020
作者:
A
avakh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support cpp invert operation
上级
60927ef1
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
284 addition
and
11 deletion
+284
-11
mindspore/ccsrc/minddata/dataset/api/python_bindings.cc
mindspore/ccsrc/minddata/dataset/api/python_bindings.cc
+5
-0
mindspore/ccsrc/minddata/dataset/kernels/image/CMakeLists.txt
...spore/ccsrc/minddata/dataset/kernels/image/CMakeLists.txt
+1
-0
mindspore/ccsrc/minddata/dataset/kernels/image/invert_op.cc
mindspore/ccsrc/minddata/dataset/kernels/image/invert_op.cc
+57
-0
mindspore/ccsrc/minddata/dataset/kernels/image/invert_op.h
mindspore/ccsrc/minddata/dataset/kernels/image/invert_op.h
+44
-0
mindspore/ccsrc/minddata/dataset/kernels/tensor_op.h
mindspore/ccsrc/minddata/dataset/kernels/tensor_op.h
+1
-0
mindspore/dataset/transforms/vision/c_transforms.py
mindspore/dataset/transforms/vision/c_transforms.py
+7
-0
tests/ut/data/dataset/golden/invert_01_result_c.npz
tests/ut/data/dataset/golden/invert_01_result_c.npz
+0
-0
tests/ut/data/dataset/golden/invert_01_result_py.npz
tests/ut/data/dataset/golden/invert_01_result_py.npz
+0
-0
tests/ut/python/dataset/test_invert.py
tests/ut/python/dataset/test_invert.py
+169
-11
未找到文件。
mindspore/ccsrc/minddata/dataset/api/python_bindings.cc
浏览文件 @
35c3a637
...
...
@@ -54,6 +54,7 @@
#include "minddata/dataset/kernels/image/decode_op.h"
#include "minddata/dataset/kernels/image/hwc_to_chw_op.h"
#include "minddata/dataset/kernels/image/image_utils.h"
#include "minddata/dataset/kernels/image/invert_op.h"
#include "minddata/dataset/kernels/image/normalize_op.h"
#include "minddata/dataset/kernels/image/pad_op.h"
#include "minddata/dataset/kernels/image/random_color_adjust_op.h"
...
...
@@ -362,6 +363,10 @@ void bindTensorOps1(py::module *m) {
.
def
(
py
::
init
<
float
,
float
,
float
,
float
,
float
,
float
>
(),
py
::
arg
(
"meanR"
),
py
::
arg
(
"meanG"
),
py
::
arg
(
"meanB"
),
py
::
arg
(
"stdR"
),
py
::
arg
(
"stdG"
),
py
::
arg
(
"stdB"
));
(
void
)
py
::
class_
<
InvertOp
,
TensorOp
,
std
::
shared_ptr
<
InvertOp
>>
(
*
m
,
"InvertOp"
,
"Tensor operation to apply invert on RGB images."
)
.
def
(
py
::
init
<>
());
(
void
)
py
::
class_
<
RescaleOp
,
TensorOp
,
std
::
shared_ptr
<
RescaleOp
>>
(
*
m
,
"RescaleOp"
,
"Tensor operation to rescale an image. Takes scale and shift."
)
.
def
(
py
::
init
<
float
,
float
>
(),
py
::
arg
(
"rescale"
),
py
::
arg
(
"shift"
));
...
...
mindspore/ccsrc/minddata/dataset/kernels/image/CMakeLists.txt
浏览文件 @
35c3a637
...
...
@@ -6,6 +6,7 @@ add_library(kernels-image OBJECT
decode_op.cc
hwc_to_chw_op.cc
image_utils.cc
invert_op.cc
normalize_op.cc
pad_op.cc
random_color_adjust_op.cc
...
...
mindspore/ccsrc/minddata/dataset/kernels/image/invert_op.cc
0 → 100644
浏览文件 @
35c3a637
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "minddata/dataset/kernels/image/invert_op.h"
#include "minddata/dataset/kernels/image/image_utils.h"
#include "minddata/dataset/core/cv_tensor.h"
#include "minddata/dataset/util/status.h"
namespace
mindspore
{
namespace
dataset
{
// only supports RGB images
Status
InvertOp
::
Compute
(
const
std
::
shared_ptr
<
Tensor
>
&
input
,
std
::
shared_ptr
<
Tensor
>
*
output
)
{
IO_CHECK
(
input
,
output
);
try
{
std
::
shared_ptr
<
CVTensor
>
input_cv
=
CVTensor
::
AsCVTensor
(
input
);
cv
::
Mat
input_img
=
input_cv
->
mat
();
if
(
!
input_cv
->
mat
().
data
)
{
RETURN_STATUS_UNEXPECTED
(
"Could not convert to CV Tensor"
);
}
if
(
input_cv
->
Rank
()
!=
3
)
{
RETURN_STATUS_UNEXPECTED
(
"Shape not <H,W,C>"
);
}
int
num_channels
=
input_cv
->
shape
()[
2
];
if
(
num_channels
!=
3
)
{
RETURN_STATUS_UNEXPECTED
(
"The shape is incorrect: num of channels != 3"
);
}
auto
output_cv
=
std
::
make_shared
<
CVTensor
>
(
input_cv
->
shape
(),
input_cv
->
type
());
RETURN_UNEXPECTED_IF_NULL
(
output_cv
);
output_cv
->
mat
()
=
cv
::
Scalar
::
all
(
255
)
-
input_img
;
*
output
=
std
::
static_pointer_cast
<
Tensor
>
(
output_cv
);
}
catch
(
const
cv
::
Exception
&
e
)
{
RETURN_STATUS_UNEXPECTED
(
"Error in invert"
);
}
return
Status
::
OK
();
}
}
// namespace dataset
}
// namespace mindspore
mindspore/ccsrc/minddata/dataset/kernels/image/invert_op.h
0 → 100644
浏览文件 @
35c3a637
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DATASET_KERNELS_IMAGE_INVERT_OP_H
#define DATASET_KERNELS_IMAGE_INVERT_OP_H
#include <memory>
#include <string>
#include "minddata/dataset/core/tensor.h"
#include "minddata/dataset/kernels/tensor_op.h"
#include "minddata/dataset/util/status.h"
namespace
mindspore
{
namespace
dataset
{
class
InvertOp
:
public
TensorOp
{
public:
InvertOp
()
{}
~
InvertOp
()
=
default
;
// Description: A function that prints info about the node
void
Print
(
std
::
ostream
&
out
)
const
override
{
out
<<
Name
();
}
Status
Compute
(
const
std
::
shared_ptr
<
Tensor
>
&
input
,
std
::
shared_ptr
<
Tensor
>
*
output
)
override
;
std
::
string
Name
()
const
override
{
return
kInvertOp
;
}
};
}
// namespace dataset
}
// namespace mindspore
#endif // DATASET_KERNELS_IMAGE_INVERT_OP_H
mindspore/ccsrc/minddata/dataset/kernels/tensor_op.h
浏览文件 @
35c3a637
...
...
@@ -92,6 +92,7 @@ constexpr char kDecodeOp[] = "DecodeOp";
constexpr
char
kCenterCropOp
[]
=
"CenterCropOp"
;
constexpr
char
kCutOutOp
[]
=
"CutOutOp"
;
constexpr
char
kHwcToChwOp
[]
=
"HwcToChwOp"
;
constexpr
char
kInvertOp
[]
=
"InvertOp"
;
constexpr
char
kNormalizeOp
[]
=
"NormalizeOp"
;
constexpr
char
kPadOp
[]
=
"PadOp"
;
constexpr
char
kRandomColorAdjustOp
[]
=
"RandomColorAdjustOp"
;
...
...
mindspore/dataset/transforms/vision/c_transforms.py
浏览文件 @
35c3a637
...
...
@@ -71,6 +71,13 @@ def parse_padding(padding):
return
padding
class
Invert
(
cde
.
InvertOp
):
"""
Apply invert on input image in RGB mode.
does not have input arguments.
"""
class
Decode
(
cde
.
DecodeOp
):
"""
Decode the input image in RGB mode.
...
...
tests/ut/data/dataset/golden/invert_01_result_c.npz
0 → 100644
浏览文件 @
35c3a637
文件已添加
tests/ut/data/dataset/golden/invert_01_result.npz
→
tests/ut/data/dataset/golden/invert_01_result
_py
.npz
浏览文件 @
35c3a637
文件已移动
tests/ut/python/dataset/test_invert.py
浏览文件 @
35c3a637
...
...
@@ -19,18 +19,20 @@ import numpy as np
import
mindspore.dataset.engine
as
de
import
mindspore.dataset.transforms.vision.py_transforms
as
F
import
mindspore.dataset.transforms.vision.c_transforms
as
C
from
mindspore
import
log
as
logger
from
util
import
visualize_list
,
save_and_check_md5
from
util
import
visualize_list
,
save_and_check_md5
,
diff_mse
DATA_DIR
=
"../data/dataset/testImageNetData/train/"
GENERATE_GOLDEN
=
False
def
test_invert
(
plot
=
False
):
def
test_invert_py
(
plot
=
False
):
"""
Test Invert
Test Invert
python op
"""
logger
.
info
(
"Test Invert"
)
logger
.
info
(
"Test Invert
Python op
"
)
# Original Images
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
...
...
@@ -52,7 +54,7 @@ def test_invert(plot=False):
np
.
transpose
(
image
,
(
0
,
2
,
3
,
1
)),
axis
=
0
)
# Color Inverted Images
# Color Inverted Images
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
transforms_invert
=
F
.
ComposeOp
([
F
.
Decode
(),
...
...
@@ -83,11 +85,143 @@ def test_invert(plot=False):
visualize_list
(
images_original
,
images_invert
)
def
test_invert_md5
():
def
test_invert_c
(
plot
=
False
):
"""
Test Invert Cpp op
"""
logger
.
info
(
"Test Invert cpp op"
)
# Original Images
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
transforms_original
=
[
C
.
Decode
(),
C
.
Resize
(
size
=
[
224
,
224
])]
ds_original
=
ds
.
map
(
input_columns
=
"image"
,
operations
=
transforms_original
)
ds_original
=
ds_original
.
batch
(
512
)
for
idx
,
(
image
,
_
)
in
enumerate
(
ds_original
):
if
idx
==
0
:
images_original
=
image
else
:
images_original
=
np
.
append
(
images_original
,
image
,
axis
=
0
)
# Invert Images
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
transform_invert
=
[
C
.
Decode
(),
C
.
Resize
(
size
=
[
224
,
224
]),
C
.
Invert
()]
ds_invert
=
ds
.
map
(
input_columns
=
"image"
,
operations
=
transform_invert
)
ds_invert
=
ds_invert
.
batch
(
512
)
for
idx
,
(
image
,
_
)
in
enumerate
(
ds_invert
):
if
idx
==
0
:
images_invert
=
image
else
:
images_invert
=
np
.
append
(
images_invert
,
image
,
axis
=
0
)
if
plot
:
visualize_list
(
images_original
,
images_invert
)
num_samples
=
images_original
.
shape
[
0
]
mse
=
np
.
zeros
(
num_samples
)
for
i
in
range
(
num_samples
):
mse
[
i
]
=
diff_mse
(
images_invert
[
i
],
images_original
[
i
])
logger
.
info
(
"MSE= {}"
.
format
(
str
(
np
.
mean
(
mse
))))
def
test_invert_py_c
(
plot
=
False
):
"""
Test Invert Cpp op and python op
"""
logger
.
info
(
"Test Invert cpp and python op"
)
# Invert Images in cpp
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
ds
=
ds
.
map
(
input_columns
=
[
"image"
],
operations
=
[
C
.
Decode
(),
C
.
Resize
((
224
,
224
))])
ds_c_invert
=
ds
.
map
(
input_columns
=
"image"
,
operations
=
C
.
Invert
())
ds_c_invert
=
ds_c_invert
.
batch
(
512
)
for
idx
,
(
image
,
_
)
in
enumerate
(
ds_c_invert
):
if
idx
==
0
:
images_c_invert
=
image
else
:
images_c_invert
=
np
.
append
(
images_c_invert
,
image
,
axis
=
0
)
# invert images in python
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
ds
=
ds
.
map
(
input_columns
=
[
"image"
],
operations
=
[
C
.
Decode
(),
C
.
Resize
((
224
,
224
))])
transforms_p_invert
=
F
.
ComposeOp
([
lambda
img
:
img
.
astype
(
np
.
uint8
),
F
.
ToPIL
(),
F
.
Invert
(),
np
.
array
])
ds_p_invert
=
ds
.
map
(
input_columns
=
"image"
,
operations
=
transforms_p_invert
())
ds_p_invert
=
ds_p_invert
.
batch
(
512
)
for
idx
,
(
image
,
_
)
in
enumerate
(
ds_p_invert
):
if
idx
==
0
:
images_p_invert
=
image
else
:
images_p_invert
=
np
.
append
(
images_p_invert
,
image
,
axis
=
0
)
num_samples
=
images_c_invert
.
shape
[
0
]
mse
=
np
.
zeros
(
num_samples
)
for
i
in
range
(
num_samples
):
mse
[
i
]
=
diff_mse
(
images_p_invert
[
i
],
images_c_invert
[
i
])
logger
.
info
(
"MSE= {}"
.
format
(
str
(
np
.
mean
(
mse
))))
if
plot
:
visualize_list
(
images_c_invert
,
images_p_invert
,
visualize_mode
=
2
)
def
test_invert_one_channel
():
"""
Test Invert with md5 check
Test Invert cpp op with one channel image
"""
logger
.
info
(
"Test Invert C Op With One Channel Images"
)
c_op
=
C
.
Invert
()
try
:
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
ds
=
ds
.
map
(
input_columns
=
[
"image"
],
operations
=
[
C
.
Decode
(),
C
.
Resize
((
224
,
224
)),
lambda
img
:
np
.
array
(
img
[:,
:,
0
])])
ds
.
map
(
input_columns
=
"image"
,
operations
=
c_op
)
except
RuntimeError
as
e
:
logger
.
info
(
"Got an exception in DE: {}"
.
format
(
str
(
e
)))
assert
"The shape"
in
str
(
e
)
def
test_invert_md5_py
():
"""
logger
.
info
(
"Test Invert with md5 check"
)
Test Invert python op with md5 check
"""
logger
.
info
(
"Test Invert python op with md5 check"
)
# Generate dataset
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
...
...
@@ -98,10 +232,34 @@ def test_invert_md5():
data
=
ds
.
map
(
input_columns
=
"image"
,
operations
=
transforms_invert
())
# Compare with expected md5 from images
filename
=
"invert_01_result.npz"
filename
=
"invert_01_result_py.npz"
save_and_check_md5
(
data
,
filename
,
generate_golden
=
GENERATE_GOLDEN
)
def
test_invert_md5_c
():
"""
Test Invert cpp op with md5 check
"""
logger
.
info
(
"Test Invert cpp op with md5 check"
)
# Generate dataset
ds
=
de
.
ImageFolderDatasetV2
(
dataset_dir
=
DATA_DIR
,
shuffle
=
False
)
transforms_invert
=
[
C
.
Decode
(),
C
.
Resize
(
size
=
[
224
,
224
]),
C
.
Invert
(),
F
.
ToTensor
()]
data
=
ds
.
map
(
input_columns
=
"image"
,
operations
=
transforms_invert
)
# Compare with expected md5 from images
filename
=
"invert_01_result_c.npz"
save_and_check_md5
(
data
,
filename
,
generate_golden
=
GENERATE_GOLDEN
)
if
__name__
==
"__main__"
:
test_invert
(
plot
=
True
)
test_invert_md5
()
test_invert_py
(
plot
=
False
)
test_invert_c
(
plot
=
False
)
test_invert_py_c
(
plot
=
False
)
test_invert_one_channel
()
test_invert_md5_py
()
test_invert_md5_c
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录