How to normalize an image using pytorch

This recipe helps you normalize an image using pytorch

Recipe Objective

How to normalize an image using pytorch?

This is achieved by using transforms.functional package in which for normalization we have to use the .normalize method in which we have to define the values of mean and standard deviation after that it will retuned a normalized image.For normalized image we have to convert our original image into tensor and then back to pil image for output. Lets understand this with practical implementation.

PyTorch vs Tensorflow - Which One Should You Choose For Your Next Deep Learning Project ?

Step 1 - Import library

import torch
import torchvision.transforms.functional as fn
from PIL import Image

Step 2 - Load the Image

img = Image.open("/content/Pytorch_Exercise_47_normalize_image.jpg")
print("This is the size of image:",img.size, "\n")
img

This is the size of image: (1200, 602)

Step 3 - Convert to tensor

tensor_image = fn.to_tensor(img)
tensor_image

tensor([[[0.6039, 0.6039, 0.6039,  ..., 0.6588, 0.6588, 0.6588],
         [0.6039, 0.6039, 0.6039,  ..., 0.6588, 0.6588, 0.6588],
         [0.6039, 0.6039, 0.6000,  ..., 0.6588, 0.6588, 0.6588],
         ...,
         [0.4196, 0.4353, 0.4471,  ..., 0.4588, 0.4588, 0.4588],
         [0.4275, 0.4235, 0.4275,  ..., 0.4431, 0.4549, 0.4706],
         [0.4275, 0.4235, 0.4314,  ..., 0.4431, 0.4510, 0.4667]],

        [[0.6667, 0.6667, 0.6667,  ..., 0.6824, 0.6824, 0.6824],
         [0.6667, 0.6667, 0.6667,  ..., 0.6824, 0.6824, 0.6824],
         [0.6667, 0.6667, 0.6627,  ..., 0.6824, 0.6824, 0.6824],
         ...,
         [0.5137, 0.5216, 0.5333,  ..., 0.5412, 0.5373, 0.5373],
         [0.5137, 0.5098, 0.5137,  ..., 0.5255, 0.5373, 0.5490],
         [0.5137, 0.5098, 0.5098,  ..., 0.5255, 0.5333, 0.5451]],

        [[0.4196, 0.4196, 0.4196,  ..., 0.4314, 0.4314, 0.4314],
         [0.4196, 0.4196, 0.4196,  ..., 0.4314, 0.4314, 0.4314],
         [0.4196, 0.4196, 0.4157,  ..., 0.4314, 0.4314, 0.4314],
         ...,
         [0.2235, 0.2353, 0.2510,  ..., 0.2706, 0.2902, 0.2980],
         [0.2196, 0.2235, 0.2314,  ..., 0.2549, 0.2824, 0.3098],
         [0.2196, 0.2235, 0.2275,  ..., 0.2549, 0.2784, 0.2980]]])

Step 4 - Print mean and std of image tensor

print("The mean of our original image tensor is:",tensor_image.mean())
print("The standard deviation of our original image tensor is:",tensor_image.std())

The mean of our original image tensor is: tensor(0.5491)
The standard deviation of our original image tensor is: tensor(0.1796)

Step 5 - Normalize the image

normalize = fn.normalize(tensor_image, mean=[0.5000], std=[.1000])
normalize

tensor([[[ 1.0392,  1.0392,  1.0392,  ...,  1.5882,  1.5882,  1.5882],
         [ 1.0392,  1.0392,  1.0392,  ...,  1.5882,  1.5882,  1.5882],
         [ 1.0392,  1.0392,  1.0000,  ...,  1.5882,  1.5882,  1.5882],
         ...,
         [-0.8039, -0.6471, -0.5294,  ..., -0.4118, -0.4118, -0.4118],
         [-0.7255, -0.7647, -0.7255,  ..., -0.5686, -0.4510, -0.2941],
         [-0.7255, -0.7647, -0.6863,  ..., -0.5686, -0.4902, -0.3333]],

        [[ 1.6667,  1.6667,  1.6667,  ...,  1.8235,  1.8235,  1.8235],
         [ 1.6667,  1.6667,  1.6667,  ...,  1.8235,  1.8235,  1.8235],
         [ 1.6667,  1.6667,  1.6275,  ...,  1.8235,  1.8235,  1.8235],
         ...,
         [ 0.1373,  0.2157,  0.3333,  ...,  0.4118,  0.3725,  0.3725],
         [ 0.1373,  0.0980,  0.1373,  ...,  0.2549,  0.3725,  0.4902],
         [ 0.1373,  0.0980,  0.0980,  ...,  0.2549,  0.3333,  0.4510]],

        [[-0.8039, -0.8039, -0.8039,  ..., -0.6863, -0.6863, -0.6863],
         [-0.8039, -0.8039, -0.8039,  ..., -0.6863, -0.6863, -0.6863],
         [-0.8039, -0.8039, -0.8431,  ..., -0.6863, -0.6863, -0.6863],
         ...,
         [-2.7647, -2.6471, -2.4902,  ..., -2.2941, -2.0980, -2.0196],
         [-2.8039, -2.7647, -2.6863,  ..., -2.4510, -2.1765, -1.9020],
         [-2.8039, -2.7647, -2.7255,  ..., -2.4510, -2.2157, -2.0196]]])

Step 6 - Convert to PIL image

pil = fn.to_pil_image(normalize)
pil

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.