darknet/src/maxpool_layer.c

117 lines
3.6 KiB
C
Raw Normal View History

2013-11-04 23:11:01 +04:00
#include "maxpool_layer.h"
2015-01-23 03:38:24 +03:00
#include "cuda.h"
2013-11-13 22:50:38 +04:00
#include <stdio.h>
2015-05-11 23:46:49 +03:00
image get_maxpool_image(maxpool_layer l)
2013-11-13 22:50:38 +04:00
{
2015-05-11 23:46:49 +03:00
int h = (l.h-1)/l.stride + 1;
int w = (l.w-1)/l.stride + 1;
int c = l.c;
return float_to_image(w,h,c,l.output);
2013-11-13 22:50:38 +04:00
}
2013-11-04 23:11:01 +04:00
2015-05-11 23:46:49 +03:00
image get_maxpool_delta(maxpool_layer l)
2013-12-03 04:41:40 +04:00
{
2015-05-11 23:46:49 +03:00
int h = (l.h-1)/l.stride + 1;
int w = (l.w-1)/l.stride + 1;
int c = l.c;
return float_to_image(w,h,c,l.delta);
2013-12-03 04:41:40 +04:00
}
2015-05-11 23:46:49 +03:00
maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride)
2013-11-04 23:11:01 +04:00
{
2014-08-08 23:04:15 +04:00
fprintf(stderr, "Maxpool Layer: %d x %d x %d image, %d size, %d stride\n", h,w,c,size,stride);
2015-05-11 23:46:49 +03:00
maxpool_layer l = {0};
l.type = MAXPOOL;
l.batch = batch;
l.h = h;
l.w = w;
l.c = c;
2015-05-12 00:36:45 +03:00
l.out_h = (h-1)/stride + 1;
l.out_w = (w-1)/stride + 1;
2015-05-11 23:46:49 +03:00
l.out_c = c;
l.outputs = l.out_h * l.out_w * l.out_c;
l.inputs = l.outputs;
l.size = size;
l.stride = stride;
2015-05-12 00:36:45 +03:00
int output_size = l.out_h * l.out_w * l.out_c * batch;
2015-05-11 23:46:49 +03:00
l.indexes = calloc(output_size, sizeof(int));
l.output = calloc(output_size, sizeof(float));
l.delta = calloc(output_size, sizeof(float));
2014-10-22 01:49:18 +04:00
#ifdef GPU
2015-05-11 23:46:49 +03:00
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);
2014-10-22 01:49:18 +04:00
#endif
2015-05-11 23:46:49 +03:00
return l;
2013-11-04 23:11:01 +04:00
}
2015-05-11 23:46:49 +03:00
void resize_maxpool_layer(maxpool_layer *l, int h, int w)
2014-03-13 08:57:34 +04:00
{
2015-05-11 23:46:49 +03:00
l->h = h;
l->w = w;
int output_size = ((h-1)/l->stride+1) * ((w-1)/l->stride+1) * l->c * l->batch;
l->output = realloc(l->output, output_size * sizeof(float));
l->delta = realloc(l->delta, output_size * sizeof(float));
2015-02-11 06:41:03 +03:00
#ifdef GPU
2015-05-11 23:46:49 +03:00
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);
2015-02-11 06:41:03 +03:00
#endif
2014-03-13 08:57:34 +04:00
}
2015-05-11 23:46:49 +03:00
void forward_maxpool_layer(const maxpool_layer l, network_state state)
2013-11-04 23:11:01 +04:00
{
2015-05-11 23:46:49 +03:00
int b,i,j,k,m,n;
int w_offset = (-l.size-1)/2 + 1;
int h_offset = (-l.size-1)/2 + 1;
2013-11-13 22:50:38 +04:00
2015-05-11 23:46:49 +03:00
int h = (l.h-1)/l.stride + 1;
int w = (l.w-1)/l.stride + 1;
int c = l.c;
2014-08-08 23:04:15 +04:00
2015-05-11 23:46:49 +03:00
for(b = 0; b < l.batch; ++b){
2014-10-17 02:17:23 +04:00
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;
2015-05-11 23:46:49 +03:00
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);
2015-03-12 08:20:15 +03:00
float val = (valid != 0) ? state.input[index] : -FLT_MAX;
2014-10-17 02:17:23 +04:00
max_i = (val > max) ? index : max_i;
max = (val > max) ? val : max;
2014-08-09 19:16:37 +04:00
}
}
2015-05-11 23:46:49 +03:00
l.output[out_index] = max;
l.indexes[out_index] = max_i;
2014-08-09 19:16:37 +04:00
}
2014-08-08 23:04:15 +04:00
}
}
}
}
2015-05-11 23:46:49 +03:00
void backward_maxpool_layer(const maxpool_layer l, network_state state)
2013-12-03 04:41:40 +04:00
{
2014-08-09 19:16:37 +04:00
int i;
2015-05-11 23:46:49 +03:00
int h = (l.h-1)/l.stride + 1;
int w = (l.w-1)/l.stride + 1;
int c = l.c;
memset(state.delta, 0, l.batch*l.h*l.w*l.c*sizeof(float));
for(i = 0; i < h*w*c*l.batch; ++i){
int index = l.indexes[i];
state.delta[index] += l.delta[i];
2013-12-03 04:41:40 +04:00
}
}