From cf32e7e9b843560eb7ec3ed16e5b19f0f7156724 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@burninator.cs.washington.edu>
Date: Sat, 25 Jun 2016 23:12:00 +0000
Subject: [PATCH] colors

---
 src/maxpool_layer.c |  141 +++++++++++++++++++++++++---------------------
 1 files changed, 77 insertions(+), 64 deletions(-)

diff --git a/src/maxpool_layer.c b/src/maxpool_layer.c
index ef7176d..7af4954 100644
--- a/src/maxpool_layer.c
+++ b/src/maxpool_layer.c
@@ -2,109 +2,122 @@
 #include "cuda.h"
 #include <stdio.h>
 
-image get_maxpool_image(maxpool_layer layer)
+image get_maxpool_image(maxpool_layer l)
 {
-    int h = (layer.h-1)/layer.stride + 1;
-    int w = (layer.w-1)/layer.stride + 1;
-    int c = layer.c;
-    return float_to_image(h,w,c,layer.output);
+    int h = l.out_h;
+    int w = l.out_w;
+    int c = l.c;
+    return float_to_image(w,h,c,l.output);
 }
 
-image get_maxpool_delta(maxpool_layer layer)
+image get_maxpool_delta(maxpool_layer l)
 {
-    int h = (layer.h-1)/layer.stride + 1;
-    int w = (layer.w-1)/layer.stride + 1;
-    int c = layer.c;
-    return float_to_image(h,w,c,layer.delta);
+    int h = l.out_h;
+    int w = l.out_w;
+    int c = l.c;
+    return float_to_image(w,h,c,l.delta);
 }
 
-maxpool_layer *make_maxpool_layer(int batch, int h, int w, int c, int size, int stride)
+maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride)
 {
     fprintf(stderr, "Maxpool Layer: %d x %d x %d image, %d size, %d stride\n", h,w,c,size,stride);
-    maxpool_layer *layer = calloc(1, sizeof(maxpool_layer));
-    layer->batch = batch;
-    layer->h = h;
-    layer->w = w;
-    layer->c = c;
-    layer->size = size;
-    layer->stride = stride;
-    int output_size = ((h-1)/stride+1) * ((w-1)/stride+1) * c * batch;
-    layer->indexes = calloc(output_size, sizeof(int));
-    layer->output =  calloc(output_size, sizeof(float));
-    layer->delta =   calloc(output_size, sizeof(float));
+    maxpool_layer l = {0};
+    l.type = MAXPOOL;
+    l.batch = batch;
+    l.h = h;
+    l.w = w;
+    l.c = c;
+    l.out_w = (w-1)/stride + 1;
+    l.out_h = (h-1)/stride + 1;
+    l.out_c = c;
+    l.outputs = l.out_h * l.out_w * l.out_c;
+    l.inputs = h*w*c;
+    l.size = size;
+    l.stride = stride;
+    int output_size = l.out_h * l.out_w * l.out_c * batch;
+    l.indexes = calloc(output_size, sizeof(int));
+    l.output =  calloc(output_size, sizeof(float));
+    l.delta =   calloc(output_size, sizeof(float));
     #ifdef GPU
-    layer->indexes_gpu = cuda_make_int_array(output_size);
-    layer->output_gpu  = cuda_make_array(layer->output, output_size);
-    layer->delta_gpu   = cuda_make_array(layer->delta, output_size);
+    l.indexes_gpu = cuda_make_int_array(output_size);
+    l.output_gpu  = cuda_make_array(l.output, output_size);
+    l.delta_gpu   = cuda_make_array(l.delta, output_size);
     #endif
-    return layer;
+    return l;
 }
 
-void resize_maxpool_layer(maxpool_layer *layer, int h, int w)
+void resize_maxpool_layer(maxpool_layer *l, int w, int h)
 {
-    layer->h = h;
-    layer->w = w;
-    int output_size = ((h-1)/layer->stride+1) * ((w-1)/layer->stride+1) * layer->c * layer->batch;
-    layer->output = realloc(layer->output, output_size * sizeof(float));
-    layer->delta = realloc(layer->delta, output_size * sizeof(float));
+    int stride = l->stride;
+    l->h = h;
+    l->w = w;
+    l->inputs = h*w*l->c;
+
+    l->out_w = (w-1)/stride + 1;
+    l->out_h = (h-1)/stride + 1;
+    l->outputs = l->out_w * l->out_h * l->c;
+    int output_size = l->outputs * l->batch;
+
+    l->indexes = realloc(l->indexes, output_size * sizeof(int));
+    l->output = realloc(l->output, output_size * sizeof(float));
+    l->delta = realloc(l->delta, output_size * sizeof(float));
 
     #ifdef GPU
-    cuda_free((float *)layer->indexes_gpu);
-    cuda_free(layer->output_gpu);
-    cuda_free(layer->delta_gpu);
-    layer->indexes_gpu = cuda_make_int_array(output_size);
-    layer->output_gpu  = cuda_make_array(layer->output, output_size);
-    layer->delta_gpu   = cuda_make_array(layer->delta, output_size);
+    cuda_free((float *)l->indexes_gpu);
+    cuda_free(l->output_gpu);
+    cuda_free(l->delta_gpu);
+    l->indexes_gpu = cuda_make_int_array(output_size);
+    l->output_gpu  = cuda_make_array(l->output, output_size);
+    l->delta_gpu   = cuda_make_array(l->delta,  output_size);
     #endif
 }
 
-void forward_maxpool_layer(const maxpool_layer layer, float *input)
+void forward_maxpool_layer(const maxpool_layer l, network_state state)
 {
-    int b,i,j,k,l,m;
-    int w_offset = (-layer.size-1)/2 + 1;
-    int h_offset = (-layer.size-1)/2 + 1;
+    int b,i,j,k,m,n;
+    int w_offset = (-l.size-1)/2 + 1;
+    int h_offset = (-l.size-1)/2 + 1;
 
-    int h = (layer.h-1)/layer.stride + 1;
-    int w = (layer.w-1)/layer.stride + 1;
-    int c = layer.c;
+    int h = (l.h-1)/l.stride + 1;
+    int w = (l.w-1)/l.stride + 1;
+    int c = l.c;
 
-    for(b = 0; b < layer.batch; ++b){
+    for(b = 0; b < l.batch; ++b){
         for(k = 0; k < c; ++k){
             for(i = 0; i < h; ++i){
                 for(j = 0; j < w; ++j){
                     int out_index = j + w*(i + h*(k + c*b));
                     float max = -FLT_MAX;
                     int max_i = -1;
-                    for(l = 0; l < layer.size; ++l){
-                        for(m = 0; m < layer.size; ++m){
-                            int cur_h = h_offset + i*layer.stride + l;
-                            int cur_w = w_offset + j*layer.stride + m;
-                            int index = cur_w + layer.w*(cur_h + layer.h*(k + b*layer.c));
-                            int valid = (cur_h >= 0 && cur_h < layer.h &&
-                                         cur_w >= 0 && cur_w < layer.w);
-                            float val = (valid != 0) ? input[index] : -FLT_MAX;
+                    for(n = 0; n < l.size; ++n){
+                        for(m = 0; m < l.size; ++m){
+                            int cur_h = h_offset + i*l.stride + n;
+                            int cur_w = w_offset + j*l.stride + m;
+                            int index = cur_w + l.w*(cur_h + l.h*(k + b*l.c));
+                            int valid = (cur_h >= 0 && cur_h < l.h &&
+                                         cur_w >= 0 && cur_w < l.w);
+                            float val = (valid != 0) ? state.input[index] : -FLT_MAX;
                             max_i = (val > max) ? index : max_i;
                             max   = (val > max) ? val   : max;
                         }
                     }
-                    layer.output[out_index] = max;
-                    layer.indexes[out_index] = max_i;
+                    l.output[out_index] = max;
+                    l.indexes[out_index] = max_i;
                 }
             }
         }
     }
 }
 
-void backward_maxpool_layer(const maxpool_layer layer, float *delta)
+void backward_maxpool_layer(const maxpool_layer l, network_state state)
 {
     int i;
-    int h = (layer.h-1)/layer.stride + 1;
-    int w = (layer.w-1)/layer.stride + 1;
-    int c = layer.c;
-    memset(delta, 0, layer.batch*layer.h*layer.w*layer.c*sizeof(float));
-    for(i = 0; i < h*w*c*layer.batch; ++i){
-        int index = layer.indexes[i];
-        delta[index] += layer.delta[i];
+    int h = (l.h-1)/l.stride + 1;
+    int w = (l.w-1)/l.stride + 1;
+    int c = l.c;
+    for(i = 0; i < h*w*c*l.batch; ++i){
+        int index = l.indexes[i];
+        state.delta[index] += l.delta[i];
     }
 }
 

--
Gitblit v1.10.0