2015-07-14 01:04:21 +03:00
|
|
|
#include "avgpool_layer.h"
|
|
|
|
#include "cuda.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
avgpool_layer make_avgpool_layer(int batch, int w, int h, int c)
|
|
|
|
{
|
2016-11-16 11:15:46 +03:00
|
|
|
fprintf(stderr, "avg %4d x%4d x%4d -> %4d\n", w, h, c, c);
|
2015-07-14 01:04:21 +03:00
|
|
|
avgpool_layer l = {0};
|
|
|
|
l.type = AVGPOOL;
|
|
|
|
l.batch = batch;
|
|
|
|
l.h = h;
|
|
|
|
l.w = w;
|
|
|
|
l.c = c;
|
|
|
|
l.out_w = 1;
|
|
|
|
l.out_h = 1;
|
|
|
|
l.out_c = c;
|
|
|
|
l.outputs = l.out_c;
|
|
|
|
l.inputs = h*w*c;
|
|
|
|
int output_size = l.outputs * batch;
|
|
|
|
l.output = calloc(output_size, sizeof(float));
|
|
|
|
l.delta = calloc(output_size, sizeof(float));
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward = forward_avgpool_layer;
|
|
|
|
l.backward = backward_avgpool_layer;
|
2015-07-14 01:04:21 +03:00
|
|
|
#ifdef GPU
|
2016-09-25 09:12:54 +03:00
|
|
|
l.forward_gpu = forward_avgpool_layer_gpu;
|
|
|
|
l.backward_gpu = backward_avgpool_layer_gpu;
|
2015-07-14 01:04:21 +03:00
|
|
|
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_avgpool_layer(avgpool_layer *l, int w, int h)
|
|
|
|
{
|
|
|
|
l->w = w;
|
2016-01-19 02:40:14 +03:00
|
|
|
l->h = h;
|
|
|
|
l->inputs = h*w*l->c;
|
2015-07-14 01:04:21 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void forward_avgpool_layer(const avgpool_layer l, network net)
|
2015-07-14 01:04:21 +03:00
|
|
|
{
|
|
|
|
int b,i,k;
|
|
|
|
|
|
|
|
for(b = 0; b < l.batch; ++b){
|
|
|
|
for(k = 0; k < l.c; ++k){
|
|
|
|
int out_index = k + b*l.c;
|
|
|
|
l.output[out_index] = 0;
|
|
|
|
for(i = 0; i < l.h*l.w; ++i){
|
|
|
|
int in_index = i + l.h*l.w*(k + b*l.c);
|
2017-04-10 05:56:42 +03:00
|
|
|
l.output[out_index] += net.input[in_index];
|
2015-07-14 01:04:21 +03:00
|
|
|
}
|
|
|
|
l.output[out_index] /= l.h*l.w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:56:42 +03:00
|
|
|
void backward_avgpool_layer(const avgpool_layer l, network net)
|
2015-07-14 01:04:21 +03:00
|
|
|
{
|
|
|
|
int b,i,k;
|
|
|
|
|
|
|
|
for(b = 0; b < l.batch; ++b){
|
|
|
|
for(k = 0; k < l.c; ++k){
|
|
|
|
int out_index = k + b*l.c;
|
|
|
|
for(i = 0; i < l.h*l.w; ++i){
|
|
|
|
int in_index = i + l.h*l.w*(k + b*l.c);
|
2017-04-10 05:56:42 +03:00
|
|
|
net.delta[in_index] += l.delta[out_index] / (l.h*l.w);
|
2015-07-14 01:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|