2
$\begingroup$

As I know it is recommended to normalize the input to a neural network ($\mu=0,\sigma=1$). Assuming I use a CNN that uses RGB-images as input, how do i have to normalize?

  • Over all the data of the complete batch (at once)?
  • Each image independent of each other?
  • For each $x$/$y$ pair (=pixel) over the batch?
    • For each color channel or all at once?

Thank you very much

-edit-

Or is it sufficient to use a BatchNormalization-Layer as the first layer in the neural network?

$\endgroup$
4
$\begingroup$

Usually, for RGB images you just subtract half the range and divide by the full range: $$x:=\frac{x-\frac{range}{2}}{range}$$ where $range=255$ in this case since each pixel is a value from 0 to 255. This will not result in having zero mean and unit variance but for most applications it is close enough and works well.

If this is not working for you, the correct way to scale the data to have zero mean and unit variance is to take each channel individually; calculate the mean and variance of all the pixels in all the images in that channel; then scale each channel individually as follows: $$x_R:=\frac{x_R-\mu_R}{\sigma_R} \;\; x_G:=\frac{x_G-\mu_G}{\sigma_G} \;\; x_B:=\frac{x_B-\mu_B}{\sigma_B}$$

Each pixel in a channel is the same feature, just in a different position in space, this is especially true when using a CNN.

Note: If $\sigma$ is too small for one or more of the channels you can cap it at minimum value to avoid numerical stability issues e.g. $\sigma= max(\sigma, 0.0001)$

$\endgroup$
  • $\begingroup$ Thanks for the great answer:) But why is $range=256$ and not $255$? $255$ seems for me to be the more natural choice, because then the values are distributed in $[0,1]$? $\endgroup$ – Kevin Meier Oct 9 '17 at 10:09
  • 1
    $\begingroup$ You are right it should be 255, I will correct the answer $\endgroup$ – Miguel Oct 9 '17 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.