maxpool_layer.c
Go to the documentation of this file.
00001 #include "maxpool_layer.h"
00002 #include "cuda.h"
00003 #include <stdio.h>
00004 
00005 image get_maxpool_image(maxpool_layer l)
00006 {
00007     int h = l.out_h;
00008     int w = l.out_w;
00009     int c = l.c;
00010     return float_to_image(w,h,c,l.output);
00011 }
00012 
00013 image get_maxpool_delta(maxpool_layer l)
00014 {
00015     int h = l.out_h;
00016     int w = l.out_w;
00017     int c = l.c;
00018     return float_to_image(w,h,c,l.delta);
00019 }
00020 
00021 maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding)
00022 {
00023     maxpool_layer l = {0};
00024     l.type = MAXPOOL;
00025     l.batch = batch;
00026     l.h = h;
00027     l.w = w;
00028     l.c = c;
00029     l.pad = padding;
00030     l.out_w = (w + 2*padding)/stride;
00031     l.out_h = (h + 2*padding)/stride;
00032     l.out_c = c;
00033     l.outputs = l.out_h * l.out_w * l.out_c;
00034     l.inputs = h*w*c;
00035     l.size = size;
00036     l.stride = stride;
00037     int output_size = l.out_h * l.out_w * l.out_c * batch;
00038     l.indexes = calloc(output_size, sizeof(int));
00039     l.output =  calloc(output_size, sizeof(float));
00040     l.delta =   calloc(output_size, sizeof(float));
00041     l.forward = forward_maxpool_layer;
00042     l.backward = backward_maxpool_layer;
00043     #ifdef GPU
00044     l.forward_gpu = forward_maxpool_layer_gpu;
00045     l.backward_gpu = backward_maxpool_layer_gpu;
00046     l.indexes_gpu = cuda_make_int_array(output_size);
00047     l.output_gpu  = cuda_make_array(l.output, output_size);
00048     l.delta_gpu   = cuda_make_array(l.delta, output_size);
00049     #endif
00050     fprintf(stderr, "max          %d x %d / %d  %4d x%4d x%4d   ->  %4d x%4d x%4d\n", size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c);
00051     return l;
00052 }
00053 
00054 void resize_maxpool_layer(maxpool_layer *l, int w, int h)
00055 {
00056     l->h = h;
00057     l->w = w;
00058     l->inputs = h*w*l->c;
00059 
00060     l->out_w = (w + 2*l->pad)/l->stride;
00061     l->out_h = (h + 2*l->pad)/l->stride;
00062     l->outputs = l->out_w * l->out_h * l->c;
00063     int output_size = l->outputs * l->batch;
00064 
00065     l->indexes = realloc(l->indexes, output_size * sizeof(int));
00066     l->output = realloc(l->output, output_size * sizeof(float));
00067     l->delta = realloc(l->delta, output_size * sizeof(float));
00068 
00069     #ifdef GPU
00070     cuda_free((float *)l->indexes_gpu);
00071     cuda_free(l->output_gpu);
00072     cuda_free(l->delta_gpu);
00073     l->indexes_gpu = cuda_make_int_array(output_size);
00074     l->output_gpu  = cuda_make_array(l->output, output_size);
00075     l->delta_gpu   = cuda_make_array(l->delta,  output_size);
00076     #endif
00077 }
00078 
00079 void forward_maxpool_layer(const maxpool_layer l, network_state state)
00080 {
00081     int b,i,j,k,m,n;
00082     int w_offset = -l.pad;
00083     int h_offset = -l.pad;
00084 
00085     int h = l.out_h;
00086     int w = l.out_w;
00087     int c = l.c;
00088 
00089     for(b = 0; b < l.batch; ++b){
00090         for(k = 0; k < c; ++k){
00091             for(i = 0; i < h; ++i){
00092                 for(j = 0; j < w; ++j){
00093                     int out_index = j + w*(i + h*(k + c*b));
00094                     float max = -FLT_MAX;
00095                     int max_i = -1;
00096                     for(n = 0; n < l.size; ++n){
00097                         for(m = 0; m < l.size; ++m){
00098                             int cur_h = h_offset + i*l.stride + n;
00099                             int cur_w = w_offset + j*l.stride + m;
00100                             int index = cur_w + l.w*(cur_h + l.h*(k + b*l.c));
00101                             int valid = (cur_h >= 0 && cur_h < l.h &&
00102                                          cur_w >= 0 && cur_w < l.w);
00103                             float val = (valid != 0) ? state.input[index] : -FLT_MAX;
00104                             max_i = (val > max) ? index : max_i;
00105                             max   = (val > max) ? val   : max;
00106                         }
00107                     }
00108                     l.output[out_index] = max;
00109                     l.indexes[out_index] = max_i;
00110                 }
00111             }
00112         }
00113     }
00114 }
00115 
00116 void backward_maxpool_layer(const maxpool_layer l, network_state state)
00117 {
00118     int i;
00119     int h = l.out_h;
00120     int w = l.out_w;
00121     int c = l.c;
00122     for(i = 0; i < h*w*c*l.batch; ++i){
00123         int index = l.indexes[i];
00124         state.delta[index] += l.delta[i];
00125     }
00126 }
00127 


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30