1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
| void forward_yolo_layer(const layer l, network_state state) { int i, j, b, t, n; memcpy(l.output, state.input, l.outputs*l.batch * sizeof(float)); #ifndef GPU for (b = 0; b < l.batch; ++b) { for (n = 0; n < l.n; ++n) { int index = entry_index(l, b, n*l.w*l.h, 0); activate_array(l.output + index, 2 * l.w*l.h, LOGISTIC); scal_add_cpu(2 * l.w*l.h, l.scale_x_y, -0.5*(l.scale_x_y - 1), l.output + index, 1); index = entry_index(l, b, n*l.w*l.h, 4); activate_array(l.output + index, (1 + l.classes)*l.w*l.h, LOGISTIC); } } #endif
memset(l.delta, 0, l.outputs * l.batch * sizeof(float)); if (!state.train) return; float tot_iou = 0; float tot_giou = 0; float tot_diou = 0; float tot_ciou = 0; float tot_iou_loss = 0; float tot_giou_loss = 0; float tot_diou_loss = 0; float tot_ciou_loss = 0; float recall = 0; float recall75 = 0; float avg_cat = 0; float avg_obj = 0; float avg_anyobj = 0; int count = 0; int class_count = 0; *(l.cost) = 0; for (b = 0; b < l.batch; ++b) { for (j = 0; j < l.h; ++j) { for (i = 0; i < l.w; ++i) { for (n = 0; n < l.n; ++n) { int box_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 0); box pred = get_yolo_box(l.output, l.biases, l.mask[n], box_index, i, j, l.w, l.h, state.net.w, state.net.h, l.w*l.h); float best_match_iou = 0; int best_match_t = 0; float best_iou = 0; int best_t = 0; for (t = 0; t < l.max_boxes; ++t) { box truth = float_to_box_stride(state.truth + t*(4 + 1) + b*l.truths, 1); int class_id = state.truth[t*(4 + 1) + b*l.truths + 4]; if (class_id >= l.classes) { printf(" Warning: in txt-labels class_id=%d >= classes=%d in cfg-file. In txt-labels class_id should be [from 0 to %d] \n", class_id, l.classes, l.classes - 1); printf(" truth.x = %f, truth.y = %f, truth.w = %f, truth.h = %f, class_id = %d \n", truth.x, truth.y, truth.w, truth.h, class_id); getchar(); continue; } if (!truth.x) break;
int class_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4 + 1); int obj_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4); float objectness = l.output[obj_index]; int class_id_match = compare_yolo_class(l.output, l.classes, class_index, l.w*l.h, objectness, class_id, 0.25f);
float iou = box_iou(pred, truth); if (iou > best_match_iou && class_id_match == 1) { best_match_iou = iou; best_match_t = t; } if (iou > best_iou) { best_iou = iou; best_t = t; } } int obj_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4); avg_anyobj += l.output[obj_index]; l.delta[obj_index] = l.cls_normalizer * (0 - l.output[obj_index]); if (best_match_iou > l.ignore_thresh) { l.delta[obj_index] = 0; } if (best_iou > l.truth_thresh) { l.delta[obj_index] = l.cls_normalizer * (1 - l.output[obj_index]); int class_id = state.truth[best_t*(4 + 1) + b*l.truths + 4]; if (l.map) class_id = l.map[class_id]; int class_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4 + 1); delta_yolo_class(l.output, l.delta, class_index, class_id, l.classes, l.w*l.h, 0, l.focal_loss, l.label_smooth_eps, l.classes_multipliers); box truth = float_to_box_stride(state.truth + best_t*(4 + 1) + b*l.truths, 1); const float class_multiplier = (l.classes_multipliers) ? l.classes_multipliers[class_id] : 1.0f; delta_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 * class_multiplier, l.iou_loss, 1, l.max_delta); } } } } for (t = 0; t < l.max_boxes; ++t) { box truth = float_to_box_stride(state.truth + t*(4 + 1) + b*l.truths, 1); if (truth.x < 0 || truth.y < 0 || truth.x > 1 || truth.y > 1 || truth.w < 0 || truth.h < 0) { char buff[256]; printf(" Wrong label: truth.x = %f, truth.y = %f, truth.w = %f, truth.h = %f \n", truth.x, truth.y, truth.w, truth.h); sprintf(buff, "echo \"Wrong label: truth.x = %f, truth.y = %f, truth.w = %f, truth.h = %f\" >> bad_label.list", truth.x, truth.y, truth.w, truth.h); system(buff); } int class_id = state.truth[t*(4 + 1) + b*l.truths + 4]; if (class_id >= l.classes) continue;
if (!truth.x) break; float best_iou = 0; int best_n = 0; i = (truth.x * l.w); j = (truth.y * l.h); box truth_shift = truth; truth_shift.x = truth_shift.y = 0; for (n = 0; n < l.total; ++n) { box pred = { 0 }; pred.w = l.biases[2 * n] / state.net.w; pred.h = l.biases[2 * n + 1] / state.net.h; float iou = box_iou(pred, truth_shift); if (iou > best_iou) { best_iou = iou; best_n = n; } } int mask_n = int_index(l.mask, best_n, l.n); if (mask_n >= 0) { int class_id = state.truth[t*(4 + 1) + b*l.truths + 4]; if (l.map) class_id = l.map[class_id]; int box_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 0); const float class_multiplier = (l.classes_multipliers) ? l.classes_multipliers[class_id] : 1.0f; ious all_ious = delta_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 * class_multiplier, l.iou_loss, 1, l.max_delta);
tot_iou += all_ious.iou; tot_iou_loss += 1 - all_ious.iou; tot_giou += all_ious.giou; tot_giou_loss += 1 - all_ious.giou;
tot_diou += all_ious.diou; tot_diou_loss += 1 - all_ious.diou;
tot_ciou += all_ious.ciou; tot_ciou_loss += 1 - all_ious.ciou; int obj_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 4); avg_obj += l.output[obj_index]; l.delta[obj_index] = class_multiplier * l.cls_normalizer * (1 - l.output[obj_index]); int class_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 4 + 1); delta_yolo_class(l.output, l.delta, class_index, class_id, l.classes, l.w*l.h, &avg_cat, l.focal_loss, l.label_smooth_eps, l.classes_multipliers);
++count; ++class_count; if (all_ious.iou > .5) recall += 1; if (all_ious.iou > .75) recall75 += 1; }
for (n = 0; n < l.total; ++n) { int mask_n = int_index(l.mask, n, l.n); if (mask_n >= 0 && n != best_n && l.iou_thresh < 1.0f) { box pred = { 0 }; pred.w = l.biases[2 * n] / state.net.w; pred.h = l.biases[2 * n + 1] / state.net.h; float iou = box_iou(pred, truth_shift);
if (iou > l.iou_thresh) { int class_id = state.truth[t*(4 + 1) + b*l.truths + 4]; if (l.map) class_id = l.map[class_id];
int box_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 0); const float class_multiplier = (l.classes_multipliers) ? l.classes_multipliers[class_id] : 1.0f; ious all_ious = delta_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 * class_multiplier, l.iou_loss, 1, l.max_delta);
tot_iou += all_ious.iou; tot_iou_loss += 1 - all_ious.iou; tot_giou += all_ious.giou; tot_giou_loss += 1 - all_ious.giou;
tot_diou += all_ious.diou; tot_diou_loss += 1 - all_ious.diou;
tot_ciou += all_ious.ciou; tot_ciou_loss += 1 - all_ious.ciou;
int obj_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 4); avg_obj += l.output[obj_index]; l.delta[obj_index] = class_multiplier * l.cls_normalizer * (1 - l.output[obj_index]);
int class_index = entry_index(l, b, mask_n*l.w*l.h + j*l.w + i, 4 + 1); delta_yolo_class(l.output, l.delta, class_index, class_id, l.classes, l.w*l.h, &avg_cat, l.focal_loss, l.label_smooth_eps, l.classes_multipliers);
++count; ++class_count; if (all_ious.iou > .5) recall += 1; if (all_ious.iou > .75) recall75 += 1; } } } }
for (j = 0; j < l.h; ++j) { for (i = 0; i < l.w; ++i) { for (n = 0; n < l.n; ++n) { int box_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 0); int class_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4 + 1); const int stride = l.w*l.h; averages_yolo_deltas(class_index, box_index, stride, l.classes, l.delta); } } } }
int stride = l.w*l.h; float* no_iou_loss_delta = (float *)calloc(l.batch * l.outputs, sizeof(float)); memcpy(no_iou_loss_delta, l.delta, l.batch * l.outputs * sizeof(float)); for (b = 0; b < l.batch; ++b) { for (j = 0; j < l.h; ++j) { for (i = 0; i < l.w; ++i) { for (n = 0; n < l.n; ++n) { int index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 0); no_iou_loss_delta[index + 0 * stride] = 0; no_iou_loss_delta[index + 1 * stride] = 0; no_iou_loss_delta[index + 2 * stride] = 0; no_iou_loss_delta[index + 3 * stride] = 0; } } } } float classification_loss = l.cls_normalizer * pow(mag_array(no_iou_loss_delta, l.outputs * l.batch), 2); free(no_iou_loss_delta); float loss = pow(mag_array(l.delta, l.outputs * l.batch), 2); float iou_loss = loss - classification_loss;
float avg_iou_loss = 0; if (l.iou_loss == MSE) { *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2); } else { if (l.iou_loss == GIOU) { avg_iou_loss = count > 0 ? l.iou_normalizer * (tot_giou_loss / count) : 0; } else { avg_iou_loss = count > 0 ? l.iou_normalizer * (tot_iou_loss / count) : 0; } *(l.cost) = avg_iou_loss + classification_loss; }
loss /= l.batch; classification_loss /= l.batch; iou_loss /= l.batch;
printf("v3 (%s loss, Normalizer: (iou: %f, cls: %f) Region %d Avg (IOU: %f, GIOU: %f), Class: %f, Obj: %f, No Obj: %f, .5R: %f, .75R: %f, count: %d, loss = %f, class_loss = %f, iou_loss = %f\n", (l.iou_loss == MSE ? "mse" : (l.iou_loss == GIOU ? "giou" : "iou")), l.iou_normalizer, l.cls_normalizer, state.index, tot_iou / count, tot_giou / count, avg_cat / class_count, avg_obj / count, avg_anyobj / (l.w*l.h*l.n*l.batch), recall / count, recall75 / count, count, loss, classification_loss, iou_loss); }
|