@@ -32,7 +32,7 @@ static inline bool LessEqualZero(T x) {
32
32
}
33
33
34
34
template <typename T>
35
- static T SCE (T x, T label) {
35
+ static T SigmoidCrossEntropy (T x, T label) {
36
36
return (x > 0 ? x : 0.0 ) - x * label + std::log (1.0 + std::exp (-std::abs (x)));
37
37
}
38
38
@@ -42,7 +42,7 @@ static T L2Loss(T x, T y) {
42
42
}
43
43
44
44
template <typename T>
45
- static T SCEGrad (T x, T label) {
45
+ static T SigmoidCrossEntropyGrad (T x, T label) {
46
46
return 1.0 / (1.0 + std::exp (-x)) - label;
47
47
}
48
48
@@ -62,7 +62,7 @@ static int GetMaskIndex(std::vector<int> mask, int val) {
62
62
63
63
template <typename T>
64
64
struct Box {
65
- float x, y, w, h;
65
+ T x, y, w, h;
66
66
};
67
67
68
68
template <typename T>
@@ -128,8 +128,8 @@ static void CalcBoxLocationLoss(T* loss, const T* input, Box<T> gt,
128
128
T th = std::log (gt.h * input_size / anchors[2 * an_idx + 1 ]);
129
129
130
130
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;
133
133
loss[0 ] += L2Loss<T>(input[box_idx + 2 * stride], tw) * scale;
134
134
loss[0 ] += L2Loss<T>(input[box_idx + 3 * stride], th) * scale;
135
135
}
@@ -145,9 +145,10 @@ static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input,
145
145
T th = std::log (gt.h * input_size / anchors[2 * an_idx + 1 ]);
146
146
147
147
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;
149
150
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;
151
152
input_grad[box_idx + 2 * stride] =
152
153
L2LossGrad<T>(input[box_idx + 2 * stride], tw) * scale * loss;
153
154
input_grad[box_idx + 3 * stride] =
@@ -160,7 +161,7 @@ static inline void CalcLabelLoss(T* loss, const T* input, const int index,
160
161
const int stride) {
161
162
for (int i = 0 ; i < class_num; i++) {
162
163
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 );
164
165
}
165
166
}
166
167
@@ -172,7 +173,7 @@ static inline void CalcLabelLossGrad(T* input_grad, const T loss,
172
173
for (int i = 0 ; i < class_num; i++) {
173
174
T pred = input[index + i * stride];
174
175
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;
176
177
}
177
178
}
178
179
@@ -187,11 +188,11 @@ static inline void CalcObjnessLoss(T* loss, const T* input, const T* objness,
187
188
for (int l = 0 ; l < w; l++) {
188
189
T obj = objness[k * w + l];
189
190
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 );
192
193
} else if (obj > -0.5 ) {
193
194
// 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 );
195
196
}
196
197
}
197
198
}
@@ -213,9 +214,11 @@ static inline void CalcObjnessLossGrad(T* input_grad, const T* loss,
213
214
for (int l = 0 ; l < w; l++) {
214
215
T obj = objness[k * w + l];
215
216
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];
217
219
} 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];
219
222
}
220
223
}
221
224
}
@@ -256,15 +259,15 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
256
259
auto anchor_mask = ctx.Attr <std::vector<int >>(" anchor_mask" );
257
260
int class_num = ctx.Attr <int >(" class_num" );
258
261
float ignore_thresh = ctx.Attr <float >(" ignore_thresh" );
259
- int downsample = ctx.Attr <int >(" downsample " );
262
+ int downsample_ratio = ctx.Attr <int >(" downsample_ratio " );
260
263
261
264
const int n = input->dims ()[0 ];
262
265
const int h = input->dims ()[2 ];
263
266
const int w = input->dims ()[3 ];
264
267
const int an_num = anchors.size () / 2 ;
265
268
const int mask_num = anchor_mask.size ();
266
269
const int b = gt_box->dims ()[1 ];
267
- int input_size = downsample * h;
270
+ int input_size = downsample_ratio * h;
268
271
269
272
const int stride = h * w;
270
273
const int an_stride = (class_num + 5 ) * stride;
@@ -308,7 +311,7 @@ class Yolov3LossKernel : public framework::OpKernel<T> {
308
311
}
309
312
}
310
313
311
- // If best IoU is greater then ignore_thresh,
314
+ // If best IoU is bigger then ignore_thresh,
312
315
// ignore the objectness loss.
313
316
if (best_iou > ignore_thresh) {
314
317
int obj_idx = (i * mask_num + j) * stride + k * w + l;
@@ -388,15 +391,15 @@ class Yolov3LossGradKernel : public framework::OpKernel<T> {
388
391
auto anchors = ctx.Attr <std::vector<int >>(" anchors" );
389
392
auto anchor_mask = ctx.Attr <std::vector<int >>(" anchor_mask" );
390
393
int class_num = ctx.Attr <int >(" class_num" );
391
- int downsample = ctx.Attr <int >(" downsample " );
394
+ int downsample_ratio = ctx.Attr <int >(" downsample_ratio " );
392
395
393
396
const int n = input_grad->dims ()[0 ];
394
397
const int c = input_grad->dims ()[1 ];
395
398
const int h = input_grad->dims ()[2 ];
396
399
const int w = input_grad->dims ()[3 ];
397
400
const int mask_num = anchor_mask.size ();
398
401
const int b = gt_match_mask->dims ()[1 ];
399
- int input_size = downsample * h;
402
+ int input_size = downsample_ratio * h;
400
403
401
404
const int stride = h * w;
402
405
const int an_stride = (class_num + 5 ) * stride;
0 commit comments