Speed-up inference with Batch Normalization Folding

How to remove the batch normalization layer to make your neural networks faster.

Nathan Hubens
Towards Data Science

--

Introduction

Batch Normalization is a technique which takes care of normalizing the input of each layer to make the training process faster and more stable. In practice, it is an extra layer that we generally add after the computation layer and before the non-linearity.

It consists of 2 steps:

  • Normalize the batch by first subtracting its mean μ, then dividing it by its standard deviation σ.
  • Further scale by a factor γ and shift by a factor β. Those are the parameters of the batch normalization layer, required in case of the network not needing the data to have a mean of 0 and a standard deviation of 1.

Due to its efficiency for training neural networks, batch normalization is now widely used. But how useful is it at inference time?

Once the training has ended, each batch normalization layer possesses a specific set of γ and β, but also μ and σ, the latter being computed using an exponentially weighted average during training. It means that during inference, the batch normalization acts as a simple linear transformation of what comes out of the previous layer, often a convolution.

As a convolution is also a linear transformation, it also means that both operations can be merged into a single linear transformation!

This would remove some unnecessary parameters but also reduce the number of operations to be performed at inference time.

How to do that in practice?

With a little bit of math, we can easily rearrange the terms of the convolution to take the batch normalization into account.

As a little reminder, the convolution operation followed by the batch normalization operation can be expressed, for an input x, as:

So, if we re-arrange the W and b of the convolution to take the parameters of the batch normalization into account, as such:

We can remove the batch normalization layer and still have the same results!

Note: Usually, you don’t have a bias in a layer preceding a batch normalization layer. It is useless and a waste of parameters as any constant will be canceled out by the batch normalization.

How efficient is it?

We will try for 2 common architectures:

  • VGG16 with batch norm
  • ResNet50

Just for the demonstration, we will use ImageNette dataset and PyTorch. Both networks will be trained for 5 epochs and what changes in terms of parameter number and inference time.

1. VGG16

Let’s start by training VGG16 for 5 epochs (the final accuracy doesn’t matter):

Then show its number of parameters:

The initial inference time for a single image is:

So now if we apply batch normalization folding, we have:

And:

So 8448 parameters removed and even better, almost 0.4 ms faster inference! Most importantly, this is completely lossless, there is absolutely no change in terms of performance:

Let’s see how it behaves in the case of Resnet50!

2. Resnet50

Same, we start by training it for 5 epochs:

The initial amount of parameters is:

And inference time is:

After using batch normalization folding, we have:

And:

So now, we have 26,560 parameters removed and even more impressive, an inference time reduce by 1.5ms! And still without any drop in performance.

So if we can reduce the inference time and the number of parameters of our models without enduring any drop in performance, why shouldn’t we always do it?

I hope that this blog post helped you! Feel free to give me feedback or ask me questions is something is not clear enough.

--

--