| | |
| | | #include "maxpool_layer.h" |
| | | #include "cuda.h" |
| | | #include <stdio.h> |
| | | |
| | | maxpool_layer *make_maxpool_layer(int h, int w, int c, int stride) |
| | | image get_maxpool_image(maxpool_layer l) |
| | | { |
| | | maxpool_layer *layer = calloc(1, sizeof(maxpool_layer)); |
| | | layer->stride = stride; |
| | | layer->output = make_image((h-1)/stride+1, (w-1)/stride+1, c); |
| | | return layer; |
| | | int h = l.out_h; |
| | | int w = l.out_w; |
| | | int c = l.c; |
| | | return float_to_image(w,h,c,l.output); |
| | | } |
| | | |
| | | void run_maxpool_layer(const image input, const maxpool_layer layer) |
| | | image get_maxpool_delta(maxpool_layer l) |
| | | { |
| | | int i,j,k; |
| | | for(i = 0; i < layer.output.h*layer.output.w*layer.output.c; ++i) layer.output.data[i] = -DBL_MAX; |
| | | for(i = 0; i < input.h; ++i){ |
| | | for(j = 0; j < input.w; ++j){ |
| | | for(k = 0; k < input.c; ++k){ |
| | | double val = get_pixel(input, i, j, k); |
| | | double cur = get_pixel(layer.output, i/layer.stride, j/layer.stride, k); |
| | | if(val > cur) set_pixel(layer.output, i/layer.stride, j/layer.stride, k, val); |
| | | 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, int padding) |
| | | { |
| | | fprintf(stderr, "Maxpool Layer: %d x %d x %d image, %d size, %d stride\n", h,w,c,size,stride); |
| | | maxpool_layer l = {0}; |
| | | l.type = MAXPOOL; |
| | | l.batch = batch; |
| | | l.h = h; |
| | | l.w = w; |
| | | l.c = c; |
| | | l.pad = padding; |
| | | l.out_w = (w + 2*padding - size + 1)/stride + 1; |
| | | l.out_h = (h + 2*padding - size + 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 |
| | | 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 l; |
| | | } |
| | | |
| | | void resize_maxpool_layer(maxpool_layer *l, int w, int h) |
| | | { |
| | | l->h = h; |
| | | l->w = w; |
| | | l->inputs = h*w*l->c; |
| | | |
| | | l->out_w = (w + 2*l->pad - l->size + 1)/l->stride + 1; |
| | | l->out_h = (h + 2*l->pad - l->size + 1)/l->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 *)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 l, network_state state) |
| | | { |
| | | int b,i,j,k,m,n; |
| | | int w_offset = -l.pad; |
| | | int h_offset = -l.pad; |
| | | |
| | | int h = l.out_h; |
| | | int w = l.out_w; |
| | | int c = l.c; |
| | | |
| | | 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(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; |
| | | } |
| | | } |
| | | l.output[out_index] = max; |
| | | l.indexes[out_index] = max_i; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | void backward_maxpool_layer(const maxpool_layer l, network_state state) |
| | | { |
| | | int i; |
| | | int h = l.out_h; |
| | | int w = l.out_w; |
| | | 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]; |
| | | } |
| | | } |
| | | |