#include "normalization_layer.h" #include "blas.h" #include namespace DN{ layer make_normalization_layer(int batch, int w, int h, int c, int size, float alpha, float beta, float kappa) { fprintf(stderr, "Local Response Normalization Layer: %d x %d x %d image, %d size\n", w, h, c, size); layer l; memset(&l, 0, sizeof(layer)); l.type = NORMALIZATION; l.batch = batch; l.h = l.out_h = h; l.w = l.out_w = w; l.c = l.out_c = c; l.kappa = kappa; l.size = size; l.alpha = alpha; l.beta = beta; l.output = (float*)calloc(h * w * c * batch, sizeof(float)); l.delta = (float*)calloc(h * w * c * batch, sizeof(float)); l.squared = (float*)calloc(h * w * c * batch, sizeof(float)); l.norms = (float*)calloc(h * w * c * batch, sizeof(float)); l.inputs = w*h*c; l.outputs = l.inputs; l.forward = forward_normalization_layer; l.backward = backward_normalization_layer; #ifdef GPU l.forward_gpu = forward_normalization_layer_gpu; l.backward_gpu = backward_normalization_layer_gpu; l.output_gpu = cuda_make_array(l.output, h * w * c * batch); l.delta_gpu = cuda_make_array(l.delta, h * w * c * batch); l.squared_gpu = cuda_make_array(l.squared, h * w * c * batch); l.norms_gpu = cuda_make_array(l.norms, h * w * c * batch); #endif return l; } void resize_normalization_layer(layer *l, int w, int h) { int c = l->c; int batch = l->batch; l->h = h; l->w = w; l->out_h = h; l->out_w = w; l->inputs = w*h*c; l->outputs = l->inputs; l->output = (float*)realloc(l->output, h * w * c * batch * sizeof(float)); l->delta = (float*)realloc(l->delta, h * w * c * batch * sizeof(float)); l->squared = (float*)realloc(l->squared, h * w * c * batch * sizeof(float)); l->norms = (float*)realloc(l->norms, h * w * c * batch * sizeof(float)); #ifdef GPU cuda_free(l->output_gpu); cuda_free(l->delta_gpu); cuda_free(l->squared_gpu); cuda_free(l->norms_gpu); l->output_gpu = cuda_make_array(l->output, h * w * c * batch); l->delta_gpu = cuda_make_array(l->delta, h * w * c * batch); l->squared_gpu = cuda_make_array(l->squared, h * w * c * batch); l->norms_gpu = cuda_make_array(l->norms, h * w * c * batch); #endif } void forward_normalization_layer(const layer l, network net) { int k, b; int w = l.w; int h = l.h; int c = l.c; scal_cpu(w*h*c*l.batch, 0, l.squared, 1); for (b = 0; b < l.batch; ++b){ float *squared = l.squared + w*h*c*b; float *norms = l.norms + w*h*c*b; float *input = net.input + w*h*c*b; pow_cpu(w*h*c, 2, input, 1, squared, 1); const_cpu(w*h, l.kappa, norms, 1); for (k = 0; k < l.size / 2; ++k){ axpy_cpu(w*h, l.alpha, squared + w*h*k, 1, norms, 1); } for (k = 1; k < l.c; ++k){ copy_cpu(w*h, norms + w*h*(k - 1), 1, norms + w*h*k, 1); int prev = k - ((l.size - 1) / 2) - 1; int next = k + (l.size / 2); if (prev >= 0) axpy_cpu(w*h, -l.alpha, squared + w*h*prev, 1, norms + w*h*k, 1); if (next < l.c) axpy_cpu(w*h, l.alpha, squared + w*h*next, 1, norms + w*h*k, 1); } } pow_cpu(w*h*c*l.batch, -l.beta, l.norms, 1, l.output, 1); mul_cpu(w*h*c*l.batch, net.input, 1, l.output, 1); } void backward_normalization_layer(const layer l, network net) { // TODO This is approximate ;-) // Also this should add in to delta instead of overwritting. int w = l.w; int h = l.h; int c = l.c; pow_cpu(w*h*c*l.batch, -l.beta, l.norms, 1, net.delta, 1); mul_cpu(w*h*c*l.batch, l.delta, 1, net.delta, 1); } #ifdef GPU void forward_normalization_layer_gpu(const layer layer, network net) { int k,b; int w = layer.w; int h = layer.h; int c = layer.c; scal_gpu(w*h*c*layer.batch, 0, layer.squared_gpu, 1); for(b = 0; b < layer.batch; ++b){ float *squared = layer.squared_gpu + w*h*c*b; float *norms = layer.norms_gpu + w*h*c*b; float *input = net.input_gpu + w*h*c*b; pow_gpu(w*h*c, 2, input, 1, squared, 1); const_gpu(w*h, layer.kappa, norms, 1); for(k = 0; k < layer.size/2; ++k){ axpy_gpu(w*h, layer.alpha, squared + w*h*k, 1, norms, 1); } for(k = 1; k < layer.c; ++k){ copy_gpu(w*h, norms + w*h*(k-1), 1, norms + w*h*k, 1); int prev = k - ((layer.size-1)/2) - 1; int next = k + (layer.size/2); if(prev >= 0) axpy_gpu(w*h, -layer.alpha, squared + w*h*prev, 1, norms + w*h*k, 1); if(next < layer.c) axpy_gpu(w*h, layer.alpha, squared + w*h*next, 1, norms + w*h*k, 1); } } pow_gpu(w*h*c*layer.batch, -layer.beta, layer.norms_gpu, 1, layer.output_gpu, 1); mul_gpu(w*h*c*layer.batch, net.input_gpu, 1, layer.output_gpu, 1); } void backward_normalization_layer_gpu(const layer layer, network net) { // TODO This is approximate ;-) int w = layer.w; int h = layer.h; int c = layer.c; pow_gpu(w*h*c*layer.batch, -layer.beta, layer.norms_gpu, 1, net.delta_gpu, 1); mul_gpu(w*h*c*layer.batch, layer.delta_gpu, 1, net.delta_gpu, 1); } #endif };