Does Pytorch automatically Normalizes Image to (0,1)?

Puneet_Sharma: <p>I am new to Pytorch, I was just trying out some datasets. While using the <code>torchvision.transforms.Normalize</code> I noted that most of the example out there were using 0.5 as mean and std to normalize the images in range (-1,1) but this will only work if our image data is already in (0,1) form and when i tried out normalizing my data (using mean and std as 0.5) by myself, my data was converted to range (-1,1), this means that when I loaded the data it was converted into (0,1) some where in the code.<br>
Am I right that Pytorch automatically Normalizes Image to (0,1) when we load it and if yes which line of code is doing this?</p>
<pre><code class="lang-auto">import torch
from torchvision import transforms, datasets
Transformations = transforms.Compose([transforms.RandomSizedCrop(224), transforms.ToTensor(), 
                                      transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])
                                      ])
image_data = datasets.ImageFolder('C:/users/sharm/OneDrive/Desktop/Mnist/dogImages/train', transform = Transformations)
data_loader = torch.utils.data.DataLoader(image_data, batch_size = 50, shuffle = True)
iteration = iter(data_loader)
images, labels = iteration.next()
print (images)
</code></pre>
<p>Thank you</p>

Nikronic: <p>Hello<br>
Pytorch default <a href="https://github.com/pytorch/vision" rel="nofollow noopener"> backend</a> for images are Pillow, and when you use <a href="https://pytorch.org/docs/master/torchvision/transforms.html#torchvision.transforms.ToTensor" rel="nofollow noopener"><code>ToTensor()</code></a> class, PyTorch automatically converts all images into <code>[0,1]</code>.</p>
<p>Here is the <a href="https://github.com/pytorch/vision/blob/71322cba652b4ba7bcaa7ae5ee86f539d1ae3a2b/torchvision/transforms/functional.py#L38" rel="nofollow noopener">source</a>.</p>
<p>regards</p>

itskeshav: <p>I don’t think so that it automatically converts all image into [0,1]<br>
have a look :-:</p>
<p>import numpy as np<br>
from torchvision import transforms<br>
a = np.full((3,3), 255)<br>
print(a)<br>
x = transforms.ToTensor()<br>
y = x(a)<br>
print(y)</p>
<p>[[255 255 255]<br>
[255 255 255]<br>
[255 255 255]]<br>
tensor([[[255, 255, 255],<br>
[255, 255, 255],<br>
[255, 255, 255]]])</p>

Nikronic: <p>Hi,</p>
<p>The issue is that numpy image is a byte/uint8 array and that is why there is conversion to ByteTensor in the source code I referenced.<br>
The way you initialized your array, it’s <code>int64</code> dtype which is not image in the definitions of numpy or PIL.</p>
<p>To get desired result, you can convert <code>a</code> using <code>a = a.astype(np.uint8)</code>.</p>
<p>Bests</p>

btnbrd: <p>Hello,</p>
<p>Is it possible to scale image to [0, 1] after transforms.Normalize ?</p>

ptrblck: <p>You could replace <code>Normalize</code> with a custom scaling dividing the data by its max. value or just use <code>ToTensor</code> which will already create tensors in this range.</p>

lucamar: <p>Updated <a href="https://pytorch.org/vision/main/generated/torchvision.transforms.ToTensor.html" rel="noopener nofollow ugc"><code>ToTensor()</code></a> link</p>