darknet  v3
detection_layer.c
Go to the documentation of this file.
1 #include "detection_layer.h"
2 #include "activations.h"
3 #include "softmax_layer.h"
4 #include "blas.h"
5 #include "box.h"
6 #include "cuda.h"
7 #include "utils.h"
8 
9 #include <stdio.h>
10 #include <assert.h>
11 #include <string.h>
12 #include <stdlib.h>
13 
14 detection_layer make_detection_layer(int batch, int inputs, int n, int side, int classes, int coords, int rescore)
15 {
16  detection_layer l = {0};
17  l.type = DETECTION;
18 
19  l.n = n;
20  l.batch = batch;
21  l.inputs = inputs;
22  l.classes = classes;
23  l.coords = coords;
24  l.rescore = rescore;
25  l.side = side;
26  l.w = side;
27  l.h = side;
28  assert(side*side*((1 + l.coords)*l.n + l.classes) == inputs);
29  l.cost = calloc(1, sizeof(float));
30  l.outputs = l.inputs;
31  l.truths = l.side*l.side*(1+l.coords+l.classes);
32  l.output = calloc(batch*l.outputs, sizeof(float));
33  l.delta = calloc(batch*l.outputs, sizeof(float));
34 
37 #ifdef GPU
38  l.forward_gpu = forward_detection_layer_gpu;
39  l.backward_gpu = backward_detection_layer_gpu;
40  l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
41  l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
42 #endif
43 
44  fprintf(stderr, "Detection Layer\n");
45  srand(0);
46 
47  return l;
48 }
49 
51 {
52  int locations = l.side*l.side;
53  int i,j;
54  memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
55  //if(l.reorg) reorg(l.output, l.w*l.h, size*l.n, l.batch, 1);
56  int b;
57  if (l.softmax){
58  for(b = 0; b < l.batch; ++b){
59  int index = b*l.inputs;
60  for (i = 0; i < locations; ++i) {
61  int offset = i*l.classes;
62  softmax(l.output + index + offset, l.classes, 1, 1,
63  l.output + index + offset);
64  }
65  }
66  }
67  if(net.train){
68  float avg_iou = 0;
69  float avg_cat = 0;
70  float avg_allcat = 0;
71  float avg_obj = 0;
72  float avg_anyobj = 0;
73  int count = 0;
74  *(l.cost) = 0;
75  int size = l.inputs * l.batch;
76  memset(l.delta, 0, size * sizeof(float));
77  for (b = 0; b < l.batch; ++b){
78  int index = b*l.inputs;
79  for (i = 0; i < locations; ++i) {
80  int truth_index = (b*locations + i)*(1+l.coords+l.classes);
81  int is_obj = net.truth[truth_index];
82  for (j = 0; j < l.n; ++j) {
83  int p_index = index + locations*l.classes + i*l.n + j;
84  l.delta[p_index] = l.noobject_scale*(0 - l.output[p_index]);
85  *(l.cost) += l.noobject_scale*pow(l.output[p_index], 2);
86  avg_anyobj += l.output[p_index];
87  }
88 
89  int best_index = -1;
90  float best_iou = 0;
91  float best_rmse = 20;
92 
93  if (!is_obj){
94  continue;
95  }
96 
97  int class_index = index + i*l.classes;
98  for(j = 0; j < l.classes; ++j) {
99  l.delta[class_index+j] = l.class_scale * (net.truth[truth_index+1+j] - l.output[class_index+j]);
100  *(l.cost) += l.class_scale * pow(net.truth[truth_index+1+j] - l.output[class_index+j], 2);
101  if(net.truth[truth_index + 1 + j]) avg_cat += l.output[class_index+j];
102  avg_allcat += l.output[class_index+j];
103  }
104 
105  box truth = float_to_box(net.truth + truth_index + 1 + l.classes, 1);
106  truth.x /= l.side;
107  truth.y /= l.side;
108 
109  for(j = 0; j < l.n; ++j){
110  int box_index = index + locations*(l.classes + l.n) + (i*l.n + j) * l.coords;
111  box out = float_to_box(l.output + box_index, 1);
112  out.x /= l.side;
113  out.y /= l.side;
114 
115  if (l.sqrt){
116  out.w = out.w*out.w;
117  out.h = out.h*out.h;
118  }
119 
120  float iou = box_iou(out, truth);
121  //iou = 0;
122  float rmse = box_rmse(out, truth);
123  if(best_iou > 0 || iou > 0){
124  if(iou > best_iou){
125  best_iou = iou;
126  best_index = j;
127  }
128  }else{
129  if(rmse < best_rmse){
130  best_rmse = rmse;
131  best_index = j;
132  }
133  }
134  }
135 
136  if(l.forced){
137  if(truth.w*truth.h < .1){
138  best_index = 1;
139  }else{
140  best_index = 0;
141  }
142  }
143  if(l.random && *(net.seen) < 64000){
144  best_index = rand()%l.n;
145  }
146 
147  int box_index = index + locations*(l.classes + l.n) + (i*l.n + best_index) * l.coords;
148  int tbox_index = truth_index + 1 + l.classes;
149 
150  box out = float_to_box(l.output + box_index, 1);
151  out.x /= l.side;
152  out.y /= l.side;
153  if (l.sqrt) {
154  out.w = out.w*out.w;
155  out.h = out.h*out.h;
156  }
157  float iou = box_iou(out, truth);
158 
159  //printf("%d,", best_index);
160  int p_index = index + locations*l.classes + i*l.n + best_index;
161  *(l.cost) -= l.noobject_scale * pow(l.output[p_index], 2);
162  *(l.cost) += l.object_scale * pow(1-l.output[p_index], 2);
163  avg_obj += l.output[p_index];
164  l.delta[p_index] = l.object_scale * (1.-l.output[p_index]);
165 
166  if(l.rescore){
167  l.delta[p_index] = l.object_scale * (iou - l.output[p_index]);
168  }
169 
170  l.delta[box_index+0] = l.coord_scale*(net.truth[tbox_index + 0] - l.output[box_index + 0]);
171  l.delta[box_index+1] = l.coord_scale*(net.truth[tbox_index + 1] - l.output[box_index + 1]);
172  l.delta[box_index+2] = l.coord_scale*(net.truth[tbox_index + 2] - l.output[box_index + 2]);
173  l.delta[box_index+3] = l.coord_scale*(net.truth[tbox_index + 3] - l.output[box_index + 3]);
174  if(l.sqrt){
175  l.delta[box_index+2] = l.coord_scale*(sqrt(net.truth[tbox_index + 2]) - l.output[box_index + 2]);
176  l.delta[box_index+3] = l.coord_scale*(sqrt(net.truth[tbox_index + 3]) - l.output[box_index + 3]);
177  }
178 
179  *(l.cost) += pow(1-iou, 2);
180  avg_iou += iou;
181  ++count;
182  }
183  }
184 
185  if(0){
186  float *costs = calloc(l.batch*locations*l.n, sizeof(float));
187  for (b = 0; b < l.batch; ++b) {
188  int index = b*l.inputs;
189  for (i = 0; i < locations; ++i) {
190  for (j = 0; j < l.n; ++j) {
191  int p_index = index + locations*l.classes + i*l.n + j;
192  costs[b*locations*l.n + i*l.n + j] = l.delta[p_index]*l.delta[p_index];
193  }
194  }
195  }
196  int indexes[100];
197  top_k(costs, l.batch*locations*l.n, 100, indexes);
198  float cutoff = costs[indexes[99]];
199  for (b = 0; b < l.batch; ++b) {
200  int index = b*l.inputs;
201  for (i = 0; i < locations; ++i) {
202  for (j = 0; j < l.n; ++j) {
203  int p_index = index + locations*l.classes + i*l.n + j;
204  if (l.delta[p_index]*l.delta[p_index] < cutoff) l.delta[p_index] = 0;
205  }
206  }
207  }
208  free(costs);
209  }
210 
211 
212  *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
213 
214 
215  printf("Detection Avg IOU: %f, Pos Cat: %f, All Cat: %f, Pos Obj: %f, Any Obj: %f, count: %d\n", avg_iou/count, avg_cat/count, avg_allcat/(count*l.classes), avg_obj/count, avg_anyobj/(l.batch*locations*l.n), count);
216  //if(l.reorg) reorg(l.delta, l.w*l.h, size*l.n, l.batch, 0);
217  }
218 }
219 
221 {
222  axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, net.delta, 1);
223 }
224 
225 void get_detection_detections(layer l, int w, int h, float thresh, detection *dets)
226 {
227  int i,j,n;
228  float *predictions = l.output;
229  //int per_cell = 5*num+classes;
230  for (i = 0; i < l.side*l.side; ++i){
231  int row = i / l.side;
232  int col = i % l.side;
233  for(n = 0; n < l.n; ++n){
234  int index = i*l.n + n;
235  int p_index = l.side*l.side*l.classes + i*l.n + n;
236  float scale = predictions[p_index];
237  int box_index = l.side*l.side*(l.classes + l.n) + (i*l.n + n)*4;
238  box b;
239  b.x = (predictions[box_index + 0] + col) / l.side * w;
240  b.y = (predictions[box_index + 1] + row) / l.side * h;
241  b.w = pow(predictions[box_index + 2], (l.sqrt?2:1)) * w;
242  b.h = pow(predictions[box_index + 3], (l.sqrt?2:1)) * h;
243  dets[index].bbox = b;
244  dets[index].objectness = scale;
245  for(j = 0; j < l.classes; ++j){
246  int class_index = i*l.classes;
247  float prob = scale*predictions[class_index+j];
248  dets[index].prob[j] = (prob > thresh) ? prob : 0;
249  }
250  }
251  }
252 }
253 
254 #ifdef GPU
255 
256 void forward_detection_layer_gpu(const detection_layer l, network net)
257 {
258  if(!net.train){
259  copy_gpu(l.batch*l.inputs, net.input_gpu, 1, l.output_gpu, 1);
260  return;
261  }
262 
263  cuda_pull_array(net.input_gpu, net.input, l.batch*l.inputs);
264  forward_detection_layer(l, net);
265  cuda_push_array(l.output_gpu, l.output, l.batch*l.outputs);
266  cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
267 }
268 
269 void backward_detection_layer_gpu(detection_layer l, network net)
270 {
271  axpy_gpu(l.batch*l.inputs, 1, l.delta_gpu, 1, net.delta_gpu, 1);
272  //copy_gpu(l.batch*l.inputs, l.delta_gpu, 1, net.delta_gpu, 1);
273 }
274 #endif
275 
float object_scale
Definition: darknet.h:190
detection_layer make_detection_layer(int batch, int inputs, int n, int side, int classes, int coords, int rescore)
int w
Definition: darknet.h:140
int n
Definition: darknet.h:142
int truths
Definition: darknet.h:139
int rescore
Definition: darknet.h:175
float h
Definition: darknet.h:520
void(* forward_gpu)(struct layer, struct network)
Definition: darknet.h:126
float * truth
Definition: darknet.h:485
void(* backward_gpu)(struct layer, struct network)
Definition: darknet.h:127
size_t * seen
Definition: darknet.h:437
void top_k(float *a, int n, int k, int *index)
Definition: utils.c:237
void axpy_gpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
void backward_detection_layer(const detection_layer l, network net)
void(* forward)(struct layer, struct network)
Definition: darknet.h:123
void forward_detection_layer(const detection_layer l, network net)
void softmax(float *input, int n, float temp, int stride, float *output)
Definition: blas.c:305
int side
Definition: darknet.h:146
float * delta
Definition: darknet.h:486
float class_scale
Definition: darknet.h:193
void get_detection_detections(layer l, int w, int h, float thresh, detection *dets)
float w
Definition: darknet.h:520
float box_rmse(box a, box b)
Definition: box.c:184
float x
Definition: darknet.h:520
int train
Definition: darknet.h:488
int h
Definition: darknet.h:140
float * delta
Definition: darknet.h:245
int inputs
Definition: darknet.h:134
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
Definition: blas.c:178
void(* backward)(struct layer, struct network)
Definition: darknet.h:124
float * prob
Definition: darknet.h:526
float objectness
Definition: darknet.h:528
int batch
Definition: darknet.h:131
float * output
Definition: darknet.h:246
void copy_gpu(int N, float *X, int INCX, float *Y, int INCY)
box bbox
Definition: darknet.h:524
int sqrt
Definition: darknet.h:152
int forced
Definition: darknet.h:132
float box_iou(box a, box b)
Definition: box.c:179
int coords
Definition: darknet.h:173
int random
Definition: darknet.h:195
int classes
Definition: darknet.h:172
int softmax
Definition: darknet.h:171
LAYER_TYPE type
Definition: darknet.h:120
float * input
Definition: darknet.h:484
float y
Definition: darknet.h:520
box float_to_box(float *f, int stride)
Definition: box.c:91
int outputs
Definition: darknet.h:135
float mag_array(float *a, int n)
Definition: utils.c:574
float coord_scale
Definition: darknet.h:189
list classes
Definition: voc_label.py:9
float * cost
Definition: darknet.h:222
float noobject_scale
Definition: darknet.h:191
Definition: darknet.h:519
Definition: darknet.h:119