From 4d2fefd75a57dfd6e60680eaf7408c82e15a025d Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Thu, 17 Aug 2017 23:12:44 +0000
Subject: [PATCH] More usage rand_s()
---
src/reorg_layer.c | 91 ++++++++++++++++++++++-----------------------
1 files changed, 44 insertions(+), 47 deletions(-)
diff --git a/src/reorg_layer.c b/src/reorg_layer.c
index 5bc257a..2abca8f 100644
--- a/src/reorg_layer.c
+++ b/src/reorg_layer.c
@@ -4,7 +4,7 @@
#include <stdio.h>
-layer make_reorg_layer(int batch, int h, int w, int c, int stride)
+layer make_reorg_layer(int batch, int w, int h, int c, int stride, int reverse)
{
layer l = {0};
l.type = REORG;
@@ -13,10 +13,17 @@
l.h = h;
l.w = w;
l.c = c;
- l.out_w = w*stride;
- l.out_h = h*stride;
- l.out_c = c/(stride*stride);
- 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);
+ 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);
+ }
+ l.reverse = reverse;
+ 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;
@@ -25,25 +32,33 @@
l.forward = forward_reorg_layer;
l.backward = backward_reorg_layer;
- #ifdef GPU
+#ifdef GPU
l.forward_gpu = forward_reorg_layer_gpu;
l.backward_gpu = backward_reorg_layer_gpu;
l.output_gpu = cuda_make_array(l.output, output_size);
l.delta_gpu = cuda_make_array(l.delta, output_size);
- #endif
+#endif
return l;
}
void resize_reorg_layer(layer *l, int w, int h)
{
int stride = l->stride;
+ int c = l->c;
l->h = h;
l->w = w;
- l->out_w = w*stride;
- l->out_h = h*stride;
+ 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);
+ }
l->outputs = l->out_h * l->out_w * l->out_c;
l->inputs = l->outputs;
@@ -52,66 +67,48 @@
l->output = realloc(l->output, output_size * sizeof(float));
l->delta = realloc(l->delta, output_size * sizeof(float));
- #ifdef GPU
+#ifdef GPU
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);
- #endif
+#endif
}
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.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);
}
}
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.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);
}
}
#ifdef GPU
void forward_reorg_layer_gpu(layer l, network_state state)
{
- reorg_ongpu(state.input, l.w, l.h, l.c, l.batch, l.stride, 1, l.output_gpu);
+ 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);
+ }
}
void backward_reorg_layer_gpu(layer l, network_state state)
{
- reorg_ongpu(l.delta_gpu, l.w, l.h, l.c, l.batch, l.stride, 0, state.delta);
+ 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);
+ }
}
#endif
--
Gitblit v1.10.0