Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
91594e50
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
91594e50
编写于
6月 12, 2020
作者:
D
dengkaipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix pisa
上级
33f02d99
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
95 addition
and
40 deletion
+95
-40
ppdet/modeling/losses/pisa_utils.py
ppdet/modeling/losses/pisa_utils.py
+24
-16
ppdet/modeling/losses/yolo_loss.py
ppdet/modeling/losses/yolo_loss.py
+71
-24
未找到文件。
ppdet/modeling/losses/pisa_utils.py
浏览文件 @
91594e50
...
...
@@ -20,25 +20,33 @@ import numpy as np
__all__
=
[
'get_isr_p_func'
]
def
get_isr_p_func
(
pos_iou_thresh
=
0.2
5
,
bias
=
0
,
k
=
2
):
def
get_isr_p_func
(
max_box_num
=
50
,
pos_iou_thresh
=
0.
5
,
bias
=
0
,
k
=
2
):
def
irs_p
(
x
):
np
.
save
(
"data"
,
x
)
x
=
np
.
array
(
x
)
max_ious
=
x
[:,
:,
0
]
gt_inds
=
x
[:,
:,
1
].
astype
(
'int32'
)
cls
=
x
[:,
:,
2
].
astype
(
'int32'
)
gt_label
=
x
[:,
:
max_box_num
]
gt_score
=
x
[:,
max_box_num
:
2
*
max_box_num
]
remain
=
x
[:,
2
*
max_box_num
:]
pn
=
remain
.
shape
[
1
]
//
3
max_ious
=
remain
[:,
:
pn
]
gt_inds
=
remain
[:,
pn
:
2
*
pn
].
astype
(
'int32'
)
cls
=
remain
[:,
2
*
pn
:].
astype
(
'int32'
)
# # n_{max}: max gt box num in each class
# valid_gt = gt_box[:, :, 2] > 0.
# valid_gt_label = gt_label[valid_gt]
# max_l_num = np.bincount(valid_gt_label).max()
pos_mask
=
max_ious
>
pos_iou_thresh
if
not
np
.
any
(
pos_mask
):
return
np
.
zeros
([
max_ious
.
shape
[
0
],
pn
,
2
]).
astype
(
'float32'
)
cls_target
=
np
.
zeros_like
(
max_ious
)
cls_target_weights
=
np
.
zeros_like
(
max_ious
)
for
i
in
range
(
gt_label
.
shape
[
0
]):
cls_target
[
i
]
=
gt_label
[
i
,
gt_inds
[
i
]]
cls_target_weights
[
i
]
=
gt_score
[
i
,
gt_inds
[
i
]]
# cls_target *= pos_mask.astype('float32')
# divide gt index in each sample
gt_inds
=
gt_inds
+
np
.
arange
(
gt_inds
.
shape
[
0
])[:,
np
.
newaxis
]
*
gt_inds
.
shape
[
1
]
0
])[:,
np
.
newaxis
]
*
max_box_num
all_pos_weights
=
np
.
ones_like
(
max_ious
)
pos_mask
=
max_ious
>
pos_iou_thresh
all_pos_weights
=
np
.
zeros_like
(
max_ious
)
cls
=
np
.
reshape
(
cls
,
list
(
max_ious
.
shape
)
+
[
-
1
])
max_ious
=
max_ious
[
pos_mask
]
pos_weights
=
all_pos_weights
[
pos_mask
]
...
...
@@ -58,12 +66,12 @@ def get_isr_p_func(pos_iou_thresh=0.25, bias=0, k=2):
l_max_iou_rank
=
np
.
argsort
(
-
l_max_ious
).
argsort
().
astype
(
'float32'
)
weight_factor
=
np
.
clip
(
max_l_num
-
l_max_iou_rank
,
0.
,
None
)
/
max_l_num
weight_factor
=
np
.
power
(
bias
+
(
1
-
bias
)
*
weight_factor
,
k
)
pos_weights
[
l_inds
]
*=
weight_factor
pos_weights
=
pos_weights
/
np
.
mean
(
pos_weights
)
pos_weights
[
l_inds
]
=
np
.
power
(
bias
+
(
1
-
bias
)
*
weight_factor
,
k
)
pos_weights
=
pos_weights
/
max
(
np
.
mean
(
pos_weights
),
1e-6
)
all_pos_weights
[
pos_mask
]
=
pos_weights
cls_target_weights
*=
all_pos_weights
return
all_pos_weights
return
np
.
stack
([
cls_target
,
cls_target_weights
],
axis
=-
1
)
return
irs_p
...
...
ppdet/modeling/losses/yolo_loss.py
浏览文件 @
91594e50
...
...
@@ -66,7 +66,7 @@ class YOLOv3Loss(object):
anchor_masks
,
mask_anchors
,
num_classes
,
prefix_name
):
if
self
.
_use_fine_grained_loss
:
return
self
.
_get_fine_grained_loss
(
outputs
,
targets
,
gt_box
,
gt_label
,
self
.
_batch_size
,
outputs
,
targets
,
gt_box
,
gt_label
,
gt_score
,
self
.
_batch_size
,
num_classes
,
mask_anchors
,
self
.
_ignore_thresh
)
else
:
losses
=
[]
...
...
@@ -93,7 +93,7 @@ class YOLOv3Loss(object):
return
{
'loss'
:
sum
(
losses
)}
def
_get_fine_grained_loss
(
self
,
outputs
,
targets
,
gt_box
,
gt_label
,
batch_size
,
num_classes
,
mask_anchors
,
gt_score
,
batch_size
,
num_classes
,
mask_anchors
,
ignore_thresh
):
"""
Calculate fine grained YOLOv3 loss
...
...
@@ -123,6 +123,7 @@ class YOLOv3Loss(object):
"YOLOv3 output layer number not equal target number"
loss_xys
,
loss_whs
,
loss_objs
,
loss_clss
=
[],
[],
[],
[]
loss_carls
,
loss_isrp_clss
=
[],
[]
if
self
.
_iou_loss
is
not
None
:
loss_ious
=
[]
if
self
.
_iou_aware_loss
is
not
None
:
...
...
@@ -144,22 +145,54 @@ class YOLOv3Loss(object):
sorted_iou
,
sorted_gt_inds
=
fluid
.
layers
.
argsort
(
iou
,
axis
=-
1
,
descending
=
True
)
max_iou
=
sorted_iou
[:,
:,
0
:
1
]
max_iou
=
sorted_iou
[:,
:,
0
]
gt_inds
=
fluid
.
layers
.
cast
(
sorted_gt_inds
[:,
:,
0
:
1
],
dtype
=
'float32'
)
pred_cls
=
fluid
.
layers
.
argmax
(
cls
,
axis
=-
1
)
pred_cls
=
fluid
.
layers
.
reshape
(
pred_cls
,
[
batch_size
,
-
1
,
1
])
sorted_gt_inds
[:,
:,
0
],
dtype
=
'float32'
)
cls_score
=
fluid
.
layers
.
sigmoid
(
cls
)
sorted_cls_score
,
sorted_pred_cls
=
fluid
.
layers
.
argsort
(
cls_score
,
axis
=-
1
,
descending
=
True
)
pred_cls
=
fluid
.
layers
.
reshape
(
sorted_pred_cls
[:,
:,
:,
:,
0
],
[
batch_size
,
-
1
])
pred_cls
=
fluid
.
layers
.
cast
(
pred_cls
,
dtype
=
'float32'
)
gt_label_fp32
=
fluid
.
layers
.
cast
(
gt_label
,
dtype
=
'float32'
)
isr_p_input
=
fluid
.
layers
.
concat
(
[
max_iou
,
gt_inds
,
pred_cls
],
axis
=-
1
)
[
gt_label_fp32
,
gt_score
,
max_iou
,
gt_inds
,
pred_cls
],
axis
=-
1
)
isr_p
=
get_isr_p_func
()
pos_weights
=
fluid
.
layers
.
zeros_like
(
max_iou
)
fluid
.
layers
.
py_func
(
isr_p
,
isr_p_input
,
pos_weights
)
isr_p_output
=
fluid
.
layers
.
zeros_like
(
sorted_iou
[:,
:,
:
2
]
)
fluid
.
layers
.
py_func
(
isr_p
,
isr_p_input
,
isr_p_output
)
tobj_shape
=
fluid
.
layers
.
shape
(
tobj
)
pos_weights
=
fluid
.
layers
.
reshape
(
pos_weights
,
(
isr_p_output
=
fluid
.
layers
.
reshape
(
isr_p_output
,
(
-
1
,
an_num
,
tobj_shape
[
2
],
tobj_shape
[
3
],
2
))
cls_target
=
fluid
.
layers
.
cast
(
isr_p_output
[:,
:,
:,
:,
0
:
1
],
dtype
=
'int32'
)
cls_target
=
fluid
.
layers
.
one_hot
(
cls_target
,
num_classes
)
cls_target_weights
=
isr_p_output
[:,
:,
:,
:,
1
]
cls_target_weights
.
stop_gradient
=
True
loss_isrp_cls
=
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
cls
,
cls_target
)
loss_isrp_cls
=
fluid
.
layers
.
elementwise_mul
(
loss_isrp_cls
,
cls_target_weights
,
axis
=
0
)
loss_isrp_cls
=
fluid
.
layers
.
reduce_sum
(
loss_isrp_cls
,
dim
=
[
1
,
2
,
3
])
bias
=
0.2
pos_cls_score
=
fluid
.
layers
.
reduce_sum
(
cls_score
*
cls_target
,
dim
=
[
-
1
])
pos_cls_score
=
fluid
.
layers
.
reshape
(
pos_cls_score
,
[
batch_size
,
-
1
,
])
pos_mask
=
fluid
.
layers
.
cast
(
sorted_iou
[:,
:,
0
]
>
0.5
,
dtype
=
'float32'
)
carl_weights
=
bias
+
(
1
-
bias
)
*
pos_cls_score
*
pos_mask
carl_weights
*=
fluid
.
layers
.
reduce_sum
(
pos_mask
)
/
fluid
.
layers
.
reduce_sum
(
carl_weights
)
carl_weights
=
fluid
.
layers
.
reshape
(
carl_weights
,
(
-
1
,
an_num
,
tobj_shape
[
2
],
tobj_shape
[
3
]))
tobj
=
tobj
*
pos_weights
# isr_tobj = tobj * pos_weights
# loss_cls = fluid.layers.sigmoid_cross_entropy_with_logits(cls, tcls)
...
...
@@ -172,18 +205,26 @@ class YOLOv3Loss(object):
# new_loss_cls.stop_gradient = True
# pos_loss_cls_ratio = orig_loss_cls / new_loss_cls
tscale_tobj
=
tscale
*
tobj
loss_x
=
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
,
tx
)
*
tscale_tobj
loss_x
=
fluid
.
layers
.
reduce_sum
(
loss_x
,
dim
=
[
1
,
2
,
3
])
loss_y
=
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
y
,
ty
)
*
tscale_tobj
loss_y
=
fluid
.
layers
.
reduce_sum
(
loss_y
,
dim
=
[
1
,
2
,
3
])
loss_x
=
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
,
tx
)
*
tscale
loss_y
=
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
y
,
ty
)
*
tscale
loss_xy
=
loss_x
+
loss_y
# NOTE: we refined loss function of (w, h) as L1Loss
loss_w
=
fluid
.
layers
.
abs
(
w
-
tw
)
*
tscale_tobj
loss_w
=
fluid
.
layers
.
reduce_sum
(
loss_w
,
dim
=
[
1
,
2
,
3
])
loss_h
=
fluid
.
layers
.
abs
(
h
-
th
)
*
tscale_tobj
loss_h
=
fluid
.
layers
.
reduce_sum
(
loss_h
,
dim
=
[
1
,
2
,
3
])
loss_w
=
fluid
.
layers
.
abs
(
w
-
tw
)
*
tscale
loss_h
=
fluid
.
layers
.
abs
(
h
-
th
)
*
tscale
loss_wh
=
loss_w
+
loss_h
loss_carl
=
(
loss_xy
+
loss_wh
)
*
carl_weights
loss_carl
=
fluid
.
layers
.
reduce_sum
(
loss_carl
,
dim
=
[
1
,
2
,
3
])
# loss_x = fluid.layers.reduce_sum(loss_x, dim=[1, 2, 3])
# loss_y = fluid.layers.reduce_sum(loss_y, dim=[1, 2, 3])
# loss_w = fluid.layers.reduce_sum(loss_w, dim=[1, 2, 3])
# loss_h = fluid.layers.reduce_sum(loss_h, dim=[1, 2, 3])
loss_xy
=
fluid
.
layers
.
reduce_sum
(
loss_xy
*
tobj
,
dim
=
[
1
,
2
,
3
])
loss_wh
=
fluid
.
layers
.
reduce_sum
(
loss_wh
*
tobj
,
dim
=
[
1
,
2
,
3
])
if
self
.
_iou_loss
is
not
None
:
loss_iou
=
self
.
_iou_loss
(
x
,
y
,
w
,
h
,
tx
,
ty
,
tw
,
th
,
anchors
,
downsample
,
self
.
_batch_size
)
...
...
@@ -200,6 +241,8 @@ class YOLOv3Loss(object):
loss_iou_aware
,
dim
=
[
1
,
2
,
3
])
loss_iou_awares
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_iou_aware
))
# tobj = tobj * pos_weights
loss_obj_pos
,
loss_obj_neg
=
self
.
_calc_obj_loss
(
output
,
obj
,
tobj
,
iou
,
an_num
,
self
.
_ignore_thresh
,
scale_x_y
)
...
...
@@ -207,8 +250,10 @@ class YOLOv3Loss(object):
loss_cls
=
fluid
.
layers
.
elementwise_mul
(
loss_cls
,
tobj
,
axis
=
0
)
loss_cls
=
fluid
.
layers
.
reduce_sum
(
loss_cls
,
dim
=
[
1
,
2
,
3
,
4
])
loss_xys
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_x
+
loss_y
))
loss_whs
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_w
+
loss_h
))
loss_xys
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_xy
))
loss_whs
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_wh
))
loss_isrp_clss
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_isrp_cls
))
loss_carls
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_carl
))
loss_objs
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_obj_pos
+
loss_obj_neg
))
loss_clss
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_cls
))
...
...
@@ -216,6 +261,8 @@ class YOLOv3Loss(object):
losses_all
=
{
"loss_xy"
:
fluid
.
layers
.
sum
(
loss_xys
),
"loss_wh"
:
fluid
.
layers
.
sum
(
loss_whs
),
"loss_isrp_cls"
:
fluid
.
layers
.
sum
(
loss_isrp_clss
),
"loss_carl"
:
fluid
.
layers
.
sum
(
loss_carls
),
"loss_obj"
:
fluid
.
layers
.
sum
(
loss_objs
),
"loss_cls"
:
fluid
.
layers
.
sum
(
loss_clss
),
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录