How to normalize a custom dataset

transforms.Normalize([0.5], [0.5]), as stated in the documentation, can be applied to Tensors only! Therefore you need to add another transform in your transforms.Compose() argument list: the ToTensor transform.

It would look like this:

transform = transforms.Compose([transforms.ToTensor,
                                transforms.Normalize([0.5], [0.5])])

Your code should have failed, because applying Normalize() on images does not work, but it hasn’t, since you never actually called the self.transform function on your image.

For this, you need to write the following, after loading the image (assuming that your pandas table contains the complete filepath for the images) in the __getitem__() function:

from PIL import Image
img_pil = Image.open(img)
img_normalized = self.transform(img_pil)