darknet/src/reorg_layer.c

115 lines
3.0 KiB
C
Raw Normal View History

2016-08-06 01:27:07 +03:00
#include "reorg_layer.h"
#include "cuda.h"
#include "blas.h"
#include <stdio.h>
2016-11-19 08:51:36 +03:00
layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse)
2016-08-06 01:27:07 +03:00
{
layer l = {0};
l.type = REORG;
l.batch = batch;
l.stride = stride;
l.h = h;
l.w = w;
l.c = c;
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;
2016-11-17 23:18:19 +03:00
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);
2016-08-06 01:27:07 +03:00
l.outputs = l.out_h * l.out_w * l.out_c;
l.inputs = h*w*c;
int output_size = l.out_h * l.out_w * l.out_c * batch;
l.output = calloc(output_size, sizeof(float));
l.delta = calloc(output_size, sizeof(float));
l.forward = forward_reorg_layer;
l.backward = backward_reorg_layer;
2016-11-08 10:42:19 +03:00
#ifdef GPU
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
}
void forward_reorg_layer(const layer l, network_state state)
{
2016-11-17 23:18:19 +03:00
if(l.reverse){
reorg_cpu(state.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.output);
}else {
reorg_cpu(state.input, l.w, l.h, l.c, l.batch, l.stride, 0, l.output);
2016-08-06 01:27:07 +03:00
}
}
void backward_reorg_layer(const layer l, network_state state)
{
2016-11-17 23:18:19 +03:00
if(l.reverse){
reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 0, state.delta);
}else{
reorg_cpu(l.delta, l.w, l.h, l.c, l.batch, l.stride, 1, state.delta);
2016-08-06 01:27:07 +03:00
}
}
#ifdef GPU
void forward_reorg_layer_gpu(layer l, network_state state)
{
2016-11-08 10:42:19 +03:00
if(l.reverse){
reorg_ongpu(state.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.output_gpu);
}else {
reorg_ongpu(state.input, l.w, l.h, l.c, l.batch, l.stride, 0, l.output_gpu);
}
2016-08-06 01:27:07 +03:00
}
void backward_reorg_layer_gpu(layer l, network_state state)
{
2016-11-08 10:42:19 +03:00
if(l.reverse){
reorg_ongpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, state.delta);
}else{
reorg_ongpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 1, state.delta);
}
2016-08-06 01:27:07 +03:00
}
#endif