Skip to content

Commit 2b89f59

Browse files
tink2123heavengate
authored andcommitted
add attr use_label_smooth test=develop
1 parent 8218e30 commit 2b89f59

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ paddle.fluid.layers.generate_mask_labels ArgSpec(args=['im_info', 'gt_classes',
324324
paddle.fluid.layers.iou_similarity ArgSpec(args=['x', 'y', 'name'], varargs=None, keywords=None, defaults=(None,))
325325
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))
326326
paddle.fluid.layers.polygon_box_transform ArgSpec(args=['input', 'name'], varargs=None, keywords=None, defaults=(None,))
327-
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,))
327+
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,))
328328
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))
329329
paddle.fluid.layers.accuracy ArgSpec(args=['input', 'label', 'k', 'correct', 'total'], varargs=None, keywords=None, defaults=(1, None, None))
330330
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class Yolov3LossOp : public framework::OperatorWithKernel {
4646
auto anchor_mask = ctx->Attrs().Get<std::vector<int>>("anchor_mask");
4747
int mask_num = anchor_mask.size();
4848
auto class_num = ctx->Attrs().Get<int>("class_num");
49+
4950
PADDLE_ENFORCE_EQ(dim_x.size(), 4, "Input(X) should be a 4-D tensor.");
5051
PADDLE_ENFORCE_EQ(dim_x[2], dim_x[3],
5152
"Input(X) dim[3] and dim[4] should be euqal.");
@@ -156,6 +157,8 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker {
156157
AddAttr<float>("ignore_thresh",
157158
"The ignore threshold to ignore confidence loss.")
158159
.SetDefault(0.7);
160+
AddAttr<bool>("use_label_smooth", "bool,default True", "use label smooth")
161+
.SetDefault(true);
159162
AddComment(R"DOC(
160163
This operator generate yolov3 loss by given predict result and ground
161164
truth boxes.

paddle/fluid/operators/yolov3_loss_op.h

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,41 @@ static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input,
157157
template <typename T>
158158
static inline void CalcLabelLoss(T* loss, const T* input, const int index,
159159
const int label, const T score,
160-
const int class_num, const int stride) {
161-
for (int i = 0; i < class_num; i++) {
162-
T pred = input[index + i * stride] < -0.5 ? input[index + i * stride]
163-
: 1.0 / class_num;
164-
loss[0] += SCE<T>(pred, (i == label) ? score : 0.0);
160+
const int class_num, const int stride,
161+
const bool use_label_smooth) {
162+
if (use_label_smooth) {
163+
for (int i = 0; i < class_num; i++) {
164+
T pred = input[index + i * stride] < -0.5 ? input[index + i * stride]
165+
: 1.0 / class_num;
166+
loss[0] += SCE<T>(pred, (i == label) ? score : 0.0);
167+
}
168+
} else {
169+
for (int i = 0; i < class_num; i++) {
170+
T pred = input[index + i * stride];
171+
loss[0] += SCE<T>(pred, (i == label) ? score : 0.0);
172+
}
165173
}
166174
}
167175

168176
template <typename T>
169177
static inline void CalcLabelLossGrad(T* input_grad, const T loss,
170178
const T* input, const int index,
171179
const int label, const T score,
172-
const int class_num, const int stride) {
173-
for (int i = 0; i < class_num; i++) {
174-
T pred = input[index + i * stride] < -0.5 ? input[index + i * stride]
175-
: 1.0 / class_num;
176-
input_grad[index + i * stride] =
177-
SCEGrad<T>(pred, (i == label) ? score : 0.0) * loss;
180+
const int class_num, const int stride,
181+
const bool use_label_smooth) {
182+
if (use_label_smooth) {
183+
for (int i = 0; i < class_num; i++) {
184+
T pred = input[index + i * stride] < -0.5 ? input[index + i * stride]
185+
: 1.0 / class_num;
186+
input_grad[index + i * stride] =
187+
SCEGrad<T>(pred, (i == label) ? score : 0.0) * loss;
188+
}
189+
} else {
190+
for (int i = 0; i < class_num; i++) {
191+
T pred = input[index + i * stride];
192+
input_grad[index + i * stride] =
193+
SCEGrad<T>(pred, (i == label) ? score : 0.0) * loss;
194+
}
178195
}
179196
}
180197

@@ -255,6 +272,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
255272
int class_num = ctx.Attr<int>("class_num");
256273
float ignore_thresh = ctx.Attr<float>("ignore_thresh");
257274
int downsample = ctx.Attr<int>("downsample");
275+
bool use_label_smooth = ctx.Attr<bool>("use_label_smooth");
258276

259277
const int n = input->dims()[0];
260278
const int h = input->dims()[2];
@@ -364,7 +382,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
364382
int label_idx = GetEntryIndex(i, mask_idx, gj * w + gi, mask_num,
365383
an_stride, stride, 5);
366384
CalcLabelLoss<T>(loss_data + i, input_data, label_idx, label, score,
367-
class_num, stride);
385+
class_num, stride, use_label_smooth);
368386
}
369387
}
370388
}
@@ -390,6 +408,7 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
390408
auto anchor_mask = ctx.Attr<std::vector<int>>("anchor_mask");
391409
int class_num = ctx.Attr<int>("class_num");
392410
int downsample = ctx.Attr<int>("downsample");
411+
bool use_label_smooth = ctx.Attr<bool>("use_label_smooth");
393412

394413
const int n = input_grad->dims()[0];
395414
const int c = input_grad->dims()[1];
@@ -432,7 +451,8 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
432451
int label_idx = GetEntryIndex(i, mask_idx, gj * w + gi, mask_num,
433452
an_stride, stride, 5);
434453
CalcLabelLossGrad<T>(input_grad_data, loss_grad_data[i], input_data,
435-
label_idx, label, score, class_num, stride);
454+
label_idx, label, score, class_num, stride,
455+
use_label_smooth);
436456
}
437457
}
438458
}

python/paddle/fluid/layers/detection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def yolov3_loss(x,
418418
class_num,
419419
ignore_thresh,
420420
downsample,
421+
use_label_smooth=True,
421422
name=None):
422423
"""
423424
${comment}
@@ -438,6 +439,7 @@ def yolov3_loss(x,
438439
class_num (int): ${class_num_comment}
439440
ignore_thresh (float): ${ignore_thresh_comment}
440441
downsample (int): ${downsample_comment}
442+
use_label_smooth(bool): ${use_label_smooth_comment}
441443
name (string): the name of yolov3 loss
442444
443445
Returns:
@@ -451,6 +453,7 @@ def yolov3_loss(x,
451453
TypeError: Attr anchors of yolov3_loss must be list or tuple
452454
TypeError: Attr class_num of yolov3_loss must be an integer
453455
TypeError: Attr ignore_thresh of yolov3_loss must be a float number
456+
TypeError: Attr use_label_smooth of yolov3_loss must be a bool value
454457
455458
Examples:
456459
.. code-block:: python
@@ -479,6 +482,8 @@ def yolov3_loss(x,
479482
raise TypeError("Attr anchor_mask of yolov3_loss must be list or tuple")
480483
if not isinstance(class_num, int):
481484
raise TypeError("Attr class_num of yolov3_loss must be an integer")
485+
if not isinstance(class_num, int):
486+
raise TypeError("Attr ues_label_smooth of yolov3 must be a bool value")
482487
if not isinstance(ignore_thresh, float):
483488
raise TypeError(
484489
"Attr ignore_thresh of yolov3_loss must be a float number")
@@ -498,6 +503,7 @@ def yolov3_loss(x,
498503
"class_num": class_num,
499504
"ignore_thresh": ignore_thresh,
500505
"downsample": downsample,
506+
"use_label_smooth": use_label_smooth
501507
}
502508

503509
helper.append_op(

python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def YOLOv3Loss(x, gtbox, gtlabel, gtscore, attrs):
7676
class_num = attrs["class_num"]
7777
ignore_thresh = attrs['ignore_thresh']
7878
downsample = attrs['downsample']
79+
#use_label_smooth = attrs['use_label_smooth']
7980
input_size = downsample * h
8081
x = x.reshape((n, mask_num, 5 + class_num, h, w)).transpose((0, 1, 3, 4, 2))
8182
loss = np.zeros((n)).astype('float32')
@@ -176,6 +177,7 @@ def setUp(self):
176177
"class_num": self.class_num,
177178
"ignore_thresh": self.ignore_thresh,
178179
"downsample": self.downsample,
180+
"use_label_smooth": self.use_label_smooth,
179181
}
180182

181183
self.inputs = {
@@ -215,6 +217,12 @@ def initTestCase(self):
215217
self.downsample = 32
216218
self.x_shape = (3, len(self.anchor_mask) * (5 + self.class_num), 5, 5)
217219
self.gtbox_shape = (3, 10, 4)
220+
self.use_label_smooth = True
221+
222+
223+
class TestYolov3LossWithLabelSmooth(TestYolov3LossOp):
224+
def set_label_smooth(self):
225+
self.use_label_smooth = True
218226

219227

220228
if __name__ == "__main__":

0 commit comments

Comments
 (0)