Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
a2c38d89
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看板
提交
a2c38d89
编写于
8月 24, 2020
作者:
M
Mahdi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added float32 support for CutMixBatch
上级
a117b6dc
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
42 addition
and
17 deletion
+42
-17
mindspore/ccsrc/minddata/dataset/kernels/image/cutmix_batch_op.cc
...e/ccsrc/minddata/dataset/kernels/image/cutmix_batch_op.cc
+3
-3
mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.cc
...spore/ccsrc/minddata/dataset/kernels/image/image_utils.cc
+21
-9
mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.h
mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.h
+11
-0
mindspore/ccsrc/minddata/dataset/kernels/image/mixup_batch_op.cc
...re/ccsrc/minddata/dataset/kernels/image/mixup_batch_op.cc
+4
-4
tests/ut/python/dataset/test_cutmix_batch_op.py
tests/ut/python/dataset/test_cutmix_batch_op.py
+3
-1
未找到文件。
mindspore/ccsrc/minddata/dataset/kernels/image/cutmix_batch_op.cc
浏览文件 @
a2c38d89
...
...
@@ -50,7 +50,7 @@ void CutMixBatchOp::GetCropBox(int height, int width, float lam, int *x, int *y,
Status
CutMixBatchOp
::
Compute
(
const
TensorRow
&
input
,
TensorRow
*
output
)
{
if
(
input
.
size
()
<
2
)
{
RETURN_STATUS_UNEXPECTED
(
"Both images and labels columns are required for this operation"
);
RETURN_STATUS_UNEXPECTED
(
"Both images and labels columns are required for this operation
.
"
);
}
std
::
vector
<
std
::
shared_ptr
<
Tensor
>>
images
;
...
...
@@ -59,10 +59,10 @@ Status CutMixBatchOp::Compute(const TensorRow &input, TensorRow *output) {
// Check inputs
if
(
image_shape
.
size
()
!=
4
||
image_shape
[
0
]
!=
label_shape
[
0
])
{
RETURN_STATUS_UNEXPECTED
(
"You must make sure images are HWC or CHW and batch before calling CutMixBatch."
);
RETURN_STATUS_UNEXPECTED
(
"You must make sure images are HWC or CHW and batch
ed
before calling CutMixBatch."
);
}
if
(
label_shape
.
size
()
!=
2
)
{
RETURN_STATUS_UNEXPECTED
(
"CutMixBatch: Label's must be in one-hot format and in a batch"
);
RETURN_STATUS_UNEXPECTED
(
"CutMixBatch: Label's must be in one-hot format and in a batch
.
"
);
}
if
((
image_shape
[
1
]
!=
1
&&
image_shape
[
1
]
!=
3
)
&&
image_batch_format_
==
ImageBatchFormat
::
kNCHW
)
{
RETURN_STATUS_UNEXPECTED
(
"CutMixBatch: Image doesn't match the given image format."
);
...
...
mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.cc
浏览文件 @
a2c38d89
...
...
@@ -415,9 +415,7 @@ Status MaskWithTensor(const std::shared_ptr<Tensor> &sub_mat, std::shared_ptr<Te
for
(
int
i
=
0
;
i
<
crop_width
;
i
++
)
{
for
(
int
j
=
0
;
j
<
crop_height
;
j
++
)
{
for
(
int
c
=
0
;
c
<
number_of_channels
;
c
++
)
{
uint8_t
pixel_value
;
RETURN_IF_NOT_OK
(
sub_mat
->
GetItemAt
(
&
pixel_value
,
{
j
,
i
,
c
}));
RETURN_IF_NOT_OK
((
*
input
)
->
SetItemAt
({
y
+
j
,
x
+
i
,
c
},
pixel_value
));
RETURN_IF_NOT_OK
(
CopyTensorValue
(
sub_mat
,
input
,
{
j
,
i
,
c
},
{
y
+
j
,
x
+
i
,
c
}));
}
}
}
...
...
@@ -432,9 +430,7 @@ Status MaskWithTensor(const std::shared_ptr<Tensor> &sub_mat, std::shared_ptr<Te
for
(
int
i
=
0
;
i
<
crop_width
;
i
++
)
{
for
(
int
j
=
0
;
j
<
crop_height
;
j
++
)
{
for
(
int
c
=
0
;
c
<
number_of_channels
;
c
++
)
{
uint8_t
pixel_value
;
RETURN_IF_NOT_OK
(
sub_mat
->
GetItemAt
(
&
pixel_value
,
{
c
,
j
,
i
}));
RETURN_IF_NOT_OK
((
*
input
)
->
SetItemAt
({
c
,
y
+
j
,
x
+
i
},
pixel_value
));
RETURN_IF_NOT_OK
(
CopyTensorValue
(
sub_mat
,
input
,
{
c
,
j
,
i
},
{
c
,
y
+
j
,
x
+
i
}));
}
}
}
...
...
@@ -447,9 +443,7 @@ Status MaskWithTensor(const std::shared_ptr<Tensor> &sub_mat, std::shared_ptr<Te
}
for
(
int
i
=
0
;
i
<
crop_width
;
i
++
)
{
for
(
int
j
=
0
;
j
<
crop_height
;
j
++
)
{
uint8_t
pixel_value
;
RETURN_IF_NOT_OK
(
sub_mat
->
GetItemAt
(
&
pixel_value
,
{
j
,
i
}));
RETURN_IF_NOT_OK
((
*
input
)
->
SetItemAt
({
y
+
j
,
x
+
i
},
pixel_value
));
RETURN_IF_NOT_OK
(
CopyTensorValue
(
sub_mat
,
input
,
{
j
,
i
},
{
y
+
j
,
x
+
i
}));
}
}
}
else
{
...
...
@@ -458,6 +452,24 @@ Status MaskWithTensor(const std::shared_ptr<Tensor> &sub_mat, std::shared_ptr<Te
return
Status
::
OK
();
}
Status
CopyTensorValue
(
const
std
::
shared_ptr
<
Tensor
>
&
source_tensor
,
std
::
shared_ptr
<
Tensor
>
*
dest_tensor
,
const
std
::
vector
<
int64_t
>
&
source_indx
,
const
std
::
vector
<
int64_t
>
&
dest_indx
)
{
if
(
source_tensor
->
type
()
!=
(
*
dest_tensor
)
->
type
())
RETURN_STATUS_UNEXPECTED
(
"CopyTensorValue: source and destination tensor must have the same type."
);
if
(
source_tensor
->
type
()
==
DataType
::
DE_UINT8
)
{
uint8_t
pixel_value
;
RETURN_IF_NOT_OK
(
source_tensor
->
GetItemAt
(
&
pixel_value
,
source_indx
));
RETURN_IF_NOT_OK
((
*
dest_tensor
)
->
SetItemAt
(
dest_indx
,
pixel_value
));
}
else
if
(
source_tensor
->
type
()
==
DataType
::
DE_FLOAT32
)
{
float
pixel_value
;
RETURN_IF_NOT_OK
(
source_tensor
->
GetItemAt
(
&
pixel_value
,
source_indx
));
RETURN_IF_NOT_OK
((
*
dest_tensor
)
->
SetItemAt
(
dest_indx
,
pixel_value
));
}
else
{
RETURN_STATUS_UNEXPECTED
(
"CopyTensorValue: Tensor type is not supported. Tensor type must be float32 or uint8."
);
}
return
Status
::
OK
();
}
Status
SwapRedAndBlue
(
std
::
shared_ptr
<
Tensor
>
input
,
std
::
shared_ptr
<
Tensor
>
*
output
)
{
try
{
std
::
shared_ptr
<
CVTensor
>
input_cv
=
CVTensor
::
AsCVTensor
(
std
::
move
(
input
));
...
...
mindspore/ccsrc/minddata/dataset/kernels/image/image_utils.h
浏览文件 @
a2c38d89
...
...
@@ -133,6 +133,17 @@ Status HwcToChw(std::shared_ptr<Tensor> input, std::shared_ptr<Tensor> *output);
Status
MaskWithTensor
(
const
std
::
shared_ptr
<
Tensor
>
&
sub_mat
,
std
::
shared_ptr
<
Tensor
>
*
input
,
int
x
,
int
y
,
int
width
,
int
height
,
ImageFormat
image_format
);
/// \brief Copies a value from a source tensor into a destination tensor
/// \note This is meant for images and therefore only works if tensor is uint8 or float32
/// \param[in] source_tensor The tensor we take the value from
/// \param[in] dest_tensor The pointer to the tensor we want to copy the value to
/// \param[in] source_indx index of the value in the source tensor
/// \param[in] dest_indx index of the value in the destination tensor
/// \param[out] dest_tensor Copies the value to the given dest_tensor and returns it
/// @return Status ok/error
Status
CopyTensorValue
(
const
std
::
shared_ptr
<
Tensor
>
&
source_tensor
,
std
::
shared_ptr
<
Tensor
>
*
dest_tensor
,
const
std
::
vector
<
int64_t
>
&
source_indx
,
const
std
::
vector
<
int64_t
>
&
dest_indx
);
/// \brief Swap the red and blue pixels (RGB <-> BGR)
/// \param input: Tensor of shape <H,W,3> and any OpenCv compatible type, see CVTensor.
/// \param output: Swapped image of same shape and type
...
...
mindspore/ccsrc/minddata/dataset/kernels/image/mixup_batch_op.cc
浏览文件 @
a2c38d89
...
...
@@ -29,7 +29,7 @@ MixUpBatchOp::MixUpBatchOp(float alpha) : alpha_(alpha) { rnd_.seed(GetSeed());
Status
MixUpBatchOp
::
Compute
(
const
TensorRow
&
input
,
TensorRow
*
output
)
{
if
(
input
.
size
()
<
2
)
{
RETURN_STATUS_UNEXPECTED
(
"Both images and labels columns are required for this operation"
);
RETURN_STATUS_UNEXPECTED
(
"Both images and labels columns are required for this operation
.
"
);
}
std
::
vector
<
std
::
shared_ptr
<
CVTensor
>>
images
;
...
...
@@ -38,13 +38,13 @@ Status MixUpBatchOp::Compute(const TensorRow &input, TensorRow *output) {
// Check inputs
if
(
image_shape
.
size
()
!=
4
||
image_shape
[
0
]
!=
label_shape
[
0
])
{
RETURN_STATUS_UNEXPECTED
(
"You must make sure images are HWC or CHW and batch
before calling MixUpBatch
"
);
RETURN_STATUS_UNEXPECTED
(
"You must make sure images are HWC or CHW and batch
ed before calling MixUpBatch.
"
);
}
if
(
label_shape
.
size
()
!=
2
)
{
RETURN_STATUS_UNEXPECTED
(
"MixUpBatch: Label's must be in one-hot format and in a batch"
);
RETURN_STATUS_UNEXPECTED
(
"MixUpBatch: Label's must be in one-hot format and in a batch
.
"
);
}
if
((
image_shape
[
1
]
!=
1
&&
image_shape
[
1
]
!=
3
)
&&
(
image_shape
[
3
]
!=
1
&&
image_shape
[
3
]
!=
3
))
{
RETURN_STATUS_UNEXPECTED
(
"MixUpBatch: Images must be in the shape of HWC or CHW"
);
RETURN_STATUS_UNEXPECTED
(
"MixUpBatch: Images must be in the shape of HWC or CHW
.
"
);
}
// Move images into a vector of CVTensors
...
...
tests/ut/python/dataset/test_cutmix_batch_op.py
浏览文件 @
a2c38d89
...
...
@@ -76,7 +76,7 @@ def test_cutmix_batch_success1(plot=False):
def
test_cutmix_batch_success2
(
plot
=
False
):
"""
Test CutMixBatch op with default values for alpha and prob on a batch of HWC images
Test CutMixBatch op with default values for alpha and prob on a batch of
rescaled
HWC images
"""
logger
.
info
(
"test_cutmix_batch_success2"
)
...
...
@@ -95,6 +95,8 @@ def test_cutmix_batch_success2(plot=False):
data1
=
ds
.
Cifar10Dataset
(
DATA_DIR
,
num_samples
=
10
,
shuffle
=
False
)
one_hot_op
=
data_trans
.
OneHot
(
num_classes
=
10
)
data1
=
data1
.
map
(
input_columns
=
[
"label"
],
operations
=
one_hot_op
)
rescale_op
=
vision
.
Rescale
((
1.0
/
255.0
),
0.0
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
],
operations
=
rescale_op
)
cutmix_batch_op
=
vision
.
CutMixBatch
(
mode
.
ImageBatchFormat
.
NHWC
)
data1
=
data1
.
batch
(
5
,
drop_remainder
=
True
)
data1
=
data1
.
map
(
input_columns
=
[
"image"
,
"label"
],
operations
=
cutmix_batch_op
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录