Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
2a494247
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
1 年多 前同步成功
通知
696
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看板
未验证
提交
2a494247
编写于
4月 12, 2021
作者:
C
cnn
提交者:
GitHub
4月 12, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove duplicate code (#2580)
上级
e3d703d7
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
183 addition
and
224 deletion
+183
-224
configs/dota/_base_/s2anet.yml
configs/dota/_base_/s2anet.yml
+3
-1
configs/dota/s2anet_conv_1x_dota.yml
configs/dota/s2anet_conv_1x_dota.yml
+5
-4
ppdet/data/transform/batch_operators.py
ppdet/data/transform/batch_operators.py
+1
-45
ppdet/data/transform/operators.py
ppdet/data/transform/operators.py
+11
-93
ppdet/modeling/bbox_utils.py
ppdet/modeling/bbox_utils.py
+122
-1
ppdet/modeling/heads/s2anet_head.py
ppdet/modeling/heads/s2anet_head.py
+31
-31
ppdet/modeling/post_process.py
ppdet/modeling/post_process.py
+10
-49
未找到文件。
configs/dota/_base_/s2anet.yml
浏览文件 @
2a494247
...
...
@@ -33,7 +33,7 @@ S2ANetHead:
feat_in
:
256
feat_out
:
256
num_classes
:
15
align_conv_type
:
'
Align
Conv'
# AlignConv Conv
align_conv_type
:
'
Conv'
# AlignConv Conv
align_conv_size
:
3
use_sigmoid_cls
:
True
...
...
@@ -51,3 +51,5 @@ S2ANetBBoxPostProcess:
keep_top_k
:
-1
score_threshold
:
0.05
nms_threshold
:
0.1
normalized
:
False
#background_label: -1
configs/dota/s2anet_conv_1x_dota.yml
浏览文件 @
2a494247
_BASE_
:
[
'
../datasets/dota
_debug
.yml'
,
'
../datasets/dota.yml'
,
'
../runtime.yml'
,
'
_base_/s2anet_optimizer_1x.yml'
,
'
_base_/s2anet.yml'
,
'
_base_/s2anet_reader.yml'
,
]
weights
:
output/s2anet_1x_dota/model_final
S2ANetHead
:
anchor_strides
:
[
8
,
16
,
32
,
64
,
128
]
anchor_scales
:
[
4
]
anchor_ratios
:
[
1.0
]
anchor_strides
:
[
8
,
16
,
32
,
64
,
128
]
anchor_scales
:
[
4
]
anchor_ratios
:
[
1.0
]
anchor_assign
:
RBoxAssigner
stacked_convs
:
2
feat_in
:
256
...
...
ppdet/data/transform/batch_operators.py
浏览文件 @
2a494247
...
...
@@ -752,50 +752,6 @@ class RboxPadBatch(BaseOperator):
self
.
pad_to_stride
=
pad_to_stride
self
.
pad_gt
=
pad_gt
def
poly_to_rbox
(
self
,
polys
):
"""
poly:[x0,y0,x1,y1,x2,y2,x3,y3]
to
rotated_boxes:[x_ctr,y_ctr,w,h,angle]
"""
rotated_boxes
=
[]
for
poly
in
polys
:
poly
=
np
.
array
(
poly
[:
8
],
dtype
=
np
.
float32
)
pt1
=
(
poly
[
0
],
poly
[
1
])
pt2
=
(
poly
[
2
],
poly
[
3
])
pt3
=
(
poly
[
4
],
poly
[
5
])
pt4
=
(
poly
[
6
],
poly
[
7
])
edge1
=
np
.
sqrt
((
pt1
[
0
]
-
pt2
[
0
])
*
(
pt1
[
0
]
-
pt2
[
0
])
+
(
pt1
[
1
]
-
pt2
[
1
])
*
(
pt1
[
1
]
-
pt2
[
1
]))
edge2
=
np
.
sqrt
((
pt2
[
0
]
-
pt3
[
0
])
*
(
pt2
[
0
]
-
pt3
[
0
])
+
(
pt2
[
1
]
-
pt3
[
1
])
*
(
pt2
[
1
]
-
pt3
[
1
]))
width
=
max
(
edge1
,
edge2
)
height
=
min
(
edge1
,
edge2
)
angle
=
0
if
edge1
>
edge2
:
angle
=
np
.
arctan2
(
np
.
float
(
pt2
[
1
]
-
pt1
[
1
]),
np
.
float
(
pt2
[
0
]
-
pt1
[
0
]))
elif
edge2
>=
edge1
:
angle
=
np
.
arctan2
(
np
.
float
(
pt4
[
1
]
-
pt1
[
1
]),
np
.
float
(
pt4
[
0
]
-
pt1
[
0
]))
def
norm_angle
(
angle
,
range
=
[
-
np
.
pi
/
4
,
np
.
pi
]):
return
(
angle
-
range
[
0
])
%
range
[
1
]
+
range
[
0
]
angle
=
norm_angle
(
angle
)
x_ctr
=
np
.
float
(
pt1
[
0
]
+
pt3
[
0
])
/
2.0
y_ctr
=
np
.
float
(
pt1
[
1
]
+
pt3
[
1
])
/
2.0
rotated_box
=
np
.
array
([
x_ctr
,
y_ctr
,
width
,
height
,
angle
])
rotated_boxes
.
append
(
rotated_box
)
ret_rotated_boxes
=
np
.
array
(
rotated_boxes
)
assert
ret_rotated_boxes
.
shape
[
1
]
==
5
return
ret_rotated_boxes
def
__call__
(
self
,
samples
,
context
=
None
):
"""
Args:
...
...
@@ -883,7 +839,7 @@ class RboxPadBatch(BaseOperator):
sample
[
'is_crowd'
]
=
is_crowd_data
# ploy to rbox
polys
=
sample
[
'gt_rbox2poly'
]
rbox
=
self
.
poly_to_rbox
(
polys
)
rbox
=
bbox_utils
.
poly_to_rbox
(
polys
)
sample
[
'gt_rbox'
]
=
rbox
return
samples
ppdet/data/transform/operators.py
浏览文件 @
2a494247
...
...
@@ -541,10 +541,11 @@ class RandomFlip(BaseOperator):
oldx2
=
bbox
[:,
2
].
copy
()
oldx3
=
bbox
[:,
4
].
copy
()
oldx4
=
bbox
[:,
6
].
copy
()
bbox
[:,
0
]
=
width
-
oldx
2
bbox
[:,
2
]
=
width
-
oldx
1
bbox
[:,
0
]
=
width
-
oldx
1
bbox
[:,
2
]
=
width
-
oldx
2
bbox
[:,
4
]
=
width
-
oldx3
bbox
[:,
6
]
=
width
-
oldx4
bbox
=
[
bbox_utils
.
get_best_begin_point_single
(
e
)
for
e
in
bbox
]
return
bbox
def
apply
(
self
,
sample
,
context
=
None
):
...
...
@@ -579,7 +580,7 @@ class RandomFlip(BaseOperator):
sample
[
'gt_segm'
]
=
sample
[
'gt_segm'
][:,
:,
::
-
1
]
if
'gt_rbox2poly'
in
sample
and
sample
[
'gt_rbox2poly'
].
any
():
sample
[
'gt_rbox2poly'
]
=
self
.
apply_
b
box
(
sample
[
'gt_rbox2poly'
],
sample
[
'gt_rbox2poly'
]
=
self
.
apply_
r
box
(
sample
[
'gt_rbox2poly'
],
width
)
sample
[
'flipped'
]
=
True
...
...
@@ -1973,99 +1974,16 @@ class Rbox2Poly(BaseOperator):
def
apply
(
self
,
sample
,
context
=
None
):
assert
'gt_rbox'
in
sample
assert
sample
[
'gt_rbox'
].
shape
[
1
]
==
5
rrect
=
sample
[
'gt_rbox'
]
bbox_num
=
rrect
.
shape
[
0
]
x_ctr
=
rrect
[:,
0
]
y_ctr
=
rrect
[:,
1
]
width
=
rrect
[:,
2
]
height
=
rrect
[:,
3
]
angle
=
rrect
[:,
4
]
tl_x
,
tl_y
,
br_x
,
br_y
=
-
width
/
2
,
-
height
/
2
,
width
/
2
,
height
/
2
# rect 2x4
rect
=
np
.
array
([[
tl_x
,
br_x
,
br_x
,
tl_x
],
[
tl_y
,
tl_y
,
br_y
,
br_y
]])
R
=
np
.
array
([[
np
.
cos
(
angle
),
-
np
.
sin
(
angle
)],
[
np
.
sin
(
angle
),
np
.
cos
(
angle
)]])
poly
=
[]
for
i
in
range
(
R
.
shape
[
2
]):
tmp_r
=
R
[:,
:,
i
].
reshape
(
2
,
2
)
poly
.
append
(
tmp_r
.
dot
(
rect
[:,
:,
i
]))
# poly:[M, 2, 4]
poly
=
np
.
array
(
poly
)
coor_x
=
poly
[:,
0
,
:
4
]
+
x_ctr
.
reshape
(
bbox_num
,
1
)
coor_y
=
poly
[:,
1
,
:
4
]
+
y_ctr
.
reshape
(
bbox_num
,
1
)
poly
=
np
.
stack
(
[
coor_x
[:,
0
],
coor_y
[:,
0
],
coor_x
[:,
1
],
coor_y
[:,
1
],
coor_x
[:,
2
],
coor_y
[:,
2
],
coor_x
[:,
3
],
coor_y
[:,
3
]
],
axis
=
1
)
rrects
=
sample
[
'gt_rbox'
]
x_ctr
=
rrects
[:,
0
]
y_ctr
=
rrects
[:,
1
]
width
=
rrects
[:,
2
]
height
=
rrects
[:,
3
]
x1
=
x_ctr
-
width
/
2.0
y1
=
y_ctr
-
height
/
2.0
x2
=
x_ctr
+
width
/
2.0
y2
=
y_ctr
+
height
/
2.0
sample
[
'gt_bbox'
]
=
np
.
stack
([
x1
,
y1
,
x2
,
y2
],
axis
=
1
)
sample
[
'gt_rbox2poly'
]
=
poly
return
sample
@
register_op
class
Poly2Rbox
(
BaseOperator
):
"""
Convert poly format to rbbox format.
"""
def
__init__
(
self
):
super
(
Poly2Rbox
,
self
).
__init__
()
def
poly_to_rbox
(
self
,
polys
):
"""
poly:[x0,y0,x1,y1,x2,y2,x3,y3]
to
rotated_boxes:[x_ctr,y_ctr,w,h,angle]
"""
rotated_boxes
=
[]
for
poly
in
polys
:
poly
=
np
.
array
(
poly
[:
8
],
dtype
=
np
.
float32
)
pt1
=
(
poly
[
0
],
poly
[
1
])
pt2
=
(
poly
[
2
],
poly
[
3
])
pt3
=
(
poly
[
4
],
poly
[
5
])
pt4
=
(
poly
[
6
],
poly
[
7
])
edge1
=
np
.
sqrt
((
pt1
[
0
]
-
pt2
[
0
])
*
(
pt1
[
0
]
-
pt2
[
0
])
+
(
pt1
[
1
]
-
pt2
[
1
])
*
(
pt1
[
1
]
-
pt2
[
1
]))
edge2
=
np
.
sqrt
((
pt2
[
0
]
-
pt3
[
0
])
*
(
pt2
[
0
]
-
pt3
[
0
])
+
(
pt2
[
1
]
-
pt3
[
1
])
*
(
pt2
[
1
]
-
pt3
[
1
]))
width
=
max
(
edge1
,
edge2
)
height
=
min
(
edge1
,
edge2
)
angle
=
0
if
edge1
>
edge2
:
angle
=
np
.
arctan2
(
np
.
float
(
pt2
[
1
]
-
pt1
[
1
]),
np
.
float
(
pt2
[
0
]
-
pt1
[
0
]))
elif
edge2
>=
edge1
:
angle
=
np
.
arctan2
(
np
.
float
(
pt4
[
1
]
-
pt1
[
1
]),
np
.
float
(
pt4
[
0
]
-
pt1
[
0
]))
def
norm_angle
(
angle
,
range
=
[
-
np
.
pi
/
4
,
np
.
pi
]):
return
(
angle
-
range
[
0
])
%
range
[
1
]
+
range
[
0
]
angle
=
norm_angle
(
angle
)
x_ctr
=
np
.
float
(
pt1
[
0
]
+
pt3
[
0
])
/
2
y_ctr
=
np
.
float
(
pt1
[
1
]
+
pt3
[
1
])
/
2
rotated_box
=
np
.
array
([
x_ctr
,
y_ctr
,
width
,
height
,
angle
])
rotated_boxes
.
append
(
rotated_box
)
ret_rotated_boxes
=
np
.
array
(
rotated_boxes
)
assert
ret_rotated_boxes
.
shape
[
1
]
==
5
return
ret_rotated_boxes
def
apply
(
self
,
sample
,
context
=
None
):
assert
'gt_rbox2poly'
in
sample
poly
=
sample
[
'gt_rbox2poly'
]
rbox
=
self
.
poly_to_rbox
(
poly
)
sample
[
'gt_rbox'
]
=
rbox
polys
=
bbox_utils
.
rbox2poly
(
rrects
)
sample
[
'gt_rbox2poly'
]
=
polys
return
sample
ppdet/modeling/bbox_utils.py
浏览文件 @
2a494247
...
...
@@ -404,4 +404,125 @@ def bbox_decode(bbox_preds,
anchors
,
bbox_delta
,
means
,
stds
,
wh_ratio_clip
=
1e-6
)
bboxes
=
paddle
.
reshape
(
bboxes
,
[
H
,
W
,
5
])
bboxes_list
.
append
(
bboxes
)
return
paddle
.
stack
(
bboxes_list
,
axis
=
0
)
\ No newline at end of file
return
paddle
.
stack
(
bboxes_list
,
axis
=
0
)
def
poly_to_rbox
(
polys
):
"""
poly:[x0,y0,x1,y1,x2,y2,x3,y3]
to
rotated_boxes:[x_ctr,y_ctr,w,h,angle]
"""
rotated_boxes
=
[]
for
poly
in
polys
:
poly
=
np
.
array
(
poly
[:
8
],
dtype
=
np
.
float32
)
pt1
=
(
poly
[
0
],
poly
[
1
])
pt2
=
(
poly
[
2
],
poly
[
3
])
pt3
=
(
poly
[
4
],
poly
[
5
])
pt4
=
(
poly
[
6
],
poly
[
7
])
edge1
=
np
.
sqrt
((
pt1
[
0
]
-
pt2
[
0
])
*
(
pt1
[
0
]
-
pt2
[
0
])
+
(
pt1
[
1
]
-
pt2
[
1
])
*
(
pt1
[
1
]
-
pt2
[
1
]))
edge2
=
np
.
sqrt
((
pt2
[
0
]
-
pt3
[
0
])
*
(
pt2
[
0
]
-
pt3
[
0
])
+
(
pt2
[
1
]
-
pt3
[
1
])
*
(
pt2
[
1
]
-
pt3
[
1
]))
width
=
max
(
edge1
,
edge2
)
height
=
min
(
edge1
,
edge2
)
rbox_angle
=
0
if
edge1
>
edge2
:
rbox_angle
=
np
.
arctan2
(
np
.
float
(
pt2
[
1
]
-
pt1
[
1
]),
np
.
float
(
pt2
[
0
]
-
pt1
[
0
]))
elif
edge2
>=
edge1
:
rbox_angle
=
np
.
arctan2
(
np
.
float
(
pt4
[
1
]
-
pt1
[
1
]),
np
.
float
(
pt4
[
0
]
-
pt1
[
0
]))
def
norm_angle
(
angle
,
range
=
[
-
np
.
pi
/
4
,
np
.
pi
]):
return
(
angle
-
range
[
0
])
%
range
[
1
]
+
range
[
0
]
rbox_angle
=
norm_angle
(
rbox_angle
)
x_ctr
=
np
.
float
(
pt1
[
0
]
+
pt3
[
0
])
/
2
y_ctr
=
np
.
float
(
pt1
[
1
]
+
pt3
[
1
])
/
2
rotated_box
=
np
.
array
([
x_ctr
,
y_ctr
,
width
,
height
,
rbox_angle
])
rotated_boxes
.
append
(
rotated_box
)
ret_rotated_boxes
=
np
.
array
(
rotated_boxes
)
assert
ret_rotated_boxes
.
shape
[
1
]
==
5
return
ret_rotated_boxes
def
cal_line_length
(
point1
,
point2
):
import
math
return
math
.
sqrt
(
math
.
pow
(
point1
[
0
]
-
point2
[
0
],
2
)
+
math
.
pow
(
point1
[
1
]
-
point2
[
1
],
2
))
def
get_best_begin_point_single
(
coordinate
):
x1
,
y1
,
x2
,
y2
,
x3
,
y3
,
x4
,
y4
=
coordinate
xmin
=
min
(
x1
,
x2
,
x3
,
x4
)
ymin
=
min
(
y1
,
y2
,
y3
,
y4
)
xmax
=
max
(
x1
,
x2
,
x3
,
x4
)
ymax
=
max
(
y1
,
y2
,
y3
,
y4
)
combinate
=
[[[
x1
,
y1
],
[
x2
,
y2
],
[
x3
,
y3
],
[
x4
,
y4
]],
[[
x4
,
y4
],
[
x1
,
y1
],
[
x2
,
y2
],
[
x3
,
y3
]],
[[
x3
,
y3
],
[
x4
,
y4
],
[
x1
,
y1
],
[
x2
,
y2
]],
[[
x2
,
y2
],
[
x3
,
y3
],
[
x4
,
y4
],
[
x1
,
y1
]]]
dst_coordinate
=
[[
xmin
,
ymin
],
[
xmax
,
ymin
],
[
xmax
,
ymax
],
[
xmin
,
ymax
]]
force
=
100000000.0
force_flag
=
0
for
i
in
range
(
4
):
temp_force
=
cal_line_length
(
combinate
[
i
][
0
],
dst_coordinate
[
0
])
\
+
cal_line_length
(
combinate
[
i
][
1
],
dst_coordinate
[
1
])
\
+
cal_line_length
(
combinate
[
i
][
2
],
dst_coordinate
[
2
])
\
+
cal_line_length
(
combinate
[
i
][
3
],
dst_coordinate
[
3
])
if
temp_force
<
force
:
force
=
temp_force
force_flag
=
i
if
force_flag
!=
0
:
pass
return
np
.
array
(
combinate
[
force_flag
]).
reshape
(
8
)
def
rbox2poly_single
(
rrect
):
"""
rrect:[x_ctr,y_ctr,w,h,angle]
to
poly:[x0,y0,x1,y1,x2,y2,x3,y3]
"""
x_ctr
,
y_ctr
,
width
,
height
,
angle
=
rrect
[:
5
]
tl_x
,
tl_y
,
br_x
,
br_y
=
-
width
/
2
,
-
height
/
2
,
width
/
2
,
height
/
2
# rect 2x4
rect
=
np
.
array
([[
tl_x
,
br_x
,
br_x
,
tl_x
],
[
tl_y
,
tl_y
,
br_y
,
br_y
]])
R
=
np
.
array
([[
np
.
cos
(
angle
),
-
np
.
sin
(
angle
)],
[
np
.
sin
(
angle
),
np
.
cos
(
angle
)]])
# poly
poly
=
R
.
dot
(
rect
)
x0
,
x1
,
x2
,
x3
=
poly
[
0
,
:
4
]
+
x_ctr
y0
,
y1
,
y2
,
y3
=
poly
[
1
,
:
4
]
+
y_ctr
poly
=
np
.
array
([
x0
,
y0
,
x1
,
y1
,
x2
,
y2
,
x3
,
y3
],
dtype
=
np
.
float32
)
poly
=
get_best_begin_point_single
(
poly
)
return
poly
def
rbox2poly
(
rrects
):
"""
rrect:[x_ctr,y_ctr,w,h,angle]
to
poly:[x0,y0,x1,y1,x2,y2,x3,y3]
"""
polys
=
[]
for
rrect
in
rrects
:
x_ctr
,
y_ctr
,
width
,
height
,
angle
=
rrect
[:
5
]
tl_x
,
tl_y
,
br_x
,
br_y
=
-
width
/
2
,
-
height
/
2
,
width
/
2
,
height
/
2
rect
=
np
.
array
([[
tl_x
,
br_x
,
br_x
,
tl_x
],
[
tl_y
,
tl_y
,
br_y
,
br_y
]])
R
=
np
.
array
([[
np
.
cos
(
angle
),
-
np
.
sin
(
angle
)],
[
np
.
sin
(
angle
),
np
.
cos
(
angle
)]])
poly
=
R
.
dot
(
rect
)
x0
,
x1
,
x2
,
x3
=
poly
[
0
,
:
4
]
+
x_ctr
y0
,
y1
,
y2
,
y3
=
poly
[
1
,
:
4
]
+
y_ctr
poly
=
np
.
array
([
x0
,
y0
,
x1
,
y1
,
x2
,
y2
,
x3
,
y3
],
dtype
=
np
.
float32
)
poly
=
get_best_begin_point_single
(
poly
)
polys
.
append
(
poly
)
polys
=
np
.
array
(
polys
)
return
polys
ppdet/modeling/heads/s2anet_head.py
浏览文件 @
2a494247
...
...
@@ -227,7 +227,7 @@ class S2ANetHead(nn.Layer):
align_conv_type (str): align_conv_type ['Conv', 'AlignConv']
align_conv_size (int): kernel size of align_conv
use_sigmoid_cls (bool): use sigmoid_cls or not
reg_loss_weight (list):
reg loss weight
reg_loss_weight (list):
loss weight for regression
"""
__shared__
=
[
'num_classes'
]
__inject__
=
[
'anchor_assign'
]
...
...
@@ -258,7 +258,7 @@ class S2ANetHead(nn.Layer):
self
.
anchor_base_sizes
=
list
(
anchor_strides
)
self
.
target_means
=
target_means
self
.
target_stds
=
target_stds
assert
align_conv_type
in
[
'AlignConv'
,
'Conv'
]
assert
align_conv_type
in
[
'AlignConv'
,
'Conv'
,
'DCN'
]
self
.
align_conv_type
=
align_conv_type
self
.
align_conv_size
=
align_conv_size
...
...
@@ -277,12 +277,6 @@ class S2ANetHead(nn.Layer):
S2ANetAnchorGenerator
(
anchor_base
,
anchor_scales
,
anchor_ratios
))
# featmap_sizes
self
.
featmap_sizes
=
[]
self
.
base_anchors
=
[]
self
.
rbox_anchors
=
[]
self
.
refine_anchor_list
=
[]
self
.
fam_cls_convs
=
nn
.
Sequential
()
self
.
fam_reg_convs
=
nn
.
Sequential
()
...
...
@@ -341,6 +335,22 @@ class S2ANetHead(nn.Layer):
padding
=
(
self
.
align_conv_size
-
1
)
//
2
,
bias_attr
=
ParamAttr
(
initializer
=
Constant
(
0
)))
elif
self
.
align_conv_type
==
"DCN"
:
self
.
align_conv_offset
=
nn
.
Conv2D
(
self
.
feat_out
,
2
*
self
.
align_conv_size
**
2
,
1
,
weight_attr
=
ParamAttr
(
initializer
=
Normal
(
0.0
,
0.01
)),
bias_attr
=
ParamAttr
(
initializer
=
Constant
(
0
)))
self
.
align_conv
=
paddle
.
vision
.
ops
.
DeformConv2D
(
self
.
feat_out
,
self
.
feat_out
,
self
.
align_conv_size
,
padding
=
(
self
.
align_conv_size
-
1
)
//
2
,
weight_attr
=
ParamAttr
(
initializer
=
Normal
(
0.0
,
0.01
)),
bias_attr
=
False
)
self
.
or_conv
=
nn
.
Conv2D
(
self
.
feat_out
,
self
.
feat_out
,
...
...
@@ -400,6 +410,11 @@ class S2ANetHead(nn.Layer):
weight_attr
=
ParamAttr
(
initializer
=
Normal
(
0.0
,
0.01
)),
bias_attr
=
ParamAttr
(
initializer
=
Constant
(
0
)))
self
.
base_anchors
=
dict
()
self
.
featmap_sizes
=
dict
()
self
.
base_anchors
=
dict
()
self
.
refine_anchor_list
=
[]
def
forward
(
self
,
feats
):
fam_reg_branch_list
=
[]
fam_cls_branch_list
=
[]
...
...
@@ -453,9 +468,6 @@ class S2ANetHead(nn.Layer):
elif
self
.
align_conv_type
==
'DCN'
:
align_offset
=
self
.
align_conv_offset
(
feat
)
align_feat
=
self
.
align_conv
(
feat
,
align_offset
)
elif
self
.
align_conv_type
==
'GA_DCN'
:
align_offset
=
self
.
align_conv_offset
(
feat
)
align_feat
=
self
.
align_conv
(
feat
,
align_offset
)
elif
self
.
align_conv_type
==
'Conv'
:
align_feat
=
self
.
align_conv
(
feat
)
...
...
@@ -582,6 +594,9 @@ class S2ANetHead(nn.Layer):
fam_bbox_pred
=
paddle
.
squeeze
(
fam_bbox_pred
,
axis
=
0
)
fam_bbox_pred
=
paddle
.
reshape
(
fam_bbox_pred
,
[
-
1
,
5
])
fam_bbox
=
self
.
smooth_l1_loss
(
fam_bbox_pred
,
feat_bbox_targets
)
loss_weight
=
paddle
.
to_tensor
(
self
.
reg_loss_weight
,
dtype
=
'float32'
,
stop_gradient
=
True
)
fam_bbox
=
paddle
.
multiply
(
fam_bbox
,
loss_weight
)
feat_bbox_weights
=
paddle
.
to_tensor
(
feat_bbox_weights
,
stop_gradient
=
True
)
fam_bbox
=
fam_bbox
*
feat_bbox_weights
...
...
@@ -590,6 +605,7 @@ class S2ANetHead(nn.Layer):
fam_bbox_losses
.
append
(
fam_bbox_total
)
fam_cls_loss
=
paddle
.
add_n
(
fam_cls_losses
)
fam_cls_loss
=
fam_cls_loss
*
2.0
fam_reg_loss
=
paddle
.
add_n
(
fam_bbox_losses
)
return
fam_cls_loss
,
fam_reg_loss
...
...
@@ -660,6 +676,9 @@ class S2ANetHead(nn.Layer):
odm_bbox_pred
=
paddle
.
squeeze
(
odm_bbox_pred
,
axis
=
0
)
odm_bbox_pred
=
paddle
.
reshape
(
odm_bbox_pred
,
[
-
1
,
5
])
odm_bbox
=
self
.
smooth_l1_loss
(
odm_bbox_pred
,
feat_bbox_targets
)
loss_weight
=
paddle
.
to_tensor
(
self
.
reg_loss_weight
,
dtype
=
'float32'
,
stop_gradient
=
True
)
odm_bbox
=
paddle
.
multiply
(
odm_bbox
,
loss_weight
)
feat_bbox_weights
=
paddle
.
to_tensor
(
feat_bbox_weights
,
stop_gradient
=
True
)
odm_bbox
=
odm_bbox
*
feat_bbox_weights
...
...
@@ -667,6 +686,7 @@ class S2ANetHead(nn.Layer):
odm_bbox_losses
.
append
(
odm_bbox_total
)
odm_cls_loss
=
paddle
.
add_n
(
odm_cls_losses
)
odm_cls_loss
=
odm_cls_loss
*
2.0
odm_reg_loss
=
paddle
.
add_n
(
odm_bbox_losses
)
return
odm_cls_loss
,
odm_reg_loss
...
...
@@ -799,25 +819,6 @@ class S2ANetHead(nn.Layer):
return
refine_anchors_list
,
valid_flag_list
def
rbox2poly_single
(
self
,
rrect
,
get_best_begin_point
=
False
):
"""
rrect:[x_ctr,y_ctr,w,h,angle]
to
poly:[x0,y0,x1,y1,x2,y2,x3,y3]
"""
x_ctr
,
y_ctr
,
width
,
height
,
angle
=
rrect
[:
5
]
tl_x
,
tl_y
,
br_x
,
br_y
=
-
width
/
2
,
-
height
/
2
,
width
/
2
,
height
/
2
# rect 2x4
rect
=
np
.
array
([[
tl_x
,
br_x
,
br_x
,
tl_x
],
[
tl_y
,
tl_y
,
br_y
,
br_y
]])
R
=
np
.
array
([[
np
.
cos
(
angle
),
-
np
.
sin
(
angle
)],
[
np
.
sin
(
angle
),
np
.
cos
(
angle
)]])
# poly
poly
=
R
.
dot
(
rect
)
x0
,
x1
,
x2
,
x3
=
poly
[
0
,
:
4
]
+
x_ctr
y0
,
y1
,
y2
,
y3
=
poly
[
1
,
:
4
]
+
y_ctr
poly
=
np
.
array
([
x0
,
y0
,
x1
,
y1
,
x2
,
y2
,
x3
,
y3
],
dtype
=
np
.
float32
)
return
poly
def
get_bboxes
(
self
,
cls_score_list
,
bbox_pred_list
,
mlvl_anchors
,
nms_pre
,
cls_out_channels
,
use_sigmoid_cls
):
assert
len
(
cls_score_list
)
==
len
(
bbox_pred_list
)
==
len
(
mlvl_anchors
)
...
...
@@ -855,7 +856,6 @@ class S2ANetHead(nn.Layer):
target_stds
=
(
1.0
,
1.0
,
1.0
,
1.0
,
1.0
)
bboxes
=
bbox_utils
.
delta2rbox
(
anchors
,
bbox_pred
,
target_means
,
target_stds
)
mlvl_bboxes
.
append
(
bboxes
)
mlvl_scores
.
append
(
scores
)
...
...
ppdet/modeling/post_process.py
浏览文件 @
2a494247
...
...
@@ -17,7 +17,7 @@ import paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
ppdet.core.workspace
import
register
from
ppdet.modeling.bbox_utils
import
nonempty_bbox
from
ppdet.modeling.bbox_utils
import
nonempty_bbox
,
rbox2poly
from
.
import
ops
try
:
from
collections.abc
import
Sequence
...
...
@@ -231,45 +231,6 @@ class S2ANetBBoxPostProcess(object):
self
.
nms
=
nms
self
.
origin_shape_list
=
[]
def
rbox2poly
(
self
,
rrect
,
get_best_begin_point
=
True
):
"""
rrect: [N, 5] [x_ctr,y_ctr,w,h,angle]
to
poly:[x0,y0,x1,y1,x2,y2,x3,y3]
"""
bbox_num
=
rrect
.
shape
[
0
]
x_ctr
=
rrect
[:,
0
]
y_ctr
=
rrect
[:,
1
]
width
=
rrect
[:,
2
]
height
=
rrect
[:,
3
]
angle
=
rrect
[:,
4
]
tl_x
,
tl_y
,
br_x
,
br_y
=
-
width
/
2
,
-
height
/
2
,
width
/
2
,
height
/
2
# rect 2x4
rect
=
np
.
array
([[
tl_x
,
br_x
,
br_x
,
tl_x
],
[
tl_y
,
tl_y
,
br_y
,
br_y
]])
R
=
np
.
array
([[
np
.
cos
(
angle
),
-
np
.
sin
(
angle
)],
[
np
.
sin
(
angle
),
np
.
cos
(
angle
)]])
# R:[2,2,M] rect:[2,4,M]
#poly = R.dot(rect)
poly
=
[]
for
i
in
range
(
R
.
shape
[
2
]):
poly
.
append
(
R
[:,
:,
i
].
dot
(
rect
[:,
:,
i
]))
# poly:[M, 2, 4]
poly
=
np
.
array
(
poly
)
coor_x
=
poly
[:,
0
,
:
4
]
+
x_ctr
.
reshape
(
bbox_num
,
1
)
coor_y
=
poly
[:,
1
,
:
4
]
+
y_ctr
.
reshape
(
bbox_num
,
1
)
poly
=
np
.
stack
(
[
coor_x
[:,
0
],
coor_y
[:,
0
],
coor_x
[:,
1
],
coor_y
[:,
1
],
coor_x
[:,
2
],
coor_y
[:,
2
],
coor_x
[:,
3
],
coor_y
[:,
3
]
],
axis
=
1
)
if
get_best_begin_point
:
poly_lst
=
[
get_best_begin_point_single
(
e
)
for
e
in
poly
]
poly
=
np
.
array
(
poly_lst
)
return
poly
def
get_prediction
(
self
,
pred_scores
,
pred_bboxes
,
im_shape
,
scale_factor
):
"""
pred_scores : [N, M] score
...
...
@@ -278,7 +239,7 @@ class S2ANetBBoxPostProcess(object):
scale_factor : [N, 2] scale_factor
"""
# TODO: support bs>1
pred_ploys
=
self
.
rbox2poly
(
pred_bboxes
.
numpy
(),
False
)
pred_ploys
=
rbox2poly
(
pred_bboxes
.
numpy
()
)
pred_ploys
=
paddle
.
to_tensor
(
pred_ploys
)
pred_ploys
=
paddle
.
reshape
(
pred_ploys
,
[
1
,
pred_ploys
.
shape
[
0
],
pred_ploys
.
shape
[
1
]])
...
...
@@ -332,14 +293,14 @@ class S2ANetBBoxPostProcess(object):
bboxes
[:,
1
::
2
]
=
bboxes
[:,
1
::
2
]
/
scale_factor
[
1
]
zeros
=
paddle
.
zeros_like
(
origin_h
)
x1
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
0
],
origin_w
),
zeros
)
y1
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
1
],
origin_h
),
zeros
)
x2
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
2
],
origin_w
),
zeros
)
y2
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
3
],
origin_h
),
zeros
)
x3
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
4
],
origin_w
),
zeros
)
y3
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
5
],
origin_h
),
zeros
)
x4
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
6
],
origin_w
),
zeros
)
y4
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
7
],
origin_h
),
zeros
)
x1
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
0
],
origin_w
-
1
),
zeros
)
y1
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
1
],
origin_h
-
1
),
zeros
)
x2
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
2
],
origin_w
-
1
),
zeros
)
y2
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
3
],
origin_h
-
1
),
zeros
)
x3
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
4
],
origin_w
-
1
),
zeros
)
y3
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
5
],
origin_h
-
1
),
zeros
)
x4
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
6
],
origin_w
-
1
),
zeros
)
y4
=
paddle
.
maximum
(
paddle
.
minimum
(
bboxes
[:,
7
],
origin_h
-
1
),
zeros
)
bbox
=
paddle
.
stack
([
x1
,
y1
,
x2
,
y2
,
x3
,
y3
,
x4
,
y4
],
axis
=-
1
)
bboxes
=
(
bbox
,
bbox_num
)
return
bboxes
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录