- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The **Normalize()** transform normalizes an image with mean and standard deviation. The **torchvision.transforms** module provides many important transforms that can be used to perform different types of manipulations on the image data.

**Normalize()** accepts only tensor images of any size. A tensor image is a torch tensor. A tensor image may have n number of channels. The **Normalize()** transform normalizes the tensor image for each channel.

As this transform supports only tensor image, the PIL images should be first converted to a torch tensor. And after applying **Normalize()** transform, we convert the normalized torch tensor to a PIL image.

We could use the following steps to normalize an image with mean and standard deviation −

Import the required libraries. In all the following examples, the required Python libraries are

**torch, Pillow,**and**torchvision**. Make sure you have already installed them.

import torch import torchvision import torchvision.transforms as T from PIL import Image

Read the input image. The input image is a PIL image or a torch tensor. If the input image is PIL image, convert it to a torch tensor.

img = Image.open('sunset.jpg') # convert image to torch tensor imgTensor = T.ToTensor()(img)

Define a transform to normalize the image with mean and standard deviation. Here, we use mean and std of the ImageNet dataset.

transform = T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))

Apply the above-defined transform on the input image to normalize the image.

normalized_imgTensor = transform(imgTensor)

Convert the normalized tensor image to PIL image.

normalized_img = T.ToPILImage()(normalized_imgTensor)

Show the normalized image.

normalized _img.show()

This image is used as the input file in all the following examples.

The following Python program normalizes the input image to mean and standard deviation. We use the mean and standard deviation of ImageNet dataset.

# import required libraries import torch import torchvision.transforms as T from PIL import Image # Read the input image img = Image.open('sunset.jpg') # convert image to torch tensor imgTensor = T.ToTensor()(img) # define a transform to normalize the tensor transform = T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)) # normalize the converted tensor using above defined transform normalized_imgTensor = transform(imgTensor) # convert the normalized tensor to PIL image normalized_img = T.ToPILImage()(normalized_imgTensor) # display the normalized PIL image normalized_img.show()

It will produce the following output −

In this example, we define a **Compose transform** to perform three transformations.

Convert the PIL image to tensor image.

Normalize the tensor image.

Convert the normalized image tensor to PIL image.

# import required libraries import torch import torchvision.transforms as T from PIL import Image # read the input image img = Image.open('sunset.jpg') # define a transform to: # convert the PIL image to tensor # normalize the tensor # convert the tensor to PIL image transform = T.Compose([ T.ToTensor(), T.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), T.ToPILImage()]) # apply the above tensor on input image img = transform(img) img.show()

It will produce the following output −

- Related Questions & Answers
- How to compute the mean and standard deviation of a tensor in PyTorch?
- Plot mean and standard deviation in Matplotlib
- How to create boxplot using mean and standard deviation in R?
- How to find mean and standard deviation from frequency table in R?
- PyTorch – How to convert an image to grayscale?
- How to create a line chart with mean and standard deviation using ggplot2 in R?
- PyTorch – How to rotate an image by an angle?
- Variance and Standard Deviation
- Python – Mean deviation of Elements
- How to normalize a tensor in PyTorch?
- PyTorch – How to resize an image to a given size?
- PyTorch – How to perform random affine transformation of an image?
- PyTorch – How to crop an image at a random location?
- Absolute Deviation and Absolute Mean Deviation using NumPy
- How to find mean across the image channels in PyTorch?

Advertisements