2013-11-04 23:11:01 +04:00
|
|
|
#include "maxpool_layer.h"
|
2013-11-13 22:50:38 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
image get_maxpool_image(maxpool_layer layer)
|
|
|
|
{
|
|
|
|
int h = (layer.h-1)/layer.stride + 1;
|
|
|
|
int w = (layer.w-1)/layer.stride + 1;
|
|
|
|
int c = layer.c;
|
2014-01-29 04:28:42 +04:00
|
|
|
return float_to_image(h,w,c,layer.output);
|
2013-11-13 22:50:38 +04:00
|
|
|
}
|
2013-11-04 23:11:01 +04:00
|
|
|
|
2013-12-03 04:41:40 +04:00
|
|
|
image get_maxpool_delta(maxpool_layer layer)
|
|
|
|
{
|
|
|
|
int h = (layer.h-1)/layer.stride + 1;
|
|
|
|
int w = (layer.w-1)/layer.stride + 1;
|
|
|
|
int c = layer.c;
|
2014-01-29 04:28:42 +04:00
|
|
|
return float_to_image(h,w,c,layer.delta);
|
2013-12-03 04:41:40 +04:00
|
|
|
}
|
|
|
|
|
2014-08-08 23:04:15 +04: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);
|
2013-11-07 04:09:41 +04:00
|
|
|
maxpool_layer *layer = calloc(1, sizeof(maxpool_layer));
|
2014-03-13 08:57:34 +04:00
|
|
|
layer->batch = batch;
|
2013-11-13 22:50:38 +04:00
|
|
|
layer->h = h;
|
|
|
|
layer->w = w;
|
|
|
|
layer->c = c;
|
2014-08-08 23:04:15 +04:00
|
|
|
layer->size = size;
|
2013-11-07 04:09:41 +04:00
|
|
|
layer->stride = stride;
|
2014-10-17 02:17:23 +04:00
|
|
|
layer->indexes = calloc(((h-1)/stride+1) * ((w-1)/stride+1) * c*batch, sizeof(int));
|
2014-07-14 09:07:51 +04:00
|
|
|
layer->output = calloc(((h-1)/stride+1) * ((w-1)/stride+1) * c*batch, sizeof(float));
|
|
|
|
layer->delta = calloc(((h-1)/stride+1) * ((w-1)/stride+1) * c*batch, sizeof(float));
|
2013-11-04 23:11:01 +04:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2014-03-13 08:57:34 +04:00
|
|
|
void resize_maxpool_layer(maxpool_layer *layer, int h, int w, int c)
|
|
|
|
{
|
|
|
|
layer->h = h;
|
|
|
|
layer->w = w;
|
|
|
|
layer->c = c;
|
2014-07-14 09:07:51 +04:00
|
|
|
layer->output = realloc(layer->output, ((h-1)/layer->stride+1) * ((w-1)/layer->stride+1) * c * layer->batch* sizeof(float));
|
|
|
|
layer->delta = realloc(layer->delta, ((h-1)/layer->stride+1) * ((w-1)/layer->stride+1) * c * layer->batch*sizeof(float));
|
2014-03-13 08:57:34 +04:00
|
|
|
}
|
|
|
|
|
2014-08-09 19:16:37 +04:00
|
|
|
void forward_maxpool_layer(const maxpool_layer layer, float *input)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2014-10-17 02:17:23 +04:00
|
|
|
int b,i,j,k,l,m;
|
|
|
|
int w_offset = (-layer.size-1)/2 + 1;
|
|
|
|
int h_offset = (-layer.size-1)/2 + 1;
|
2013-11-13 22:50:38 +04:00
|
|
|
|
2014-10-17 02:17:23 +04:00
|
|
|
int h = (layer.h-1)/layer.stride + 1;
|
|
|
|
int w = (layer.w-1)/layer.stride + 1;
|
|
|
|
int c = layer.c;
|
2014-08-08 23:04:15 +04:00
|
|
|
|
2014-10-17 02:17:23 +04:00
|
|
|
for(b = 0; b < layer.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] : -INFINITY;
|
|
|
|
max_i = (val > max) ? index : max_i;
|
|
|
|
max = (val > max) ? val : max;
|
2014-08-09 19:16:37 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-17 02:17:23 +04:00
|
|
|
layer.output[out_index] = max;
|
|
|
|
layer.indexes[out_index] = max_i;
|
2014-08-09 19:16:37 +04:00
|
|
|
}
|
2014-08-08 23:04:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 19:16:37 +04:00
|
|
|
void backward_maxpool_layer(const maxpool_layer layer, float *input, float *delta)
|
2013-12-03 04:41:40 +04:00
|
|
|
{
|
2014-08-09 19:16:37 +04:00
|
|
|
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){
|
2014-10-17 02:17:23 +04:00
|
|
|
int index = layer.indexes[i];
|
2014-08-09 19:16:37 +04:00
|
|
|
delta[index] += layer.delta[i];
|
2013-12-03 04:41:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|