2017-06-02 06:31:13 +03:00
|
|
|
#include "reorg_layer.h"
|
|
|
|
#include "cuda.h"
|
|
|
|
#include "blas.h"
|
2017-06-01 07:06:35 +03:00
|
|
|
|
2016-08-06 01:27:07 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse, int flatten, int extra)
|
2016-08-06 01:27:07 +03:00
|
|
|
{
|
|
|
|
layer l = {0};
|
|
|
|
l.type = REORG;
|
|
|
|
l.batch = batch;
|
|
|
|
l.stride = stride;
|
2017-03-27 09:42:30 +03:00
|
|
|
l.extra = extra;
|
2016-08-06 01:27:07 +03:00
|
|
|
l.h = h;
|
|
|
|
l.w = w;
|
|
|
|
l.c = c;
|
2017-03-27 09:42:30 +03:00
|
|
|
l.flatten = flatten;
|
2016-11-08 10:42:19 +03:00
|
|
|
if(reverse){
|
|
|
|
l.out_w = w*stride;
|
|
|
|
l.out_h = h*stride;
|
|
|
|
l.out_c = c/(stride*stride);
|
|
|
|
}else{
|
|
|
|
l.out_w = w/stride;
|
|
|
|
l.out_h = h/stride;
|
|
|
|
l.out_c = c*(stride*stride);
|
|
|
|
}
|
2016-11-16 09:53:58 +03:00
|
|
|
l.reverse = reverse;
|
2017-03-27 09:42:30 +03:00
|
|
|
|
2016-08-06 01:27:07 +03:00
|
|
|
l.outputs = l.out_h * l.out_w * l.out_c;
|
|
|
|
l.inputs = h*w*c;
|
2017-03-27 09:42:30 +03:00
|
|
|
if(l.extra){
|
|
|
|
l.out_w = l.out_h = l.out_c = 0;
|
|
|
|
l.outputs = l.inputs + l.extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(extra){
|
|
|
|
fprintf(stderr, "reorg %4d -> %4d\n", l.inputs, l.outputs);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "reorg /%2d %4d x%4d x%4d -> %4d x%4d x%4d\n", stride, w, h, c, l.out_w, l.out_h, l.out_c);
|
|
|
|
}
|
|
|
|
int output_size = l.outputs * batch;
|
2016-08-06 01:27:07 +03:00
|
|
|
l.output = calloc(output_size, sizeof(float));
|
|
|
|
l.delta = calloc(output_size, sizeof(float));
|
2016-09-25 09:12:54 +03:00
|
|
|
|
|
|
|
l.forward = forward_reorg_layer;
|
|
|
|
l.backward = backward_reorg_layer;
|
2016-11-08 10:42:19 +03:00
|
|
|
#ifdef GPU
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward_gpu = forward_reorg_layer_gpu;
|
|
|
|
l.backward_gpu = backward_reorg_layer_gpu;
|
|
|
|
|
2016-08-06 01:27:07 +03:00
|
|
|
l.output_gpu = cuda_make_array(l.output, output_size);
|
|
|
|
l.delta_gpu = cuda_make_array(l.delta, output_size);
|
2016-11-08 10:42:19 +03:00
|
|
|
#endif
|
2016-08-06 01:27:07 +03:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize_reorg_layer(layer *l, int w, int h)
|
|
|
|
{
|
|
|
|
int stride = l->stride;
|
2016-11-16 09:53:58 +03:00
|
|
|
int c = l->c;
|
2016-08-06 01:27:07 +03:00
|
|
|
|
|
|
|
l->h = h;
|
|
|
|
l->w = w;
|
|
|
|
|
2016-11-16 09:53:58 +03:00
|
|
|
if(l->reverse){
|
|
|
|
l->out_w = w*stride;
|
|
|
|
l->out_h = h*stride;
|
|
|
|
l->out_c = c/(stride*stride);
|
|
|
|
}else{
|
|
|
|
l->out_w = w/stride;
|
|
|
|
l->out_h = h/stride;
|
|
|
|
l->out_c = c*(stride*stride);
|
|
|
|
}
|
2016-08-06 01:27:07 +03:00
|
|
|
|
|
|
|
l->outputs = l->out_h * l->out_w * l->out_c;
|
|
|
|
l->inputs = l->outputs;
|
|
|
|
int output_size = l->outputs * l->batch;
|
|
|
|
|
|
|
|
l->output = realloc(l->output, output_size * sizeof(float));
|
|
|
|
l->delta = realloc(l->delta, output_size * sizeof(float));
|
|
|
|
|
2016-11-08 10:42:19 +03:00
|
|
|
#ifdef GPU
|
2016-08-06 01:27:07 +03:00
|
|
|
cuda_free(l->output_gpu);
|
|
|
|
cuda_free(l->delta_gpu);
|
|
|
|
l->output_gpu = cuda_make_array(l->output, output_size);
|
|
|
|
l->delta_gpu = cuda_make_array(l->delta, output_size);
|
2016-11-08 10:42:19 +03:00
|
|
|
#endif
|
2016-08-06 01:27:07 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void forward_reorg_layer(const layer l, network net)
|
2016-08-06 01:27:07 +03:00
|
|
|
{
|
2017-03-27 09:42:30 +03:00
|
|
|
int i;
|
|
|
|
if(l.flatten){
|
2017-04-10 05:56:42 +03:00
|
|
|
memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
|
2017-03-27 09:42:30 +03:00
|
|
|
if(l.reverse){
|
|
|
|
flatten(l.output, l.w*l.h, l.c, l.batch, 0);
|
|
|
|
}else{
|
|
|
|
flatten(l.output, l.w*l.h, l.c, l.batch, 1);
|
|
|
|
}
|
|
|
|
} else if (l.extra) {
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
2017-04-10 05:56:42 +03:00
|
|
|
copy_cpu(l.inputs, net.input + i*l.inputs, 1, l.output + i*l.outputs, 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
|
|
|
} else if (l.reverse){
|
2017-04-10 05:56:42 +03:00
|
|
|
reorg_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.output);
|
2017-03-27 09:42:30 +03:00
|
|
|
} else {
|
2017-04-10 05:56:42 +03:00
|
|
|
reorg_cpu(net.input, l.w, l.h, l.c, l.batch, l.stride, 0, l.output);
|
2016-08-06 01:27:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void backward_reorg_layer(const layer l, network net)
|
2016-08-06 01:27:07 +03:00
|
|
|
{
|
2017-03-27 09:42:30 +03:00
|
|
|
int i;
|
|
|
|
if(l.flatten){
|
2017-04-10 05:56:42 +03:00
|
|
|
memcpy(net.delta, l.delta, l.outputs*l.batch*sizeof(float));
|
2017-03-27 09:42:30 +03:00
|
|
|
if(l.reverse){
|
2017-04-10 05:56:42 +03:00
|
|
|
flatten(net.delta, l.w*l.h, l.c, l.batch, 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
}else{
|
2017-04-10 05:56:42 +03:00
|
|
|
flatten(net.delta, l.w*l.h, l.c, l.batch, 0);
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
|
|
|
} else if(l.reverse){
|
2017-04-10 05:56:42 +03:00
|
|
|
reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 0, net.delta);
|
2017-03-27 09:42:30 +03:00
|
|
|
} else if (l.extra) {
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
2017-04-10 05:56:42 +03:00
|
|
|
copy_cpu(l.inputs, l.delta + i*l.outputs, 1, net.delta + i*l.inputs, 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
2016-11-17 23:18:19 +03:00
|
|
|
}else{
|
2017-04-10 05:56:42 +03:00
|
|
|
reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 1, net.delta);
|
2016-08-06 01:27:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GPU
|
2017-04-10 05:56:42 +03:00
|
|
|
void forward_reorg_layer_gpu(layer l, network net)
|
2016-08-06 01:27:07 +03:00
|
|
|
{
|
2017-03-27 09:42:30 +03:00
|
|
|
int i;
|
|
|
|
if(l.flatten){
|
|
|
|
if(l.reverse){
|
2017-06-18 23:05:37 +03:00
|
|
|
flatten_gpu(net.input_gpu, l.w*l.h, l.c, l.batch, 0, l.output_gpu);
|
2017-03-27 09:42:30 +03:00
|
|
|
}else{
|
2017-06-18 23:05:37 +03:00
|
|
|
flatten_gpu(net.input_gpu, l.w*l.h, l.c, l.batch, 1, l.output_gpu);
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
|
|
|
} else if (l.extra) {
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_gpu(l.inputs, net.input_gpu + i*l.inputs, 1, l.output_gpu + i*l.outputs, 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
|
|
|
} else if (l.reverse) {
|
2017-06-18 23:05:37 +03:00
|
|
|
reorg_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, l.output_gpu);
|
2016-11-08 10:42:19 +03:00
|
|
|
}else {
|
2017-06-18 23:05:37 +03:00
|
|
|
reorg_gpu(net.input_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, l.output_gpu);
|
2016-11-08 10:42:19 +03:00
|
|
|
}
|
2016-08-06 01:27:07 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void backward_reorg_layer_gpu(layer l, network net)
|
2016-08-06 01:27:07 +03:00
|
|
|
{
|
2017-03-27 09:42:30 +03:00
|
|
|
if(l.flatten){
|
|
|
|
if(l.reverse){
|
2017-06-18 23:05:37 +03:00
|
|
|
flatten_gpu(l.delta_gpu, l.w*l.h, l.c, l.batch, 1, net.delta_gpu);
|
2017-03-27 09:42:30 +03:00
|
|
|
}else{
|
2017-06-18 23:05:37 +03:00
|
|
|
flatten_gpu(l.delta_gpu, l.w*l.h, l.c, l.batch, 0, net.delta_gpu);
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
|
|
|
} else if (l.extra) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < l.batch; ++i){
|
2017-06-18 23:05:37 +03:00
|
|
|
copy_gpu(l.inputs, l.delta_gpu + i*l.outputs, 1, net.delta_gpu + i*l.inputs, 1);
|
2017-03-27 09:42:30 +03:00
|
|
|
}
|
|
|
|
} else if(l.reverse){
|
2017-06-18 23:05:37 +03:00
|
|
|
reorg_gpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, net.delta_gpu);
|
2017-03-27 09:42:30 +03:00
|
|
|
} else {
|
2017-06-18 23:05:37 +03:00
|
|
|
reorg_gpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, net.delta_gpu);
|
2016-11-08 10:42:19 +03:00
|
|
|
}
|
2016-08-06 01:27:07 +03:00
|
|
|
}
|
|
|
|
#endif
|