Speed-up inference with Batch Normalization Folding
How to remove the batch normalization layer to make your neural networks faster.
It consists of 2 steps:
- Normalize the batch by first subtracting its mean $\mu$, then dividing it by its standard deviation $\sigma$.
- Further scale by a factor $\gamma$ and shift by a factor $\beta$. 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 $\gamma$ and $\beta$, but also $\mu$ and $\sigma$, 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:
$$ \Large \begin{aligned} z &=W * x+b \\ \text { out } &=\gamma \cdot \frac{z-\mu}{\sqrt{\sigma^{2}+\epsilon}}+\beta \end{aligned} $$So, if we re-arrange the $W$ and $b$ of the convolution to take the parameters of the batch normalization into account, as such:
$$ \Large \begin{aligned} w_{\text {fold }} &=\gamma \cdot \frac{W}{\sqrt{\sigma^{2}+\epsilon}} \\ b_{\text {fold }} &=\gamma \cdot \frac{b-\mu}{\sqrt{\sigma^{2}+\epsilon}}+\beta \end{aligned} $$We can remove the batch normalization layer and still have the same results!
Then show its number of parameters:
We can get the initial inference time by using the %%timeit
magic command:
%%timeit
model(x[0][None].cuda())
So now if we apply batch normalization folding, we have:
And:
%%timeit
folded_model(x[0][None].cuda())
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:
folded_learner.validate()
Let’s see how it behaves in the case of Resnet50!
The initial amount of parameters is:
And inference time is:
%%timeit
model(x[0][None].cuda())
After using batch normalization folding, we have:
And:
%%timeit
final_model(x[0][None].cuda())
So now, we have 26,560 parameters removed and even more impressive, an inference time reduce by 1.7ms! And still without any drop in performance.
final_learner.validate()
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.
Code available at this address!