提交 2b89f590 编写于 作者: T tink2123 提交者: dengkaipeng

add attr use_label_smooth test=develop

上级 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', 'gtscore', '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', 'label_smooth', '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))
......
...@@ -46,6 +46,7 @@ class Yolov3LossOp : public framework::OperatorWithKernel { ...@@ -46,6 +46,7 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
auto anchor_mask = ctx->Attrs().Get<std::vector<int>>("anchor_mask"); auto anchor_mask = ctx->Attrs().Get<std::vector<int>>("anchor_mask");
int mask_num = anchor_mask.size(); int mask_num = anchor_mask.size();
auto class_num = ctx->Attrs().Get<int>("class_num"); 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.size(), 4, "Input(X) should be a 4-D tensor.");
PADDLE_ENFORCE_EQ(dim_x[2], dim_x[3], PADDLE_ENFORCE_EQ(dim_x[2], dim_x[3],
"Input(X) dim[3] and dim[4] should be euqal."); "Input(X) dim[3] and dim[4] should be euqal.");
...@@ -156,6 +157,8 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -156,6 +157,8 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr<float>("ignore_thresh", AddAttr<float>("ignore_thresh",
"The ignore threshold to ignore confidence loss.") "The ignore threshold to ignore confidence loss.")
.SetDefault(0.7); .SetDefault(0.7);
AddAttr<bool>("use_label_smooth", "bool,default True", "use label smooth")
.SetDefault(true);
AddComment(R"DOC( AddComment(R"DOC(
This operator generate yolov3 loss by given predict result and ground This operator generate yolov3 loss by given predict result and ground
truth boxes. truth boxes.
......
...@@ -157,11 +157,19 @@ static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input, ...@@ -157,11 +157,19 @@ 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 T score, const int label, const T score,
const int class_num, const int stride) { const int class_num, const int stride,
for (int i = 0; i < class_num; i++) { const bool use_label_smooth) {
T pred = input[index + i * stride] < -0.5 ? input[index + i * stride] if (use_label_smooth) {
: 1.0 / class_num; for (int i = 0; i < class_num; i++) {
loss[0] += SCE<T>(pred, (i == label) ? score : 0.0); T pred = input[index + i * stride] < -0.5 ? input[index + i * stride]
: 1.0 / class_num;
loss[0] += SCE<T>(pred, (i == label) ? score : 0.0);
}
} else {
for (int i = 0; i < class_num; i++) {
T pred = input[index + i * stride];
loss[0] += SCE<T>(pred, (i == label) ? score : 0.0);
}
} }
} }
...@@ -169,12 +177,21 @@ template <typename T> ...@@ -169,12 +177,21 @@ 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 T score, const int label, const T score,
const int class_num, const int stride) { const int class_num, const int stride,
for (int i = 0; i < class_num; i++) { const bool use_label_smooth) {
T pred = input[index + i * stride] < -0.5 ? input[index + i * stride] if (use_label_smooth) {
: 1.0 / class_num; for (int i = 0; i < class_num; i++) {
input_grad[index + i * stride] = T pred = input[index + i * stride] < -0.5 ? input[index + i * stride]
SCEGrad<T>(pred, (i == label) ? score : 0.0) * loss; : 1.0 / class_num;
input_grad[index + i * stride] =
SCEGrad<T>(pred, (i == label) ? score : 0.0) * loss;
}
} else {
for (int i = 0; i < class_num; i++) {
T pred = input[index + i * stride];
input_grad[index + i * stride] =
SCEGrad<T>(pred, (i == label) ? score : 0.0) * loss;
}
} }
} }
...@@ -255,6 +272,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> { ...@@ -255,6 +272,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
int class_num = ctx.Attr<int>("class_num"); int class_num = ctx.Attr<int>("class_num");
float ignore_thresh = ctx.Attr<float>("ignore_thresh"); float ignore_thresh = ctx.Attr<float>("ignore_thresh");
int downsample = ctx.Attr<int>("downsample"); int downsample = ctx.Attr<int>("downsample");
bool use_label_smooth = ctx.Attr<bool>("use_label_smooth");
const int n = input->dims()[0]; const int n = input->dims()[0];
const int h = input->dims()[2]; const int h = input->dims()[2];
...@@ -364,7 +382,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> { ...@@ -364,7 +382,7 @@ class Yolov3LossKernel : public framework::OpKernel<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, score, CalcLabelLoss<T>(loss_data + i, input_data, label_idx, label, score,
class_num, stride); class_num, stride, use_label_smooth);
} }
} }
} }
...@@ -390,6 +408,7 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> { ...@@ -390,6 +408,7 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
auto anchor_mask = ctx.Attr<std::vector<int>>("anchor_mask"); auto anchor_mask = ctx.Attr<std::vector<int>>("anchor_mask");
int class_num = ctx.Attr<int>("class_num"); int class_num = ctx.Attr<int>("class_num");
int downsample = ctx.Attr<int>("downsample"); int downsample = ctx.Attr<int>("downsample");
bool use_label_smooth = ctx.Attr<bool>("use_label_smooth");
const int n = input_grad->dims()[0]; const int n = input_grad->dims()[0];
const int c = input_grad->dims()[1]; const int c = input_grad->dims()[1];
...@@ -432,7 +451,8 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> { ...@@ -432,7 +451,8 @@ class Yolov3LossGradKernel : public framework::OpKernel<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, score, class_num, stride); label_idx, label, score, class_num, stride,
use_label_smooth);
} }
} }
} }
......
...@@ -418,6 +418,7 @@ def yolov3_loss(x, ...@@ -418,6 +418,7 @@ def yolov3_loss(x,
class_num, class_num,
ignore_thresh, ignore_thresh,
downsample, downsample,
use_label_smooth=True,
name=None): name=None):
""" """
${comment} ${comment}
...@@ -438,6 +439,7 @@ def yolov3_loss(x, ...@@ -438,6 +439,7 @@ def yolov3_loss(x,
class_num (int): ${class_num_comment} class_num (int): ${class_num_comment}
ignore_thresh (float): ${ignore_thresh_comment} ignore_thresh (float): ${ignore_thresh_comment}
downsample (int): ${downsample_comment} downsample (int): ${downsample_comment}
use_label_smooth(bool): ${use_label_smooth_comment}
name (string): the name of yolov3 loss name (string): the name of yolov3 loss
Returns: Returns:
...@@ -451,6 +453,7 @@ def yolov3_loss(x, ...@@ -451,6 +453,7 @@ def yolov3_loss(x,
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
TypeError: Attr use_label_smooth of yolov3_loss must be a bool value
Examples: Examples:
.. code-block:: python .. code-block:: python
...@@ -479,6 +482,8 @@ def yolov3_loss(x, ...@@ -479,6 +482,8 @@ def yolov3_loss(x,
raise TypeError("Attr anchor_mask of yolov3_loss must be list or tuple") raise TypeError("Attr anchor_mask of yolov3_loss must be list or tuple")
if not isinstance(class_num, int): if not isinstance(class_num, int):
raise TypeError("Attr class_num of yolov3_loss must be an integer") raise TypeError("Attr class_num of yolov3_loss must be an integer")
if not isinstance(class_num, int):
raise TypeError("Attr ues_label_smooth of yolov3 must be a bool value")
if not isinstance(ignore_thresh, float): if not isinstance(ignore_thresh, float):
raise TypeError( raise TypeError(
"Attr ignore_thresh of yolov3_loss must be a float number") "Attr ignore_thresh of yolov3_loss must be a float number")
...@@ -498,6 +503,7 @@ def yolov3_loss(x, ...@@ -498,6 +503,7 @@ def yolov3_loss(x,
"class_num": class_num, "class_num": class_num,
"ignore_thresh": ignore_thresh, "ignore_thresh": ignore_thresh,
"downsample": downsample, "downsample": downsample,
"use_label_smooth": use_label_smooth
} }
helper.append_op( helper.append_op(
......
...@@ -76,6 +76,7 @@ def YOLOv3Loss(x, gtbox, gtlabel, gtscore, attrs): ...@@ -76,6 +76,7 @@ def YOLOv3Loss(x, gtbox, gtlabel, gtscore, attrs):
class_num = attrs["class_num"] class_num = attrs["class_num"]
ignore_thresh = attrs['ignore_thresh'] ignore_thresh = attrs['ignore_thresh']
downsample = attrs['downsample'] downsample = attrs['downsample']
#use_label_smooth = attrs['use_label_smooth']
input_size = downsample * h input_size = downsample * h
x = x.reshape((n, mask_num, 5 + class_num, h, w)).transpose((0, 1, 3, 4, 2)) x = x.reshape((n, mask_num, 5 + class_num, h, w)).transpose((0, 1, 3, 4, 2))
loss = np.zeros((n)).astype('float32') loss = np.zeros((n)).astype('float32')
...@@ -176,6 +177,7 @@ class TestYolov3LossOp(OpTest): ...@@ -176,6 +177,7 @@ class TestYolov3LossOp(OpTest):
"class_num": self.class_num, "class_num": self.class_num,
"ignore_thresh": self.ignore_thresh, "ignore_thresh": self.ignore_thresh,
"downsample": self.downsample, "downsample": self.downsample,
"use_label_smooth": self.use_label_smooth,
} }
self.inputs = { self.inputs = {
...@@ -215,6 +217,12 @@ class TestYolov3LossOp(OpTest): ...@@ -215,6 +217,12 @@ class TestYolov3LossOp(OpTest):
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, 10, 4) self.gtbox_shape = (3, 10, 4)
self.use_label_smooth = True
class TestYolov3LossWithLabelSmooth(TestYolov3LossOp):
def set_label_smooth(self):
self.use_label_smooth = True
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册