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;
|
|
|
|
return double_to_image(h,w,c,layer.output);
|
|
|
|
}
|
2013-11-04 23:11:01 +04:00
|
|
|
|
2013-11-07 04:09:41 +04:00
|
|
|
maxpool_layer *make_maxpool_layer(int h, int w, int c, int stride)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2013-11-13 22:50:38 +04:00
|
|
|
printf("Maxpool Layer: %d x %d x %d image, %d stride\n", h,w,c,stride);
|
2013-11-07 04:09:41 +04:00
|
|
|
maxpool_layer *layer = calloc(1, sizeof(maxpool_layer));
|
2013-11-13 22:50:38 +04:00
|
|
|
layer->h = h;
|
|
|
|
layer->w = w;
|
|
|
|
layer->c = c;
|
2013-11-07 04:09:41 +04:00
|
|
|
layer->stride = stride;
|
2013-11-13 22:50:38 +04:00
|
|
|
layer->output = calloc(((h-1)/stride+1) * ((w-1)/stride+1) * c, sizeof(double));
|
|
|
|
layer->delta = calloc(((h-1)/stride+1) * ((w-1)/stride+1) * c, sizeof(double));
|
2013-11-04 23:11:01 +04:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:50:38 +04:00
|
|
|
void forward_maxpool_layer(const maxpool_layer layer, double *in)
|
2013-11-04 23:11:01 +04:00
|
|
|
{
|
2013-11-13 22:50:38 +04:00
|
|
|
image input = double_to_image(layer.h, layer.w, layer.c, in);
|
|
|
|
image output = get_maxpool_image(layer);
|
2013-11-04 23:11:01 +04:00
|
|
|
int i,j,k;
|
2013-11-13 22:50:38 +04:00
|
|
|
for(i = 0; i < output.h*output.w*output.c; ++i) output.data[i] = -DBL_MAX;
|
|
|
|
for(k = 0; k < input.c; ++k){
|
|
|
|
for(i = 0; i < input.h; ++i){
|
|
|
|
for(j = 0; j < input.w; ++j){
|
2013-11-04 23:11:01 +04:00
|
|
|
double val = get_pixel(input, i, j, k);
|
2013-11-13 22:50:38 +04:00
|
|
|
double cur = get_pixel(output, i/layer.stride, j/layer.stride, k);
|
|
|
|
if(val > cur) set_pixel(output, i/layer.stride, j/layer.stride, k, val);
|
2013-11-04 23:11:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 22:50:38 +04:00
|
|
|
|