Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
0c4acc83
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0c4acc83
编写于
12月 16, 2018
作者:
D
dengkaipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
imporve yolo loss implement. test=develop
上级
2fbfef2e
变更
5
展开全部
隐藏空白更改
内联
并排
Showing
5 changed file
with
267 addition
and
267 deletion
+267
-267
paddle/fluid/operators/yolov3_loss_op.cc
paddle/fluid/operators/yolov3_loss_op.cc
+2
-15
paddle/fluid/operators/yolov3_loss_op.h
paddle/fluid/operators/yolov3_loss_op.h
+240
-192
python/paddle/fluid/layers/detection.py
python/paddle/fluid/layers/detection.py
+12
-22
python/paddle/fluid/tests/unittests/op_test.py
python/paddle/fluid/tests/unittests/op_test.py
+0
-2
python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py
python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py
+13
-36
未找到文件。
paddle/fluid/operators/yolov3_loss_op.cc
浏览文件 @
0c4acc83
...
...
@@ -34,11 +34,12 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
auto
dim_gtbox
=
ctx
->
GetInputDim
(
"GTBox"
);
auto
dim_gtlabel
=
ctx
->
GetInputDim
(
"GTLabel"
);
auto
anchors
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"anchors"
);
int
anchor_num
=
anchors
.
size
()
/
2
;
auto
class_num
=
ctx
->
Attrs
().
Get
<
int
>
(
"class_num"
);
PADDLE_ENFORCE_EQ
(
dim_x
.
size
(),
4
,
"Input(X) should be a 4-D tensor."
);
PADDLE_ENFORCE_EQ
(
dim_x
[
2
],
dim_x
[
3
],
"Input(X) dim[3] and dim[4] should be euqal."
);
PADDLE_ENFORCE_EQ
(
dim_x
[
1
],
anchor
s
.
size
()
/
2
*
(
5
+
class_num
),
PADDLE_ENFORCE_EQ
(
dim_x
[
1
],
anchor
_num
*
(
5
+
class_num
),
"Input(X) dim[1] should be equal to (anchor_number * (5 "
"+ class_num))."
);
PADDLE_ENFORCE_EQ
(
dim_gtbox
.
size
(),
3
,
...
...
@@ -105,20 +106,6 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker {
.
SetDefault
(
406
);
AddAttr
<
float
>
(
"ignore_thresh"
,
"The ignore threshold to ignore confidence loss."
);
AddAttr
<
float
>
(
"loss_weight_xy"
,
"The weight of x, y location loss."
)
.
SetDefault
(
1.0
);
AddAttr
<
float
>
(
"loss_weight_wh"
,
"The weight of w, h location loss."
)
.
SetDefault
(
1.0
);
AddAttr
<
float
>
(
"loss_weight_conf_target"
,
"The weight of confidence score loss in locations with target object."
)
.
SetDefault
(
1.0
);
AddAttr
<
float
>
(
"loss_weight_conf_notarget"
,
"The weight of confidence score loss in locations without "
"target object."
)
.
SetDefault
(
1.0
);
AddAttr
<
float
>
(
"loss_weight_class"
,
"The weight of classification loss."
)
.
SetDefault
(
1.0
);
AddComment
(
R"DOC(
This operator generate yolov3 loss by given predict result and ground
truth boxes.
...
...
paddle/fluid/operators/yolov3_loss_op.h
浏览文件 @
0c4acc83
此差异已折叠。
点击以展开。
python/paddle/fluid/layers/detection.py
浏览文件 @
0c4acc83
...
...
@@ -416,11 +416,6 @@ def yolov3_loss(x,
class_num
,
ignore_thresh
,
input_size
,
loss_weight_xy
=
None
,
loss_weight_wh
=
None
,
loss_weight_conf_target
=
None
,
loss_weight_conf_notarget
=
None
,
loss_weight_class
=
None
,
name
=
None
):
"""
${comment}
...
...
@@ -438,11 +433,6 @@ def yolov3_loss(x,
class_num (int): ${class_num_comment}
ignore_thresh (float): ${ignore_thresh_comment}
input_size (int): ${input_size_comment}
loss_weight_xy (float|None): ${loss_weight_xy_comment}
loss_weight_wh (float|None): ${loss_weight_wh_comment}
loss_weight_conf_target (float|None): ${loss_weight_conf_target_comment}
loss_weight_conf_notarget (float|None): ${loss_weight_conf_notarget_comment}
loss_weight_class (float|None): ${loss_weight_class_comment}
name (string): the name of yolov3 loss
Returns:
...
...
@@ -495,18 +485,18 @@ def yolov3_loss(x,
"input_size"
:
input_size
,
}
if
loss_weight_xy
is
not
None
and
isinstance
(
loss_weight_xy
,
float
):
self
.
attrs
[
'loss_weight_xy'
]
=
loss_weight_xy
if
loss_weight_wh
is
not
None
and
isinstance
(
loss_weight_wh
,
float
):
self
.
attrs
[
'loss_weight_wh'
]
=
loss_weight_wh
if
loss_weight_conf_target
is
not
None
and
isinstance
(
loss_weight_conf_target
,
float
):
self
.
attrs
[
'loss_weight_conf_target'
]
=
loss_weight_conf_target
if
loss_weight_conf_notarget
is
not
None
and
isinstance
(
loss_weight_conf_notarget
,
float
):
self
.
attrs
[
'loss_weight_conf_notarget'
]
=
loss_weight_conf_notarget
if
loss_weight_class
is
not
None
and
isinstance
(
loss_weight_class
,
float
):
self
.
attrs
[
'loss_weight_class'
]
=
loss_weight_class
#
if loss_weight_xy is not None and isinstance(loss_weight_xy, float):
#
self.attrs['loss_weight_xy'] = loss_weight_xy
#
if loss_weight_wh is not None and isinstance(loss_weight_wh, float):
#
self.attrs['loss_weight_wh'] = loss_weight_wh
#
if loss_weight_conf_target is not None and isinstance(
#
loss_weight_conf_target, float):
#
self.attrs['loss_weight_conf_target'] = loss_weight_conf_target
#
if loss_weight_conf_notarget is not None and isinstance(
#
loss_weight_conf_notarget, float):
#
self.attrs['loss_weight_conf_notarget'] = loss_weight_conf_notarget
#
if loss_weight_class is not None and isinstance(loss_weight_class, float):
#
self.attrs['loss_weight_class'] = loss_weight_class
helper
.
append_op
(
type
=
'yolov3_loss'
,
...
...
python/paddle/fluid/tests/unittests/op_test.py
浏览文件 @
0c4acc83
...
...
@@ -470,8 +470,6 @@ class OpTest(unittest.TestCase):
]
analytic_grads
=
self
.
_get_gradient
(
inputs_to_check
,
place
,
output_names
,
no_grad_set
)
# print(numeric_grads[0][0, 4, :, :])
# print(analytic_grads[0][0, 4, :, :])
self
.
_assert_is_close
(
numeric_grads
,
analytic_grads
,
inputs_to_check
,
max_relative_error
,
...
...
python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py
浏览文件 @
0c4acc83
...
...
@@ -80,8 +80,8 @@ def build_target(gtboxes, gtlabel, attrs, grid_size):
class_num
=
attrs
[
"class_num"
]
input_size
=
attrs
[
"input_size"
]
an_num
=
len
(
anchors
)
//
2
conf_mask
=
np
.
ones
((
n
,
an_num
,
grid_size
,
grid_size
)).
astype
(
'float32'
)
obj_mask
=
np
.
zeros
((
n
,
an_num
,
grid_size
,
grid_size
)).
astype
(
'float32'
)
noobj_mask
=
np
.
ones
((
n
,
an_num
,
grid_size
,
grid_size
)).
astype
(
'float32'
)
tx
=
np
.
zeros
((
n
,
an_num
,
grid_size
,
grid_size
)).
astype
(
'float32'
)
ty
=
np
.
zeros
((
n
,
an_num
,
grid_size
,
grid_size
)).
astype
(
'float32'
)
tw
=
np
.
zeros
((
n
,
an_num
,
grid_size
,
grid_size
)).
astype
(
'float32'
)
...
...
@@ -114,10 +114,10 @@ def build_target(gtboxes, gtlabel, attrs, grid_size):
max_iou
=
iou
best_an_index
=
k
if
iou
>
ignore_thresh
:
noobj
_mask
[
i
,
best_an_index
,
gj
,
gi
]
=
0
conf
_mask
[
i
,
best_an_index
,
gj
,
gi
]
=
0
conf_mask
[
i
,
best_an_index
,
gj
,
gi
]
=
1
obj_mask
[
i
,
best_an_index
,
gj
,
gi
]
=
1
noobj_mask
[
i
,
best_an_index
,
gj
,
gi
]
=
0
tx
[
i
,
best_an_index
,
gj
,
gi
]
=
gx
-
gi
ty
[
i
,
best_an_index
,
gj
,
gi
]
=
gy
-
gj
tw
[
i
,
best_an_index
,
gj
,
gi
]
=
np
.
log
(
gw
/
anchors
[
2
*
...
...
@@ -129,7 +129,7 @@ def build_target(gtboxes, gtlabel, attrs, grid_size):
tconf
[
i
,
best_an_index
,
gj
,
gi
]
=
1
tcls
[
i
,
best_an_index
,
gj
,
gi
,
gt_label
]
=
1
return
(
tx
,
ty
,
tw
,
th
,
tweight
,
tconf
,
tcls
,
obj_mask
,
no
obj_mask
)
return
(
tx
,
ty
,
tw
,
th
,
tweight
,
tconf
,
tcls
,
conf_mask
,
obj_mask
)
def
YoloV3Loss
(
x
,
gtbox
,
gtlabel
,
attrs
):
...
...
@@ -144,11 +144,9 @@ def YoloV3Loss(x, gtbox, gtlabel, attrs):
pred_conf
=
x
[:,
:,
:,
:,
4
]
pred_cls
=
x
[:,
:,
:,
:,
5
:]
tx
,
ty
,
tw
,
th
,
tweight
,
tconf
,
tcls
,
obj_mask
,
no
obj_mask
=
build_target
(
tx
,
ty
,
tw
,
th
,
tweight
,
tconf
,
tcls
,
conf_mask
,
obj_mask
=
build_target
(
gtbox
,
gtlabel
,
attrs
,
x
.
shape
[
2
])
# print("obj_mask: ", obj_mask[0, 0, :, :])
# print("noobj_mask: ", noobj_mask[0, 0, :, :])
obj_weight
=
obj_mask
*
tweight
obj_mask_expand
=
np
.
tile
(
np
.
expand_dims
(
obj_mask
,
4
),
(
1
,
1
,
1
,
1
,
int
(
attrs
[
'class_num'
])))
...
...
@@ -156,30 +154,19 @@ def YoloV3Loss(x, gtbox, gtlabel, attrs):
loss_y
=
sce
(
pred_y
,
ty
,
obj_weight
)
loss_w
=
l1loss
(
pred_w
,
tw
,
obj_weight
)
loss_h
=
l1loss
(
pred_h
,
th
,
obj_weight
)
loss_conf_target
=
sce
(
pred_conf
,
tconf
,
obj_mask
)
loss_conf_notarget
=
sce
(
pred_conf
,
tconf
,
noobj_mask
)
loss_obj
=
sce
(
pred_conf
,
tconf
,
conf_mask
)
loss_class
=
sce
(
pred_cls
,
tcls
,
obj_mask_expand
)
# print("loss_xy: ", loss_x + loss_y)
# print("loss_wh: ", loss_w + loss_h)
# print("loss_conf_target: ", loss_conf_target)
# print("loss_conf_notarget: ", loss_conf_notarget)
# print("loss_class: ", loss_class)
# print("python loss_xy: ", loss_x + loss_y)
# print("python loss_wh: ", loss_w + loss_h)
# print("python loss_obj: ", loss_obj)
# print("python loss_class: ", loss_class)
return
attrs
[
'loss_weight_xy'
]
*
(
loss_x
+
loss_y
)
\
+
attrs
[
'loss_weight_wh'
]
*
(
loss_w
+
loss_h
)
\
+
attrs
[
'loss_weight_conf_target'
]
*
loss_conf_target
\
+
attrs
[
'loss_weight_conf_notarget'
]
*
loss_conf_notarget
\
+
attrs
[
'loss_weight_class'
]
*
loss_class
return
loss_x
+
loss_y
+
loss_w
+
loss_h
+
loss_obj
+
loss_class
class
TestYolov3LossOp
(
OpTest
):
def
setUp
(
self
):
self
.
loss_weight_xy
=
1.0
self
.
loss_weight_wh
=
1.0
self
.
loss_weight_conf_target
=
1.0
self
.
loss_weight_conf_notarget
=
1.0
self
.
loss_weight_class
=
1.0
self
.
initTestCase
()
self
.
op_type
=
'yolov3_loss'
x
=
logit
(
np
.
random
.
uniform
(
0
,
1
,
self
.
x_shape
).
astype
(
'float32'
))
...
...
@@ -192,11 +179,6 @@ class TestYolov3LossOp(OpTest):
"class_num"
:
self
.
class_num
,
"ignore_thresh"
:
self
.
ignore_thresh
,
"input_size"
:
self
.
input_size
,
"loss_weight_xy"
:
self
.
loss_weight_xy
,
"loss_weight_wh"
:
self
.
loss_weight_wh
,
"loss_weight_conf_target"
:
self
.
loss_weight_conf_target
,
"loss_weight_conf_notarget"
:
self
.
loss_weight_conf_notarget
,
"loss_weight_class"
:
self
.
loss_weight_class
,
}
self
.
inputs
=
{
'X'
:
x
,
'GTBox'
:
gtbox
,
'GTLabel'
:
gtlabel
}
...
...
@@ -215,17 +197,12 @@ class TestYolov3LossOp(OpTest):
max_relative_error
=
0.31
)
def
initTestCase
(
self
):
self
.
anchors
=
[
12
,
12
]
self
.
anchors
=
[
12
,
12
,
11
,
13
]
self
.
class_num
=
5
self
.
ignore_thresh
=
0.
3
self
.
ignore_thresh
=
0.
5
self
.
input_size
=
416
self
.
x_shape
=
(
3
,
len
(
self
.
anchors
)
//
2
*
(
5
+
self
.
class_num
),
5
,
5
)
self
.
gtbox_shape
=
(
3
,
5
,
4
)
self
.
loss_weight_xy
=
1.2
self
.
loss_weight_wh
=
0.8
self
.
loss_weight_conf_target
=
2.0
self
.
loss_weight_conf_notarget
=
1.0
self
.
loss_weight_class
=
1.5
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录