22 l.
c = n*(classes + coords + 1);
28 l.
cost = calloc(1,
sizeof(
float));
29 l.
biases = calloc(n*2,
sizeof(
float));
31 l.
outputs = h*w*n*(classes + coords + 1);
37 for(i = 0; i < n*2; ++i){
50 fprintf(stderr,
"detection\n");
68 cuda_free(l->delta_gpu);
69 cuda_free(l->output_gpu);
76 box get_region_box(
float *x,
float *biases,
int n,
int index,
int i,
int j,
int w,
int h,
int stride)
79 b.
x = (i + x[index + 0*stride]) / w;
80 b.
y = (j + x[index + 1*stride]) / h;
81 b.
w = exp(x[index + 2*stride]) * biases[2*n] / w;
82 b.
h = exp(x[index + 3*stride]) * biases[2*n+1] / h;
86 float delta_region_box(
box truth,
float *x,
float *biases,
int n,
int index,
int i,
int j,
int w,
int h,
float *delta,
float scale,
int stride)
89 float iou =
box_iou(pred, truth);
91 float tx = (truth.
x*w - i);
92 float ty = (truth.
y*h - j);
93 float tw = log(truth.
w*w / biases[2*n]);
94 float th = log(truth.
h*h / biases[2*n + 1]);
96 delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
97 delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
98 delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
99 delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
103 void delta_region_mask(
float *truth,
float *x,
int n,
int index,
float *delta,
int stride,
int scale)
106 for(i = 0; i < n; ++i){
107 delta[index + i*stride] = scale*(truth[i] - x[index + i*stride]);
118 pred *= output[index + stride*
class];
119 int g = hier->
group[
class];
122 delta[index + stride*(offset + i)] = scale * (0 - output[index + stride*(offset + i)]);
124 delta[index + stride*
class] = scale * (1 - output[index + stride*
class]);
126 class = hier->parent[class];
130 if (delta[index] && tag){
131 delta[index + stride*
class] = scale * (1 - output[index + stride*
class]);
135 delta[index + stride*n] = scale * (((n ==
class)?1 : 0) - output[index + stride*n]);
136 if(n ==
class) *avg_cat += output[index + stride*n];
143 return log(x/(1.-x));
153 int n = location / (l.
w*l.
h);
154 int loc = location % (l.
w*l.
h);
164 for (b = 0; b < l.
batch; ++b){
165 for(n = 0; n < l.
n; ++n){
189 if(!net.
train)
return;
194 float avg_anyobj = 0;
198 for (b = 0; b < l.
batch; ++b) {
201 for(t = 0; t < 30; ++
t){
207 if(truth.x > 100000 && truth.y > 100000){
208 for(n = 0; n < l.
n*l.
w*l.
h; ++n){
211 float scale = l.
output[obj_index];
223 else l.
delta[obj_index] = 0;
224 l.
delta[obj_index] = 0;
230 if(onlyclass)
continue;
232 for (j = 0; j < l.
h; ++j) {
233 for (i = 0; i < l.
w; ++i) {
234 for (n = 0; n < l.
n; ++n) {
238 for(t = 0; t < 30; ++
t){
241 float iou =
box_iou(pred, truth);
242 if (iou > best_iou) {
247 avg_anyobj += l.
output[obj_index];
250 if (best_iou > l.
thresh) {
251 l.
delta[obj_index] = 0;
254 if(*(net.
seen) < 12800){
256 truth.
x = (i + .5)/l.
w;
257 truth.
y = (j + .5)/l.
h;
260 delta_region_box(truth, l.
output, l.
biases, n, box_index, i, j, l.
w, l.
h, l.
delta, .01, l.
w*l.
h);
265 for(t = 0; t < 30; ++
t){
273 box truth_shift = truth;
276 for(n = 0; n < l.
n; ++n){
285 float iou =
box_iou(pred, truth_shift);
293 float iou =
delta_region_box(truth, l.
output, l.
biases, best_n, box_index, i, j, l.
w, l.
h, l.
delta, l.
coord_scale * (2 - truth.
w*truth.
h), l.
w*l.
h);
295 int mask_index =
entry_index(l, b, best_n*l.
w*l.
h + j*l.
w + i, 4);
298 if(iou > .5) recall += 1;
302 avg_obj += l.
output[obj_index];
312 if (l.
map)
class = l.map[class];
320 printf(
"Region Avg IOU: %f, Class: %f, Obj: %f, No Obj: %f, Avg Recall: %f, count: %d\n", avg_iou/count, avg_cat/class_count, avg_obj/count, avg_anyobj/(l.
w*l.
h*l.
n*l.
batch), recall/count, count);
341 if (((
float)netw/w) < ((
float)neth/h)) {
343 new_h = (h * netw)/w;
346 new_w = (w * neth)/h;
348 for (i = 0; i < n; ++i){
350 b.
x = (b.
x - (netw - new_w)/2./netw) / ((float)new_w/netw);
351 b.
y = (b.
y - (neth - new_h)/2./neth) / ((float)new_h/neth);
352 b.
w *= (float)netw/new_w;
353 b.
h *= (float)neth/new_h;
367 float *predictions = l.
output;
370 for (j = 0; j < l.
h; ++j) {
371 for (i = 0; i < l.
w/2; ++i) {
372 for (n = 0; n < l.
n; ++n) {
374 int i1 = z*l.
w*l.
h*l.
n + n*l.
w*l.
h + j*l.
w + i;
375 int i2 = z*l.
w*l.
h*l.
n + n*l.
w*l.
h + j*l.
w + (l.
w - i - 1);
376 float swap = flip[i1];
380 flip[i1] = -flip[i1];
381 flip[i2] = -flip[i2];
387 for(i = 0; i < l.
outputs; ++i){
391 for (i = 0; i < l.
w*l.
h; ++i){
394 for(n = 0; n < l.
n; ++n){
395 int index = n*l.
w*l.
h + i;
396 for(j = 0; j < l.
classes; ++j){
397 dets[index].
prob[j] = 0;
402 float scale = l.
background ? 1 : predictions[obj_index];
404 dets[index].
objectness = scale > thresh ? scale : 0;
405 if(dets[index].mask){
406 for(j = 0; j < l.
coords - 4; ++j){
416 for(j = 0; j < 200; ++j){
418 float prob = scale*predictions[class_index];
419 dets[index].
prob[j] = (prob > thresh) ? prob : 0;
423 dets[index].
prob[j] = (scale > thresh) ? scale : 0;
426 if(dets[index].objectness){
427 for(j = 0; j < l.
classes; ++j){
429 float prob = scale*predictions[class_index];
430 dets[index].
prob[j] = (prob > thresh) ? prob : 0;
445 for (b = 0; b < l.
batch; ++b){
446 for(n = 0; n < l.
n; ++n){
474 if(!net.
train)
return;
478 void backward_region_layer_gpu(
const layer l,
network net)
481 for (b = 0; b < l.
batch; ++b){
482 for(n = 0; n < l.
n; ++n){
500 for (i = 0; i < l.
w*l.
h; ++i){
501 for(n = 0; n < l.
n; ++n){
int hierarchy_top_prediction(float *predictions, tree *hier, float thresh, int stride)
void delta_region_mask(float *truth, float *x, int n, int index, float *delta, int stride, int scale)
void(* forward_gpu)(struct layer, struct network)
void(* backward_gpu)(struct layer, struct network)
void axpy_gpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
void(* forward)(struct layer, struct network)
void delta_region_class(float *output, float *delta, int index, int class, int classes, tree *hier, float scale, int stride, float *avg_cat, int tag)
void zero_objectness(layer l)
int entry_index(layer l, int batch, int location, int entry)
box get_region_box(float *x, float *biases, int n, int index, int i, int j, int w, int h, int stride)
void softmax_cpu(float *input, int n, int batch, int batch_offset, int groups, int group_offset, int stride, float temp, float *output)
void resize_region_layer(layer *l, int w, int h)
void(* backward)(struct layer, struct network)
void forward_region_layer(const layer l, network net)
float delta_region_box(box truth, float *x, float *biases, int n, int index, int i, int j, int w, int h, float *delta, float scale, int stride)
void copy_gpu(int N, float *X, int INCX, float *Y, int INCY)
void correct_region_boxes(detection *dets, int n, int w, int h, int netw, int neth, int relative)
float box_iou(box a, box b)
void softmax_gpu(float *input, int n, int batch, int batch_offset, int groups, int group_offset, int stride, float temp, float *output)
void activate_array(float *x, const int n, const ACTIVATION a)
void softmax_tree(float *input, int spatial, int batch, int stride, float temp, float *output, tree hier)
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_region_layer(const layer l, network net)
void get_region_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, float tree_thresh, int relative, detection *dets)
float get_hierarchy_probability(float *x, tree *hier, int c, int stride)
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride)
layer make_region_layer(int batch, int w, int h, int n, int classes, int coords)
void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta)