darknet/src/maxpool_layer.c

128 lines
3.8 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-07-08 10:36:43 +03:00
int h = l.out_h;
int w = l.out_w;
2015-05-11 23:46:49 +03:00
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-07-08 10:36:43 +03:00
int h = l.out_h;
int w = l.out_w;
2015-05-11 23:46:49 +03:00
int c = l.c;
return float_to_image(w,h,c,l.delta);
2013-12-03 04:41:40 +04:00
}
2016-09-02 02:48:41 +03:00
maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding)
2013-11-04 23:11:01 +04:00
{
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;
2016-09-02 02:48:41 +03:00
l.pad = padding;
2016-11-19 08:51:36 +03:00
l.out_w = (w + 2*padding)/stride;
l.out_h = (h + 2*padding)/stride;
2015-05-11 23:46:49 +03:00
l.out_c = c;
l.outputs = l.out_h * l.out_w * l.out_c;
2015-07-08 10:36:43 +03:00
l.inputs = h*w*c;
2015-05-11 23:46:49 +03:00
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));
l.forward = forward_maxpool_layer;
l.backward = backward_maxpool_layer;
2014-10-22 01:49:18 +04:00
#ifdef GPU
l.forward_gpu = forward_maxpool_layer_gpu;
l.backward_gpu = backward_maxpool_layer_gpu;
2017-05-29 21:59:27 +03:00
l.indexes_gpu = cuda_make_int_array(0, output_size);
2015-05-11 23:46:49 +03:00
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
2016-11-16 11:15:46 +03:00
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);
2015-05-11 23:46:49 +03:00
return l;
2013-11-04 23:11:01 +04:00
}
2015-07-08 10:36:43 +03:00
void resize_maxpool_layer(maxpool_layer *l, int w, int h)
2014-03-13 08:57:34 +04:00
{
2015-05-11 23:46:49 +03:00
l->h = h;
l->w = w;
2016-01-19 02:40:14 +03:00
l->inputs = h*w*l->c;
2015-07-08 10:36:43 +03:00
2016-11-19 08:51:36 +03:00
l->out_w = (w + 2*l->pad)/l->stride;
l->out_h = (h + 2*l->pad)/l->stride;
2015-07-08 10:36:43 +03:00
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));
2015-05-11 23:46:49 +03:00
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);
2017-05-29 21:59:27 +03:00
l->indexes_gpu = cuda_make_int_array(0, output_size);
2015-09-01 21:21:01 +03:00
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
}
void forward_maxpool_layer(const maxpool_layer l, network net)
2013-11-04 23:11:01 +04:00
{
2015-05-11 23:46:49 +03:00
int b,i,j,k,m,n;
2016-09-02 02:48:41 +03:00
int w_offset = -l.pad;
int h_offset = -l.pad;
2013-11-13 22:50:38 +04:00
2016-09-02 02:48:41 +03:00
int h = l.out_h;
int w = l.out_w;
2015-05-11 23:46:49 +03:00
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);
float val = (valid != 0) ? net.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
}
}
}
}
void backward_maxpool_layer(const maxpool_layer l, network net)
2013-12-03 04:41:40 +04:00
{
2014-08-09 19:16:37 +04:00
int i;
2016-09-02 02:48:41 +03:00
int h = l.out_h;
int w = l.out_w;
2015-05-11 23:46:49 +03:00
int c = l.c;
for(i = 0; i < h*w*c*l.batch; ++i){
int index = l.indexes[i];
net.delta[index] += l.delta[i];
2013-12-03 04:41:40 +04:00
}
}