00001 #include "batchnorm_layer.h" 00002 #include "blas.h" 00003 #include <stdio.h> 00004 00005 layer make_batchnorm_layer(int batch, int w, int h, int c) 00006 { 00007 fprintf(stderr, "Batch Normalization Layer: %d x %d x %d image\n", w,h,c); 00008 layer layer = {0}; 00009 layer.type = BATCHNORM; 00010 layer.batch = batch; 00011 layer.h = layer.out_h = h; 00012 layer.w = layer.out_w = w; 00013 layer.c = layer.out_c = c; 00014 layer.output = calloc(h * w * c * batch, sizeof(float)); 00015 layer.delta = calloc(h * w * c * batch, sizeof(float)); 00016 layer.inputs = w*h*c; 00017 layer.outputs = layer.inputs; 00018 00019 layer.scales = calloc(c, sizeof(float)); 00020 layer.scale_updates = calloc(c, sizeof(float)); 00021 int i; 00022 for(i = 0; i < c; ++i){ 00023 layer.scales[i] = 1; 00024 } 00025 00026 layer.mean = calloc(c, sizeof(float)); 00027 layer.variance = calloc(c, sizeof(float)); 00028 00029 layer.rolling_mean = calloc(c, sizeof(float)); 00030 layer.rolling_variance = calloc(c, sizeof(float)); 00031 00032 layer.forward = forward_batchnorm_layer; 00033 layer.backward = backward_batchnorm_layer; 00034 #ifdef GPU 00035 layer.forward_gpu = forward_batchnorm_layer_gpu; 00036 layer.backward_gpu = backward_batchnorm_layer_gpu; 00037 00038 layer.output_gpu = cuda_make_array(layer.output, h * w * c * batch); 00039 layer.delta_gpu = cuda_make_array(layer.delta, h * w * c * batch); 00040 00041 layer.scales_gpu = cuda_make_array(layer.scales, c); 00042 layer.scale_updates_gpu = cuda_make_array(layer.scale_updates, c); 00043 00044 layer.mean_gpu = cuda_make_array(layer.mean, c); 00045 layer.variance_gpu = cuda_make_array(layer.variance, c); 00046 00047 layer.rolling_mean_gpu = cuda_make_array(layer.mean, c); 00048 layer.rolling_variance_gpu = cuda_make_array(layer.variance, c); 00049 00050 layer.mean_delta_gpu = cuda_make_array(layer.mean, c); 00051 layer.variance_delta_gpu = cuda_make_array(layer.variance, c); 00052 00053 layer.x_gpu = cuda_make_array(layer.output, layer.batch*layer.outputs); 00054 layer.x_norm_gpu = cuda_make_array(layer.output, layer.batch*layer.outputs); 00055 #endif 00056 return layer; 00057 } 00058 00059 void backward_scale_cpu(float *x_norm, float *delta, int batch, int n, int size, float *scale_updates) 00060 { 00061 int i,b,f; 00062 for(f = 0; f < n; ++f){ 00063 float sum = 0; 00064 for(b = 0; b < batch; ++b){ 00065 for(i = 0; i < size; ++i){ 00066 int index = i + size*(f + n*b); 00067 sum += delta[index] * x_norm[index]; 00068 } 00069 } 00070 scale_updates[f] += sum; 00071 } 00072 } 00073 00074 void mean_delta_cpu(float *delta, float *variance, int batch, int filters, int spatial, float *mean_delta) 00075 { 00076 00077 int i,j,k; 00078 for(i = 0; i < filters; ++i){ 00079 mean_delta[i] = 0; 00080 for (j = 0; j < batch; ++j) { 00081 for (k = 0; k < spatial; ++k) { 00082 int index = j*filters*spatial + i*spatial + k; 00083 mean_delta[i] += delta[index]; 00084 } 00085 } 00086 mean_delta[i] *= (-1./sqrt(variance[i] + .00001f)); 00087 } 00088 } 00089 void variance_delta_cpu(float *x, float *delta, float *mean, float *variance, int batch, int filters, int spatial, float *variance_delta) 00090 { 00091 00092 int i,j,k; 00093 for(i = 0; i < filters; ++i){ 00094 variance_delta[i] = 0; 00095 for(j = 0; j < batch; ++j){ 00096 for(k = 0; k < spatial; ++k){ 00097 int index = j*filters*spatial + i*spatial + k; 00098 variance_delta[i] += delta[index]*(x[index] - mean[i]); 00099 } 00100 } 00101 variance_delta[i] *= -.5 * pow(variance[i] + .00001f, (float)(-3./2.)); 00102 } 00103 } 00104 void normalize_delta_cpu(float *x, float *mean, float *variance, float *mean_delta, float *variance_delta, int batch, int filters, int spatial, float *delta) 00105 { 00106 int f, j, k; 00107 for(j = 0; j < batch; ++j){ 00108 for(f = 0; f < filters; ++f){ 00109 for(k = 0; k < spatial; ++k){ 00110 int index = j*filters*spatial + f*spatial + k; 00111 delta[index] = delta[index] * 1./(sqrt(variance[f]) + .00001f) + variance_delta[f] * 2. * (x[index] - mean[f]) / (spatial * batch) + mean_delta[f]/(spatial*batch); 00112 } 00113 } 00114 } 00115 } 00116 00117 void resize_batchnorm_layer(layer *layer, int w, int h) 00118 { 00119 fprintf(stderr, "Not implemented\n"); 00120 } 00121 00122 void forward_batchnorm_layer(layer l, network_state state) 00123 { 00124 if(l.type == BATCHNORM) copy_cpu(l.outputs*l.batch, state.input, 1, l.output, 1); 00125 if(l.type == CONNECTED){ 00126 l.out_c = l.outputs; 00127 l.out_h = l.out_w = 1; 00128 } 00129 if(state.train){ 00130 mean_cpu(l.output, l.batch, l.out_c, l.out_h*l.out_w, l.mean); 00131 variance_cpu(l.output, l.mean, l.batch, l.out_c, l.out_h*l.out_w, l.variance); 00132 00133 scal_cpu(l.out_c, .99, l.rolling_mean, 1); 00134 axpy_cpu(l.out_c, .01, l.mean, 1, l.rolling_mean, 1); 00135 scal_cpu(l.out_c, .99, l.rolling_variance, 1); 00136 axpy_cpu(l.out_c, .01, l.variance, 1, l.rolling_variance, 1); 00137 00138 copy_cpu(l.outputs*l.batch, l.output, 1, l.x, 1); 00139 normalize_cpu(l.output, l.mean, l.variance, l.batch, l.out_c, l.out_h*l.out_w); 00140 copy_cpu(l.outputs*l.batch, l.output, 1, l.x_norm, 1); 00141 } else { 00142 normalize_cpu(l.output, l.rolling_mean, l.rolling_variance, l.batch, l.out_c, l.out_h*l.out_w); 00143 } 00144 scale_bias(l.output, l.scales, l.batch, l.out_c, l.out_h*l.out_w); 00145 } 00146 00147 void backward_batchnorm_layer(const layer l, network_state state) 00148 { 00149 backward_scale_cpu(l.x_norm, l.delta, l.batch, l.out_c, l.out_w*l.out_h, l.scale_updates); 00150 00151 scale_bias(l.delta, l.scales, l.batch, l.out_c, l.out_h*l.out_w); 00152 00153 mean_delta_cpu(l.delta, l.variance, l.batch, l.out_c, l.out_w*l.out_h, l.mean_delta); 00154 variance_delta_cpu(l.x, l.delta, l.mean, l.variance, l.batch, l.out_c, l.out_w*l.out_h, l.variance_delta); 00155 normalize_delta_cpu(l.x, l.mean, l.variance, l.mean_delta, l.variance_delta, l.batch, l.out_c, l.out_w*l.out_h, l.delta); 00156 if(l.type == BATCHNORM) copy_cpu(l.outputs*l.batch, l.delta, 1, state.delta, 1); 00157 } 00158 00159 #ifdef GPU 00160 00161 void pull_batchnorm_layer(layer l) 00162 { 00163 cuda_pull_array(l.scales_gpu, l.scales, l.c); 00164 cuda_pull_array(l.rolling_mean_gpu, l.rolling_mean, l.c); 00165 cuda_pull_array(l.rolling_variance_gpu, l.rolling_variance, l.c); 00166 } 00167 void push_batchnorm_layer(layer l) 00168 { 00169 cuda_push_array(l.scales_gpu, l.scales, l.c); 00170 cuda_push_array(l.rolling_mean_gpu, l.rolling_mean, l.c); 00171 cuda_push_array(l.rolling_variance_gpu, l.rolling_variance, l.c); 00172 } 00173 00174 void forward_batchnorm_layer_gpu(layer l, network_state state) 00175 { 00176 if(l.type == BATCHNORM) copy_ongpu(l.outputs*l.batch, state.input, 1, l.output_gpu, 1); 00177 if(l.type == CONNECTED){ 00178 l.out_c = l.outputs; 00179 l.out_h = l.out_w = 1; 00180 } 00181 if (state.train) { 00182 fast_mean_gpu(l.output_gpu, l.batch, l.out_c, l.out_h*l.out_w, l.mean_gpu); 00183 fast_variance_gpu(l.output_gpu, l.mean_gpu, l.batch, l.out_c, l.out_h*l.out_w, l.variance_gpu); 00184 00185 scal_ongpu(l.out_c, .99, l.rolling_mean_gpu, 1); 00186 axpy_ongpu(l.out_c, .01, l.mean_gpu, 1, l.rolling_mean_gpu, 1); 00187 scal_ongpu(l.out_c, .99, l.rolling_variance_gpu, 1); 00188 axpy_ongpu(l.out_c, .01, l.variance_gpu, 1, l.rolling_variance_gpu, 1); 00189 00190 copy_ongpu(l.outputs*l.batch, l.output_gpu, 1, l.x_gpu, 1); 00191 normalize_gpu(l.output_gpu, l.mean_gpu, l.variance_gpu, l.batch, l.out_c, l.out_h*l.out_w); 00192 copy_ongpu(l.outputs*l.batch, l.output_gpu, 1, l.x_norm_gpu, 1); 00193 } else { 00194 normalize_gpu(l.output_gpu, l.rolling_mean_gpu, l.rolling_variance_gpu, l.batch, l.out_c, l.out_h*l.out_w); 00195 } 00196 00197 scale_bias_gpu(l.output_gpu, l.scales_gpu, l.batch, l.out_c, l.out_h*l.out_w); 00198 } 00199 00200 void backward_batchnorm_layer_gpu(const layer l, network_state state) 00201 { 00202 backward_scale_gpu(l.x_norm_gpu, l.delta_gpu, l.batch, l.out_c, l.out_w*l.out_h, l.scale_updates_gpu); 00203 00204 scale_bias_gpu(l.delta_gpu, l.scales_gpu, l.batch, l.out_c, l.out_h*l.out_w); 00205 00206 fast_mean_delta_gpu(l.delta_gpu, l.variance_gpu, l.batch, l.out_c, l.out_w*l.out_h, l.mean_delta_gpu); 00207 fast_variance_delta_gpu(l.x_gpu, l.delta_gpu, l.mean_gpu, l.variance_gpu, l.batch, l.out_c, l.out_w*l.out_h, l.variance_delta_gpu); 00208 normalize_delta_gpu(l.x_gpu, l.mean_gpu, l.variance_gpu, l.mean_delta_gpu, l.variance_delta_gpu, l.batch, l.out_c, l.out_w*l.out_h, l.delta_gpu); 00209 if(l.type == BATCHNORM) copy_ongpu(l.outputs*l.batch, l.delta_gpu, 1, state.delta, 1); 00210 } 00211 #endif