@@ -140,7 +140,7 @@ box get_gaussian_yolo_box(float *x, float *biases, int n, int index, int i, int
140
140
return b ;
141
141
}
142
142
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 )
144
144
{
145
145
box pred = get_gaussian_yolo_box (x , biases , n , index , i , j , lw , lh , w , h , stride );
146
146
@@ -157,6 +157,7 @@ float delta_gaussian_yolo_box(box truth, float *x, float *biases, int n, int ind
157
157
float dx , dy , dw , dh ;
158
158
159
159
if (iou_loss == MSE ) {
160
+ // MSE
160
161
iou = all_ious .iou ;
161
162
162
163
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
171
172
}
172
173
else
173
174
{
175
+ // GIoU
174
176
iou = all_ious .giou ;
175
177
176
178
// 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
183
185
dy = (all_ious .dx_iou .dt + all_ious .dx_iou .db );
184
186
dw = ((-0.5 * all_ious .dx_iou .dl ) + (0.5 * all_ious .dx_iou .dr ));
185
187
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 ;
192
188
}
193
189
190
+ // Gaussian
194
191
float in_exp_x = dx / x [index + 1 * stride ];
195
192
float in_exp_x_2 = pow (in_exp_x , 2 );
196
193
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
223
220
delta [index + 7 * stride ] = 0 ;
224
221
}
225
222
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 ;
235
256
return iou ;
236
257
}
237
258
@@ -359,7 +380,7 @@ void forward_gaussian_yolo_layer(const layer l, network_state state)
359
380
int class_index = entry_gaussian_index (l , b , n * l .w * l .h + j * l .w + i , 9 );
360
381
delta_gaussian_yolo_class (l .output , l .delta , class_index , class_id , l .classes , l .w * l .h , 0 );
361
382
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 );
363
384
}
364
385
}
365
386
}
@@ -388,7 +409,7 @@ void forward_gaussian_yolo_layer(const layer l, network_state state)
388
409
int mask_n = int_index (l .mask , best_n , l .n );
389
410
if (mask_n >= 0 ){
390
411
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 );
392
413
393
414
int obj_index = entry_gaussian_index (l , b , mask_n * l .w * l .h + j * l .w + i , 8 );
394
415
avg_obj += l .output [obj_index ];
@@ -419,7 +440,7 @@ void forward_gaussian_yolo_layer(const layer l, network_state state)
419
440
420
441
if (iou > l .iou_thresh ) {
421
442
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 );
423
444
424
445
int obj_index = entry_gaussian_index (l , b , mask_n * l .w * l .h + j * l .w + i , 8 );
425
446
avg_obj += l .output [obj_index ];
0 commit comments