How to convert an image to tensor in Pytorch?

This recipe helps you convert an image to tensor in Pytorch.

Recipe Objective: How to convert an image to tensor in Pytorch?

This recipe shows how to convert a Pytorch image to tensor.

You can convert Pytorch image to tensor using the To Tensor function, which helps you convert PIL image to tensor. Let us understand this with practical implementation.

Steps Showing Pytorch Image Tensor Conversion

The following steps will show how to load image as tensor in Pytorch.

Step 1 - Import library

import torch
from torchvision import transforms
from PIL import Image

Step 2 - Take Sample data

img = Image.open("/content/yellow-orange-starburst-flower-nature-jpg-192959431.jpg")
img

Step 3 - Convert img To Tensor

convert_tensor = transforms.ToTensor()
convert_tensor(img)

tensor([[[0.1725, 0.1765, 0.1804,  ..., 0.2000, 0.2000, 0.1922],

         [0.1725, 0.1765, 0.1804,  ..., 0.2039, 0.2000, 0.1922],

         [0.1725, 0.1765, 0.1804,  ..., 0.2039, 0.2000, 0.1922],

         ...,

         [0.0314, 0.0353, 0.0392,  ..., 0.0000, 0.0000, 0.0000],

         [0.0549, 0.0588, 0.0667,  ..., 0.0000, 0.0000, 0.0000],

         [0.0784, 0.0824, 0.0902,  ..., 0.0000, 0.0000, 0.0000]],

 

        [[0.3647, 0.3686, 0.3725,  ..., 0.4314, 0.4235, 0.4157],

         [0.3647, 0.3686, 0.3725,  ..., 0.4275, 0.4235, 0.4157],

         [0.3647, 0.3686, 0.3725,  ..., 0.4275, 0.4235, 0.4157],

         ...,

         [0.2706, 0.2745, 0.2784,  ..., 0.1137, 0.1137, 0.1137],

         [0.2902, 0.2941, 0.3020,  ..., 0.1098, 0.1098, 0.1098],

         [0.3059, 0.3098, 0.3176,  ..., 0.1098, 0.1098, 0.1098]],

 

        [[0.0431, 0.0471, 0.0510,  ..., 0.1176, 0.1137, 0.1059],

         [0.0431, 0.0471, 0.0510,  ..., 0.1176, 0.1137, 0.1059],

         [0.0431, 0.0471, 0.0510,  ..., 0.1176, 0.1137, 0.1059],

         ...,

         [0.0000, 0.0039, 0.0078,  ..., 0.0039, 0.0039, 0.0039],

         [0.0078, 0.0118, 0.0196,  ..., 0.0000, 0.0000, 0.0000],

         [0.0078, 0.0196, 0.0275,  ..., 0.0000, 0.0000, 0.0000]]])

Pytorch Read Image As Tensor

In Pytorch, load image as tensor from a file using the torchvision.io.read_image() function. This function takes the path to the image file as its argument and returns a tensor of the image data.

 

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

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