Skip to content

Commit 733bb82

Browse files
committed
downsample -> downsample_ratio. test=develop
1 parent ae0b0d5 commit 733bb82

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
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', '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', 'anchors', 'anchor_mask', 'class_num', 'ignore_thresh', 'downsample_ratio', '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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Yolov3LossOpMaker : public framework::OpProtoAndCheckerMaker {
135135
"The mask index of anchors used in "
136136
"current YOLOv3 loss calculation.")
137137
.SetDefault(std::vector<int>{});
138-
AddAttr<int>("downsample",
138+
AddAttr<int>("downsample_ratio",
139139
"The downsample ratio from network input to YOLOv3 loss "
140140
"input, so 32, 16, 8 should be set for the first, second, "
141141
"and thrid YOLOv3 loss operators.")

paddle/fluid/operators/yolov3_loss_op.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static inline bool LessEqualZero(T x) {
3232
}
3333

3434
template <typename T>
35-
static T SCE(T x, T label) {
35+
static T SigmoidCrossEntropy(T x, T label) {
3636
return (x > 0 ? x : 0.0) - x * label + std::log(1.0 + std::exp(-std::abs(x)));
3737
}
3838

@@ -42,7 +42,7 @@ static T L2Loss(T x, T y) {
4242
}
4343

4444
template <typename T>
45-
static T SCEGrad(T x, T label) {
45+
static T SigmoidCrossEntropyGrad(T x, T label) {
4646
return 1.0 / (1.0 + std::exp(-x)) - label;
4747
}
4848

@@ -62,7 +62,7 @@ static int GetMaskIndex(std::vector<int> mask, int val) {
6262

6363
template <typename T>
6464
struct Box {
65-
float x, y, w, h;
65+
T x, y, w, h;
6666
};
6767

6868
template <typename T>
@@ -128,8 +128,8 @@ static void CalcBoxLocationLoss(T* loss, const T* input, Box<T> gt,
128128
T th = std::log(gt.h * input_size / anchors[2 * an_idx + 1]);
129129

130130
T scale = (2.0 - gt.w * gt.h);
131-
loss[0] += SCE<T>(input[box_idx], tx) * scale;
132-
loss[0] += SCE<T>(input[box_idx + stride], ty) * scale;
131+
loss[0] += SigmoidCrossEntropy<T>(input[box_idx], tx) * scale;
132+
loss[0] += SigmoidCrossEntropy<T>(input[box_idx + stride], ty) * scale;
133133
loss[0] += L2Loss<T>(input[box_idx + 2 * stride], tw) * scale;
134134
loss[0] += L2Loss<T>(input[box_idx + 3 * stride], th) * scale;
135135
}
@@ -145,9 +145,10 @@ static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input,
145145
T th = std::log(gt.h * input_size / anchors[2 * an_idx + 1]);
146146

147147
T scale = (2.0 - gt.w * gt.h);
148-
input_grad[box_idx] = SCEGrad<T>(input[box_idx], tx) * scale * loss;
148+
input_grad[box_idx] =
149+
SigmoidCrossEntropyGrad<T>(input[box_idx], tx) * scale * loss;
149150
input_grad[box_idx + stride] =
150-
SCEGrad<T>(input[box_idx + stride], ty) * scale * loss;
151+
SigmoidCrossEntropyGrad<T>(input[box_idx + stride], ty) * scale * loss;
151152
input_grad[box_idx + 2 * stride] =
152153
L2LossGrad<T>(input[box_idx + 2 * stride], tw) * scale * loss;
153154
input_grad[box_idx + 3 * stride] =
@@ -160,7 +161,7 @@ static inline void CalcLabelLoss(T* loss, const T* input, const int index,
160161
const int stride) {
161162
for (int i = 0; i < class_num; i++) {
162163
T pred = input[index + i * stride];
163-
loss[0] += SCE<T>(pred, (i == label) ? 1.0 : 0.0);
164+
loss[0] += SigmoidCrossEntropy<T>(pred, (i == label) ? 1.0 : 0.0);
164165
}
165166
}
166167

@@ -172,7 +173,7 @@ static inline void CalcLabelLossGrad(T* input_grad, const T loss,
172173
for (int i = 0; i < class_num; i++) {
173174
T pred = input[index + i * stride];
174175
input_grad[index + i * stride] =
175-
SCEGrad<T>(pred, (i == label) ? 1.0 : 0.0) * loss;
176+
SigmoidCrossEntropyGrad<T>(pred, (i == label) ? 1.0 : 0.0) * loss;
176177
}
177178
}
178179

@@ -187,11 +188,11 @@ static inline void CalcObjnessLoss(T* loss, const T* input, const T* objness,
187188
for (int l = 0; l < w; l++) {
188189
T obj = objness[k * w + l];
189190
if (obj > 1e-5) {
190-
// positive sample: obj = mixup score
191-
loss[i] += SCE<T>(input[k * w + l], 1.0);
191+
// positive sample: obj = 1
192+
loss[i] += SigmoidCrossEntropy<T>(input[k * w + l], 1.0);
192193
} else if (obj > -0.5) {
193194
// negetive sample: obj = 0
194-
loss[i] += SCE<T>(input[k * w + l], 0.0);
195+
loss[i] += SigmoidCrossEntropy<T>(input[k * w + l], 0.0);
195196
}
196197
}
197198
}
@@ -213,9 +214,11 @@ static inline void CalcObjnessLossGrad(T* input_grad, const T* loss,
213214
for (int l = 0; l < w; l++) {
214215
T obj = objness[k * w + l];
215216
if (obj > 1e-5) {
216-
input_grad[k * w + l] = SCEGrad<T>(input[k * w + l], 1.0) * loss[i];
217+
input_grad[k * w + l] =
218+
SigmoidCrossEntropyGrad<T>(input[k * w + l], 1.0) * loss[i];
217219
} else if (obj > -0.5) {
218-
input_grad[k * w + l] = SCEGrad<T>(input[k * w + l], 0.0) * loss[i];
220+
input_grad[k * w + l] =
221+
SigmoidCrossEntropyGrad<T>(input[k * w + l], 0.0) * loss[i];
219222
}
220223
}
221224
}
@@ -256,15 +259,15 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
256259
auto anchor_mask = ctx.Attr<std::vector<int>>("anchor_mask");
257260
int class_num = ctx.Attr<int>("class_num");
258261
float ignore_thresh = ctx.Attr<float>("ignore_thresh");
259-
int downsample = ctx.Attr<int>("downsample");
262+
int downsample_ratio = ctx.Attr<int>("downsample_ratio");
260263

261264
const int n = input->dims()[0];
262265
const int h = input->dims()[2];
263266
const int w = input->dims()[3];
264267
const int an_num = anchors.size() / 2;
265268
const int mask_num = anchor_mask.size();
266269
const int b = gt_box->dims()[1];
267-
int input_size = downsample * h;
270+
int input_size = downsample_ratio * h;
268271

269272
const int stride = h * w;
270273
const int an_stride = (class_num + 5) * stride;
@@ -308,7 +311,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
308311
}
309312
}
310313

311-
// If best IoU is greater then ignore_thresh,
314+
// If best IoU is bigger then ignore_thresh,
312315
// ignore the objectness loss.
313316
if (best_iou > ignore_thresh) {
314317
int obj_idx = (i * mask_num + j) * stride + k * w + l;
@@ -388,15 +391,15 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
388391
auto anchors = ctx.Attr<std::vector<int>>("anchors");
389392
auto anchor_mask = ctx.Attr<std::vector<int>>("anchor_mask");
390393
int class_num = ctx.Attr<int>("class_num");
391-
int downsample = ctx.Attr<int>("downsample");
394+
int downsample_ratio = ctx.Attr<int>("downsample_ratio");
392395

393396
const int n = input_grad->dims()[0];
394397
const int c = input_grad->dims()[1];
395398
const int h = input_grad->dims()[2];
396399
const int w = input_grad->dims()[3];
397400
const int mask_num = anchor_mask.size();
398401
const int b = gt_match_mask->dims()[1];
399-
int input_size = downsample * h;
402+
int input_size = downsample_ratio * h;
400403

401404
const int stride = h * w;
402405
const int an_stride = (class_num + 5) * stride;

python/paddle/fluid/layers/detection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def yolov3_loss(x,
416416
anchor_mask,
417417
class_num,
418418
ignore_thresh,
419-
downsample,
419+
downsample_ratio,
420420
name=None):
421421
"""
422422
${comment}
@@ -434,7 +434,7 @@ def yolov3_loss(x,
434434
anchor_mask (list|tuple): ${anchor_mask_comment}
435435
class_num (int): ${class_num_comment}
436436
ignore_thresh (float): ${ignore_thresh_comment}
437-
downsample (int): ${downsample_comment}
437+
downsample_ratio (int): ${downsample_ratio_comment}
438438
name (string): the name of yolov3 loss
439439
440440
Returns:
@@ -456,8 +456,8 @@ def yolov3_loss(x,
456456
gtlabel = fluid.layers.data(name='gtlabel', shape=[6, 1], dtype='int32')
457457
anchors = [10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326]
458458
anchors = [0, 1, 2]
459-
loss = fluid.layers.yolov3_loss(x=x, gtbox=gtbox, class_num=80
460-
anchors=anchors, ignore_thresh=0.5)
459+
loss = fluid.layers.yolov3_loss(x=x, gtbox=gtbox, class_num=80, anchors=anchors,
460+
ignore_thresh=0.5, downsample_ratio=32)
461461
"""
462462
helper = LayerHelper('yolov3_loss', **locals())
463463

@@ -491,7 +491,7 @@ def yolov3_loss(x,
491491
"anchor_mask": anchor_mask,
492492
"class_num": class_num,
493493
"ignore_thresh": ignore_thresh,
494-
"downsample": downsample,
494+
"downsample_ratio": downsample_ratio,
495495
}
496496

497497
helper.append_op(

0 commit comments

Comments
 (0)