2015-03-05 01:56:38 +03:00
|
|
|
#include "detection_layer.h"
|
|
|
|
#include "activations.h"
|
|
|
|
#include "softmax_layer.h"
|
|
|
|
#include "blas.h"
|
2015-06-16 09:22:44 +03:00
|
|
|
#include "box.h"
|
2015-03-05 01:56:38 +03:00
|
|
|
#include "cuda.h"
|
2015-04-24 20:27:50 +03:00
|
|
|
#include "utils.h"
|
2015-03-05 01:56:38 +03:00
|
|
|
#include <stdio.h>
|
2015-04-24 20:27:50 +03:00
|
|
|
#include <string.h>
|
2015-03-05 01:56:38 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
int get_detection_layer_locations(detection_layer l)
|
2014-07-14 09:07:51 +04:00
|
|
|
{
|
2015-06-10 10:11:41 +03:00
|
|
|
return l.inputs / (l.classes+l.coords+l.joint+(l.background || l.objectness));
|
2014-07-14 09:07:51 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
int get_detection_layer_output_size(detection_layer l)
|
2014-07-14 09:07:51 +04:00
|
|
|
{
|
2015-06-10 10:11:41 +03:00
|
|
|
return get_detection_layer_locations(l)*((l.background || l.objectness) + l.classes + l.coords);
|
2014-07-14 09:07:51 +04:00
|
|
|
}
|
|
|
|
|
2015-06-10 10:11:41 +03:00
|
|
|
detection_layer make_detection_layer(int batch, int inputs, int classes, int coords, int joint, int rescore, int background, int objectness)
|
2014-07-14 09:07:51 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
detection_layer l = {0};
|
|
|
|
l.type = DETECTION;
|
2015-03-21 22:25:14 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
l.batch = batch;
|
|
|
|
l.inputs = inputs;
|
|
|
|
l.classes = classes;
|
|
|
|
l.coords = coords;
|
|
|
|
l.rescore = rescore;
|
2015-06-10 10:11:41 +03:00
|
|
|
l.objectness = objectness;
|
2015-06-12 01:38:58 +03:00
|
|
|
l.background = background;
|
2015-06-10 10:11:41 +03:00
|
|
|
l.joint = joint;
|
2015-05-11 23:46:49 +03:00
|
|
|
l.cost = calloc(1, sizeof(float));
|
|
|
|
l.does_cost=1;
|
|
|
|
int outputs = get_detection_layer_output_size(l);
|
|
|
|
l.outputs = outputs;
|
|
|
|
l.output = calloc(batch*outputs, sizeof(float));
|
|
|
|
l.delta = calloc(batch*outputs, sizeof(float));
|
2015-03-05 01:56:38 +03:00
|
|
|
#ifdef GPU
|
2015-09-01 21:21:01 +03:00
|
|
|
l.output_gpu = cuda_make_array(l.output, batch*outputs);
|
|
|
|
l.delta_gpu = cuda_make_array(l.delta, batch*outputs);
|
2015-03-05 01:56:38 +03:00
|
|
|
#endif
|
2014-07-14 09:07:51 +04:00
|
|
|
|
2015-03-05 01:56:38 +03:00
|
|
|
fprintf(stderr, "Detection Layer\n");
|
2014-07-14 09:07:51 +04:00
|
|
|
srand(0);
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
return l;
|
2014-07-14 09:07:51 +04:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_detection_layer(const detection_layer l, network_state state)
|
2014-07-14 09:07:51 +04:00
|
|
|
{
|
2015-03-05 01:56:38 +03:00
|
|
|
int in_i = 0;
|
|
|
|
int out_i = 0;
|
2015-05-11 23:46:49 +03:00
|
|
|
int locations = get_detection_layer_locations(l);
|
2015-03-05 01:56:38 +03:00
|
|
|
int i,j;
|
2015-05-11 23:46:49 +03:00
|
|
|
for(i = 0; i < l.batch*locations; ++i){
|
2015-06-10 10:11:41 +03:00
|
|
|
int mask = (!state.truth || state.truth[out_i + (l.background || l.objectness) + l.classes + 2]);
|
2015-03-05 01:56:38 +03:00
|
|
|
float scale = 1;
|
2015-06-10 10:11:41 +03:00
|
|
|
if(l.joint) scale = state.input[in_i++];
|
|
|
|
else if(l.objectness){
|
2015-05-11 23:46:49 +03:00
|
|
|
l.output[out_i++] = 1-state.input[in_i++];
|
2015-03-24 23:20:56 +03:00
|
|
|
scale = mask;
|
|
|
|
}
|
2015-05-11 23:46:49 +03:00
|
|
|
else if(l.background) l.output[out_i++] = scale*state.input[in_i++];
|
2015-03-21 22:25:14 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
for(j = 0; j < l.classes; ++j){
|
|
|
|
l.output[out_i++] = scale*state.input[in_i++];
|
2015-03-12 08:20:15 +03:00
|
|
|
}
|
2015-06-10 10:11:41 +03:00
|
|
|
if(l.objectness){
|
2015-05-04 21:29:21 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
}else if(l.background){
|
|
|
|
softmax_array(l.output + out_i - l.classes-l.background, l.classes+l.background, l.output + out_i - l.classes-l.background);
|
|
|
|
activate_array(state.input+in_i, l.coords, LOGISTIC);
|
2015-03-05 01:56:38 +03:00
|
|
|
}
|
2015-05-11 23:46:49 +03:00
|
|
|
for(j = 0; j < l.coords; ++j){
|
|
|
|
l.output[out_i++] = mask*state.input[in_i++];
|
2015-03-12 08:20:15 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-20 20:06:42 +03:00
|
|
|
float avg_iou = 0;
|
|
|
|
int count = 0;
|
2015-05-15 20:25:05 +03:00
|
|
|
if(l.does_cost && state.train){
|
2015-05-11 23:46:49 +03:00
|
|
|
*(l.cost) = 0;
|
|
|
|
int size = get_detection_layer_output_size(l) * l.batch;
|
|
|
|
memset(l.delta, 0, size * sizeof(float));
|
|
|
|
for (i = 0; i < l.batch*locations; ++i) {
|
2015-06-10 10:11:41 +03:00
|
|
|
int classes = l.objectness+l.classes;
|
2015-05-11 23:46:49 +03:00
|
|
|
int offset = i*(classes+l.coords);
|
2015-05-04 21:29:21 +03:00
|
|
|
for (j = offset; j < offset+classes; ++j) {
|
2015-05-11 23:46:49 +03:00
|
|
|
*(l.cost) += pow(state.truth[j] - l.output[j], 2);
|
|
|
|
l.delta[j] = state.truth[j] - l.output[j];
|
2015-03-21 22:25:14 +03:00
|
|
|
}
|
2015-05-20 20:06:42 +03:00
|
|
|
|
2015-04-24 20:27:50 +03:00
|
|
|
box truth;
|
2015-05-20 20:06:42 +03:00
|
|
|
truth.x = state.truth[j+0]/7;
|
|
|
|
truth.y = state.truth[j+1]/7;
|
|
|
|
truth.w = pow(state.truth[j+2], 2);
|
|
|
|
truth.h = pow(state.truth[j+3], 2);
|
2015-08-25 04:27:42 +03:00
|
|
|
|
2015-04-24 20:27:50 +03:00
|
|
|
box out;
|
2015-05-20 20:06:42 +03:00
|
|
|
out.x = l.output[j+0]/7;
|
|
|
|
out.y = l.output[j+1]/7;
|
|
|
|
out.w = pow(l.output[j+2], 2);
|
|
|
|
out.h = pow(l.output[j+3], 2);
|
|
|
|
|
2015-04-24 20:27:50 +03:00
|
|
|
if(!(truth.w*truth.h)) continue;
|
2015-05-20 20:06:42 +03:00
|
|
|
float iou = box_iou(out, truth);
|
|
|
|
avg_iou += iou;
|
2015-05-04 21:29:21 +03:00
|
|
|
++count;
|
2015-05-20 20:06:42 +03:00
|
|
|
|
|
|
|
*(l.cost) += pow((1-iou), 2);
|
2015-05-25 21:53:10 +03:00
|
|
|
l.delta[j+0] = 4 * (state.truth[j+0] - l.output[j+0]);
|
|
|
|
l.delta[j+1] = 4 * (state.truth[j+1] - l.output[j+1]);
|
|
|
|
l.delta[j+2] = 4 * (state.truth[j+2] - l.output[j+2]);
|
|
|
|
l.delta[j+3] = 4 * (state.truth[j+3] - l.output[j+3]);
|
2015-06-10 10:11:41 +03:00
|
|
|
if(l.rescore){
|
2015-05-20 20:06:42 +03:00
|
|
|
for (j = offset; j < offset+classes; ++j) {
|
|
|
|
if(state.truth[j]) state.truth[j] = iou;
|
|
|
|
l.delta[j] = state.truth[j] - l.output[j];
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 09:07:51 +04:00
|
|
|
}
|
2015-05-20 20:06:42 +03:00
|
|
|
printf("Avg IOU: %f\n", avg_iou/count);
|
2014-07-14 09:07:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void backward_detection_layer(const detection_layer l, network_state state)
|
2014-07-14 09:07:51 +04:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
int locations = get_detection_layer_locations(l);
|
2015-03-05 01:56:38 +03:00
|
|
|
int i,j;
|
|
|
|
int in_i = 0;
|
|
|
|
int out_i = 0;
|
2015-05-11 23:46:49 +03:00
|
|
|
for(i = 0; i < l.batch*locations; ++i){
|
2015-03-05 01:56:38 +03:00
|
|
|
float scale = 1;
|
|
|
|
float latent_delta = 0;
|
2015-06-10 10:11:41 +03:00
|
|
|
if(l.joint) scale = state.input[in_i++];
|
2015-07-22 02:09:33 +03:00
|
|
|
else if (l.objectness) state.delta[in_i++] += -l.delta[out_i++];
|
|
|
|
else if (l.background) state.delta[in_i++] += scale*l.delta[out_i++];
|
2015-05-11 23:46:49 +03:00
|
|
|
for(j = 0; j < l.classes; ++j){
|
|
|
|
latent_delta += state.input[in_i]*l.delta[out_i];
|
2015-07-22 02:09:33 +03:00
|
|
|
state.delta[in_i++] += scale*l.delta[out_i++];
|
2015-03-05 01:56:38 +03:00
|
|
|
}
|
2015-03-12 08:20:15 +03:00
|
|
|
|
2015-06-10 10:11:41 +03:00
|
|
|
if (l.objectness) {
|
2015-03-27 05:13:59 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
}else if (l.background) gradient_array(l.output + out_i, l.coords, LOGISTIC, l.delta + out_i);
|
|
|
|
for(j = 0; j < l.coords; ++j){
|
2015-07-22 02:09:33 +03:00
|
|
|
state.delta[in_i++] += l.delta[out_i++];
|
2015-03-05 01:56:38 +03:00
|
|
|
}
|
2015-07-22 02:09:33 +03:00
|
|
|
if(l.joint) state.delta[in_i-l.coords-l.classes-l.joint] += latent_delta;
|
2015-03-05 01:56:38 +03:00
|
|
|
}
|
2014-07-14 09:07:51 +04:00
|
|
|
}
|
|
|
|
|
2015-03-05 01:56:38 +03:00
|
|
|
#ifdef GPU
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void forward_detection_layer_gpu(const detection_layer l, network_state state)
|
2015-03-05 01:56:38 +03:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
int outputs = get_detection_layer_output_size(l);
|
|
|
|
float *in_cpu = calloc(l.batch*l.inputs, sizeof(float));
|
2015-03-05 01:56:38 +03:00
|
|
|
float *truth_cpu = 0;
|
2015-03-12 08:20:15 +03:00
|
|
|
if(state.truth){
|
2015-05-11 23:46:49 +03:00
|
|
|
truth_cpu = calloc(l.batch*outputs, sizeof(float));
|
|
|
|
cuda_pull_array(state.truth, truth_cpu, l.batch*outputs);
|
2015-03-05 01:56:38 +03:00
|
|
|
}
|
2015-05-11 23:46:49 +03:00
|
|
|
cuda_pull_array(state.input, in_cpu, l.batch*l.inputs);
|
2015-03-12 08:20:15 +03:00
|
|
|
network_state cpu_state;
|
|
|
|
cpu_state.train = state.train;
|
|
|
|
cpu_state.truth = truth_cpu;
|
|
|
|
cpu_state.input = in_cpu;
|
2015-05-11 23:46:49 +03:00
|
|
|
forward_detection_layer(l, cpu_state);
|
|
|
|
cuda_push_array(l.output_gpu, l.output, l.batch*outputs);
|
|
|
|
cuda_push_array(l.delta_gpu, l.delta, l.batch*outputs);
|
2015-03-12 08:20:15 +03:00
|
|
|
free(cpu_state.input);
|
|
|
|
if(cpu_state.truth) free(cpu_state.truth);
|
2015-03-05 01:56:38 +03:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
void backward_detection_layer_gpu(detection_layer l, network_state state)
|
2015-03-05 01:56:38 +03:00
|
|
|
{
|
2015-05-11 23:46:49 +03:00
|
|
|
int outputs = get_detection_layer_output_size(l);
|
2015-03-05 01:56:38 +03:00
|
|
|
|
2015-05-11 23:46:49 +03:00
|
|
|
float *in_cpu = calloc(l.batch*l.inputs, sizeof(float));
|
|
|
|
float *delta_cpu = calloc(l.batch*l.inputs, sizeof(float));
|
2015-03-12 08:20:15 +03:00
|
|
|
float *truth_cpu = 0;
|
|
|
|
if(state.truth){
|
2015-05-11 23:46:49 +03:00
|
|
|
truth_cpu = calloc(l.batch*outputs, sizeof(float));
|
|
|
|
cuda_pull_array(state.truth, truth_cpu, l.batch*outputs);
|
2015-03-12 08:20:15 +03:00
|
|
|
}
|
|
|
|
network_state cpu_state;
|
|
|
|
cpu_state.train = state.train;
|
|
|
|
cpu_state.input = in_cpu;
|
|
|
|
cpu_state.truth = truth_cpu;
|
|
|
|
cpu_state.delta = delta_cpu;
|
2015-03-05 01:56:38 +03:00
|
|
|
|
2015-07-22 02:09:33 +03:00
|
|
|
cuda_pull_array(state.input, in_cpu, l.batch*l.inputs);
|
|
|
|
cuda_pull_array(state.delta, delta_cpu, l.batch*l.inputs);
|
2015-05-11 23:46:49 +03:00
|
|
|
cuda_pull_array(l.delta_gpu, l.delta, l.batch*outputs);
|
|
|
|
backward_detection_layer(l, cpu_state);
|
|
|
|
cuda_push_array(state.delta, delta_cpu, l.batch*l.inputs);
|
2015-03-05 01:56:38 +03:00
|
|
|
|
2015-07-31 02:19:14 +03:00
|
|
|
if (truth_cpu) free(truth_cpu);
|
2015-03-05 01:56:38 +03:00
|
|
|
free(in_cpu);
|
|
|
|
free(delta_cpu);
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-14 09:07:51 +04:00
|
|
|
|