Skip to content

Commit 0d30db3

Browse files
committed
gaussian_yolo: added uc_normalizer and minor fix for iou_normalizer for GIoU.
1 parent 0cf4c16 commit 0d30db3

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/gaussian_yolo_layer.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ box get_gaussian_yolo_box(float *x, float *biases, int n, int index, int i, int
140140
return b;
141141
}
142142

143-
float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride, float iou_normalizer, IOU_LOSS iou_loss, int accumulate)
143+
float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride, float iou_normalizer, IOU_LOSS iou_loss, float uc_normalizer, int accumulate)
144144
{
145145
box pred = get_gaussian_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
146146

@@ -157,6 +157,7 @@ float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int ind
157157
float dx, dy, dw, dh;
158158

159159
if (iou_loss == MSE) {
160+
// MSE
160161
iou = all_ious.iou;
161162

162163
float tx = (truth.x*lw - i);
@@ -171,6 +172,7 @@ float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int ind
171172
}
172173
else
173174
{
175+
// GIoU
174176
iou = all_ious.giou;
175177

176178
// https://github.com/generalized-iou/g-darknet
@@ -183,14 +185,9 @@ float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int ind
183185
dy = (all_ious.dx_iou.dt + all_ious.dx_iou.db);
184186
dw = ((-0.5 * all_ious.dx_iou.dl) + (0.5 * all_ious.dx_iou.dr));
185187
dh = ((-0.5 * all_ious.dx_iou.dt) + (0.5 * all_ious.dx_iou.db));
186-
187-
// normalize iou weight
188-
dx *= iou_normalizer;
189-
dy *= iou_normalizer;
190-
dw *= iou_normalizer;
191-
dh *= iou_normalizer;
192188
}
193189

190+
// Gaussian
194191
float in_exp_x = dx / x[index+1*stride];
195192
float in_exp_x_2 = pow(in_exp_x, 2);
196193
float normal_dist_x = exp(in_exp_x_2*(-1./2.))/(sqrt(M_PI * 2.0)*(x[index+1*stride]+sigma_const));
@@ -223,15 +220,39 @@ float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int ind
223220
delta[index + 7 * stride] = 0;
224221
}
225222

226-
delta[index + 0*stride] += temp_x * in_exp_x * (1./x[index+1*stride]);
227-
delta[index + 2*stride] += temp_y * in_exp_y * (1./x[index+3*stride]);
228-
delta[index + 4*stride] += temp_w * in_exp_w * (1./x[index+5*stride]);
229-
delta[index + 6*stride] += temp_h * in_exp_h * (1./x[index+7*stride]);
230-
231-
delta[index + 1*stride] += temp_x * (in_exp_x_2/x[index+1*stride] - 1./(x[index+1*stride]+sigma_const));
232-
delta[index + 3*stride] += temp_y * (in_exp_y_2/x[index+3*stride] - 1./(x[index+3*stride]+sigma_const));
233-
delta[index + 5*stride] += temp_w * (in_exp_w_2/x[index+5*stride] - 1./(x[index+5*stride]+sigma_const));
234-
delta[index + 7*stride] += temp_h * (in_exp_h_2/x[index+7*stride] - 1./(x[index+7*stride]+sigma_const));
223+
float delta_x = temp_x * in_exp_x * (1. / x[index + 1 * stride]);
224+
float delta_y = temp_y * in_exp_y * (1. / x[index + 3 * stride]);
225+
float delta_w = temp_w * in_exp_w * (1. / x[index + 5 * stride]);
226+
float delta_h = temp_h * in_exp_h * (1. / x[index + 7 * stride]);
227+
228+
float delta_ux = temp_x * (in_exp_x_2 / x[index + 1 * stride] - 1. / (x[index + 1 * stride] + sigma_const));
229+
float delta_uy = temp_y * (in_exp_y_2 / x[index + 3 * stride] - 1. / (x[index + 3 * stride] + sigma_const));
230+
float delta_uw = temp_w * (in_exp_w_2 / x[index + 5 * stride] - 1. / (x[index + 5 * stride] + sigma_const));
231+
float delta_uh = temp_h * (in_exp_h_2 / x[index + 7 * stride] - 1. / (x[index + 7 * stride] + sigma_const));
232+
233+
if (iou_loss != MSE) {
234+
// normalize iou weight, for GIoU
235+
delta_x *= iou_normalizer;
236+
delta_y *= iou_normalizer;
237+
delta_w *= iou_normalizer;
238+
delta_h *= iou_normalizer;
239+
}
240+
// normalize Uncertainty weight
241+
delta_ux *= uc_normalizer;
242+
delta_uy *= uc_normalizer;
243+
delta_uw *= uc_normalizer;
244+
delta_uh *= uc_normalizer;
245+
246+
247+
delta[index + 0 * stride] += delta_x;
248+
delta[index + 2 * stride] += delta_y;
249+
delta[index + 4 * stride] += delta_w;
250+
delta[index + 6 * stride] += delta_h;
251+
252+
delta[index + 1 * stride] += delta_ux;
253+
delta[index + 3 * stride] += delta_uy;
254+
delta[index + 5 * stride] += delta_uw;
255+
delta[index + 7 * stride] += delta_uh;
235256
return iou;
236257
}
237258

@@ -359,7 +380,7 @@ void forward_gaussian_yolo_layer(const layer l, network_state state)
359380
int class_index = entry_gaussian_index(l, b, n*l.w*l.h + j*l.w + i, 9);
360381
delta_gaussian_yolo_class(l.output, l.delta, class_index, class_id, l.classes, l.w*l.h, 0);
361382
box truth = float_to_box_stride(state.truth + best_t*(4 + 1) + b*l.truths, 1);
362-
delta_gaussian_yolo_box(truth, l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.delta, (2-truth.w*truth.h), l.w*l.h, l.iou_normalizer, l.iou_loss, 1);
383+
delta_gaussian_yolo_box(truth, l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.delta, (2-truth.w*truth.h), l.w*l.h, l.iou_normalizer, l.iou_loss, l.uc_normalizer, 1);
363384
}
364385
}
365386
}
@@ -388,7 +409,7 @@ void forward_gaussian_yolo_layer(const layer l, network_state state)
388409
int mask_n = int_index(l.mask, best_n, l.n);
389410
if(mask_n >= 0){
390411
int box_index = entry_gaussian_index(l, b, mask_n*l.w*l.h + j*l.w + i, 0);
391-
float iou = delta_gaussian_yolo_box(truth, l.output, l.biases, best_n, box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.delta, (2-truth.w*truth.h), l.w*l.h, l.iou_normalizer, l.iou_loss, 1);
412+
float iou = delta_gaussian_yolo_box(truth, l.output, l.biases, best_n, box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.delta, (2-truth.w*truth.h), l.w*l.h, l.iou_normalizer, l.iou_loss, l.uc_normalizer, 1);
392413

393414
int obj_index = entry_gaussian_index(l, b, mask_n*l.w*l.h + j*l.w + i, 8);
394415
avg_obj += l.output[obj_index];
@@ -419,7 +440,7 @@ void forward_gaussian_yolo_layer(const layer l, network_state state)
419440

420441
if (iou > l.iou_thresh) {
421442
int box_index = entry_gaussian_index(l, b, mask_n*l.w*l.h + j*l.w + i, 0);
422-
float iou = delta_gaussian_yolo_box(truth, l.output, l.biases, n, box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.delta, (2 - truth.w*truth.h), l.w*l.h, l.iou_normalizer, l.iou_loss, 1);
443+
float iou = delta_gaussian_yolo_box(truth, l.output, l.biases, n, box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.delta, (2 - truth.w*truth.h), l.w*l.h, l.iou_normalizer, l.iou_loss, l.uc_normalizer, 1);
423444

424445
int obj_index = entry_gaussian_index(l, b, mask_n*l.w*l.h + j*l.w + i, 8);
425446
avg_obj += l.output[obj_index];

0 commit comments

Comments
 (0)