transform = transforms.Compose([transforms.ToPILImage(), transforms.ToTensor()])
Before applying the transformation
After applying the transformation
Q.1 Why the pixel values are changed?
Q.2 How to correct this?
The numpy.ndarray must be in [H, W, C] format, where H, W, and C are the height, width, and a number of channels of the image. This transform converts a PIL image to a tensor of data type torch.uint8 in the range between 0 and 255.
Q2: use torch.tensor (input_image) to convert image into a tensor instead. Show activity on this post. I was able to solve this problem by normalizing the input data before transforming it.
On the other hand, the shape for image tensor in Pytorch is slightly different from Tensorflow tensor. It is based on the following torch.Size instead: There are multiple ways to load image data and one of it is as follows: Unlike Tensorflow which uses the term expand dimension to add a new dimension, Pytorch is based on squeeze and unsqueeze.
For the “P” mode, this method translates pixels through the palette. If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette. Syntax: Image.convert (mode=None, matrix=None, dither=None, palette=0, colors=256)
Q1: transforms.ToTensor() of torchvision normalizes your input image, i.e. puts it in the range [0,1] as it is a very common preprocessing step.
Q2: use torch.tensor(input_image)
to convert image into a tensor instead.
I was able to solve this problem by normalizing the input data before transforming it.
The problem was that ToPILImage()
was discarding all the values which were greater than 1 hence the bright pixels became dark.
Here is a link to the working code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With