#include "yolo_layer.h"
#include "activations.h"
#include "blas.h"
#include "box.h"
#include "dark_cuda.h"
#include "utils.h"
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
extern int check_mistakes;
layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes, int max_boxes)
{
int i;
layer l = { (LAYER_TYPE)0 };
l.type = YOLO;
l.n = n;
l.total = total;
l.batch = batch;
l.h = h;
l.w = w;
l.c = n*(classes + 4 + 1);
l.out_w = l.w;
l.out_h = l.h;
l.out_c = l.c;
l.classes = classes;
l.cost = (float*)xcalloc(1, sizeof(float));
l.biases = (float*)xcalloc(total * 2, sizeof(float));
if(mask) l.mask = mask;
else{
l.mask = (int*)xcalloc(n, sizeof(int));
for(i = 0; i < n; ++i){
l.mask[i] = i;
}
}
l.bias_updates = (float*)xcalloc(n * 2, sizeof(float));
l.outputs = h*w*n*(classes + 4 + 1);
l.inputs = l.outputs;
l.max_boxes = max_boxes;
l.truth_size = 4 + 2;
l.truths = l.max_boxes*l.truth_size;
l.labels = (int*)xcalloc(batch * l.w*l.h*l.n, sizeof(int));
for (i = 0; i < batch * l.w*l.h*l.n; ++i) l.labels[i] = -1;
l.delta = (float*)xcalloc(batch * l.outputs, sizeof(float));
l.output = (float*)xcalloc(batch * l.outputs, sizeof(float));
for(i = 0; i < total*2; ++i){
l.biases[i] = .5;
}
l.forward = forward_yolo_layer;
l.backward = backward_yolo_layer;
#ifdef GPU
l.forward_gpu = forward_yolo_layer_gpu;
l.backward_gpu = backward_yolo_layer_gpu;
l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
l.output_avg_gpu = cuda_make_array(l.output, batch*l.outputs);
l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
free(l.output);
if (cudaSuccess == cudaHostAlloc(&l.output, batch*l.outputs*sizeof(float), cudaHostRegisterMapped)) l.output_pinned = 1;
else {
cudaGetLastError();
l.output = (float*)xcalloc(batch * l.outputs, sizeof(float));
}
free(l.delta);
if (cudaSuccess == cudaHostAlloc(&l.delta, batch*l.outputs*sizeof(float), cudaHostRegisterMapped)) l.delta_pinned = 1;
else {
cudaGetLastError();
l.delta = (float*)xcalloc(batch * l.outputs, sizeof(float));
}
#endif
fprintf(stderr, "yolo\n");
srand(time(0));
return l;
}
void resize_yolo_layer(layer *l, int w, int h)
{
l->w = w;
l->h = h;
l->outputs = h*w*l->n*(l->classes + 4 + 1);
l->inputs = l->outputs;
if (l->embedding_output) l->embedding_output = (float*)xrealloc(l->output, l->batch * l->embedding_size * l->n * l->h * l->w * sizeof(float));
if (l->labels) l->labels = (int*)xrealloc(l->labels, l->batch * l->n * l->h * l->w * sizeof(int));
if (!l->output_pinned) l->output = (float*)xrealloc(l->output, l->batch*l->outputs * sizeof(float));
if (!l->delta_pinned) l->delta = (float*)xrealloc(l->delta, l->batch*l->outputs*sizeof(float));
#ifdef GPU
if (l->output_pinned) {
CHECK_CUDA(cudaFreeHost(l->output));
if (cudaSuccess != cudaHostAlloc(&l->output, l->batch*l->outputs * sizeof(float), cudaHostRegisterMapped)) {
cudaGetLastError();
l->output = (float*)xcalloc(l->batch * l->outputs, sizeof(float));
l->output_pinned = 0;
}
}
if (l->delta_pinned) {
CHECK_CUDA(cudaFreeHost(l->delta));
if (cudaSuccess != cudaHostAlloc(&l->delta, l->batch*l->outputs * sizeof(float), cudaHostRegisterMapped)) {
cudaGetLastError();
l->delta = (float*)xcalloc(l->batch * l->outputs, sizeof(float));
l->delta_pinned = 0;
}
}
cuda_free(l->delta_gpu);
cuda_free(l->output_gpu);
cuda_free(l->output_avg_gpu);
l->delta_gpu = cuda_make_array(l->delta, l->batch*l->outputs);
l->output_gpu = cuda_make_array(l->output, l->batch*l->outputs);
l->output_avg_gpu = cuda_make_array(l->output, l->batch*l->outputs);
#endif
}
box get_yolo_box(float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, int stride)
{
box b;
b.x = (i + x[index + 0*stride]) / lw;
b.y = (j + x[index + 1*stride]) / lh;
b.w = exp(x[index + 2*stride]) * biases[2*n] / w;
b.h = exp(x[index + 3*stride]) * biases[2*n+1] / h;
return b;
}
static inline float fix_nan_inf(float val)
{
if (isnan(val) || isinf(val)) val = 0;
return val;
}
static inline float clip_value(float val, const float max_val)
{
if (val > max_val) {
val = max_val;
}
else if (val < -max_val) {
val = -max_val;
}
return val;
}
ious delta_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, float max_delta, int *rewritten_bbox)
{
if (delta[index + 0 * stride] || delta[index + 1 * stride] || delta[index + 2 * stride] || delta[index + 3 * stride]) {
(*rewritten_bbox)++;
}
ious all_ious = { 0 };
box pred = get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
all_ious.iou = box_iou(pred, truth);
all_ious.giou = box_giou(pred, truth);
all_ious.diou = box_diou(pred, truth);
all_ious.ciou = box_ciou(pred, truth);
if (pred.w == 0) { pred.w = 1.0; }
if (pred.h == 0) { pred.h = 1.0; }
if (iou_loss == MSE)
{
float tx = (truth.x*lw - i);
float ty = (truth.y*lh - j);
float tw = log(truth.w*w / biases[2 * n]);
float th = log(truth.h*h / biases[2 * n + 1]);
delta[index + 0 * stride] += scale * (tx - x[index + 0 * stride]) * iou_normalizer;
delta[index + 1 * stride] += scale * (ty - x[index + 1 * stride]) * iou_normalizer;
delta[index + 2 * stride] += scale * (tw - x[index + 2 * stride]) * iou_normalizer;
delta[index + 3 * stride] += scale * (th - x[index + 3 * stride]) * iou_normalizer;
}
else {
all_ious.dx_iou = dx_box_iou(pred, truth, iou_loss);
float dx = all_ious.dx_iou.dt;
float dy = all_ious.dx_iou.db;
float dw = all_ious.dx_iou.dl;
float dh = all_ious.dx_iou.dr;
dw *= exp(x[index + 2 * stride]);
dh *= exp(x[index + 3 * stride]);
dx *= iou_normalizer;
dy *= iou_normalizer;
dw *= iou_normalizer;
dh *= iou_normalizer;
dx = fix_nan_inf(dx);
dy = fix_nan_inf(dy);
dw = fix_nan_inf(dw);
dh = fix_nan_inf(dh);
if (max_delta != FLT_MAX) {
dx = clip_value(dx, max_delta);
dy = clip_value(dy, max_delta);
dw = clip_value(dw, max_delta);
dh = clip_value(dh, max_delta);
}
if (!accumulate) {
delta[index + 0 * stride] = 0;
delta[index + 1 * stride] = 0;
delta[index + 2 * stride] = 0;
delta[index + 3 * stride] = 0;
}
delta[index + 0 * stride] += dx;
delta[index + 1 * stride] += dy;
delta[index + 2 * stride] += dw;
delta[index + 3 * stride] += dh;
}
return all_ious;
}
void averages_yolo_deltas(int class_index, int box_index, int stride, int classes, float *delta)
{
int classes_in_one_box = 0;
int c;
for (c = 0; c < classes; ++c) {
if (delta[class_index + stride*c] > 0) classes_in_one_box++;
}
if (classes_in_one_box > 0) {
delta[box_index + 0 * stride] /= classes_in_one_box;
delta[box_index + 1 * stride] /= classes_in_one_box;
delta[box_index + 2 * stride] /= classes_in_one_box;
delta[box_index + 3 * stride] /= classes_in_one_box;
}
}
void delta_yolo_class(float *output, float *delta, int index, int class_id, int classes, int stride, float *avg_cat, int focal_loss, float label_smooth_eps, float *classes_multipliers)
{
int n;
if (delta[index + stride*class_id]){
float y_true = 1;
if(label_smooth_eps) y_true = y_true * (1 - label_smooth_eps) + 0.5*label_smooth_eps;
float result_delta = y_true - output[index + stride*class_id];
if(!isnan(result_delta) && !isinf(result_delta)) delta[index + stride*class_id] = result_delta;
if (classes_multipliers) delta[index + stride*class_id] *= classes_multipliers[class_id];
if(avg_cat) *avg_cat += output[index + stride*class_id];
return;
}
if (focal_loss) {
float alpha = 0.5;
int ti = index + stride*class_id;
float pt = output[ti] + 0.000000000000001F;
float grad = -(1 - pt) * (2 * pt*logf(pt) + pt - 1);
for (n = 0; n < classes; ++n) {
delta[index + stride*n] = (((n == class_id) ? 1 : 0) - output[index + stride*n]);
delta[index + stride*n] *= alpha*grad;
if (n == class_id && avg_cat) *avg_cat += output[index + stride*n];
}
}
else {
for (n = 0; n < classes; ++n) {
float y_true = ((n == class_id) ? 1 : 0);
if (label_smooth_eps) y_true = y_true * (1 - label_smooth_eps) + 0.5*label_smooth_eps;
float result_delta = y_true - output[index + stride*n];
if (!isnan(result_delta) && !isinf(result_delta)) delta[index + stride*n] = result_delta;
if (classes_multipliers && n == class_id) delta[index + stride*class_id] *= classes_multipliers[class_id];
if (n == class_id && avg_cat) *avg_cat += output[index + stride*n];
}
}
}
int compare_yolo_class(float *output, int classes, int class_index, int stride, float objectness, int class_id, float conf_thresh)
{
int j;
for (j = 0; j < classes; ++j) {
float prob = output[class_index + stride*j];
if (prob > conf_thresh) {
return 1;
}
}
return 0;
}
static int entry_index(layer l, int batch, int location, int entry)
{
int n = location / (l.w*l.h);
int loc = location % (l.w*l.h);
return batch*l.outputs + n*l.w*l.h*(4+l.classes+1) + entry*l.w*l.h + loc;
}
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));
printf("\nYolo layer:out_h=%d, out_w=%d, out_c=%d\n", l.out_h, l.out_w, l.out_c);
#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;
for (i = 0; i < l.batch * l.w*l.h*l.n; ++i) l.labels[i] = -1;
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) {
const int class_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4 + 1);
const int obj_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 4);
const int box_index = entry_index(l, b, n*l.w*l.h + j*l.w + i, 0);
const int stride = l.w*l.h;
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*l.truth_size + b*l.truths, 1);
if (!truth.x) break;
int class_id = state.truth[t*l.truth_size + b*l.truths + 4];
if (class_id >= l.classes || class_id < 0) {
printf("\n 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("\n 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);
if (check_mistakes) getchar();
continue;
}
float objectness = l.output[obj_index];
if (isnan(objectness) || isinf(objectness)) l.output[obj_index] = 0;
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;
}
}
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) {
const float iou_multiplier = best_match_iou*best_match_iou;
if (l.objectness_smooth) {
l.delta[obj_index] = l.cls_normalizer * (iou_multiplier - l.output[obj_index]);
int class_id = state.truth[best_match_t*l.truth_size + b*l.truths + 4];
if (l.map) class_id = l.map[class_id];
if (class_id < l.classes && class_id >= 0) {
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);
}
}
else l.delta[obj_index] = 0;
}
else if (state.net.adversarial) {
int stride = l.w*l.h;
float scale = pred.w * pred.h;
if (scale > 0) scale = sqrt(scale);
l.delta[obj_index] = scale * l.cls_normalizer * (0 - l.output[obj_index]);
int cl_id;
int found_object = 0;
for (cl_id = 0; cl_id < l.classes; ++cl_id) {
if (l.output[class_index + stride*cl_id] * l.output[obj_index] > 0.25) {
l.delta[class_index + stride*cl_id] = scale * (0 - l.output[class_index + stride*cl_id]);
found_object = 1;
}
}
if (found_object) {
for (cl_id = 0; cl_id < l.classes; ++cl_id)
if (l.output[class_index + stride*cl_id] * l.output[obj_index] < 0.25)
l.delta[class_index + stride*cl_id] = scale * (1 - l.output[class_index + stride*cl_id]);
l.delta[box_index + 0 * stride] += scale * (0 - l.output[box_index + 0 * stride]);
l.delta[box_index + 1 * stride] += scale * (0 - l.output[box_index + 1 * stride]);
l.delta[box_index + 2 * stride] += scale * (0 - l.output[box_index + 2 * stride]);
l.delta[box_index + 3 * stride] += scale * (0 - l.output[box_index + 3 * stride]);
}
}
if (best_iou > l.truth_thresh) {
const float iou_multiplier = best_iou*best_iou;
if (l.objectness_smooth) l.delta[obj_index] = l.cls_normalizer * (iou_multiplier - l.output[obj_index]);
else l.delta[obj_index] = l.cls_normalizer * (1 - l.output[obj_index]);
int class_id = state.truth[best_t*l.truth_size + b*l.truths + 4];
if (l.map) class_id = l.map[class_id];
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);
const float class_multiplier = (l.classes_multipliers) ? l.classes_multipliers[class_id] : 1.0f;
if (l.objectness_smooth) l.delta[class_index + stride*class_id] = class_multiplier * (iou_multiplier - l.output[class_index + stride*class_id]);
box truth = float_to_box_stride(state.truth + best_t*l.truth_size + b*l.truths, 1);
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, state.net.rewritten_bbox);
(*state.net.total_bbox)++;
}
}
}
}
for (t = 0; t < l.max_boxes; ++t) {
box truth = float_to_box_stride(state.truth + t*l.truth_size + b*l.truths, 1);
if (!truth.x) break;
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*l.truth_size + b*l.truths + 4];
if (class_id >= l.classes || class_id < 0) continue;
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*l.truth_size + 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, state.net.rewritten_bbox);
(*state.net.total_bbox)++;
const int truth_in_index = t*l.truth_size + b*l.truths + 5;
const int track_id = state.truth[truth_in_index];
const int truth_out_index = b*l.n*l.w*l.h + mask_n*l.w*l.h + j*l.w + i;
l.labels[truth_out_index] = track_id;
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_kind(pred, truth_shift, l.iou_thresh_kind);
if (iou > l.iou_thresh) {
int class_id = state.truth[t*l.truth_size + 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, state.net.rewritten_bbox);
(*state.net.total_bbox)++;
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);
}
}
}
}
if (count == 0) count = 1;
if (class_count == 0) class_count = 1;
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;
fprintf(stderr, "v3 (%s loss, Normalizer: (iou: %.2f, cls: %.2f) Region %d Avg (IOU: %f, GIOU: %f), Class: %f, Obj: %f, No Obj: %f, .5R: %f, .75R: %f, count: %d, class_loss = %f, iou_loss = %f, total_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,
classification_loss, iou_loss, loss);
}
void backward_yolo_layer(const layer l, network_state state)
{
axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, state.delta, 1);
}
void correct_yolo_boxes(detection *dets, int n, int w, int h, int netw, int neth, int relative, int letter)
{
int i;
int new_w = 0;
int new_h = 0;
if (letter) {
if (((float)netw / w) < ((float)neth / h)) {
new_w = netw;
new_h = (h * netw) / w;
}
else {
new_h = neth;
new_w = (w * neth) / h;
}
}
else {
new_w = netw;
new_h = neth;
}
float deltaw = netw - new_w;
float deltah = neth - new_h;
float ratiow = (float)new_w / netw;
float ratioh = (float)new_h / neth;
for (i = 0; i < n; ++i) {
box b = dets[i].bbox;
b.x = (b.x - deltaw / 2. / netw) / ratiow;
b.y = (b.y - deltah / 2. / neth) / ratioh;
b.w *= 1 / ratiow;
b.h *= 1 / ratioh;
if (!relative) {
b.x *= w;
b.w *= w;
b.y *= h;
b.h *= h;
}
dets[i].bbox = b;
}
}
int yolo_num_detections(layer l, float thresh)
{
int i, n;
int count = 0;
for(n = 0; n < l.n; ++n){
for (i = 0; i < l.w*l.h; ++i) {
int obj_index = entry_index(l, 0, n*l.w*l.h + i, 4);
if(l.output[obj_index] > thresh){
++count;
}
}
}
return count;
}
int yolo_num_detections_batch(layer l, float thresh, int batch)
{
int i, n;
int count = 0;
for (i = 0; i < l.w*l.h; ++i){
for(n = 0; n < l.n; ++n){
int obj_index = entry_index(l, batch, n*l.w*l.h + i, 4);
if(l.output[obj_index] > thresh){
++count;
}
}
}
return count;
}
void avg_flipped_yolo(layer l)
{
int i,j,n,z;
float *flip = l.output + l.outputs;
for (j = 0; j < l.h; ++j) {
for (i = 0; i < l.w/2; ++i) {
for (n = 0; n < l.n; ++n) {
for(z = 0; z < l.classes + 4 + 1; ++z){
int i1 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + i;
int i2 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + (l.w - i - 1);
float swap = flip[i1];
flip[i1] = flip[i2];
flip[i2] = swap;
if(z == 0){
flip[i1] = -flip[i1];
flip[i2] = -flip[i2];
}
}
}
}
}
for(i = 0; i < l.outputs; ++i){
l.output[i] = (l.output[i] + flip[i])/2.;
}
}
int get_yolo_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, int relative, detection *dets, int letter)
{
int i,j,n;
float *predictions = l.output;
int count = 0;
for (i = 0; i < l.w*l.h; ++i){
int row = i / l.w;
int col = i % l.w;
for(n = 0; n < l.n; ++n){
int obj_index = entry_index(l, 0, n*l.w*l.h + i, 4);
float objectness = predictions[obj_index];
if (objectness > thresh) {
int box_index = entry_index(l, 0, n*l.w*l.h + i, 0);
dets[count].bbox = get_yolo_box(predictions, l.biases, l.mask[n], box_index, col, row, l.w, l.h, netw, neth, l.w*l.h);
dets[count].objectness = objectness;
dets[count].classes = l.classes;
if (l.embedding_output) {
get_embedding(l.embedding_output, l.w, l.h, l.n*l.embedding_size, l.embedding_size, col, row, n, 0, dets[count].embeddings);
}
for (j = 0; j < l.classes; ++j) {
int class_index = entry_index(l, 0, n*l.w*l.h + i, 4 + 1 + j);
float prob = objectness*predictions[class_index];
dets[count].prob[j] = (prob > thresh) ? prob : 0;
}
++count;
}
}
}
correct_yolo_boxes(dets, count, w, h, netw, neth, relative, letter);
return count;
}
int get_yolo_detections_batch(layer l, int w, int h, int netw, int neth, float thresh, int *map, int relative, detection *dets, int letter, int batch)
{
int i,j,n;
float *predictions = l.output;
int count = 0;
for (i = 0; i < l.w*l.h; ++i){
int row = i / l.w;
int col = i % l.w;
for(n = 0; n < l.n; ++n){
int obj_index = entry_index(l, batch, n*l.w*l.h + i, 4);
float objectness = predictions[obj_index];
if (objectness > thresh) {
int box_index = entry_index(l, batch, n*l.w*l.h + i, 0);
dets[count].bbox = get_yolo_box(predictions, l.biases, l.mask[n], box_index, col, row, l.w, l.h, netw, neth, l.w*l.h);
dets[count].objectness = objectness;
dets[count].classes = l.classes;
if (l.embedding_output) {
get_embedding(l.embedding_output, l.w, l.h, l.n*l.embedding_size, l.embedding_size, col, row, n, batch, dets[count].embeddings);
}
for (j = 0; j < l.classes; ++j) {
int class_index = entry_index(l, batch, n*l.w*l.h + i, 4 + 1 + j);
float prob = objectness*predictions[class_index];
dets[count].prob[j] = (prob > thresh) ? prob : 0;
}
++count;
}
}
}
correct_yolo_boxes(dets, count, w, h, netw, neth, relative, letter);
return count;
}
#ifdef GPU
void forward_yolo_layer_gpu(const layer l, network_state state)
{
if (l.embedding_output) {
layer le = state.net.layers[l.embedding_layer_id];
cuda_pull_array_async(le.output_gpu, l.embedding_output, le.batch*le.outputs);
}
simple_copy_ongpu(l.batch*l.inputs, state.input, l.output_gpu);
int b, n;
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_ongpu(l.output_gpu + index, 2*l.w*l.h, LOGISTIC);
if (l.scale_x_y != 1) scal_add_ongpu(2 * l.w*l.h, l.scale_x_y, -0.5*(l.scale_x_y - 1), l.output_gpu + index, 1);
index = entry_index(l, b, n*l.w*l.h, 4);
activate_array_ongpu(l.output_gpu + index, (1+l.classes)*l.w*l.h, LOGISTIC);
}
}
if(!state.train || l.onlyforward){
if (l.mean_alpha && l.output_avg_gpu) mean_array_gpu(l.output_gpu, l.batch*l.outputs, l.mean_alpha, l.output_avg_gpu);
cuda_pull_array_async(l.output_gpu, l.output, l.batch*l.outputs);
CHECK_CUDA(cudaPeekAtLastError());
return;
}
float *in_cpu = (float *)xcalloc(l.batch*l.inputs, sizeof(float));
cuda_pull_array(l.output_gpu, l.output, l.batch*l.outputs);
memcpy(in_cpu, l.output, l.batch*l.outputs*sizeof(float));
float *truth_cpu = 0;
if (state.truth) {
int num_truth = l.batch*l.truths;
truth_cpu = (float *)xcalloc(num_truth, sizeof(float));
cuda_pull_array(state.truth, truth_cpu, num_truth);
}
network_state cpu_state = state;
cpu_state.net = state.net;
cpu_state.index = state.index;
cpu_state.train = state.train;
cpu_state.truth = truth_cpu;
cpu_state.input = in_cpu;
forward_yolo_layer(l, cpu_state);
cuda_push_array(l.delta_gpu, l.delta, l.batch*l.outputs);
free(in_cpu);
if (cpu_state.truth) free(cpu_state.truth);
}
void backward_yolo_layer_gpu(const layer l, network_state state)
{
axpy_ongpu(l.batch*l.inputs, state.net.loss_scale, l.delta_gpu, 1, state.delta, 1);
}
#endif