24 l.
c = n*(classes + 4 + 1);
29 l.
cost = calloc(1,
sizeof(
float));
30 l.
biases = calloc(total*2,
sizeof(
float));
31 if(mask) l.
mask = mask;
33 l.
mask = calloc(n,
sizeof(
int));
34 for(i = 0; i < n; ++i){
39 l.
outputs = h*w*n*(classes + 4 + 1);
44 for(i = 0; i < total*2; ++i){
57 fprintf(stderr,
"yolo\n");
75 cuda_free(l->delta_gpu);
76 cuda_free(l->output_gpu);
83 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)
86 b.
x = (i + x[index + 0*stride]) / lw;
87 b.
y = (j + x[index + 1*stride]) / lh;
88 b.
w = exp(x[index + 2*stride]) * biases[2*n] / w;
89 b.
h = exp(x[index + 3*stride]) * biases[2*n+1] / h;
93 float 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)
95 box pred =
get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
96 float iou =
box_iou(pred, truth);
98 float tx = (truth.
x*lw - i);
99 float ty = (truth.
y*lh - j);
100 float tw = log(truth.
w*w / biases[2*n]);
101 float th = log(truth.
h*h / biases[2*n + 1]);
103 delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
104 delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
105 delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
106 delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
115 delta[index + stride*
class] = 1 - output[index + stride*
class];
116 if(avg_cat) *avg_cat += output[index + stride*
class];
120 delta[index + stride*n] = ((n ==
class)?1 : 0) - output[index + stride*n];
121 if(n ==
class && avg_cat) *avg_cat += output[index + stride*n];
127 int n = location / (l.
w*l.
h);
128 int loc = location % (l.
w*l.
h);
138 for (b = 0; b < l.
batch; ++b){
139 for(n = 0; n < l.
n; ++n){
149 if(!net.
train)
return;
155 float avg_anyobj = 0;
159 for (b = 0; b < l.
batch; ++b) {
160 for (j = 0; j < l.
h; ++j) {
161 for (i = 0; i < l.
w; ++i) {
162 for (n = 0; n < l.
n; ++n) {
164 box pred =
get_yolo_box(l.
output, l.
biases, l.
mask[n], box_index, i, j, l.
w, l.
h, net.
w, net.
h, l.
w*l.
h);
170 float iou =
box_iou(pred, truth);
171 if (iou > best_iou) {
177 avg_anyobj += l.
output[obj_index];
180 l.
delta[obj_index] = 0;
185 int class = net.truth[best_t*(4 + 1) + b*l.
truths + 4];
186 if (l.
map)
class = l.map[class];
187 int class_index =
entry_index(l, b, n*l.
w*l.
h + j*l.
w + i, 4 + 1);
190 delta_yolo_box(truth, l.
output, l.
biases, l.
mask[n], box_index, i, j, l.
w, l.
h, net.
w, net.
h, l.
delta, (2-truth.
w*truth.
h), l.
w*l.
h);
203 box truth_shift = truth;
204 truth_shift.
x = truth_shift.
y = 0;
205 for(n = 0; n < l.
total; ++n){
209 float iou =
box_iou(pred, truth_shift);
219 float iou =
delta_yolo_box(truth, l.
output, l.
biases, best_n, box_index, i, j, l.
w, l.
h, net.
w, net.
h, l.
delta, (2-truth.
w*truth.
h), l.
w*l.
h);
222 avg_obj += l.
output[obj_index];
225 int class = net.truth[t*(4 + 1) + b*l.
truths + 4];
226 if (l.
map)
class = l.map[class];
227 int class_index =
entry_index(l, b, mask_n*l.
w*l.
h + j*l.
w + i, 4 + 1);
232 if(iou > .5) recall += 1;
233 if(iou > .75) recall75 += 1;
239 printf(
"Region %d Avg IOU: %f, Class: %f, Obj: %f, No Obj: %f, .5R: %f, .75R: %f, count: %d\n", net.
index, avg_iou/count, avg_cat/class_count, avg_obj/count, avg_anyobj/(l.
w*l.
h*l.
n*l.
batch), recall/count, recall75/count, count);
252 if (((
float)netw/w) < ((
float)neth/h)) {
254 new_h = (h * netw)/w;
257 new_w = (w * neth)/h;
259 for (i = 0; i < n; ++i){
261 b.
x = (b.
x - (netw - new_w)/2./netw) / ((float)new_w/netw);
262 b.
y = (b.
y - (neth - new_h)/2./neth) / ((float)new_h/neth);
263 b.
w *= (float)netw/new_w;
264 b.
h *= (float)neth/new_h;
279 for (i = 0; i < l.
w*l.
h; ++i){
280 for(n = 0; n < l.
n; ++n){
282 if(l.
output[obj_index] > thresh){
294 for (j = 0; j < l.
h; ++j) {
295 for (i = 0; i < l.
w/2; ++i) {
296 for (n = 0; n < l.
n; ++n) {
297 for(z = 0; z < l.
classes + 4 + 1; ++z){
298 int i1 = z*l.
w*l.
h*l.
n + n*l.
w*l.
h + j*l.
w + i;
299 int i2 = z*l.
w*l.
h*l.
n + n*l.
w*l.
h + j*l.
w + (l.
w - i - 1);
300 float swap = flip[i1];
304 flip[i1] = -flip[i1];
305 flip[i2] = -flip[i2];
311 for(i = 0; i < l.
outputs; ++i){
319 float *predictions = l.
output;
322 for (i = 0; i < l.
w*l.
h; ++i){
325 for(n = 0; n < l.
n; ++n){
327 float objectness = predictions[obj_index];
328 if(objectness <= thresh)
continue;
330 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);
333 for(j = 0; j < l.
classes; ++j){
334 int class_index =
entry_index(l, 0, n*l.
w*l.
h + i, 4 + 1 + j);
335 float prob = objectness*predictions[class_index];
336 dets[count].
prob[j] = (prob > thresh) ? prob : 0;
351 for (b = 0; b < l.
batch; ++b){
352 for(n = 0; n < l.
n; ++n){
369 void backward_yolo_layer_gpu(
const layer l,
network net)
void correct_yolo_boxes(detection *dets, int n, int w, int h, int netw, int neth, int relative)
void(* forward_gpu)(struct layer, struct network)
int int_index(int *a, int val, int n)
void(* backward_gpu)(struct layer, struct network)
int yolo_num_detections(layer l, float thresh)
layer make_yolo_layer(int batch, int w, int h, int n, int total, int *mask, int classes)
void axpy_gpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
void(* forward)(struct layer, struct network)
int entry_index(layer l, int batch, int location, int entry)
int get_yolo_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, int relative, detection *dets)
void resize_yolo_layer(layer *l, int w, int h)
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
void(* backward)(struct layer, struct network)
void avg_flipped_yolo(layer l)
void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)
void forward_yolo_layer(const layer l, network net)
void copy_gpu(int N, float *X, int INCX, float *Y, int INCY)
float box_iou(box a, box b)
void activate_array(float *x, const int n, const ACTIVATION a)
void activate_array_gpu(float *x, int n, ACTIVATION a)
box float_to_box(float *f, int stride)
float mag_array(float *a, int n)
void backward_yolo_layer(const layer l, network net)
float 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)
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)