transforms.v2.Normalize method

username: dvdsosa
cooked: <p>Hi all,</p>
<p>I’m trying to reproduce the example listed here with no success <a href="https://pytorch.org/vision/main/auto_examples/transforms/plot_transforms_getting_started.html" rel="noopener nofollow ugc">Getting started with transforms v2</a></p>
<p>The problem is the way the transformed image appears. If I remove the transforms.Normalize line of the transforms.Compose (see code) then the transformed output looks good, but it does not when using it. I attached an image so you can see what I mean (left image no transform, right image using Normalize). Or is the shown image at the right the way it is supposed a normalized image should appear? In the example of the above link, it does look different.</p>
<p>Looking forward to hearing from you,<br>
David.</p>
<pre><code class="lang-auto">"""
Normalization test using torchvision
"""
import torchvision.transforms.v2 as transforms
import torch
from PIL import Image
import matplotlib.pyplot as plt


# Load the image
image = Image.open('../../gallery/assets/astronaut.jpg')

# Convert the image to a tensor
to_tensor = transforms.ToImage()
tensor_image = to_tensor(image)

normalize = transforms.Compose([
    transforms.RandomResizedCrop(size=(224, 224), antialias=True),
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.ToDtype(torch.float32, scale=True),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

normalized_image = normalize(tensor_image)

# Convert the tensors back to PIL Images for display
tensor_to_pil = transforms.ToPILImage()
pil_image = tensor_to_pil(tensor_image)
pil_normalized_image = tensor_to_pil(normalized_image)

# Display the images
fig, axs = plt.subplots(1, 2, figsize=(15, 5))

axs[0].imshow(pil_image)
axs[0].set_title('Original Image')

axs[1].imshow(pil_normalized_image)
axs[1].set_title('Normalized Image')

for ax in axs:
    ax.axis('off')

plt.show()
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/d/1d6293a49ce9ba47e40e51d309d5d2f0b9f33ffa.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1d6293a49ce9ba47e40e51d309d5d2f0b9f33ffa" title="test"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/d/1d6293a49ce9ba47e40e51d309d5d2f0b9f33ffa_2_690x426.jpeg" alt="test" data-base62-sha1="4bX2S1XKAtUVdpcbj7Fm9UO9IWC" width="690" height="426" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/d/1d6293a49ce9ba47e40e51d309d5d2f0b9f33ffa_2_690x426.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/1/d/1d6293a49ce9ba47e40e51d309d5d2f0b9f33ffa.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/d/1d6293a49ce9ba47e40e51d309d5d2f0b9f33ffa.jpeg 2x" data-dominant-color="B9AEA9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">test</span><span class="informations">787×487 116 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>This is expected since <code>matplotlib</code> will use the default colormap to visualize the normalized floating point numbers. “Unnormalizing” the image should show the original again.</p>

username: dvdsosa
cooked: <p>Thank you very much. I just found another answer from you on the same subject in a previous post <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"> <a href="https://discuss.pytorch.org/t/understanding-transform-normalize/21730/18">Understanding transform normalize </a></p>

username: ptrblck
cooked: <p>Great! I was sure I’ve answered it before but couldn’t find the post quickly <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: Snowy
cooked: <p>I am also facing a problem that is related to normalizing the data. If I don’t normalize the data before feeding it to the model, the output has the same color quality as the original one. However, with normalization, the output image is darker (after de-normalizing). I am attaching the images below.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/e/fe0bb1722ca413368f4709bb51c23361d4837e0d.png" alt="val_epoch_5_30_gt" data-base62-sha1="Afoksk5brH5N9rnHxdGKTVPeAS9" width="256" height="256">    <img src="https://discuss.pytorch.org/uploads/default/original/3X/d/d/dd70d6fa22a4798bc33260a649697c0aa954dab5.png" alt="val_epoch_5_30_second_out" data-base62-sha1="vAXhiA867gQnhImKdfDGmQfA8w5" width="256" height="256"></p>
<p>(the first one is the GT, and the second is the output image)</p>
<p>Without normalization the model performs poorly compared to the normalized one.</p>