AlexeyAB
2018-04-03 1b2c70f82a9ec88c60bb277f9f5f0073c96691e7
src/network.c
@@ -748,3 +748,38 @@
   free(net.workspace);
#endif
}
void fuse_conv_batchnorm(network net)
{
   int j;
   for (j = 0; j < net.n; ++j) {
      layer *l = &net.layers[j];
      if (l->type == CONVOLUTIONAL) {
         printf(" Fuse Convolutional layer \t\t l->size = %d  \n", l->size);
         if (l->batch_normalize) {
            int f;
            for (f = 0; f < l->n; ++f)
            {
               l->biases[f] = l->biases[f] - l->scales[f] * l->rolling_mean[f] / (sqrtf(l->rolling_variance[f]) + .000001f);
               const size_t filter_size = l->size*l->size*l->c;
               int i;
               for (i = 0; i < filter_size; ++i) {
                  int w_index = f*filter_size + i;
                  l->weights[w_index] = l->weights[w_index] * l->scales[f] / (sqrtf(l->rolling_variance[f]) + .000001f);
               }
            }
            l->batch_normalize = 0;
            push_convolutional_layer(*l);
         }
      }
      else {
         printf(" Skip layer: %d \n", l->type);
      }
   }
}