Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
正统之独孤求败
mindspore
提交
56da3b0a
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,发现更多精彩内容 >>
提交
56da3b0a
编写于
7月 13, 2020
作者:
I
islam_amin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixing ratio bug with BoundingBoxAugment
上级
ece99192
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
32 addition
and
34 deletion
+32
-34
mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.cc
...re/ccsrc/dataset/kernels/image/bounding_box_augment_op.cc
+29
-32
mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.h
...ore/ccsrc/dataset/kernels/image/bounding_box_augment_op.h
+1
-0
tests/ut/data/dataset/golden/bounding_box_augment_crop_c_result.npz
...ata/dataset/golden/bounding_box_augment_crop_c_result.npz
+0
-0
tests/ut/data/dataset/golden/bounding_box_augment_valid_ratio_c_result.npz
...aset/golden/bounding_box_augment_valid_ratio_c_result.npz
+0
-0
tests/ut/python/dataset/test_bounding_box_augment.py
tests/ut/python/dataset/test_bounding_box_augment.py
+2
-2
未找到文件。
mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.cc
浏览文件 @
56da3b0a
...
...
@@ -26,7 +26,7 @@ namespace dataset {
const
float
BoundingBoxAugmentOp
::
kDefRatio
=
0.3
;
BoundingBoxAugmentOp
::
BoundingBoxAugmentOp
(
std
::
shared_ptr
<
TensorOp
>
transform
,
float
ratio
)
:
ratio_
(
ratio
),
transform_
(
std
::
move
(
transform
))
{
:
ratio_
(
ratio
),
uniform_
(
0
,
1
),
transform_
(
std
::
move
(
transform
))
{
rnd_
.
seed
(
GetSeed
());
}
...
...
@@ -34,41 +34,38 @@ Status BoundingBoxAugmentOp::Compute(const TensorRow &input, TensorRow *output)
IO_CHECK_VECTOR
(
input
,
output
);
BOUNDING_BOX_CHECK
(
input
);
// check if bounding boxes are valid
uint32_t
num_of_boxes
=
input
[
1
]
->
shape
()[
0
];
uint32_t
num_to_aug
=
num_of_boxes
*
ratio_
;
// cast to int
std
::
vector
<
uint32_t
>
boxes
(
num_of_boxes
);
std
::
vector
<
uint32_t
>
selected_boxes
;
for
(
uint32_t
i
=
0
;
i
<
num_of_boxes
;
i
++
)
boxes
[
i
]
=
i
;
// sample bboxes according to ratio picked by user
std
::
sample
(
boxes
.
begin
(),
boxes
.
end
(),
std
::
back_inserter
(
selected_boxes
),
num_to_aug
,
rnd_
);
std
::
shared_ptr
<
Tensor
>
crop_out
;
std
::
shared_ptr
<
Tensor
>
res_out
;
std
::
shared_ptr
<
CVTensor
>
input_restore
=
CVTensor
::
AsCVTensor
(
input
[
0
]);
for
(
uint32_t
i
=
0
;
i
<
num_to_aug
;
i
++
)
{
float
min_x
=
0
;
float
min_y
=
0
;
float
b_w
=
0
;
float
b_h
=
0
;
// get the required items
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
min_x
,
{
selected_boxes
[
i
],
0
}));
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
min_y
,
{
selected_boxes
[
i
],
1
}));
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
b_w
,
{
selected_boxes
[
i
],
2
}));
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
b_h
,
{
selected_boxes
[
i
],
3
}));
RETURN_IF_NOT_OK
(
Crop
(
input_restore
,
&
crop_out
,
static_cast
<
int
>
(
min_x
),
static_cast
<
int
>
(
min_y
),
static_cast
<
int
>
(
b_w
),
static_cast
<
int
>
(
b_h
)));
// transform the cropped bbox region
RETURN_IF_NOT_OK
(
transform_
->
Compute
(
crop_out
,
&
res_out
));
// place the transformed region back in the restored input
std
::
shared_ptr
<
CVTensor
>
res_img
=
CVTensor
::
AsCVTensor
(
res_out
);
// check if transformed crop is out of bounds of the box
if
(
res_img
->
mat
().
cols
>
b_w
||
res_img
->
mat
().
rows
>
b_h
||
res_img
->
mat
().
cols
<
b_w
||
res_img
->
mat
().
rows
<
b_h
)
{
// if so, resize to fit in the box
std
::
shared_ptr
<
TensorOp
>
resize_op
=
std
::
make_shared
<
ResizeOp
>
(
static_cast
<
int32_t
>
(
b_h
),
static_cast
<
int32_t
>
(
b_w
));
RETURN_IF_NOT_OK
(
resize_op
->
Compute
(
std
::
static_pointer_cast
<
Tensor
>
(
res_img
),
&
res_out
));
res_img
=
CVTensor
::
AsCVTensor
(
res_out
);
for
(
uint32_t
i
=
0
;
i
<
num_of_boxes
;
i
++
)
{
// using a uniform distribution to ensure op happens with probability ratio_
if
(
uniform_
(
rnd_
)
<
ratio_
)
{
float
min_x
=
0
;
float
min_y
=
0
;
float
b_w
=
0
;
float
b_h
=
0
;
// get the required items
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
min_x
,
{
i
,
0
}));
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
min_y
,
{
i
,
1
}));
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
b_w
,
{
i
,
2
}));
RETURN_IF_NOT_OK
(
input
[
1
]
->
GetItemAt
<
float
>
(
&
b_h
,
{
i
,
3
}));
RETURN_IF_NOT_OK
(
Crop
(
input_restore
,
&
crop_out
,
static_cast
<
int
>
(
min_x
),
static_cast
<
int
>
(
min_y
),
static_cast
<
int
>
(
b_w
),
static_cast
<
int
>
(
b_h
)));
// transform the cropped bbox region
RETURN_IF_NOT_OK
(
transform_
->
Compute
(
crop_out
,
&
res_out
));
// place the transformed region back in the restored input
std
::
shared_ptr
<
CVTensor
>
res_img
=
CVTensor
::
AsCVTensor
(
res_out
);
// check if transformed crop is out of bounds of the box
if
(
res_img
->
mat
().
cols
>
b_w
||
res_img
->
mat
().
rows
>
b_h
||
res_img
->
mat
().
cols
<
b_w
||
res_img
->
mat
().
rows
<
b_h
)
{
// if so, resize to fit in the box
std
::
shared_ptr
<
TensorOp
>
resize_op
=
std
::
make_shared
<
ResizeOp
>
(
static_cast
<
int32_t
>
(
b_h
),
static_cast
<
int32_t
>
(
b_w
));
RETURN_IF_NOT_OK
(
resize_op
->
Compute
(
std
::
static_pointer_cast
<
Tensor
>
(
res_img
),
&
res_out
));
res_img
=
CVTensor
::
AsCVTensor
(
res_out
);
}
res_img
->
mat
().
copyTo
(
input_restore
->
mat
()(
cv
::
Rect
(
min_x
,
min_y
,
res_img
->
mat
().
cols
,
res_img
->
mat
().
rows
)));
}
res_img
->
mat
().
copyTo
(
input_restore
->
mat
()(
cv
::
Rect
(
min_x
,
min_y
,
res_img
->
mat
().
cols
,
res_img
->
mat
().
rows
)));
}
(
*
output
).
push_back
(
std
::
move
(
std
::
static_pointer_cast
<
Tensor
>
(
input_restore
)));
(
*
output
).
push_back
(
input
[
1
]);
...
...
mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.h
浏览文件 @
56da3b0a
...
...
@@ -53,6 +53,7 @@ class BoundingBoxAugmentOp : public TensorOp {
private:
float
ratio_
;
std
::
mt19937
rnd_
;
std
::
uniform_real_distribution
<
float
>
uniform_
;
std
::
shared_ptr
<
TensorOp
>
transform_
;
};
}
// namespace dataset
...
...
tests/ut/data/dataset/golden/bounding_box_augment_crop_c_result.npz
浏览文件 @
56da3b0a
无法预览此类型文件
tests/ut/data/dataset/golden/bounding_box_augment_valid_ratio_c_result.npz
浏览文件 @
56da3b0a
无法预览此类型文件
tests/ut/python/dataset/test_bounding_box_augment.py
浏览文件 @
56da3b0a
...
...
@@ -84,8 +84,8 @@ def test_bounding_box_augment_with_crop_op(plot_vis=False):
dataVoc1
=
ds
.
VOCDataset
(
DATA_DIR
,
task
=
"Detection"
,
mode
=
"train"
,
decode
=
True
,
shuffle
=
False
)
dataVoc2
=
ds
.
VOCDataset
(
DATA_DIR
,
task
=
"Detection"
,
mode
=
"train"
,
decode
=
True
,
shuffle
=
False
)
# Ratio is set to
1 to apply rotation on all
bounding boxes.
test_op
=
c_vision
.
BoundingBoxAugment
(
c_vision
.
RandomCrop
(
50
),
0.
5
)
# Ratio is set to
0.9 to apply RandomCrop of size (50, 50) on 90% of the
bounding boxes.
test_op
=
c_vision
.
BoundingBoxAugment
(
c_vision
.
RandomCrop
(
50
),
0.
9
)
# map to apply ops
dataVoc2
=
dataVoc2
.
map
(
input_columns
=
[
"image"
,
"annotation"
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录