| | |
| | | #include <stdio.h> |
| | | |
| | | |
| | | layer make_reorg_layer(int batch, int h, int w, int c, int stride, int reverse) |
| | | layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse) |
| | | { |
| | | layer l = {0}; |
| | | l.type = REORG; |
| | |
| | | l.out_c = c*(stride*stride); |
| | | } |
| | | l.reverse = reverse; |
| | | fprintf(stderr, "Reorg Layer: %d x %d x %d image -> %d x %d x %d image, \n", w,h,c,l.out_w, l.out_h, l.out_c); |
| | | 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); |
| | | 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; |
| | |
| | | |
| | | void forward_reorg_layer(const layer l, network_state state) |
| | | { |
| | | int b,i,j,k; |
| | | |
| | | for(b = 0; b < l.batch; ++b){ |
| | | for(k = 0; k < l.c; ++k){ |
| | | for(j = 0; j < l.h; ++j){ |
| | | for(i = 0; i < l.w; ++i){ |
| | | int in_index = i + l.w*(j + l.h*(k + l.c*b)); |
| | | |
| | | int c2 = k % l.out_c; |
| | | int offset = k / l.out_c; |
| | | int w2 = i*l.stride + offset % l.stride; |
| | | int h2 = j*l.stride + offset / l.stride; |
| | | int out_index = w2 + l.out_w*(h2 + l.out_h*(c2 + l.out_c*b)); |
| | | l.output[out_index] = state.input[in_index]; |
| | | } |
| | | } |
| | | } |
| | | if (l.reverse) { |
| | | reorg_cpu(state.input, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 1, l.output); |
| | | } |
| | | else { |
| | | reorg_cpu(state.input, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 0, l.output); |
| | | } |
| | | } |
| | | |
| | | void backward_reorg_layer(const layer l, network_state state) |
| | | { |
| | | int b,i,j,k; |
| | | |
| | | for(b = 0; b < l.batch; ++b){ |
| | | for(k = 0; k < l.c; ++k){ |
| | | for(j = 0; j < l.h; ++j){ |
| | | for(i = 0; i < l.w; ++i){ |
| | | int in_index = i + l.w*(j + l.h*(k + l.c*b)); |
| | | |
| | | int c2 = k % l.out_c; |
| | | int offset = k / l.out_c; |
| | | int w2 = i*l.stride + offset % l.stride; |
| | | int h2 = j*l.stride + offset / l.stride; |
| | | int out_index = w2 + l.out_w*(h2 + l.out_h*(c2 + l.out_c*b)); |
| | | state.delta[in_index] = l.delta[out_index]; |
| | | } |
| | | } |
| | | } |
| | | if (l.reverse) { |
| | | reorg_cpu(l.delta, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 0, state.delta); |
| | | } |
| | | else { |
| | | reorg_cpu(l.delta, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 1, state.delta); |
| | | } |
| | | } |
| | | |
| | | #ifdef GPU |
| | | void forward_reorg_layer_gpu(layer l, network_state state) |
| | | { |
| | | 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); |
| | | if (l.reverse) { |
| | | reorg_ongpu(state.input, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 1, l.output_gpu); |
| | | } |
| | | else { |
| | | reorg_ongpu(state.input, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 0, l.output_gpu); |
| | | } |
| | | } |
| | | |
| | | void backward_reorg_layer_gpu(layer l, network_state state) |
| | | { |
| | | 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); |
| | | if (l.reverse) { |
| | | reorg_ongpu(l.delta_gpu, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 0, state.delta); |
| | | } |
| | | else { |
| | | reorg_ongpu(l.delta_gpu, l.out_w, l.out_h, l.out_c, l.batch, l.stride, 1, state.delta); |
| | | } |
| | | } |
| | | #endif |