Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
ed21c2c3
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ed21c2c3
编写于
10月 27, 2020
作者:
W
wangguanzhong
提交者:
GitHub
10月 27, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add iou_similarity (#1605)
上级
fd8ed6c4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
108 addition
and
27 deletion
+108
-27
ppdet/modeling/ops.py
ppdet/modeling/ops.py
+64
-1
ppdet/modeling/tests/test_base.py
ppdet/modeling/tests/test_base.py
+2
-2
ppdet/modeling/tests/test_ops.py
ppdet/modeling/tests/test_ops.py
+42
-24
未找到文件。
ppdet/modeling/ops.py
浏览文件 @
ed21c2c3
...
...
@@ -30,7 +30,7 @@ __all__ = [
#'prior_box',
#'anchor_generator',
#'generate_proposals',
#
'iou_similarity',
'iou_similarity'
,
#'box_coder',
#'yolo_box',
#'multiclass_nms',
...
...
@@ -240,6 +240,69 @@ def roi_align(input,
return
align_out
def
iou_similarity
(
x
,
y
,
box_normalized
=
True
,
name
=
None
):
"""
Computes intersection-over-union (IOU) between two box lists.
Box list 'X' should be a LoDTensor and 'Y' is a common Tensor,
boxes in 'Y' are shared by all instance of the batched inputs of X.
Given two boxes A and B, the calculation of IOU is as follows:
$$
IOU(A, B) =
\\
frac{area(A
\\
cap B)}{area(A)+area(B)-area(A
\\
cap B)}
$$
Args:
x (Tensor): Box list X is a 2-D Tensor with shape [N, 4] holds N
boxes, each box is represented as [xmin, ymin, xmax, ymax],
the shape of X is [N, 4]. [xmin, ymin] is the left top
coordinate of the box if the input is image feature map, they
are close to the origin of the coordinate system.
[xmax, ymax] is the right bottom coordinate of the box.
The data type is float32 or float64.
y (Tensor): Box list Y holds M boxes, each box is represented as
[xmin, ymin, xmax, ymax], the shape of X is [N, 4].
[xmin, ymin] is the left top coordinate of the box if the
input is image feature map, and [xmax, ymax] is the right
bottom coordinate of the box. The data type is float32 or float64.
box_normalized(bool): Whether treat the priorbox as a normalized box.
Set true by default.
name(str, optional): For detailed information, please refer
to :ref:`api_guide_Name`. Usually name is no need to set and
None by default.
Returns:
Tensor: The output of iou_similarity op, a tensor with shape [N, M]
representing pairwise iou scores. The data type is same with x.
Examples:
.. code-block:: python
import numpy as np
import paddle
paddle.enable_static()
x = paddle.data(name='x', shape=[None, 4], dtype='float32')
y = paddle.data(name='y', shape=[None, 4], dtype='float32')
iou = ops.iou_similarity(x=x, y=y)
"""
if
in_dygraph_mode
():
out
=
core
.
ops
.
iou_similarity
(
x
,
y
,
'box_normalized'
,
box_normalized
)
return
out
helper
=
LayerHelper
(
"iou_similarity"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
helper
.
append_op
(
type
=
"iou_similarity"
,
inputs
=
{
"X"
:
x
,
"Y"
:
y
},
attrs
=
{
"box_normalized"
:
box_normalized
},
outputs
=
{
"Out"
:
out
})
return
out
def
collect_fpn_proposals
(
multi_rois
,
multi_scores
,
min_level
,
...
...
ppdet/modeling/tests/test_base.py
浏览文件 @
ed21c2c3
...
...
@@ -48,7 +48,7 @@ class LayerTest(unittest.TestCase):
program
=
Program
()
with
fluid
.
scope_guard
(
scope
):
with
fluid
.
program_guard
(
program
):
paddle
.
manual_
seed
(
self
.
seed
)
paddle
.
seed
(
self
.
seed
)
paddle
.
framework
.
random
.
_manual_program_seed
(
self
.
seed
)
yield
...
...
@@ -68,6 +68,6 @@ class LayerTest(unittest.TestCase):
def
dynamic_graph
(
self
,
force_to_use_cpu
=
False
):
with
fluid
.
dygraph
.
guard
(
self
.
_get_place
(
force_to_use_cpu
=
force_to_use_cpu
)):
paddle
.
manual_
seed
(
self
.
seed
)
paddle
.
seed
(
self
.
seed
)
paddle
.
framework
.
random
.
_manual_program_seed
(
self
.
seed
)
yield
ppdet/modeling/tests/test_ops.py
浏览文件 @
ed21c2c3
...
...
@@ -31,6 +31,18 @@ import ppdet.modeling.ops as ops
from
ppdet.modeling.tests.test_base
import
LayerTest
def
make_rois
(
h
,
w
,
rois_num
,
output_size
):
rois
=
np
.
zeros
((
0
,
4
)).
astype
(
'float32'
)
for
roi_num
in
rois_num
:
roi
=
np
.
zeros
((
roi_num
,
4
)).
astype
(
'float32'
)
roi
[:,
0
]
=
np
.
random
.
randint
(
0
,
h
-
output_size
[
0
],
size
=
roi_num
)
roi
[:,
1
]
=
np
.
random
.
randint
(
0
,
w
-
output_size
[
1
],
size
=
roi_num
)
roi
[:,
2
]
=
np
.
random
.
randint
(
roi
[:,
0
]
+
output_size
[
0
],
h
)
roi
[:,
3
]
=
np
.
random
.
randint
(
roi
[:,
1
]
+
output_size
[
1
],
w
)
rois
=
np
.
vstack
((
rois
,
roi
))
return
rois
class
TestCollectFpnProposals
(
LayerTest
):
def
test_collect_fpn_proposals
(
self
):
multi_bboxes_np
=
[]
...
...
@@ -223,7 +235,7 @@ class TestROIAlign(LayerTest):
inputs_np
=
np
.
random
.
rand
(
b
,
c
,
h
,
w
).
astype
(
'float32'
)
rois_num
=
[
4
,
6
]
output_size
=
(
7
,
7
)
rois_np
=
self
.
make_rois
(
h
,
w
,
rois_num
,
output_size
)
rois_np
=
make_rois
(
h
,
w
,
rois_num
,
output_size
)
rois_num_np
=
np
.
array
(
rois_num
).
astype
(
'int32'
)
with
self
.
static_graph
():
inputs
=
paddle
.
static
.
data
(
...
...
@@ -261,17 +273,6 @@ class TestROIAlign(LayerTest):
self
.
assertTrue
(
np
.
array_equal
(
output_np
,
output_dy_np
))
def
make_rois
(
self
,
h
,
w
,
rois_num
,
output_size
):
rois
=
np
.
zeros
((
0
,
4
)).
astype
(
'float32'
)
for
roi_num
in
rois_num
:
roi
=
np
.
zeros
((
roi_num
,
4
)).
astype
(
'float32'
)
roi
[:,
0
]
=
np
.
random
.
randint
(
0
,
h
-
output_size
[
0
],
size
=
roi_num
)
roi
[:,
1
]
=
np
.
random
.
randint
(
0
,
w
-
output_size
[
1
],
size
=
roi_num
)
roi
[:,
2
]
=
np
.
random
.
randint
(
roi
[:,
0
]
+
output_size
[
0
],
h
)
roi
[:,
3
]
=
np
.
random
.
randint
(
roi
[:,
1
]
+
output_size
[
1
],
w
)
rois
=
np
.
vstack
((
rois
,
roi
))
return
rois
def
test_roi_align_error
(
self
):
program
=
Program
()
with
program_guard
(
program
):
...
...
@@ -293,7 +294,7 @@ class TestROIPool(LayerTest):
inputs_np
=
np
.
random
.
rand
(
b
,
c
,
h
,
w
).
astype
(
'float32'
)
rois_num
=
[
4
,
6
]
output_size
=
(
7
,
7
)
rois_np
=
self
.
make_rois
(
h
,
w
,
rois_num
,
output_size
)
rois_np
=
make_rois
(
h
,
w
,
rois_num
,
output_size
)
rois_num_np
=
np
.
array
(
rois_num
).
astype
(
'int32'
)
with
self
.
static_graph
():
inputs
=
paddle
.
static
.
data
(
...
...
@@ -331,17 +332,6 @@ class TestROIPool(LayerTest):
self
.
assertTrue
(
np
.
array_equal
(
output_np
,
output_dy_np
))
def
make_rois
(
self
,
h
,
w
,
rois_num
,
output_size
):
rois
=
np
.
zeros
((
0
,
4
)).
astype
(
'float32'
)
for
roi_num
in
rois_num
:
roi
=
np
.
zeros
((
roi_num
,
4
)).
astype
(
'float32'
)
roi
[:,
0
]
=
np
.
random
.
randint
(
0
,
h
-
output_size
[
0
],
size
=
roi_num
)
roi
[:,
1
]
=
np
.
random
.
randint
(
0
,
w
-
output_size
[
1
],
size
=
roi_num
)
roi
[:,
2
]
=
np
.
random
.
randint
(
roi
[:,
0
]
+
output_size
[
0
],
h
)
roi
[:,
3
]
=
np
.
random
.
randint
(
roi
[:,
1
]
+
output_size
[
1
],
w
)
rois
=
np
.
vstack
((
rois
,
roi
))
return
rois
def
test_roi_pool_error
(
self
):
program
=
Program
()
with
program_guard
(
program
):
...
...
@@ -357,5 +347,33 @@ class TestROIPool(LayerTest):
output_size
=
(
7
,
7
))
class
TestIoUSimilarity
(
LayerTest
):
def
test_iou_similarity
(
self
):
b
,
c
,
h
,
w
=
2
,
12
,
20
,
20
inputs_np
=
np
.
random
.
rand
(
b
,
c
,
h
,
w
).
astype
(
'float32'
)
output_size
=
(
7
,
7
)
x_np
=
make_rois
(
h
,
w
,
[
20
],
output_size
)
y_np
=
make_rois
(
h
,
w
,
[
10
],
output_size
)
with
self
.
static_graph
():
x
=
paddle
.
static
.
data
(
name
=
'x'
,
shape
=
[
20
,
4
],
dtype
=
'float32'
)
y
=
paddle
.
static
.
data
(
name
=
'y'
,
shape
=
[
10
,
4
],
dtype
=
'float32'
)
iou
=
ops
.
iou_similarity
(
x
=
x
,
y
=
y
)
iou_np
,
=
self
.
get_static_graph_result
(
feed
=
{
'x'
:
x_np
,
'y'
:
y_np
,
},
fetch_list
=
[
iou
],
with_lod
=
False
)
with
self
.
dynamic_graph
():
x_dy
=
base
.
to_variable
(
x_np
)
y_dy
=
base
.
to_variable
(
y_np
)
iou_dy
=
ops
.
iou_similarity
(
x
=
x_dy
,
y
=
y_dy
)
iou_dy_np
=
iou_dy
.
numpy
()
self
.
assertTrue
(
np
.
array_equal
(
iou_np
,
iou_dy_np
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录