How to Resize Images in PyTorch

As a Python developer with over a decade of experience, I’ve worked extensively with image processing libraries. PyTorch’s image manipulation capabilities have become indispensable in my deep learning projects.

A client recently asked me to create an image classification model to identify various American landmarks. The dataset included images of different sizes, ranging from small 100×100 pixel photos of the Statue of Liberty to large 4K images of the Golden Gate Bridge.

This inconsistency created a major problem. Neural networks expect uniform input sizes, so I needed to resize all these images. That’s when PyTorch’s resizing functionality saved the day.

In this guide, I will share the exact methods I use to resize images in PyTorch, along with practical examples that you can implement right away.

Methods to Resize Images in PyTorch

Now, I will explain methods to resize images in PyTorch.

Method 1: Use torchvision.transforms

The easiest way to resize images in PyTorch is through the transforms module. I use this method in nearly 80% of my projects because of its simplicity.

import torch
from torchvision import transforms
from PIL import Image

# Load an image
img = Image.open(r'C:\Users\Public\grand_canyon.jpg')
print(f"Original size: {img.size}")

# Resize transform
resize_transform = transforms.Resize((224, 224))
resized_img = resize_transform(img)
print(f"New size: {resized_img.size}")

# Convert to tensor
to_tensor = transforms.ToTensor()
img_tensor = to_tensor(resized_img)
print(f"Tensor shape: {img_tensor.shape}")

You can refer to the screenshot below to see the output:

pytorch resize

The transforms.Resize() function takes a tuple of (height, width) or a single integer. If you provide a single integer, it will maintain the aspect ratio while resizing the smallest edge to that size.

Read Use PyTorch Cat function

Method 2: Use transforms.Compose for Multiple Operations

When building image pipelines, I often need to perform multiple transformations. The transforms.Compose function lets you chain operations together:

# Create a transform pipeline
transform_pipeline = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], 
                         std=[0.229, 0.224, 0.225])
])

# Apply all transforms at once
normalized_tensor = transform_pipeline(img)

This approach is particularly useful when preprocessing images for neural networks. The normalization values I’ve used are standard for models pretrained on ImageNet.

Check out PyTorch Stack Tutorial

Method 3: Functional Approach with F.interpolate

When I need more control or want to resize batches of images directly as tensors, I use the functional API:

import torch
import torch.nn.functional as F
from torchvision import transforms
from PIL import Image

# Step 1: Load an image using PIL
img = Image.open(r'C:\Users\Public\grand_canyon.jpg')  # Replace path as needed

# Step 2: Convert image to tensor
to_tensor = transforms.ToTensor()
img_tensor = to_tensor(img)  # Shape: [C, H, W]

# Step 3: Add batch dimension for interpolate (needed shape: [N, C, H, W])
batched_tensor = img_tensor.unsqueeze(0)  # Shape: [1, C, H, W]

# Step 4: Resize the image tensor using bilinear interpolation
resized_tensor = F.interpolate(batched_tensor, size=(224, 224), mode='bilinear', align_corners=False)

print(f"Resized tensor shape: {resized_tensor.shape}")

You can refer to the screenshot below to see the output:

torch resize

This method is extremely powerful when working with batches of images already loaded as tensors. It’s the approach I typically use in production systems.

Read Create PyTorch Empty Tensor

Method 4: Resize with Different Interpolation Modes

The quality of resized images can vary significantly based on the interpolation method. PyTorch offers several options:

# Different interpolation methods
nearest_resize = transforms.Resize((224, 224), interpolation=transforms.InterpolationMode.NEAREST)
bilinear_resize = transforms.Resize((224, 224), interpolation=transforms.InterpolationMode.BILINEAR)
bicubic_resize = transforms.Resize((224, 224), interpolation=transforms.InterpolationMode.BICUBIC)

# Apply different methods
nearest_img = nearest_resize(img)
bilinear_img = bilinear_resize(img)
bicubic_img = bicubic_resize(img)

From my experience:

  • NEAREST is fastest but lowest quality
  • BILINEAR is a good balance and my default choice
  • BICUBIC gives highest quality but is slower

When I worked on a project analyzing satellite imagery of U.S. national parks, bicubic interpolation preserved more details in landscape features, which was crucial for the model’s accuracy.

Method 5: Preserve Aspect Ratio

Sometimes maintaining the aspect ratio is critical. When I developed a facial recognition system for a major U.S. retail chain, distorted faces significantly reduced accuracy.

import torch
from torchvision import transforms
from PIL import Image

# Step 1: Load the image
img = Image.open(r'C:\Users\Public\grand_canyon.jpg')  # Replace path if needed

# Step 2: Resize while preserving aspect ratio (shorter side = 256)
preserve_aspect = transforms.Resize(256)

# Step 3: Crop the center 224x224 region
center_crop = transforms.CenterCrop(224)

# Step 4: Compose both transforms
transform = transforms.Compose([
    preserve_aspect,
    center_crop
])

# Step 5: Apply the transform
final_img = transform(img)

# Step 6: Show results
print(f"Original size: {img.size}")
print(f"Final size with preserved aspect ratio: {final_img.size}")

You can refer to the screenshot below to see the output:

torchvision resize

Check out PyTorch Flatten

Method 6: Resize Image Datasets

When working with datasets, I typically define transformations as part of the dataset loading:

from torchvision import datasets

# Create dataset with transform
data_transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
])

dataset = datasets.ImageFolder(root='./us_landmarks', transform=data_transform)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True)

# Now all images will be automatically resized when loaded

This approach is extremely efficient when working with large datasets like my project on American license plate recognition, which involved over 100,000 images from different states.

Read PyTorch Conv3d

Best Practices for Image Resizing in PyTorch

Through years of working with PyTorch, I’ve developed these guidelines:

  1. Choose the right interpolation mode: For photorealistic images like landscapes or faces, use BICUBIC. For simpler images or when speed matters, BILINEAR works well.
  2. Consider memory usage: Resizing to larger dimensions increases memory consumption. When I worked with drone footage of U.S. farmland, I had to balance image size with batch size.
  3. Preserve aspect ratio when possible: Use a combination of Resize and CenterCrop instead of forcing images into unnatural proportions.
  4. Normalize after resizing: Always normalize your images after resizing, but before feeding them to your model.
  5. Augment during training: Consider combining resizing with random crops or flips during training for better generalization.

After implementing these practices in my U.S. landmark classification project, the model accuracy improved from 78% to 91%.

PyTorch’s flexible image resizing capabilities have helped me solve countless computer vision challenges over the years. Whether you’re building a model to recognize American cars, analyze medical images, or process satellite imagery, proper resizing is essential for success.

Start with the simpler transforms. Resize method for most projects, and progress to F.interpolate when you need more control or performance. With these techniques, you’ll be well-equipped to handle images of any size in your deep learning projects.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.