transoforms.Normalize grayscale image

Hi,

I normalize grayscale image by the following code. however, I got the following error:

ValueError: Expected tensor to be a tensor image of size (..., C, H, W). Got tensor.size() = torch.Size([64, 64])

image = cv2.imread(path, -1)
image = torch.from_numpy(image).type(torch.float)
image = transforms.Normalize([0.5, ], [0.5, ])(image)

Your image seems to be missing the channel dimension as given in the error message:

image = torch.randn(64, 64)
image = transforms.Normalize([0.5, ], [0.5, ])(image)
# ValueError: Expected tensor to be a tensor image of size (..., C, H, W). Got tensor.size() = torch.Size([64, 64])

image = torch.randn(1, 64, 64)
image = transforms.Normalize([0.5, ], [0.5, ])(image) # works

so unsqueeze it before passing the input to the transformation.

@ptrblck
thank you!
unsqueeze really works.

@ptrblck
I got one more weird issue:

# load binary image 64X64 with 0 and 255 value in the image.
self.data_transforms = transforms.Compose([
                transforms.Resize((28, 28)),
                transforms.Normalize([0.485,], [0.229])
            ])
image= torch.unsqueeze(torch.from_numpy(image).type(torch.float), 0)
image = self.data_transforms(image)

after the above code, the normalized data in image is very big, like:
[[[ 458.0684, 713.7275, 713.7274, 713.7275, 713.7275, 1111.4192, 969.3862, -2.1179, -2.1179, -2.1179, -2.1179, -2.1179, 742.1346, 1111.4192, 1111.4192, 1111.4192, 1111.4192, 1111.4192, 1111.4192, 367.1666, -2.1179, -2.1179, -2.1179, -2.1179, 554.6549, 1111.4192, 1111.4192, 1111.4192], ......

Based the data normalized by the above code, the training accuracy keeps 10% unchanged during the training.

If I remove the transforms.Normalize, the training accuracy could be 98.50%.

The above 2 experiments prove that my code about how to use transforms.Normalize is not correct.

I would like to make it work, which may make the accuracy better.

The used stats in Normalize assume the input tensor has values in the range [0, 1], which doesn’t seem to be the case.
Check the min and max values of image before passing it to self.data_transform and make sure the Normalize stats fit its range.