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-22 01:49:18 +04:00
|
|
|
int output_size = ((h-1)/stride+1) * ((w-1)/stride+1) * c * batch;
|
|
|
|
layer->indexes = calloc(output_size, sizeof(int));
|
|
|
|
layer->output = calloc(output_size, sizeof(float));
|
|
|
|
layer->delta = calloc(output_size, sizeof(float));
|
|
|
|
#ifdef GPU
|
|
|
|
layer->indexes_cl = cl_make_int_array(layer->indexes, output_size);
|
|
|
|
layer->output_cl = cl_make_array(layer->output, output_size);
|
|
|
|
layer->delta_cl = cl_make_array(layer->delta, output_size);
|
|
|
|
#endif
|
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);
|
2014-10-22 01:49:18 +04:00
|
|
|
float val = (valid != 0) ? 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
|
|
|
}
|
|
|
|
}
|
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-10-22 01:49:18 +04:00
|
|
|
void backward_maxpool_layer(const maxpool_layer layer, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 01:49:18 +04:00
|
|
|
#ifdef GPU
|
|
|
|
cl_kernel get_forward_kernel()
|
|
|
|
{
|
|
|
|
static int init = 0;
|
|
|
|
static cl_kernel kernel;
|
|
|
|
if(!init){
|
|
|
|
kernel = get_kernel("src/maxpool_layer.cl", "forward", 0);
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
return kernel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void forward_maxpool_layer_gpu(maxpool_layer layer, cl_mem input)
|
|
|
|
{
|
|
|
|
int h = (layer.h-1)/layer.stride + 1;
|
|
|
|
int w = (layer.w-1)/layer.stride + 1;
|
|
|
|
int c = layer.c;
|
|
|
|
cl_kernel kernel = get_forward_kernel();
|
|
|
|
cl_command_queue queue = cl.queue;
|
|
|
|
|
|
|
|
cl_uint i = 0;
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.h), (void*) &layer.h);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.w), (void*) &layer.w);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.c), (void*) &layer.c);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.stride), (void*) &layer.stride);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.size), (void*) &layer.size);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(input), (void*) &input);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.output_cl), (void*) &layer.output_cl);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.indexes_cl), (void*) &layer.indexes_cl);
|
|
|
|
check_error(cl);
|
|
|
|
|
|
|
|
const size_t global_size[] = {h*w*c*layer.batch};
|
|
|
|
|
2014-11-19 00:51:04 +03:00
|
|
|
cl.error = clEnqueueNDRangeKernel(queue, kernel, 1, 0, global_size, 0, 0, 0, 0);
|
2014-10-22 01:49:18 +04:00
|
|
|
check_error(cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
cl_kernel get_backward_kernel()
|
|
|
|
{
|
|
|
|
static int init = 0;
|
|
|
|
static cl_kernel kernel;
|
|
|
|
if(!init){
|
|
|
|
kernel = get_kernel("src/maxpool_layer.cl", "backward", 0);
|
|
|
|
init = 1;
|
|
|
|
}
|
|
|
|
return kernel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void backward_maxpool_layer_gpu(maxpool_layer layer, cl_mem delta)
|
|
|
|
{
|
|
|
|
cl_kernel kernel = get_backward_kernel();
|
|
|
|
cl_command_queue queue = cl.queue;
|
|
|
|
|
|
|
|
cl_uint i = 0;
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.h), (void*) &layer.h);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.w), (void*) &layer.w);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.c), (void*) &layer.c);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.stride), (void*) &layer.stride);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.size), (void*) &layer.size);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.delta_cl), (void*) &layer.delta_cl);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(delta), (void*) &delta);
|
|
|
|
cl.error = clSetKernelArg(kernel, i++, sizeof(layer.indexes_cl), (void*) &layer.indexes_cl);
|
|
|
|
check_error(cl);
|
|
|
|
|
|
|
|
const size_t global_size[] = {layer.h*layer.w*layer.c*layer.batch};
|
|
|
|
|
2014-11-19 00:51:04 +03:00
|
|
|
cl.error = clEnqueueNDRangeKernel(queue, kernel, 1, 0, global_size, 0, 0, 0, 0);
|
2014-10-22 01:49:18 +04:00
|
|
|
check_error(cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|