How to convert an image to a PyTorch Tensor?


A PyTorch tensor is an n-dimensional array (matrix) containing elements of a single data type. A tensor is like a numpy array. The difference between numpy arrays and PyTorch tensors is that the tensors utilize the GPUs to accelerate the numeric computations. For the accelerated computations, the images are converted to the tensors.

To convert an image to a PyTorch tensor, we can take the following steps −

Steps

  • Import the required libraries. The required libraries are torch, torchvision, Pillow.

  • Read the image. The image must be either a PIL image or a numpy.ndarray (HxWxC) in the range [0, 255]. Here H, W, and C are the height, width, and the number of channels of the image.

  • Define a transform to convert the image to tensor. We use transforms.ToTensor() to define a transform.

  • Convert the image to tensor using the above-defined transform.

Input Image

Example 1

# Import the required libraries
import torch
from PIL import Image
import torchvision.transforms as transforms

# Read the image
image = Image.open('Penguins.jpg')

# Define a transform to convert the image to tensor
transform = transforms.ToTensor()

# Convert the image to PyTorch tensor
tensor = transform(image)

# print the converted image tensor
print(tensor)

Output

tensor([[[0.4510, 0.4549, 0.4667, ..., 0.3333, 0.3333, 0.3333],
         [0.4549, 0.4510, 0.4627, ..., 0.3373, 0.3373, 0.3373],
         [0.4667, 0.4588, 0.4667, ..., 0.3451, 0.3451, 0.3412],
         ...,
         [0.6706, 0.5020, 0.5490, ..., 0.4627, 0.4275, 0.3333],
         [0.4196, 0.5922, 0.6784, ..., 0.4627, 0.4549, 0.3569],
         [0.3569, 0.3529, 0.4784, ..., 0.3922, 0.4314, 0.3490]],
         [[0.6824, 0.6863, 0.7020, ..., 0.6392, 0.6392, 0.6392],
         [0.6863, 0.6824, 0.6980, ..., 0.6314, 0.6314, 0.6314],
         [0.6980, 0.6902, 0.6980, ..., 0.6392, 0.6392, 0.6353],
         ...,
         [0.7255, 0.5412, 0.5765, ..., 0.5255, 0.5020, 0.4157],
         [0.4706, 0.6314, 0.7098, ..., 0.5255, 0.5294, 0.4392],
         [0.4196, 0.3961, 0.5020, ..., 0.4510, 0.5059, 0.4314]],
         [[0.8157, 0.8196, 0.8353, ..., 0.7922, 0.7922, 0.7922],
         [0.8196, 0.8157, 0.8314, ..., 0.7882, 0.7882, 0.7882],
         [0.8314, 0.8235, 0.8314, ..., 0.7961, 0.7961, 0.7922],
         ...,
         [0.6235, 0.5059, 0.6157, ..., 0.4863, 0.4941, 0.4196],
         [0.3922, 0.6000, 0.7176, ..., 0.4863, 0.5216, 0.4431],
         [0.3686, 0.3647, 0.4863, ..., 0.4235, 0.4980, 0.4353]]])

In the above Python program, we have converted a PIL image to a tensor.

Example 2

We can also read the image using OpenCV. The image read using OpenCV are of type numpy.ndarray. We can convert a numpy.ndarray to a tensor using transforms.ToTensor(). Have a look at the following example.

# Import the required libraries
import torch
import cv2
import torchvision.transforms as transforms

# Read the image
image = cv2.imread('Penguins.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Define a transform to convert the image to tensor
transform = transforms.ToTensor()

# Convert the image to PyTorch tensor
tensor = transform(image)

# Print the converted image tensor
print(tensor)

Output

tensor([[[0.4510, 0.4549, 0.4667, ..., 0.3333, 0.3333, 0.3333],
         [0.4549, 0.4510, 0.4627, ..., 0.3373, 0.3373, 0.3373],
         [0.4667, 0.4588, 0.4667, ..., 0.3451, 0.3451, 0.3412],
         ...,
         [0.6706, 0.5020, 0.5490, ..., 0.4627, 0.4275, 0.3333],
         [0.4196, 0.5922, 0.6784, ..., 0.4627, 0.4549, 0.3569],
         [0.3569, 0.3529, 0.4784, ..., 0.3922, 0.4314, 0.3490]],
         [[0.6824, 0.6863, 0.7020, ..., 0.6392, 0.6392, 0.6392],
         [0.6863, 0.6824, 0.6980, ..., 0.6314, 0.6314, 0.6314],
         [0.6980, 0.6902, 0.6980, ..., 0.6392, 0.6392, 0.6353],
         ...,
         [0.7255, 0.5412, 0.5765, ..., 0.5255, 0.5020, 0.4157],
         [0.4706, 0.6314, 0.7098, ..., 0.5255, 0.5294, 0.4392],
         [0.4196, 0.3961, 0.5020, ..., 0.4510, 0.5059, 0.4314]],
         [[0.8157, 0.8196, 0.8353, ..., 0.7922, 0.7922, 0.7922],
         [0.8196, 0.8157, 0.8314, ..., 0.7882, 0.7882, 0.7882],
         [0.8314, 0.8235, 0.8314, ..., 0.7961, 0.7961, 0.7922],
         ...,
         [0.6235, 0.5059, 0.6157, ..., 0.4863, 0.4941, 0.4196],
         [0.3922, 0.6000, 0.7176, ..., 0.4863, 0.5216, 0.4431],
         [0.3686, 0.3647, 0.4863, ..., 0.4235, 0.4980, 0.4353]]])

Updated on: 06-Nov-2021

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements