Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While converting a PIL image into a tensor why the pixels are changing?

transform = transforms.Compose([transforms.ToPILImage(), transforms.ToTensor()])

Before applying the transformation

Before applying the transformation

After applying the transformation

After applying the transformation

Q.1 Why the pixel values are changed?
Q.2 How to correct this?

like image 788
atin Avatar asked Apr 21 '20 19:04

atin


People also ask

How to convert a PIL image to a tensor in Python?

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.

How to convert image to tensor in Q2?

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.

What is the difference between image tensor in PyTorch and TensorFlow?

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.

How do you convert pixels to colors without a palette?

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)


2 Answers

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.

like image 187
Noé Achache Avatar answered Sep 21 '22 04:09

Noé Achache


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.

like image 41
atin Avatar answered Sep 22 '22 04:09

atin