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