Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8218e301
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8218e301
编写于
1月 04, 2019
作者:
D
dengkaipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add gtscore. test=develop
上级
3c08f620
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
57 addition
and
23 deletion
+57
-23
paddle/fluid/API.spec
paddle/fluid/API.spec
+1
-1
paddle/fluid/operators/yolov3_loss_op.cc
paddle/fluid/operators/yolov3_loss_op.cc
+18
-2
paddle/fluid/operators/yolov3_loss_op.h
paddle/fluid/operators/yolov3_loss_op.h
+14
-8
python/paddle/fluid/layers/detection.py
python/paddle/fluid/layers/detection.py
+13
-4
python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py
python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py
+11
-8
未找到文件。
paddle/fluid/API.spec
浏览文件 @
8218e301
...
@@ -324,7 +324,7 @@ paddle.fluid.layers.generate_mask_labels ArgSpec(args=['im_info', 'gt_classes',
...
@@ -324,7 +324,7 @@ paddle.fluid.layers.generate_mask_labels ArgSpec(args=['im_info', 'gt_classes',
paddle.fluid.layers.iou_similarity ArgSpec(args=['x', 'y', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.iou_similarity ArgSpec(args=['x', 'y', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.box_coder ArgSpec(args=['prior_box', 'prior_box_var', 'target_box', 'code_type', 'box_normalized', 'name'], varargs=None, keywords=None, defaults=('encode_center_size', True, None))
paddle.fluid.layers.box_coder ArgSpec(args=['prior_box', 'prior_box_var', 'target_box', 'code_type', 'box_normalized', 'name'], varargs=None, keywords=None, defaults=('encode_center_size', True, None))
paddle.fluid.layers.polygon_box_transform ArgSpec(args=['input', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.polygon_box_transform ArgSpec(args=['input', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.yolov3_loss ArgSpec(args=['x', 'gtbox', 'gtlabel', 'anchors', 'anchor_mask', 'class_num', 'ignore_thresh', 'downsample', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.yolov3_loss ArgSpec(args=['x', 'gtbox', 'gtlabel', '
gtscore', '
anchors', 'anchor_mask', 'class_num', 'ignore_thresh', 'downsample', 'name'], varargs=None, keywords=None, defaults=(None,))
paddle.fluid.layers.multiclass_nms ArgSpec(args=['bboxes', 'scores', 'score_threshold', 'nms_top_k', 'keep_top_k', 'nms_threshold', 'normalized', 'nms_eta', 'background_label', 'name'], varargs=None, keywords=None, defaults=(0.3, True, 1.0, 0, None))
paddle.fluid.layers.multiclass_nms ArgSpec(args=['bboxes', 'scores', 'score_threshold', 'nms_top_k', 'keep_top_k', 'nms_threshold', 'normalized', 'nms_eta', 'background_label', 'name'], varargs=None, keywords=None, defaults=(0.3, True, 1.0, 0, None))
paddle.fluid.layers.accuracy ArgSpec(args=['input', 'label', 'k', 'correct', 'total'], varargs=None, keywords=None, defaults=(1, None, None))
paddle.fluid.layers.accuracy ArgSpec(args=['input', 'label', 'k', 'correct', 'total'], varargs=None, keywords=None, defaults=(1, None, None))
paddle.fluid.layers.auc ArgSpec(args=['input', 'label', 'curve', 'num_thresholds', 'topk', 'slide_steps'], varargs=None, keywords=None, defaults=('ROC', 4095, 1, 1))
paddle.fluid.layers.auc ArgSpec(args=['input', 'label', 'curve', 'num_thresholds', 'topk', 'slide_steps'], varargs=None, keywords=None, defaults=('ROC', 4095, 1, 1))
...
...
paddle/fluid/operators/yolov3_loss_op.cc
浏览文件 @
8218e301
...
@@ -27,6 +27,8 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
...
@@ -27,6 +27,8 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
"Input(GTBox) of Yolov3LossOp should not be null."
);
"Input(GTBox) of Yolov3LossOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"GTLabel"
),
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"GTLabel"
),
"Input(GTLabel) of Yolov3LossOp should not be null."
);
"Input(GTLabel) of Yolov3LossOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"GTScore"
),
"Input(GTScore) of Yolov3LossOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Loss"
),
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Loss"
),
"Output(Loss) of Yolov3LossOp should not be null."
);
"Output(Loss) of Yolov3LossOp should not be null."
);
PADDLE_ENFORCE
(
PADDLE_ENFORCE
(
...
@@ -38,6 +40,7 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
...
@@ -38,6 +40,7 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
auto
dim_x
=
ctx
->
GetInputDim
(
"X"
);
auto
dim_x
=
ctx
->
GetInputDim
(
"X"
);
auto
dim_gtbox
=
ctx
->
GetInputDim
(
"GTBox"
);
auto
dim_gtbox
=
ctx
->
GetInputDim
(
"GTBox"
);
auto
dim_gtlabel
=
ctx
->
GetInputDim
(
"GTLabel"
);
auto
dim_gtlabel
=
ctx
->
GetInputDim
(
"GTLabel"
);
auto
dim_gtscore
=
ctx
->
GetInputDim
(
"GTScore"
);
auto
anchors
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"anchors"
);
auto
anchors
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"anchors"
);
int
anchor_num
=
anchors
.
size
()
/
2
;
int
anchor_num
=
anchors
.
size
()
/
2
;
auto
anchor_mask
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"anchor_mask"
);
auto
anchor_mask
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"anchor_mask"
);
...
@@ -54,11 +57,17 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
...
@@ -54,11 +57,17 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
"Input(GTBox) should be a 3-D tensor"
);
"Input(GTBox) should be a 3-D tensor"
);
PADDLE_ENFORCE_EQ
(
dim_gtbox
[
2
],
4
,
"Input(GTBox) dim[2] should be 5"
);
PADDLE_ENFORCE_EQ
(
dim_gtbox
[
2
],
4
,
"Input(GTBox) dim[2] should be 5"
);
PADDLE_ENFORCE_EQ
(
dim_gtlabel
.
size
(),
2
,
PADDLE_ENFORCE_EQ
(
dim_gtlabel
.
size
(),
2
,
"Input(GT
Box
) should be a 2-D tensor"
);
"Input(GT
Label
) should be a 2-D tensor"
);
PADDLE_ENFORCE_EQ
(
dim_gtlabel
[
0
],
dim_gtbox
[
0
],
PADDLE_ENFORCE_EQ
(
dim_gtlabel
[
0
],
dim_gtbox
[
0
],
"Input(GTBox) and Input(GTLabel) dim[0] should be same"
);
"Input(GTBox) and Input(GTLabel) dim[0] should be same"
);
PADDLE_ENFORCE_EQ
(
dim_gtlabel
[
1
],
dim_gtbox
[
1
],
PADDLE_ENFORCE_EQ
(
dim_gtlabel
[
1
],
dim_gtbox
[
1
],
"Input(GTBox) and Input(GTLabel) dim[1] should be same"
);
"Input(GTBox) and Input(GTLabel) dim[1] should be same"
);
PADDLE_ENFORCE_EQ
(
dim_gtscore
.
size
(),
2
,
"Input(GTScore) should be a 2-D tensor"
);
PADDLE_ENFORCE_EQ
(
dim_gtscore
[
0
],
dim_gtbox
[
0
],
"Input(GTBox) and Input(GTScore) dim[0] should be same"
);
PADDLE_ENFORCE_EQ
(
dim_gtscore
[
1
],
dim_gtbox
[
1
],
"Input(GTBox) and Input(GTScore) dim[1] should be same"
);
PADDLE_ENFORCE_GT
(
anchors
.
size
(),
0
,
PADDLE_ENFORCE_GT
(
anchors
.
size
(),
0
,
"Attr(anchors) length should be greater then 0."
);
"Attr(anchors) length should be greater then 0."
);
PADDLE_ENFORCE_EQ
(
anchors
.
size
()
%
2
,
0
,
PADDLE_ENFORCE_EQ
(
anchors
.
size
()
%
2
,
0
,
...
@@ -109,8 +118,13 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -109,8 +118,13 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker {
AddInput
(
"GTLabel"
,
AddInput
(
"GTLabel"
,
"The input tensor of ground truth label, "
"The input tensor of ground truth label, "
"This is a 2-D tensor with shape of [N, max_box_num], "
"This is a 2-D tensor with shape of [N, max_box_num], "
"and each element shou
dl
be an integer to indicate the "
"and each element shou
ld
be an integer to indicate the "
"box class id."
);
"box class id."
);
AddInput
(
"GTScore"
,
"The score of GTLabel, This is a 2-D tensor in same shape "
"GTLabel, and score values should in range (0, 1). This "
"input is for GTLabel score can be not 1.0 in image mixup "
"augmentation."
);
AddOutput
(
"Loss"
,
AddOutput
(
"Loss"
,
"The output yolov3 loss tensor, "
"The output yolov3 loss tensor, "
"This is a 1-D tensor with shape of [N]"
);
"This is a 1-D tensor with shape of [N]"
);
...
@@ -228,6 +242,7 @@ class Yolov3LossGradMaker : public framework::SingleGradOpDescMaker {
...
@@ -228,6 +242,7 @@ class Yolov3LossGradMaker : public framework::SingleGradOpDescMaker {
op
->
SetInput
(
"X"
,
Input
(
"X"
));
op
->
SetInput
(
"X"
,
Input
(
"X"
));
op
->
SetInput
(
"GTBox"
,
Input
(
"GTBox"
));
op
->
SetInput
(
"GTBox"
,
Input
(
"GTBox"
));
op
->
SetInput
(
"GTLabel"
,
Input
(
"GTLabel"
));
op
->
SetInput
(
"GTLabel"
,
Input
(
"GTLabel"
));
op
->
SetInput
(
"GTScore"
,
Input
(
"GTScore"
));
op
->
SetInput
(
framework
::
GradVarName
(
"Loss"
),
OutputGrad
(
"Loss"
));
op
->
SetInput
(
framework
::
GradVarName
(
"Loss"
),
OutputGrad
(
"Loss"
));
op
->
SetInput
(
"ObjectnessMask"
,
Output
(
"ObjectnessMask"
));
op
->
SetInput
(
"ObjectnessMask"
,
Output
(
"ObjectnessMask"
));
op
->
SetInput
(
"GTMatchMask"
,
Output
(
"GTMatchMask"
));
op
->
SetInput
(
"GTMatchMask"
,
Output
(
"GTMatchMask"
));
...
@@ -237,6 +252,7 @@ class Yolov3LossGradMaker : public framework::SingleGradOpDescMaker {
...
@@ -237,6 +252,7 @@ class Yolov3LossGradMaker : public framework::SingleGradOpDescMaker {
op
->
SetOutput
(
framework
::
GradVarName
(
"X"
),
InputGrad
(
"X"
));
op
->
SetOutput
(
framework
::
GradVarName
(
"X"
),
InputGrad
(
"X"
));
op
->
SetOutput
(
framework
::
GradVarName
(
"GTBox"
),
{});
op
->
SetOutput
(
framework
::
GradVarName
(
"GTBox"
),
{});
op
->
SetOutput
(
framework
::
GradVarName
(
"GTLabel"
),
{});
op
->
SetOutput
(
framework
::
GradVarName
(
"GTLabel"
),
{});
op
->
SetOutput
(
framework
::
GradVarName
(
"GTScore"
),
{});
return
std
::
unique_ptr
<
framework
::
OpDesc
>
(
op
);
return
std
::
unique_ptr
<
framework
::
OpDesc
>
(
op
);
}
}
};
};
...
...
paddle/fluid/operators/yolov3_loss_op.h
浏览文件 @
8218e301
...
@@ -156,25 +156,25 @@ static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input,
...
@@ -156,25 +156,25 @@ static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input,
template
<
typename
T
>
template
<
typename
T
>
static
inline
void
CalcLabelLoss
(
T
*
loss
,
const
T
*
input
,
const
int
index
,
static
inline
void
CalcLabelLoss
(
T
*
loss
,
const
T
*
input
,
const
int
index
,
const
int
label
,
const
int
class_num
,
const
int
label
,
const
T
score
,
const
int
stride
)
{
const
int
class_num
,
const
int
stride
)
{
for
(
int
i
=
0
;
i
<
class_num
;
i
++
)
{
for
(
int
i
=
0
;
i
<
class_num
;
i
++
)
{
T
pred
=
input
[
index
+
i
*
stride
]
<
-
0.5
?
input
[
index
+
i
*
stride
]
T
pred
=
input
[
index
+
i
*
stride
]
<
-
0.5
?
input
[
index
+
i
*
stride
]
:
1.0
/
class_num
;
:
1.0
/
class_num
;
loss
[
0
]
+=
SCE
<
T
>
(
pred
,
(
i
==
label
)
?
1.0
:
0.0
);
loss
[
0
]
+=
SCE
<
T
>
(
pred
,
(
i
==
label
)
?
score
:
0.0
);
}
}
}
}
template
<
typename
T
>
template
<
typename
T
>
static
inline
void
CalcLabelLossGrad
(
T
*
input_grad
,
const
T
loss
,
static
inline
void
CalcLabelLossGrad
(
T
*
input_grad
,
const
T
loss
,
const
T
*
input
,
const
int
index
,
const
T
*
input
,
const
int
index
,
const
int
label
,
const
int
class_num
,
const
int
label
,
const
T
score
,
const
int
stride
)
{
const
int
class_num
,
const
int
stride
)
{
for
(
int
i
=
0
;
i
<
class_num
;
i
++
)
{
for
(
int
i
=
0
;
i
<
class_num
;
i
++
)
{
T
pred
=
input
[
index
+
i
*
stride
]
<
-
0.5
?
input
[
index
+
i
*
stride
]
T
pred
=
input
[
index
+
i
*
stride
]
<
-
0.5
?
input
[
index
+
i
*
stride
]
:
1.0
/
class_num
;
:
1.0
/
class_num
;
input_grad
[
index
+
i
*
stride
]
=
input_grad
[
index
+
i
*
stride
]
=
SCEGrad
<
T
>
(
pred
,
(
i
==
label
)
?
1.0
:
0.0
)
*
loss
;
SCEGrad
<
T
>
(
pred
,
(
i
==
label
)
?
score
:
0.0
)
*
loss
;
}
}
}
}
...
@@ -246,6 +246,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
...
@@ -246,6 +246,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
gt_box
=
ctx
.
Input
<
Tensor
>
(
"GTBox"
);
auto
*
gt_box
=
ctx
.
Input
<
Tensor
>
(
"GTBox"
);
auto
*
gt_label
=
ctx
.
Input
<
Tensor
>
(
"GTLabel"
);
auto
*
gt_label
=
ctx
.
Input
<
Tensor
>
(
"GTLabel"
);
auto
*
gt_score
=
ctx
.
Input
<
Tensor
>
(
"GTScore"
);
auto
*
loss
=
ctx
.
Output
<
Tensor
>
(
"Loss"
);
auto
*
loss
=
ctx
.
Output
<
Tensor
>
(
"Loss"
);
auto
*
objness_mask
=
ctx
.
Output
<
Tensor
>
(
"ObjectnessMask"
);
auto
*
objness_mask
=
ctx
.
Output
<
Tensor
>
(
"ObjectnessMask"
);
auto
*
gt_match_mask
=
ctx
.
Output
<
Tensor
>
(
"GTMatchMask"
);
auto
*
gt_match_mask
=
ctx
.
Output
<
Tensor
>
(
"GTMatchMask"
);
...
@@ -269,6 +270,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
...
@@ -269,6 +270,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
gt_box_data
=
gt_box
->
data
<
T
>
();
const
T
*
gt_box_data
=
gt_box
->
data
<
T
>
();
const
int
*
gt_label_data
=
gt_label
->
data
<
int
>
();
const
int
*
gt_label_data
=
gt_label
->
data
<
int
>
();
const
T
*
gt_score_data
=
gt_score
->
data
<
T
>
();
T
*
loss_data
=
loss
->
mutable_data
<
T
>
({
n
},
ctx
.
GetPlace
());
T
*
loss_data
=
loss
->
mutable_data
<
T
>
({
n
},
ctx
.
GetPlace
());
memset
(
loss_data
,
0
,
loss
->
numel
()
*
sizeof
(
T
));
memset
(
loss_data
,
0
,
loss
->
numel
()
*
sizeof
(
T
));
int
*
obj_mask_data
=
int
*
obj_mask_data
=
...
@@ -358,9 +360,10 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
...
@@ -358,9 +360,10 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
obj_mask_data
[
obj_idx
]
=
1
;
obj_mask_data
[
obj_idx
]
=
1
;
int
label
=
gt_label_data
[
i
*
b
+
t
];
int
label
=
gt_label_data
[
i
*
b
+
t
];
T
score
=
gt_score_data
[
i
*
b
+
t
];
int
label_idx
=
GetEntryIndex
(
i
,
mask_idx
,
gj
*
w
+
gi
,
mask_num
,
int
label_idx
=
GetEntryIndex
(
i
,
mask_idx
,
gj
*
w
+
gi
,
mask_num
,
an_stride
,
stride
,
5
);
an_stride
,
stride
,
5
);
CalcLabelLoss
<
T
>
(
loss_data
+
i
,
input_data
,
label_idx
,
label
,
CalcLabelLoss
<
T
>
(
loss_data
+
i
,
input_data
,
label_idx
,
label
,
score
,
class_num
,
stride
);
class_num
,
stride
);
}
}
}
}
...
@@ -378,6 +381,7 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
...
@@ -378,6 +381,7 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
gt_box
=
ctx
.
Input
<
Tensor
>
(
"GTBox"
);
auto
*
gt_box
=
ctx
.
Input
<
Tensor
>
(
"GTBox"
);
auto
*
gt_label
=
ctx
.
Input
<
Tensor
>
(
"GTLabel"
);
auto
*
gt_label
=
ctx
.
Input
<
Tensor
>
(
"GTLabel"
);
auto
*
gt_score
=
ctx
.
Input
<
Tensor
>
(
"GTScore"
);
auto
*
input_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
input_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
loss_grad
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Loss"
));
auto
*
loss_grad
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Loss"
));
auto
*
objness_mask
=
ctx
.
Input
<
Tensor
>
(
"ObjectnessMask"
);
auto
*
objness_mask
=
ctx
.
Input
<
Tensor
>
(
"ObjectnessMask"
);
...
@@ -401,6 +405,7 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
...
@@ -401,6 +405,7 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
input_data
=
input
->
data
<
T
>
();
const
T
*
gt_box_data
=
gt_box
->
data
<
T
>
();
const
T
*
gt_box_data
=
gt_box
->
data
<
T
>
();
const
int
*
gt_label_data
=
gt_label
->
data
<
int
>
();
const
int
*
gt_label_data
=
gt_label
->
data
<
int
>
();
const
T
*
gt_score_data
=
gt_score
->
data
<
T
>
();
const
T
*
loss_grad_data
=
loss_grad
->
data
<
T
>
();
const
T
*
loss_grad_data
=
loss_grad
->
data
<
T
>
();
const
int
*
obj_mask_data
=
objness_mask
->
data
<
int
>
();
const
int
*
obj_mask_data
=
objness_mask
->
data
<
int
>
();
const
int
*
gt_match_mask_data
=
gt_match_mask
->
data
<
int
>
();
const
int
*
gt_match_mask_data
=
gt_match_mask
->
data
<
int
>
();
...
@@ -423,10 +428,11 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
...
@@ -423,10 +428,11 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
anchor_mask
[
mask_idx
],
box_idx
,
gi
,
gj
,
h
,
input_size
,
stride
);
anchor_mask
[
mask_idx
],
box_idx
,
gi
,
gj
,
h
,
input_size
,
stride
);
int
label
=
gt_label_data
[
i
*
b
+
t
];
int
label
=
gt_label_data
[
i
*
b
+
t
];
T
score
=
gt_score_data
[
i
*
b
+
t
];
int
label_idx
=
GetEntryIndex
(
i
,
mask_idx
,
gj
*
w
+
gi
,
mask_num
,
int
label_idx
=
GetEntryIndex
(
i
,
mask_idx
,
gj
*
w
+
gi
,
mask_num
,
an_stride
,
stride
,
5
);
an_stride
,
stride
,
5
);
CalcLabelLossGrad
<
T
>
(
input_grad_data
,
loss_grad_data
[
i
],
input_data
,
CalcLabelLossGrad
<
T
>
(
input_grad_data
,
loss_grad_data
[
i
],
input_data
,
label_idx
,
label
,
class_num
,
stride
);
label_idx
,
label
,
score
,
class_num
,
stride
);
}
}
}
}
}
}
...
...
python/paddle/fluid/layers/detection.py
浏览文件 @
8218e301
...
@@ -412,6 +412,7 @@ def polygon_box_transform(input, name=None):
...
@@ -412,6 +412,7 @@ def polygon_box_transform(input, name=None):
def
yolov3_loss
(
x
,
def
yolov3_loss
(
x
,
gtbox
,
gtbox
,
gtlabel
,
gtlabel
,
gtscore
,
anchors
,
anchors
,
anchor_mask
,
anchor_mask
,
class_num
,
class_num
,
...
@@ -428,8 +429,10 @@ def yolov3_loss(x,
...
@@ -428,8 +429,10 @@ def yolov3_loss(x,
and x, y, w, h should be relative value of input image.
and x, y, w, h should be relative value of input image.
N is the batch number and B is the max box number in
N is the batch number and B is the max box number in
an image.
an image.
gtlabel (Variable): class id of ground truth boxes, shoud be in
s
shape
gtlabel (Variable): class id of ground truth boxes, shoud be in shape
of [N, B].
of [N, B].
gtscore (Variable): score of gtlabel, should be in same shape with gtlabel
and score value in range (0, 1).
anchors (list|tuple): ${anchors_comment}
anchors (list|tuple): ${anchors_comment}
anchor_mask (list|tuple): ${anchor_mask_comment}
anchor_mask (list|tuple): ${anchor_mask_comment}
class_num (int): ${class_num_comment}
class_num (int): ${class_num_comment}
...
@@ -444,6 +447,7 @@ def yolov3_loss(x,
...
@@ -444,6 +447,7 @@ def yolov3_loss(x,
TypeError: Input x of yolov3_loss must be Variable
TypeError: Input x of yolov3_loss must be Variable
TypeError: Input gtbox of yolov3_loss must be Variable"
TypeError: Input gtbox of yolov3_loss must be Variable"
TypeError: Input gtlabel of yolov3_loss must be Variable"
TypeError: Input gtlabel of yolov3_loss must be Variable"
TypeError: Input gtscore of yolov3_loss must be Variable"
TypeError: Attr anchors of yolov3_loss must be list or tuple
TypeError: Attr anchors of yolov3_loss must be list or tuple
TypeError: Attr class_num of yolov3_loss must be an integer
TypeError: Attr class_num of yolov3_loss must be an integer
TypeError: Attr ignore_thresh of yolov3_loss must be a float number
TypeError: Attr ignore_thresh of yolov3_loss must be a float number
...
@@ -467,6 +471,8 @@ def yolov3_loss(x,
...
@@ -467,6 +471,8 @@ def yolov3_loss(x,
raise
TypeError
(
"Input gtbox of yolov3_loss must be Variable"
)
raise
TypeError
(
"Input gtbox of yolov3_loss must be Variable"
)
if
not
isinstance
(
gtlabel
,
Variable
):
if
not
isinstance
(
gtlabel
,
Variable
):
raise
TypeError
(
"Input gtlabel of yolov3_loss must be Variable"
)
raise
TypeError
(
"Input gtlabel of yolov3_loss must be Variable"
)
if
not
isinstance
(
gtscore
,
Variable
):
raise
TypeError
(
"Input gtscore of yolov3_loss must be Variable"
)
if
not
isinstance
(
anchors
,
list
)
and
not
isinstance
(
anchors
,
tuple
):
if
not
isinstance
(
anchors
,
list
)
and
not
isinstance
(
anchors
,
tuple
):
raise
TypeError
(
"Attr anchors of yolov3_loss must be list or tuple"
)
raise
TypeError
(
"Attr anchors of yolov3_loss must be list or tuple"
)
if
not
isinstance
(
anchor_mask
,
list
)
and
not
isinstance
(
anchor_mask
,
tuple
):
if
not
isinstance
(
anchor_mask
,
list
)
and
not
isinstance
(
anchor_mask
,
tuple
):
...
@@ -496,9 +502,12 @@ def yolov3_loss(x,
...
@@ -496,9 +502,12 @@ def yolov3_loss(x,
helper
.
append_op
(
helper
.
append_op
(
type
=
'yolov3_loss'
,
type
=
'yolov3_loss'
,
inputs
=
{
"X"
:
x
,
inputs
=
{
"GTBox"
:
gtbox
,
"X"
:
x
,
"GTLabel"
:
gtlabel
},
"GTBox"
:
gtbox
,
"GTLabel"
:
gtlabel
,
"GTScore"
:
gtscore
},
outputs
=
{
outputs
=
{
'Loss'
:
loss
,
'Loss'
:
loss
,
'ObjectnessMask'
:
objectness_mask
,
'ObjectnessMask'
:
objectness_mask
,
...
...
python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py
浏览文件 @
8218e301
...
@@ -66,7 +66,7 @@ def batch_xywh_box_iou(box1, box2):
...
@@ -66,7 +66,7 @@ def batch_xywh_box_iou(box1, box2):
return
inter_area
/
union
return
inter_area
/
union
def
YOLOv3Loss
(
x
,
gtbox
,
gtlabel
,
attrs
):
def
YOLOv3Loss
(
x
,
gtbox
,
gtlabel
,
gtscore
,
attrs
):
n
,
c
,
h
,
w
=
x
.
shape
n
,
c
,
h
,
w
=
x
.
shape
b
=
gtbox
.
shape
[
1
]
b
=
gtbox
.
shape
[
1
]
anchors
=
attrs
[
'anchors'
]
anchors
=
attrs
[
'anchors'
]
...
@@ -148,7 +148,7 @@ def YOLOv3Loss(x, gtbox, gtlabel, attrs):
...
@@ -148,7 +148,7 @@ def YOLOv3Loss(x, gtbox, gtlabel, attrs):
for
label_idx
in
range
(
class_num
):
for
label_idx
in
range
(
class_num
):
loss
[
i
]
+=
sce
(
x
[
i
,
an_idx
,
gj
,
gi
,
5
+
label_idx
],
loss
[
i
]
+=
sce
(
x
[
i
,
an_idx
,
gj
,
gi
,
5
+
label_idx
],
int
(
label_idx
==
gtlabel
[
i
,
j
]))
int
(
label_idx
==
gtlabel
[
i
,
j
])
*
gtscore
[
i
,
j
]
)
for
j
in
range
(
mask_num
*
h
*
w
):
for
j
in
range
(
mask_num
*
h
*
w
):
if
objness
[
i
,
j
]
>=
0
:
if
objness
[
i
,
j
]
>=
0
:
...
@@ -165,6 +165,7 @@ class TestYolov3LossOp(OpTest):
...
@@ -165,6 +165,7 @@ class TestYolov3LossOp(OpTest):
x
=
logit
(
np
.
random
.
uniform
(
0
,
1
,
self
.
x_shape
).
astype
(
'float32'
))
x
=
logit
(
np
.
random
.
uniform
(
0
,
1
,
self
.
x_shape
).
astype
(
'float32'
))
gtbox
=
np
.
random
.
random
(
size
=
self
.
gtbox_shape
).
astype
(
'float32'
)
gtbox
=
np
.
random
.
random
(
size
=
self
.
gtbox_shape
).
astype
(
'float32'
)
gtlabel
=
np
.
random
.
randint
(
0
,
self
.
class_num
,
self
.
gtbox_shape
[:
2
])
gtlabel
=
np
.
random
.
randint
(
0
,
self
.
class_num
,
self
.
gtbox_shape
[:
2
])
gtscore
=
np
.
random
.
random
(
self
.
gtbox_shape
[:
2
]).
astype
(
'float32'
)
gtmask
=
np
.
random
.
randint
(
0
,
2
,
self
.
gtbox_shape
[:
2
])
gtmask
=
np
.
random
.
randint
(
0
,
2
,
self
.
gtbox_shape
[:
2
])
gtbox
=
gtbox
*
gtmask
[:,
:,
np
.
newaxis
]
gtbox
=
gtbox
*
gtmask
[:,
:,
np
.
newaxis
]
gtlabel
=
gtlabel
*
gtmask
gtlabel
=
gtlabel
*
gtmask
...
@@ -180,9 +181,11 @@ class TestYolov3LossOp(OpTest):
...
@@ -180,9 +181,11 @@ class TestYolov3LossOp(OpTest):
self
.
inputs
=
{
self
.
inputs
=
{
'X'
:
x
,
'X'
:
x
,
'GTBox'
:
gtbox
.
astype
(
'float32'
),
'GTBox'
:
gtbox
.
astype
(
'float32'
),
'GTLabel'
:
gtlabel
.
astype
(
'int32'
)
'GTLabel'
:
gtlabel
.
astype
(
'int32'
),
'GTScore'
:
gtscore
.
astype
(
'float32'
)
}
}
loss
,
objness
,
gt_matches
=
YOLOv3Loss
(
x
,
gtbox
,
gtlabel
,
self
.
attrs
)
loss
,
objness
,
gt_matches
=
YOLOv3Loss
(
x
,
gtbox
,
gtlabel
,
gtscore
,
self
.
attrs
)
self
.
outputs
=
{
self
.
outputs
=
{
'Loss'
:
loss
,
'Loss'
:
loss
,
'ObjectnessMask'
:
objness
,
'ObjectnessMask'
:
objness
,
...
@@ -198,8 +201,8 @@ class TestYolov3LossOp(OpTest):
...
@@ -198,8 +201,8 @@ class TestYolov3LossOp(OpTest):
self
.
check_grad_with_place
(
self
.
check_grad_with_place
(
place
,
[
'X'
],
place
,
[
'X'
],
'Loss'
,
'Loss'
,
no_grad_set
=
set
([
"GTBox"
,
"GTLabel"
]),
no_grad_set
=
set
([
"GTBox"
,
"GTLabel"
,
"GTScore"
]),
max_relative_error
=
0.
15
)
max_relative_error
=
0.
2
)
def
initTestCase
(
self
):
def
initTestCase
(
self
):
self
.
anchors
=
[
self
.
anchors
=
[
...
@@ -207,11 +210,11 @@ class TestYolov3LossOp(OpTest):
...
@@ -207,11 +210,11 @@ class TestYolov3LossOp(OpTest):
373
,
326
373
,
326
]
]
self
.
anchor_mask
=
[
0
,
1
,
2
]
self
.
anchor_mask
=
[
0
,
1
,
2
]
self
.
class_num
=
5
self
.
class_num
=
10
self
.
ignore_thresh
=
0.7
self
.
ignore_thresh
=
0.7
self
.
downsample
=
32
self
.
downsample
=
32
self
.
x_shape
=
(
3
,
len
(
self
.
anchor_mask
)
*
(
5
+
self
.
class_num
),
5
,
5
)
self
.
x_shape
=
(
3
,
len
(
self
.
anchor_mask
)
*
(
5
+
self
.
class_num
),
5
,
5
)
self
.
gtbox_shape
=
(
3
,
5
,
4
)
self
.
gtbox_shape
=
(
3
,
10
,
4
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录