Tensor (image) with negative values

username: ptorres
cooked: <p>Hello, I am using OpenCV capture to get frames and pass to PyTorch model.</p>
<p>currently i’m pre processing the frame in the following way:</p>
<pre><code class="lang-auto">data_transforms = transforms.Compose(
	[
		transforms.Resize(INPUT_SIZE),
		transforms.CenterCrop(INPUT_SIZE),
		transforms.ToTensor(),
		transforms.Normalize([0.485,0.456,0.406], [0.229, 0.224, 0.225])
	]
) 

def preprocess(image):
	image = PIL.Image.fromarray(image)                 
	image = data_transforms(image)
	image = image.float()
	image = image.unsqueeze(0) # Resnet50 model seems to only accepts 4d tensor
	
return image  

while (cap.isOpened()):
     ret, frame = cap.read()
     img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
     image_data = preprocess(img).to(DEVICE)
</code></pre>
<p>But im getting negative values when printing <code>image_data</code>:</p>
<pre><code class="lang-auto">tensor([[[[ 0.1426,  0.1426,  0.1426,  ...,  0.0227,  0.0227,  0.0227],
          [ 0.1426,  0.1426,  0.1426,  ...,  0.0227,  0.0227,  0.0227],
          [ 0.1426,  0.1426,  0.1426,  ...,  0.0227,  0.0227,  0.0227],
          ...,
          [-2.1179, -2.1179, -2.1179,  ..., -2.1179, -2.1179, -2.1179],
          [-2.1179, -2.1179, -2.1179,  ..., -2.1179, -2.1179, -2.1179],
          [-2.1179, -2.1179, -2.1179,  ..., -2.1179, -2.1179, -2.1179]]
</code></pre>
<p>The values of <code>image_data</code> should not be in [0-1] values? I’m missing something?</p>

username: Nikronic
cooked: <p>Hi,</p>
<p><code>transforms.ToTensor()</code> scales your input between <code>[0, 1]</code> but <code>transforms.Normalize(mean, std)</code> computes z-score for input images (normalize images) so it is expected to see negative values and even out of range <code>[-1, 1]</code> as you are using mean and std != 0.5.</p>
<aside class="quote quote-modified" data-post="1" data-topic="40022">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/puneet_sharma/48/10067_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/does-pytorch-automatically-normalizes-image-to-0-1/40022">Does Pytorch automatically Normalizes Image to (0,1)?</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I am new to Pytorch, I was just trying out some datasets. While using the torchvision.transforms.Normalize 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. 
A…
  </blockquote>
</aside>

<aside class="quote" data-post="1" data-topic="89234">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/manoel/48/26712_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/is-it-normal-that-after-normalize-the-values-arent-in-the-range-1-1/89234">Is it normal that after Normalize the values aren't in the range [-1, 1]?</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I’m trying to compute the mean and the std of my dataset. I found <a href="https://discuss.pytorch.org/t/computing-the-mean-and-std-of-dataset/34949/13">this topic</a> and implemented some ideas. However, when I applied the normalization on the dataset and checked the values. Then I found out that most values weren’t on the range[-1, 1], some greater than 1 and 2. 
Is it normal? Or should it be best just use the scaling that the ToTensor() does? 
I used the following transforms: Resize(250), CenterCrop(224), RandomHorizontalFlip(), ToTensor().
  </blockquote>
</aside>

<p>Bests</p>

username: ptorres
cooked: <p>Thank you <a class="mention" href="/u/nikronic">@Nikronic</a>!</p>
<p>So there is no problem? In the way im doing the inference will work as expected?</p>

username: Nikronic
cooked: <p>Actually, it has been experimentally shown that this helps the models to converge faster or achieve better result.<br>
At inference time, you need to use same mean and std you have used for training, then the final outputs depend on your network may need unnormalization with same mean and std to get back to original scale.</p>