00001 #include "detection_layer.h"
00002 #include "activations.h"
00003 #include "softmax_layer.h"
00004 #include "blas.h"
00005 #include "box.h"
00006 #include "cuda.h"
00007 #include "utils.h"
00008 #include <stdio.h>
00009 #include <assert.h>
00010 #include <string.h>
00011 #include <stdlib.h>
00012
00013 detection_layer make_detection_layer(int batch, int inputs, int n, int side, int classes, int coords, int rescore)
00014 {
00015 detection_layer l = {0};
00016 l.type = DETECTION;
00017
00018 l.n = n;
00019 l.batch = batch;
00020 l.inputs = inputs;
00021 l.classes = classes;
00022 l.coords = coords;
00023 l.rescore = rescore;
00024 l.side = side;
00025 l.w = side;
00026 l.h = side;
00027 assert(side*side*((1 + l.coords)*l.n + l.classes) == inputs);
00028 l.cost = calloc(1, sizeof(float));
00029 l.outputs = l.inputs;
00030 l.truths = l.side*l.side*(1+l.coords+l.classes);
00031 l.output = calloc(batch*l.outputs, sizeof(float));
00032 l.delta = calloc(batch*l.outputs, sizeof(float));
00033
00034 l.forward = forward_detection_layer;
00035 l.backward = backward_detection_layer;
00036 #ifdef GPU
00037 l.forward_gpu = forward_detection_layer_gpu;
00038 l.backward_gpu = backward_detection_layer_gpu;
00039 l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
00040 l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
00041 #endif
00042
00043 fprintf(stderr, "Detection Layer\n");
00044 srand(0);
00045
00046 return l;
00047 }
00048
00049 void forward_detection_layer(const detection_layer l, network_state state)
00050 {
00051 int locations = l.side*l.side;
00052 int i,j;
00053 memcpy(l.output, state.input, l.outputs*l.batch*sizeof(float));
00054
00055 int b;
00056 if (l.softmax){
00057 for(b = 0; b < l.batch; ++b){
00058 int index = b*l.inputs;
00059 for (i = 0; i < locations; ++i) {
00060 int offset = i*l.classes;
00061 softmax(l.output + index + offset, l.classes, 1,
00062 l.output + index + offset);
00063 }
00064 }
00065 }
00066 if(state.train){
00067 float avg_iou = 0;
00068 float avg_cat = 0;
00069 float avg_allcat = 0;
00070 float avg_obj = 0;
00071 float avg_anyobj = 0;
00072 int count = 0;
00073 *(l.cost) = 0;
00074 int size = l.inputs * l.batch;
00075 memset(l.delta, 0, size * sizeof(float));
00076 for (b = 0; b < l.batch; ++b){
00077 int index = b*l.inputs;
00078 for (i = 0; i < locations; ++i) {
00079 int truth_index = (b*locations + i)*(1+l.coords+l.classes);
00080 int is_obj = state.truth[truth_index];
00081 for (j = 0; j < l.n; ++j) {
00082 int p_index = index + locations*l.classes + i*l.n + j;
00083 l.delta[p_index] = l.noobject_scale*(0 - l.output[p_index]);
00084 *(l.cost) += l.noobject_scale*pow(l.output[p_index], 2);
00085 avg_anyobj += l.output[p_index];
00086 }
00087
00088 int best_index = -1;
00089 float best_iou = 0;
00090 float best_rmse = 20;
00091
00092 if (!is_obj){
00093 continue;
00094 }
00095
00096 int class_index = index + i*l.classes;
00097 for(j = 0; j < l.classes; ++j) {
00098 l.delta[class_index+j] = l.class_scale * (state.truth[truth_index+1+j] - l.output[class_index+j]);
00099 *(l.cost) += l.class_scale * pow(state.truth[truth_index+1+j] - l.output[class_index+j], 2);
00100 if(state.truth[truth_index + 1 + j]) avg_cat += l.output[class_index+j];
00101 avg_allcat += l.output[class_index+j];
00102 }
00103
00104 box truth = float_to_box(state.truth + truth_index + 1 + l.classes);
00105 truth.x /= l.side;
00106 truth.y /= l.side;
00107
00108 for(j = 0; j < l.n; ++j){
00109 int box_index = index + locations*(l.classes + l.n) + (i*l.n + j) * l.coords;
00110 box out = float_to_box(l.output + box_index);
00111 out.x /= l.side;
00112 out.y /= l.side;
00113
00114 if (l.sqrt){
00115 out.w = out.w*out.w;
00116 out.h = out.h*out.h;
00117 }
00118
00119 float iou = box_iou(out, truth);
00120
00121 float rmse = box_rmse(out, truth);
00122 if(best_iou > 0 || iou > 0){
00123 if(iou > best_iou){
00124 best_iou = iou;
00125 best_index = j;
00126 }
00127 }else{
00128 if(rmse < best_rmse){
00129 best_rmse = rmse;
00130 best_index = j;
00131 }
00132 }
00133 }
00134
00135 if(l.forced){
00136 if(truth.w*truth.h < .1){
00137 best_index = 1;
00138 }else{
00139 best_index = 0;
00140 }
00141 }
00142 if(l.random && *(state.net.seen) < 64000){
00143 best_index = rand()%l.n;
00144 }
00145
00146 int box_index = index + locations*(l.classes + l.n) + (i*l.n + best_index) * l.coords;
00147 int tbox_index = truth_index + 1 + l.classes;
00148
00149 box out = float_to_box(l.output + box_index);
00150 out.x /= l.side;
00151 out.y /= l.side;
00152 if (l.sqrt) {
00153 out.w = out.w*out.w;
00154 out.h = out.h*out.h;
00155 }
00156 float iou = box_iou(out, truth);
00157
00158
00159 int p_index = index + locations*l.classes + i*l.n + best_index;
00160 *(l.cost) -= l.noobject_scale * pow(l.output[p_index], 2);
00161 *(l.cost) += l.object_scale * pow(1-l.output[p_index], 2);
00162 avg_obj += l.output[p_index];
00163 l.delta[p_index] = l.object_scale * (1.-l.output[p_index]);
00164
00165 if(l.rescore){
00166 l.delta[p_index] = l.object_scale * (iou - l.output[p_index]);
00167 }
00168
00169 l.delta[box_index+0] = l.coord_scale*(state.truth[tbox_index + 0] - l.output[box_index + 0]);
00170 l.delta[box_index+1] = l.coord_scale*(state.truth[tbox_index + 1] - l.output[box_index + 1]);
00171 l.delta[box_index+2] = l.coord_scale*(state.truth[tbox_index + 2] - l.output[box_index + 2]);
00172 l.delta[box_index+3] = l.coord_scale*(state.truth[tbox_index + 3] - l.output[box_index + 3]);
00173 if(l.sqrt){
00174 l.delta[box_index+2] = l.coord_scale*(sqrt(state.truth[tbox_index + 2]) - l.output[box_index + 2]);
00175 l.delta[box_index+3] = l.coord_scale*(sqrt(state.truth[tbox_index + 3]) - l.output[box_index + 3]);
00176 }
00177
00178 *(l.cost) += pow(1-iou, 2);
00179 avg_iou += iou;
00180 ++count;
00181 }
00182 }
00183
00184 if(0){
00185 float *costs = calloc(l.batch*locations*l.n, sizeof(float));
00186 for (b = 0; b < l.batch; ++b) {
00187 int index = b*l.inputs;
00188 for (i = 0; i < locations; ++i) {
00189 for (j = 0; j < l.n; ++j) {
00190 int p_index = index + locations*l.classes + i*l.n + j;
00191 costs[b*locations*l.n + i*l.n + j] = l.delta[p_index]*l.delta[p_index];
00192 }
00193 }
00194 }
00195 int indexes[100];
00196 top_k(costs, l.batch*locations*l.n, 100, indexes);
00197 float cutoff = costs[indexes[99]];
00198 for (b = 0; b < l.batch; ++b) {
00199 int index = b*l.inputs;
00200 for (i = 0; i < locations; ++i) {
00201 for (j = 0; j < l.n; ++j) {
00202 int p_index = index + locations*l.classes + i*l.n + j;
00203 if (l.delta[p_index]*l.delta[p_index] < cutoff) l.delta[p_index] = 0;
00204 }
00205 }
00206 }
00207 free(costs);
00208 }
00209
00210
00211 *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
00212
00213
00214 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);
00215
00216 }
00217 }
00218
00219 void backward_detection_layer(const detection_layer l, network_state state)
00220 {
00221 axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, state.delta, 1);
00222 }
00223
00224 void get_detection_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness)
00225 {
00226 int i,j,n;
00227 float *predictions = l.output;
00228
00229 for (i = 0; i < l.side*l.side; ++i){
00230 int row = i / l.side;
00231 int col = i % l.side;
00232 for(n = 0; n < l.n; ++n){
00233 int index = i*l.n + n;
00234 int p_index = l.side*l.side*l.classes + i*l.n + n;
00235 float scale = predictions[p_index];
00236 int box_index = l.side*l.side*(l.classes + l.n) + (i*l.n + n)*4;
00237 boxes[index].x = (predictions[box_index + 0] + col) / l.side * w;
00238 boxes[index].y = (predictions[box_index + 1] + row) / l.side * h;
00239 boxes[index].w = pow(predictions[box_index + 2], (l.sqrt?2:1)) * w;
00240 boxes[index].h = pow(predictions[box_index + 3], (l.sqrt?2:1)) * h;
00241 for(j = 0; j < l.classes; ++j){
00242 int class_index = i*l.classes;
00243 float prob = scale*predictions[class_index+j];
00244 probs[index][j] = (prob > thresh) ? prob : 0;
00245 }
00246 if(only_objectness){
00247 probs[index][0] = scale;
00248 }
00249 }
00250 }
00251 }
00252
00253 #ifdef GPU
00254
00255 void forward_detection_layer_gpu(const detection_layer l, network_state state)
00256 {
00257 if(!state.train){
00258 copy_ongpu(l.batch*l.inputs, state.input, 1, l.output_gpu, 1);
00259 return;
00260 }
00261
00262 float *in_cpu = calloc(l.batch*l.inputs, sizeof(float));
00263 float *truth_cpu = 0;
00264 if(state.truth){
00265 int num_truth = l.batch*l.side*l.side*(1+l.coords+l.classes);
00266 truth_cpu = calloc(num_truth, sizeof(float));
00267 cuda_pull_array(state.truth, truth_cpu, num_truth);
00268 }
00269 cuda_pull_array(state.input, in_cpu, l.batch*l.inputs);
00270 network_state cpu_state = state;
00271 cpu_state.train = state.train;
00272 cpu_state.truth = truth_cpu;
00273 cpu_state.input = in_cpu;
00274 forward_detection_layer(l, cpu_state);
00275 cuda_push_array(l.output_gpu, l.output, l.batch*l.outputs);
00276 cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
00277 free(cpu_state.input);
00278 if(cpu_state.truth) free(cpu_state.truth);
00279 }
00280
00281 void backward_detection_layer_gpu(detection_layer l, network_state state)
00282 {
00283 axpy_ongpu(l.batch*l.inputs, 1, l.delta_gpu, 1, state.delta, 1);
00284
00285 }
00286 #endif
00287