Normalizing images between 0 and 1 with ToTensor() doesn't work

Hi everyone,

I’m trying to train a segmentation network and I would like to normalize images between 0 and 1. However the ToTensor transform I use outputs very low pixel values. Here’s a code sample

img_transforms = transforms.Compose([transforms.Grayscale(num_output_channels=1),
                                     transforms.Resize((100,100), interpolation=2),
                                     transforms.ToTensor()])
img = Image.open("myimg.png")
img = np.array(img).astype(float)
img*= (255.0/img.max())
img = img.astype(np.uint8)
print(img.max())
img = Image.fromarray(img)
img = img_transforms(img)
print(img.max())

And the given output:

255
tensor(0.1137)

I don’t understand why I don’t get 1 as a max value since the ToTensor function is supposed to output values between 0 and 1.

Could anyone shed some light on what may be occurring?

Thanks

The ToTensor transformation will normalize the uint8 input image by dividing by 255 as seen here.
Based on your code, I guess that the Resize operation might lower the max. values of 255 to a smaller one (although I wouldn’t expect to see such a decrease). Could you try to use PIL.Image.NEAREST as the interpolation method and recheck the code?

Thank you @ptrblck, the resize transform was not the issue here but I figured out it was actually due to the fact that my images are mainly blue and that the Grayscale transform multiply blue values by 0.114. I solved the problem by manually setting the max value to 1. I still don’t know why the ToTensor() function didn’t normalize the values between 0 and 1 after the Grayscal transform though.

Here is my solution:

img = Image.open("myimg.png")
img = img_transforms(img)
img*= (1.0/img.max())