Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
bdf3b392
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看板
提交
bdf3b392
编写于
7月 08, 2020
作者:
L
longxiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix scalexy bug
上级
beaa62a7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
37 addition
and
21 deletion
+37
-21
ppdet/modeling/losses/iou_aware_loss.py
ppdet/modeling/losses/iou_aware_loss.py
+3
-2
ppdet/modeling/losses/iou_loss.py
ppdet/modeling/losses/iou_loss.py
+8
-4
ppdet/modeling/losses/yolo_loss.py
ppdet/modeling/losses/yolo_loss.py
+26
-15
未找到文件。
ppdet/modeling/losses/iou_aware_loss.py
浏览文件 @
bdf3b392
...
@@ -54,6 +54,7 @@ class IouAwareLoss(IouLoss):
...
@@ -54,6 +54,7 @@ class IouAwareLoss(IouLoss):
anchors
,
anchors
,
downsample_ratio
,
downsample_ratio
,
batch_size
,
batch_size
,
scale_x_y
,
eps
=
1.e-10
):
eps
=
1.e-10
):
'''
'''
Args:
Args:
...
@@ -67,9 +68,9 @@ class IouAwareLoss(IouLoss):
...
@@ -67,9 +68,9 @@ class IouAwareLoss(IouLoss):
'''
'''
pred
=
self
.
_bbox_transform
(
x
,
y
,
w
,
h
,
anchors
,
downsample_ratio
,
pred
=
self
.
_bbox_transform
(
x
,
y
,
w
,
h
,
anchors
,
downsample_ratio
,
batch_size
,
False
)
batch_size
,
False
,
scale_x_y
,
eps
)
gt
=
self
.
_bbox_transform
(
tx
,
ty
,
tw
,
th
,
anchors
,
downsample_ratio
,
gt
=
self
.
_bbox_transform
(
tx
,
ty
,
tw
,
th
,
anchors
,
downsample_ratio
,
batch_size
,
True
)
batch_size
,
True
,
scale_x_y
,
eps
)
iouk
=
self
.
_iou
(
pred
,
gt
,
ioup
,
eps
)
iouk
=
self
.
_iou
(
pred
,
gt
,
ioup
,
eps
)
iouk
.
stop_gradient
=
True
iouk
.
stop_gradient
=
True
...
...
ppdet/modeling/losses/iou_loss.py
浏览文件 @
bdf3b392
...
@@ -63,6 +63,7 @@ class IouLoss(object):
...
@@ -63,6 +63,7 @@ class IouLoss(object):
anchors
,
anchors
,
downsample_ratio
,
downsample_ratio
,
batch_size
,
batch_size
,
scale_x_y
=
1.
,
ioup
=
None
,
ioup
=
None
,
eps
=
1.e-10
):
eps
=
1.e-10
):
'''
'''
...
@@ -75,9 +76,9 @@ class IouLoss(object):
...
@@ -75,9 +76,9 @@ class IouLoss(object):
eps (float): the decimal to prevent the denominator eqaul zero
eps (float): the decimal to prevent the denominator eqaul zero
'''
'''
pred
=
self
.
_bbox_transform
(
x
,
y
,
w
,
h
,
anchors
,
downsample_ratio
,
pred
=
self
.
_bbox_transform
(
x
,
y
,
w
,
h
,
anchors
,
downsample_ratio
,
batch_size
,
False
)
batch_size
,
False
,
scale_x_y
,
eps
)
gt
=
self
.
_bbox_transform
(
tx
,
ty
,
tw
,
th
,
anchors
,
downsample_ratio
,
gt
=
self
.
_bbox_transform
(
tx
,
ty
,
tw
,
th
,
anchors
,
downsample_ratio
,
batch_size
,
True
)
batch_size
,
True
,
scale_x_y
,
eps
)
iouk
=
self
.
_iou
(
pred
,
gt
,
ioup
,
eps
)
iouk
=
self
.
_iou
(
pred
,
gt
,
ioup
,
eps
)
if
self
.
loss_square
:
if
self
.
loss_square
:
loss_iou
=
1.
-
iouk
*
iouk
loss_iou
=
1.
-
iouk
*
iouk
...
@@ -145,7 +146,7 @@ class IouLoss(object):
...
@@ -145,7 +146,7 @@ class IouLoss(object):
return
diou_term
+
ciou_term
return
diou_term
+
ciou_term
def
_bbox_transform
(
self
,
dcx
,
dcy
,
dw
,
dh
,
anchors
,
downsample_ratio
,
def
_bbox_transform
(
self
,
dcx
,
dcy
,
dw
,
dh
,
anchors
,
downsample_ratio
,
batch_size
,
is_gt
):
batch_size
,
is_gt
,
scale_x_y
,
eps
):
grid_x
=
int
(
self
.
_MAX_WI
/
downsample_ratio
)
grid_x
=
int
(
self
.
_MAX_WI
/
downsample_ratio
)
grid_y
=
int
(
self
.
_MAX_HI
/
downsample_ratio
)
grid_y
=
int
(
self
.
_MAX_HI
/
downsample_ratio
)
an_num
=
len
(
anchors
)
//
2
an_num
=
len
(
anchors
)
//
2
...
@@ -179,8 +180,11 @@ class IouLoss(object):
...
@@ -179,8 +180,11 @@ class IouLoss(object):
cy
.
gradient
=
True
cy
.
gradient
=
True
else
:
else
:
dcx_sig
=
fluid
.
layers
.
sigmoid
(
dcx
)
dcx_sig
=
fluid
.
layers
.
sigmoid
(
dcx
)
cx
=
fluid
.
layers
.
elementwise_add
(
dcx_sig
,
gi
)
/
grid_x_act
dcy_sig
=
fluid
.
layers
.
sigmoid
(
dcy
)
dcy_sig
=
fluid
.
layers
.
sigmoid
(
dcy
)
if
(
abs
(
scale_x_y
-
1.0
)
>
eps
):
dcx_sig
=
scale_x_y
*
dcx_sig
-
0.5
*
(
scale_x_y
-
1
)
dcy_sig
=
scale_x_y
*
dcy_sig
-
0.5
*
(
scale_x_y
-
1
)
cx
=
fluid
.
layers
.
elementwise_add
(
dcx_sig
,
gi
)
/
grid_x_act
cy
=
fluid
.
layers
.
elementwise_add
(
dcy_sig
,
gj
)
/
grid_y_act
cy
=
fluid
.
layers
.
elementwise_add
(
dcy_sig
,
gj
)
/
grid_y_act
anchor_w_
=
[
anchors
[
i
]
for
i
in
range
(
0
,
len
(
anchors
))
if
i
%
2
==
0
]
anchor_w_
=
[
anchors
[
i
]
for
i
in
range
(
0
,
len
(
anchors
))
if
i
%
2
==
0
]
...
...
ppdet/modeling/losses/yolo_loss.py
浏览文件 @
bdf3b392
...
@@ -92,7 +92,7 @@ class YOLOv3Loss(object):
...
@@ -92,7 +92,7 @@ class YOLOv3Loss(object):
return
{
'loss'
:
sum
(
losses
)}
return
{
'loss'
:
sum
(
losses
)}
def
_get_fine_grained_loss
(
self
,
outputs
,
targets
,
gt_box
,
batch_size
,
def
_get_fine_grained_loss
(
self
,
outputs
,
targets
,
gt_box
,
batch_size
,
num_classes
,
mask_anchors
,
ignore_thresh
):
num_classes
,
mask_anchors
,
ignore_thresh
,
eps
=
1.e-10
):
"""
"""
Calculate fine grained YOLOv3 loss
Calculate fine grained YOLOv3 loss
...
@@ -136,12 +136,25 @@ class YOLOv3Loss(object):
...
@@ -136,12 +136,25 @@ class YOLOv3Loss(object):
tx
,
ty
,
tw
,
th
,
tscale
,
tobj
,
tcls
=
self
.
_split_target
(
target
)
tx
,
ty
,
tw
,
th
,
tscale
,
tobj
,
tcls
=
self
.
_split_target
(
target
)
tscale_tobj
=
tscale
*
tobj
tscale_tobj
=
tscale
*
tobj
loss_x
=
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
x
,
tx
)
*
tscale_tobj
scale_x_y
=
self
.
scale_x_y
if
not
isinstance
(
loss_x
=
fluid
.
layers
.
reduce_sum
(
loss_x
,
dim
=
[
1
,
2
,
3
])
self
.
scale_x_y
,
Sequence
)
else
self
.
scale_x_y
[
i
]
loss_y
=
fluid
.
layers
.
sigmoid_cross_entropy_with_logits
(
y
,
ty
)
*
tscale_tobj
if
(
abs
(
scale_x_y
-
1.0
)
<
eps
):
loss_y
=
fluid
.
layers
.
reduce_sum
(
loss_y
,
dim
=
[
1
,
2
,
3
])
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
])
else
:
dx
=
scale_x_y
*
fluid
.
layers
.
sigmoid
(
x
)
-
0.5
*
(
scale_x_y
-
1.0
)
dy
=
scale_x_y
*
fluid
.
layers
.
sigmoid
(
y
)
-
0.5
*
(
scale_x_y
-
1.0
)
loss_x
=
fluid
.
layers
.
abs
(
dx
-
tx
)
*
tscale_tobj
loss_x
=
fluid
.
layers
.
reduce_sum
(
loss_x
,
dim
=
[
1
,
2
,
3
])
loss_y
=
fluid
.
layers
.
abs
(
dy
-
ty
)
*
tscale_tobj
loss_y
=
fluid
.
layers
.
reduce_sum
(
loss_y
,
dim
=
[
1
,
2
,
3
])
# NOTE: we refined loss function of (w, h) as L1Loss
# NOTE: we refined loss function of (w, h) as L1Loss
loss_w
=
fluid
.
layers
.
abs
(
w
-
tw
)
*
tscale_tobj
loss_w
=
fluid
.
layers
.
abs
(
w
-
tw
)
*
tscale_tobj
loss_w
=
fluid
.
layers
.
reduce_sum
(
loss_w
,
dim
=
[
1
,
2
,
3
])
loss_w
=
fluid
.
layers
.
reduce_sum
(
loss_w
,
dim
=
[
1
,
2
,
3
])
...
@@ -149,7 +162,7 @@ class YOLOv3Loss(object):
...
@@ -149,7 +162,7 @@ class YOLOv3Loss(object):
loss_h
=
fluid
.
layers
.
reduce_sum
(
loss_h
,
dim
=
[
1
,
2
,
3
])
loss_h
=
fluid
.
layers
.
reduce_sum
(
loss_h
,
dim
=
[
1
,
2
,
3
])
if
self
.
_iou_loss
is
not
None
:
if
self
.
_iou_loss
is
not
None
:
loss_iou
=
self
.
_iou_loss
(
x
,
y
,
w
,
h
,
tx
,
ty
,
tw
,
th
,
anchors
,
loss_iou
=
self
.
_iou_loss
(
x
,
y
,
w
,
h
,
tx
,
ty
,
tw
,
th
,
anchors
,
downsample
,
self
.
_batch_size
)
downsample
,
self
.
_batch_size
,
scale_x_y
)
loss_iou
=
loss_iou
*
tscale_tobj
loss_iou
=
loss_iou
*
tscale_tobj
loss_iou
=
fluid
.
layers
.
reduce_sum
(
loss_iou
,
dim
=
[
1
,
2
,
3
])
loss_iou
=
fluid
.
layers
.
reduce_sum
(
loss_iou
,
dim
=
[
1
,
2
,
3
])
loss_ious
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_iou
))
loss_ious
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_iou
))
...
@@ -157,14 +170,12 @@ class YOLOv3Loss(object):
...
@@ -157,14 +170,12 @@ class YOLOv3Loss(object):
if
self
.
_iou_aware_loss
is
not
None
:
if
self
.
_iou_aware_loss
is
not
None
:
loss_iou_aware
=
self
.
_iou_aware_loss
(
loss_iou_aware
=
self
.
_iou_aware_loss
(
ioup
,
x
,
y
,
w
,
h
,
tx
,
ty
,
tw
,
th
,
anchors
,
downsample
,
ioup
,
x
,
y
,
w
,
h
,
tx
,
ty
,
tw
,
th
,
anchors
,
downsample
,
self
.
_batch_size
)
self
.
_batch_size
,
scale_x_y
)
loss_iou_aware
=
loss_iou_aware
*
tobj
loss_iou_aware
=
loss_iou_aware
*
tobj
loss_iou_aware
=
fluid
.
layers
.
reduce_sum
(
loss_iou_aware
=
fluid
.
layers
.
reduce_sum
(
loss_iou_aware
,
dim
=
[
1
,
2
,
3
])
loss_iou_aware
,
dim
=
[
1
,
2
,
3
])
loss_iou_awares
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_iou_aware
))
loss_iou_awares
.
append
(
fluid
.
layers
.
reduce_mean
(
loss_iou_aware
))
scale_x_y
=
self
.
scale_x_y
if
not
isinstance
(
self
.
scale_x_y
,
Sequence
)
else
self
.
scale_x_y
[
i
]
loss_obj_pos
,
loss_obj_neg
=
self
.
_calc_obj_loss
(
loss_obj_pos
,
loss_obj_neg
=
self
.
_calc_obj_loss
(
output
,
obj
,
tobj
,
gt_box
,
self
.
_batch_size
,
anchors
,
output
,
obj
,
tobj
,
gt_box
,
self
.
_batch_size
,
anchors
,
num_classes
,
downsample
,
self
.
_ignore_thresh
,
scale_x_y
)
num_classes
,
downsample
,
self
.
_ignore_thresh
,
scale_x_y
)
...
@@ -293,7 +304,7 @@ class YOLOv3Loss(object):
...
@@ -293,7 +304,7 @@ class YOLOv3Loss(object):
downsample_ratio
=
downsample
,
downsample_ratio
=
downsample
,
clip_bbox
=
False
,
clip_bbox
=
False
,
scale_x_y
=
scale_x_y
)
scale_x_y
=
scale_x_y
)
# 2. split pred bbox and gt bbox by sample, calculate IoU between pred bbox
# 2. split pred bbox and gt bbox by sample, calculate IoU between pred bbox
# and gt bbox in each sample
# and gt bbox in each sample
if
batch_size
>
1
:
if
batch_size
>
1
:
...
@@ -322,17 +333,17 @@ class YOLOv3Loss(object):
...
@@ -322,17 +333,17 @@ class YOLOv3Loss(object):
pred
=
fluid
.
layers
.
squeeze
(
pred
,
axes
=
[
0
])
pred
=
fluid
.
layers
.
squeeze
(
pred
,
axes
=
[
0
])
gt
=
box_xywh2xyxy
(
fluid
.
layers
.
squeeze
(
gt
,
axes
=
[
0
]))
gt
=
box_xywh2xyxy
(
fluid
.
layers
.
squeeze
(
gt
,
axes
=
[
0
]))
ious
.
append
(
fluid
.
layers
.
iou_similarity
(
pred
,
gt
))
ious
.
append
(
fluid
.
layers
.
iou_similarity
(
pred
,
gt
))
iou
=
fluid
.
layers
.
stack
(
ious
,
axis
=
0
)
iou
=
fluid
.
layers
.
stack
(
ious
,
axis
=
0
)
# 3. Get iou_mask by IoU between gt bbox and prediction bbox,
# 3. Get iou_mask by IoU between gt bbox and prediction bbox,
# Get obj_mask by tobj(holds gt_score), calculate objectness loss
# Get obj_mask by tobj(holds gt_score), calculate objectness loss
max_iou
=
fluid
.
layers
.
reduce_max
(
iou
,
dim
=-
1
)
max_iou
=
fluid
.
layers
.
reduce_max
(
iou
,
dim
=-
1
)
iou_mask
=
fluid
.
layers
.
cast
(
max_iou
<=
ignore_thresh
,
dtype
=
"float32"
)
iou_mask
=
fluid
.
layers
.
cast
(
max_iou
<=
ignore_thresh
,
dtype
=
"float32"
)
if
self
.
match_score
:
if
self
.
match_score
:
max_prob
=
fluid
.
layers
.
reduce_max
(
prob
,
dim
=-
1
)
max_prob
=
fluid
.
layers
.
reduce_max
(
prob
,
dim
=-
1
)
iou_mask
=
iou_mask
*
fluid
.
layers
.
cast
(
iou_mask
=
iou_mask
*
fluid
.
layers
.
cast
(
max_prob
<=
0.25
,
dtype
=
"float32"
)
max_prob
<=
0.25
,
dtype
=
"float32"
)
output_shape
=
fluid
.
layers
.
shape
(
output
)
output_shape
=
fluid
.
layers
.
shape
(
output
)
an_num
=
len
(
anchors
)
//
2
an_num
=
len
(
anchors
)
//
2
iou_mask
=
fluid
.
layers
.
reshape
(
iou_mask
,
(
-
1
,
an_num
,
output_shape
[
2
],
iou_mask
=
fluid
.
layers
.
reshape
(
iou_mask
,
(
-
1
,
an_num
,
output_shape
[
2
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录