00001 #include "region_layer.h"
00002 #include "activations.h"
00003 #include "blas.h"
00004 #include "box.h"
00005 #include "cuda.h"
00006 #include "utils.h"
00007 #include <stdio.h>
00008 #include <assert.h>
00009 #include <string.h>
00010 #include <stdlib.h>
00011
00012 #define DOABS 1
00013
00014 region_layer make_region_layer(int batch, int w, int h, int n, int classes, int coords)
00015 {
00016 region_layer l = {0};
00017 l.type = REGION;
00018
00019 l.n = n;
00020 l.batch = batch;
00021 l.h = h;
00022 l.w = w;
00023 l.classes = classes;
00024 l.coords = coords;
00025 l.cost = calloc(1, sizeof(float));
00026 l.biases = calloc(n*2, sizeof(float));
00027 l.bias_updates = calloc(n*2, sizeof(float));
00028 l.outputs = h*w*n*(classes + coords + 1);
00029 l.inputs = l.outputs;
00030 l.truths = 30*(5);
00031 l.delta = calloc(batch*l.outputs, sizeof(float));
00032 l.output = calloc(batch*l.outputs, sizeof(float));
00033 int i;
00034 for(i = 0; i < n*2; ++i){
00035 l.biases[i] = .5;
00036 }
00037
00038 l.forward = forward_region_layer;
00039 l.backward = backward_region_layer;
00040 #ifdef GPU
00041 l.forward_gpu = forward_region_layer_gpu;
00042 l.backward_gpu = backward_region_layer_gpu;
00043 l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
00044 l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
00045 #endif
00046
00047 fprintf(stderr, "detection\n");
00048 srand(0);
00049
00050 return l;
00051 }
00052
00053 void resize_region_layer(layer *l, int w, int h)
00054 {
00055 l->w = w;
00056 l->h = h;
00057
00058 l->outputs = h*w*l->n*(l->classes + l->coords + 1);
00059 l->inputs = l->outputs;
00060
00061 l->output = realloc(l->output, l->batch*l->outputs*sizeof(float));
00062 l->delta = realloc(l->delta, l->batch*l->outputs*sizeof(float));
00063
00064 #ifdef GPU
00065 cuda_free(l->delta_gpu);
00066 cuda_free(l->output_gpu);
00067
00068 l->delta_gpu = cuda_make_array(l->delta, l->batch*l->outputs);
00069 l->output_gpu = cuda_make_array(l->output, l->batch*l->outputs);
00070 #endif
00071 }
00072
00073 box get_region_box(float *x, float *biases, int n, int index, int i, int j, int w, int h)
00074 {
00075 box b;
00076 b.x = (i + logistic_activate(x[index + 0])) / w;
00077 b.y = (j + logistic_activate(x[index + 1])) / h;
00078 b.w = exp(x[index + 2]) * biases[2*n];
00079 b.h = exp(x[index + 3]) * biases[2*n+1];
00080 if(DOABS){
00081 b.w = exp(x[index + 2]) * biases[2*n] / w;
00082 b.h = exp(x[index + 3]) * biases[2*n+1] / h;
00083 }
00084 return b;
00085 }
00086
00087 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)
00088 {
00089 box pred = get_region_box(x, biases, n, index, i, j, w, h);
00090 float iou = box_iou(pred, truth);
00091
00092 float tx = (truth.x*w - i);
00093 float ty = (truth.y*h - j);
00094 float tw = log(truth.w / biases[2*n]);
00095 float th = log(truth.h / biases[2*n + 1]);
00096 if(DOABS){
00097 tw = log(truth.w*w / biases[2*n]);
00098 th = log(truth.h*h / biases[2*n + 1]);
00099 }
00100
00101 delta[index + 0] = scale * (tx - logistic_activate(x[index + 0])) * logistic_gradient(logistic_activate(x[index + 0]));
00102 delta[index + 1] = scale * (ty - logistic_activate(x[index + 1])) * logistic_gradient(logistic_activate(x[index + 1]));
00103 delta[index + 2] = scale * (tw - x[index + 2]);
00104 delta[index + 3] = scale * (th - x[index + 3]);
00105 return iou;
00106 }
00107
00108 void delta_region_class(float *output, float *delta, int index, int class, int classes, tree *hier, float scale, float *avg_cat)
00109 {
00110 int i, n;
00111 if(hier){
00112 float pred = 1;
00113 while(class >= 0){
00114 pred *= output[index + class];
00115 int g = hier->group[class];
00116 int offset = hier->group_offset[g];
00117 for(i = 0; i < hier->group_size[g]; ++i){
00118 delta[index + offset + i] = scale * (0 - output[index + offset + i]);
00119 }
00120 delta[index + class] = scale * (1 - output[index + class]);
00121
00122 class = hier->parent[class];
00123 }
00124 *avg_cat += pred;
00125 } else {
00126 for(n = 0; n < classes; ++n){
00127 delta[index + n] = scale * (((n == class)?1 : 0) - output[index + n]);
00128 if(n == class) *avg_cat += output[index + n];
00129 }
00130 }
00131 }
00132
00133 float logit(float x)
00134 {
00135 return log(x/(1.-x));
00136 }
00137
00138 float tisnan(float x)
00139 {
00140 return (x != x);
00141 }
00142
00143 void softmax_tree(float *input, int batch, int inputs, float temp, tree *hierarchy, float *output);
00144 void forward_region_layer(const region_layer l, network_state state)
00145 {
00146 int i,j,b,t,n;
00147 int size = l.coords + l.classes + 1;
00148 memcpy(l.output, state.input, l.outputs*l.batch*sizeof(float));
00149 #ifndef GPU
00150 flatten(l.output, l.w*l.h, size*l.n, l.batch, 1);
00151 #endif
00152 for (b = 0; b < l.batch; ++b){
00153 for(i = 0; i < l.h*l.w*l.n; ++i){
00154 int index = size*i + b*l.outputs;
00155 l.output[index + 4] = logistic_activate(l.output[index + 4]);
00156 }
00157 }
00158
00159
00160 #ifndef GPU
00161 if (l.softmax_tree){
00162 for (b = 0; b < l.batch; ++b){
00163 for(i = 0; i < l.h*l.w*l.n; ++i){
00164 int index = size*i + b*l.outputs;
00165 softmax_tree(l.output + index + 5, 1, 0, 1, l.softmax_tree, l.output + index + 5);
00166 }
00167 }
00168 } else if (l.softmax){
00169 for (b = 0; b < l.batch; ++b){
00170 for(i = 0; i < l.h*l.w*l.n; ++i){
00171 int index = size*i + b*l.outputs;
00172 softmax(l.output + index + 5, l.classes, 1, l.output + index + 5);
00173 }
00174 }
00175 }
00176 #endif
00177 if(!state.train) return;
00178 memset(l.delta, 0, l.outputs * l.batch * sizeof(float));
00179 float avg_iou = 0;
00180 float recall = 0;
00181 float avg_cat = 0;
00182 float avg_obj = 0;
00183 float avg_anyobj = 0;
00184 int count = 0;
00185 int class_count = 0;
00186 *(l.cost) = 0;
00187 for (b = 0; b < l.batch; ++b) {
00188 if(l.softmax_tree){
00189 int onlyclass = 0;
00190 for(t = 0; t < 30; ++t){
00191 box truth = float_to_box(state.truth + t*5 + b*l.truths);
00192 if(!truth.x) break;
00193 int class = state.truth[t*5 + b*l.truths + 4];
00194 float maxp = 0;
00195 int maxi = 0;
00196 if(truth.x > 100000 && truth.y > 100000){
00197 for(n = 0; n < l.n*l.w*l.h; ++n){
00198 int index = size*n + b*l.outputs + 5;
00199 float p = get_hierarchy_probability(l.output + index, l.softmax_tree, class);
00200 if(p > maxp){
00201 maxp = p;
00202 maxi = n;
00203 }
00204 }
00205 int index = size*maxi + b*l.outputs + 5;
00206 delta_region_class(l.output, l.delta, index, class, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
00207 ++class_count;
00208 onlyclass = 1;
00209 break;
00210 }
00211 }
00212 if(onlyclass) continue;
00213 }
00214 for (j = 0; j < l.h; ++j) {
00215 for (i = 0; i < l.w; ++i) {
00216 for (n = 0; n < l.n; ++n) {
00217 int index = size*(j*l.w*l.n + i*l.n + n) + b*l.outputs;
00218 box pred = get_region_box(l.output, l.biases, n, index, i, j, l.w, l.h);
00219 float best_iou = 0;
00220 int best_class = -1;
00221 for(t = 0; t < 30; ++t){
00222 box truth = float_to_box(state.truth + t*5 + b*l.truths);
00223 if(!truth.x) break;
00224 float iou = box_iou(pred, truth);
00225 if (iou > best_iou) {
00226 best_class = state.truth[t*5 + b*l.truths + 4];
00227 best_iou = iou;
00228 }
00229 }
00230 avg_anyobj += l.output[index + 4];
00231 l.delta[index + 4] = l.noobject_scale * ((0 - l.output[index + 4]) * logistic_gradient(l.output[index + 4]));
00232 if(l.classfix == -1) l.delta[index + 4] = l.noobject_scale * ((best_iou - l.output[index + 4]) * logistic_gradient(l.output[index + 4]));
00233 else{
00234 if (best_iou > l.thresh) {
00235 l.delta[index + 4] = 0;
00236 if(l.classfix > 0){
00237 delta_region_class(l.output, l.delta, index + 5, best_class, l.classes, l.softmax_tree, l.class_scale*(l.classfix == 2 ? l.output[index + 4] : 1), &avg_cat);
00238 ++class_count;
00239 }
00240 }
00241 }
00242
00243 if(*(state.net.seen) < 12800){
00244 box truth = {0};
00245 truth.x = (i + .5)/l.w;
00246 truth.y = (j + .5)/l.h;
00247 truth.w = l.biases[2*n];
00248 truth.h = l.biases[2*n+1];
00249 if(DOABS){
00250 truth.w = l.biases[2*n]/l.w;
00251 truth.h = l.biases[2*n+1]/l.h;
00252 }
00253 delta_region_box(truth, l.output, l.biases, n, index, i, j, l.w, l.h, l.delta, .01);
00254 }
00255 }
00256 }
00257 }
00258 for(t = 0; t < 30; ++t){
00259 box truth = float_to_box(state.truth + t*5 + b*l.truths);
00260
00261 if(!truth.x) break;
00262 float best_iou = 0;
00263 int best_index = 0;
00264 int best_n = 0;
00265 i = (truth.x * l.w);
00266 j = (truth.y * l.h);
00267
00268 box truth_shift = truth;
00269 truth_shift.x = 0;
00270 truth_shift.y = 0;
00271
00272 for(n = 0; n < l.n; ++n){
00273 int index = size*(j*l.w*l.n + i*l.n + n) + b*l.outputs;
00274 box pred = get_region_box(l.output, l.biases, n, index, i, j, l.w, l.h);
00275 if(l.bias_match){
00276 pred.w = l.biases[2*n];
00277 pred.h = l.biases[2*n+1];
00278 if(DOABS){
00279 pred.w = l.biases[2*n]/l.w;
00280 pred.h = l.biases[2*n+1]/l.h;
00281 }
00282 }
00283
00284 pred.x = 0;
00285 pred.y = 0;
00286 float iou = box_iou(pred, truth_shift);
00287 if (iou > best_iou){
00288 best_index = index;
00289 best_iou = iou;
00290 best_n = n;
00291 }
00292 }
00293
00294
00295 float iou = delta_region_box(truth, l.output, l.biases, best_n, best_index, i, j, l.w, l.h, l.delta, l.coord_scale);
00296 if(iou > .5) recall += 1;
00297 avg_iou += iou;
00298
00299
00300 avg_obj += l.output[best_index + 4];
00301 l.delta[best_index + 4] = l.object_scale * (1 - l.output[best_index + 4]) * logistic_gradient(l.output[best_index + 4]);
00302 if (l.rescore) {
00303 l.delta[best_index + 4] = l.object_scale * (iou - l.output[best_index + 4]) * logistic_gradient(l.output[best_index + 4]);
00304 }
00305
00306
00307 int class = state.truth[t*5 + b*l.truths + 4];
00308 if (l.map) class = l.map[class];
00309 delta_region_class(l.output, l.delta, best_index + 5, class, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
00310 ++count;
00311 ++class_count;
00312 }
00313 }
00314
00315 #ifndef GPU
00316 flatten(l.delta, l.w*l.h, size*l.n, l.batch, 0);
00317 #endif
00318 *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
00319 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);
00320 }
00321
00322 void backward_region_layer(const region_layer l, network_state state)
00323 {
00324 axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, state.delta, 1);
00325 }
00326
00327 void get_region_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness)
00328 {
00329 int i,j,n;
00330 float *predictions = l.output;
00331 for (i = 0; i < l.w*l.h; ++i){
00332 int row = i / l.w;
00333 int col = i % l.w;
00334 for(n = 0; n < l.n; ++n){
00335 int index = i*l.n + n;
00336 int p_index = index * (l.classes + 5) + 4;
00337 float scale = predictions[p_index];
00338 if(l.classfix == -1 && scale < .5) scale = 0;
00339 int box_index = index * (l.classes + 5);
00340 boxes[index] = get_region_box(predictions, l.biases, n, box_index, col, row, l.w, l.h);
00341 boxes[index].x *= w;
00342 boxes[index].y *= h;
00343 boxes[index].w *= w;
00344 boxes[index].h *= h;
00345
00346 int class_index = index * (l.classes + 5) + 5;
00347 if(l.softmax_tree){
00348
00349 hierarchy_predictions(predictions + class_index, l.classes, l.softmax_tree, 0);
00350 int found = 0;
00351 for(j = l.classes - 1; j >= 0; --j){
00352 if(1){
00353 if(!found && predictions[class_index + j] > .5){
00354 found = 1;
00355 } else {
00356 predictions[class_index + j] = 0;
00357 }
00358 float prob = predictions[class_index+j];
00359 probs[index][j] = (scale > thresh) ? prob : 0;
00360 }else{
00361 float prob = scale*predictions[class_index+j];
00362 probs[index][j] = (prob > thresh) ? prob : 0;
00363 }
00364 }
00365 }else{
00366 for(j = 0; j < l.classes; ++j){
00367 float prob = scale*predictions[class_index+j];
00368 probs[index][j] = (prob > thresh) ? prob : 0;
00369 }
00370 }
00371 if(only_objectness){
00372 probs[index][0] = scale;
00373 }
00374 }
00375 }
00376 }
00377
00378 #ifdef GPU
00379
00380 void forward_region_layer_gpu(const region_layer l, network_state state)
00381 {
00382
00383
00384
00385
00386
00387
00388 flatten_ongpu(state.input, l.h*l.w, l.n*(l.coords + l.classes + 1), l.batch, 1, l.output_gpu);
00389 if(l.softmax_tree){
00390 int i;
00391 int count = 5;
00392 for (i = 0; i < l.softmax_tree->groups; ++i) {
00393 int group_size = l.softmax_tree->group_size[i];
00394 softmax_gpu(l.output_gpu+count, group_size, l.classes + 5, l.w*l.h*l.n*l.batch, 1, l.output_gpu + count);
00395 count += group_size;
00396 }
00397 }else if (l.softmax){
00398 softmax_gpu(l.output_gpu+5, l.classes, l.classes + 5, l.w*l.h*l.n*l.batch, 1, l.output_gpu + 5);
00399 }
00400
00401 float *in_cpu = calloc(l.batch*l.inputs, sizeof(float));
00402 float *truth_cpu = 0;
00403 if(state.truth){
00404 int num_truth = l.batch*l.truths;
00405 truth_cpu = calloc(num_truth, sizeof(float));
00406 cuda_pull_array(state.truth, truth_cpu, num_truth);
00407 }
00408 cuda_pull_array(l.output_gpu, in_cpu, l.batch*l.inputs);
00409 network_state cpu_state = state;
00410 cpu_state.train = state.train;
00411 cpu_state.truth = truth_cpu;
00412 cpu_state.input = in_cpu;
00413 forward_region_layer(l, cpu_state);
00414
00415 free(cpu_state.input);
00416 if(!state.train) return;
00417 cuda_push_array(l.delta_gpu, l.delta, l.batch*l.outputs);
00418 if(cpu_state.truth) free(cpu_state.truth);
00419 }
00420
00421 void backward_region_layer_gpu(region_layer l, network_state state)
00422 {
00423 flatten_ongpu(l.delta_gpu, l.h*l.w, l.n*(l.coords + l.classes + 1), l.batch, 0, state.delta);
00424 }
00425 #endif
00426