Torchvision - normalization abnormal values

I have a rather simple question. I found that torchvision’s normalization is not producing expected results. After normalization, I expect the max and minimum values to be in the range of -1 and 1 but apparently it is not the case. Am I doing anything wrong? Thank you very much for your help in advance

   transforms =  transforms.Compose([transforms.ToTensor(),  transforms.Normalize(mean=[0.04717693328857422], std=[0.12218446918562346])])
   print(torch.max(image_array_copy))   #tensor(176)
   print(torch.min(image_array_copy))   #tensor(0)
   image_array_copy = np.expand_dims(image_array_copy, axis=2)
   image_array_copy = image_array_copy.reshape(image_array_copy.shape[-1], image_array_copy.shape[0], image_array_copy.shape[1])
   #Image shape now: (1, 384, 384)
   image_array_copy = transforms(image_array_copy)
   print(torch.max(image_array_copy)) #tensor(176.)
   print(torch.min(image_array_copy))  #tensor(-0.3861)

It should not necessarily be within [-1, 1]. We are centering by the data by their mean mean=[0.04717693328857422] and dividing them by their standard-deviation (std=[0.12218446918562346]). So given that the input values of images are within [0, 1], after this mean-centering and standardization, they will end-up in the interval: (0-0.047)/0.122=-0.386 and (1-0.047)/0.122=7.798.

If you want to make your outputs be within [-1, 1], then you can normalize by mean=0.5, and std=0.5:

transforms =  transforms.Compose([transforms.ToTensor(),  
                                  transforms.Normalize(mean=[0.5], std=[0.5])])
1 Like