Torchvision 0.2.1 transforms.Normalize does not work as expected

I am trying a new code using Pytorch. In this code, to load the dataset (CIFAR10), I am using torchvision’s datasets. I define two transform functions ToTensor() and Normalize(). After normalize I expect the data in the dataset should be between 0 and 1. But the max value is still 255. I also inserted a print statement inside the ‘call’ function of Normalize class in transforms.py (Lib\site-packages\torchvision\transforms\transforms.py). This print is not printed while running the code too. Not sure what is happening. Every page I visited in the internet, mentions the usage almost the same way as I do. For example some sites I visited https://github.com/adventuresinML/adventures-in-ml-code/blob/master/pytorch_nn.py https://github.com/pytorch/tutorials/blob/master/beginner_source/blitz/cifar10_tutorial.py

My code is given below. This reads the dataset with and without Normalize, then prints some stats. The min and max printed is an indicator of whether the data is normalized or not.


import torchvision as tv
import numpy as np

dataDir = ‘D:\general\ML_DL\DLCourse_VipulVaibhav\datasets\CIFAR’

trainTransform = tv.transforms.Compose([tv.transforms.ToTensor()])
trainSet = tv.datasets.CIFAR10(dataDir, train=True, download=False, transform=trainTransform)
print (trainSet.train_data.mean(axis=(0,1,2))/255)
print (trainSet.train_data.min())
print (trainSet.train_data.max())
print (trainSet.train_data.shape)

trainTransform = tv.transforms.Compose([tv.transforms.ToTensor(), tv.transforms.Normalize((0.4914, 0.4822, 0.4466), (0.247, 0.243, 0.261))])
trainSet = tv.datasets.CIFAR10(dataDir, train=True, download=False, transform=trainTransform)
print (trainSet.train_data.mean(axis=(0,1,2))/255)
print (trainSet.train_data.min())
print (trainSet.train_data.max())
print (trainSet.train_data.shape)

The output looks like,

[ 0.49139968  0.48215841  0.44653091]
0
255
(50000, 32, 32, 3)
[ 0.49139968  0.48215841  0.44653091]
0
255
(50000, 32, 32, 3)

Please help me understand this better. As most of the functions I tried, ends up with similar results - for example Grayscale, CenterCrop too.

Try to print the stats of an instance, e.g. print(trainSet[0].min()), since the transformation will be applied in __getitem__ on the fly.

Thanks ptrblck. I got an answer in the similar lines from stackoverflow here and I modified the code. It works.
Modified code, its output and some description is available at https://stackoverflow.com/questions/53332663/torchvision-0-2-1-transforms-normalize-does-not-work-as-expected/53337082#53337082