Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we normalize the image to mean=0.5, std=0.5?

I was lookking for GAN code in Github. The code I found uses pytorch. In this code, we first normalized the image to mean = 0.5, std = 0.5. Normally, normalize to min = 0 and max = 1. Or normal distribution with mean = 0 and std = 1. Why is this normalized to mean = 0.5 and std = 0.5?

transformtransfo  = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
like image 874
marlo Avatar asked Feb 03 '23 22:02

marlo


2 Answers

The values of mean and std for transform.normalize are not the desired mean and std, but rather the values to subtract and divide by, i.e., the estimated mean and std.

In your example you subtract 0.5 and then divide by 0.5 yielding an image with mean zero and values in range [-1, 1]

like image 84
Shai Avatar answered Feb 06 '23 11:02

Shai


Adding to answer by Shai,

Similar to training other neural networks, we normalize GAN inputs to (1) improve convergence of the nets, and (2) giving an equal range of value to all features so to not make some features dominate others because of wide range of value.

Also, generator in most GAN models tend to use tanh function as final activation, which maps output to [-1, 1] value range. Scaling our image data to the same value range will give equal data for discriminator to be trained upon

like image 25
Muhamad Iqbal Avatar answered Feb 06 '23 12:02

Muhamad Iqbal