From 3c08f620c248c506116dbb5a58224de9743bb048 Mon Sep 17 00:00:00 2001 From: dengkaipeng Date: Thu, 3 Jan 2019 11:16:29 +0800 Subject: [PATCH] add label smooth. test=develop --- paddle/fluid/operators/yolov3_loss_op.h | 19 ++++++++++--------- .../tests/unittests/test_yolov3_loss_op.py | 6 +++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/paddle/fluid/operators/yolov3_loss_op.h b/paddle/fluid/operators/yolov3_loss_op.h index 301e2f40334..34119b1a025 100644 --- a/paddle/fluid/operators/yolov3_loss_op.h +++ b/paddle/fluid/operators/yolov3_loss_op.h @@ -159,7 +159,9 @@ static inline void CalcLabelLoss(T* loss, const T* input, const int index, const int label, const int class_num, const int stride) { for (int i = 0; i < class_num; i++) { - loss[0] += SCE(input[index + i * stride], (i == label) ? 1.0 : 0.0); + T pred = input[index + i * stride] < -0.5 ? input[index + i * stride] + : 1.0 / class_num; + loss[0] += SCE(pred, (i == label) ? 1.0 : 0.0); } } @@ -169,8 +171,10 @@ static inline void CalcLabelLossGrad(T* input_grad, const T loss, const int label, 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(input[index + i * stride], (i == label) ? 1.0 : 0.0) * loss; + SCEGrad(pred, (i == label) ? 1.0 : 0.0) * loss; } } @@ -406,15 +410,12 @@ class Yolov3LossGradKernel : public framework::OpKernel { for (int i = 0; i < n; i++) { for (int t = 0; t < b; t++) { - Box gt = GetGtBox(gt_box_data, i, b, t); - if (LessEqualZero(gt.w) || LessEqualZero(gt.h)) { - continue; - } - int gi = static_cast(gt.x * w); - int gj = static_cast(gt.y * h); - int mask_idx = gt_match_mask_data[i * b + t]; if (mask_idx >= 0) { + Box gt = GetGtBox(gt_box_data, i, b, t); + int gi = static_cast(gt.x * w); + int gj = static_cast(gt.y * h); + int box_idx = GetEntryIndex(i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 0); CalcBoxLocationLossGrad( diff --git a/python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py b/python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py index 904bee00c12..27fb92c5890 100644 --- a/python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py +++ b/python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py @@ -86,6 +86,10 @@ def YOLOv3Loss(x, gtbox, gtlabel, attrs): pred_box[:, :, :, :, 0] = (grid_x + sigmoid(pred_box[:, :, :, :, 0])) / w pred_box[:, :, :, :, 1] = (grid_y + sigmoid(pred_box[:, :, :, :, 1])) / h + x[:, :, :, :, 5:] = np.where(x[:, :, :, :, 5:] < -0.5, x[:, :, :, :, 5:], + np.ones_like(x[:, :, :, :, 5:]) * 1.0 / + class_num) + mask_anchors = [] for m in anchor_mask: mask_anchors.append((anchors[2 * m], anchors[2 * m + 1])) @@ -207,7 +211,7 @@ class TestYolov3LossOp(OpTest): self.ignore_thresh = 0.7 self.downsample = 32 self.x_shape = (3, len(self.anchor_mask) * (5 + self.class_num), 5, 5) - self.gtbox_shape = (3, 10, 4) + self.gtbox_shape = (3, 5, 4) if __name__ == "__main__": -- GitLab