It seems like you are looking for interpolate (a function in nn.functional):

import torch.nn.functional as nnf

x = torch.rand(5, 1, 44, 44)
out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False)

If you really care about the accuracy of the interpolation, you should have a look at ResizeRight: a pytorch/numpy package that accurately deals with all sorts of "edge cases" when resizing images. This can have an effect when directly merging features of different scales: inaccurate interpolation may result in misalignments.

Answer from Shai on Stack Overflow
🌐
PyTorch
docs.pytorch.org › vision › main › generated › torchvision.transforms.Resize.html
Resize — Torchvision main documentation
Resize the input image to the given size. If the image is torch Tensor, it is expected to have […, H, W] shape, where …
🌐
PyTorch
docs.pytorch.org › vision › 0.13 › generated › torchvision.transforms.functional.resize.html
resize — Torchvision 0.13 documentation
torchvision.transforms.functional.resize(img: torch.Tensor, size: List[int], interpolation: torchvision.transforms.functional.InterpolationMode = <InterpolationMode.BILINEAR: 'bilinear'>, max_size: Optional[int] = None, antialias: Optional[bool] = None) → torch.Tensor[source]¶
🌐
Medium
medium.com › @MarkAiCode › resize-images-with-pytorch-a-comprehensive-guide-b9025b8fe79d
Resize Images with PyTorch: A Comprehensive Guide | by Markaicode | Medium
August 5, 2024 - When working with datasets, you often need to resize multiple images. Here’s how to do it efficiently: import os from torchvision.datasets import ImageFolder # Assuming your images are organized in folders by class dataset = ImageFolder( root='path/to/image/directory', transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor() ]) ) # Create a DataLoader for batch processing dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True) # Now you can iterate over batches for batch_images, batch_labels in dataloader: # Process your batch of resized images pass
🌐
Readthedocs
torcheeg.readthedocs.io › en › v1.1.0 › _modules › torcheeg › transforms › torch › resize.html
torcheeg.transforms.torch.resize — torcheeg 1.1.0 documentation
torcheeg.transforms.torch.resize ... ..base_transform import EEGTransform · [docs]class Resize(EEGTransform): r''' Use an interpolation algorithm to scale a grid-like EEG signal at the spatial dimension....
🌐
Readthedocs
torcheeg.readthedocs.io › en › v1.0.10 › _modules › torcheeg › transforms › torch › resize.html
torcheeg.transforms.torch.resize — torcheeg 1.0.10 documentation
torcheeg.transforms.torch.resize ... ..base_transform import EEGTransform · [docs]class Resize(EEGTransform): r''' Use an interpolation algorithm to scale a grid-like EEG signal at the spatial dimension....
🌐
Readthedocs
torcheeg.readthedocs.io › en › v1.0.11 › _modules › torcheeg › transforms › torch › resize.html
torcheeg.transforms.torch.resize — torcheeg 1.0.11 documentation
torcheeg.transforms.torch.resize ... ..base_transform import EEGTransform · [docs]class Resize(EEGTransform): r''' Use an interpolation algorithm to scale a grid-like EEG signal at the spatial dimension....
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-resize-a-tensor-in-pytorch
How to resize a tensor in PyTorch? - GeeksforGeeks
July 23, 2025 - The following program is to resize the 2D tensor in PyTorch using view(). ... # Import the torch library import torch # define a tensor tens = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) print("Original Tensor: \n", tens) # below is the different ways to use view() # Resize tensor to 3x4 tens_1 = tens.view(2, -1) print("\n Tensor After Resize: \n", tens_1) # other way resize tensor to 3x4 tens_2 = tens.view(-1, 4) print("\n Tensor after resize: \n", tens_2) # Other way to resize tensor to 3x4 tens_3 = tens.view(3, -1) print("\n Tensor after resize: \n", tens_3)
Find elsewhere
🌐
Python Guides
pythonguides.com › pytorch-resize-images
How To Resize Images In PyTorch - Python Guides
June 19, 2025 - 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}")
🌐
TutorialsPoint
tutorialspoint.com › pytorch-how-to-resize-an-image-to-a-given-size
PyTorch – How to resize an image to a given size?
January 6, 2022 - The Resize() transform resizes the input image to a given size. It's one of the transforms provided by the torchvision.transforms module. Resize() accepts both PIL and tensor images.
🌐
PyTorch
docs.pytorch.org › vision › 0.16 › generated › torchvision.transforms.Resize.html
Resize — Torchvision 0.16 documentation
Resize the input image to the given size. If the image is torch Tensor, it is expected to have […, H, W] shape, where …
🌐
PyTorch
docs.pytorch.org › vision › 0.15 › generated › torchvision.transforms.Resize.html
Resize — Torchvision 0.15 documentation
class torchvision.transforms.Resize(size, interpolation=InterpolationMode.BILINEAR, max_size=None, antialias='warn')[source]¶
🌐
Readthedocs
torcheeg.readthedocs.io › en › v1.1.1 › _modules › torcheeg › transforms › torch › resize.html
torcheeg.transforms.torch.resize — torcheeg 1.1.1 documentation
torcheeg.transforms.torch.resize ... ..base_transform import EEGTransform · [docs]class Resize(EEGTransform): r''' Use an interpolation algorithm to scale a grid-like EEG signal at the spatial dimension....
🌐
Medium
medium.com › @amit25173 › expert-guide-to-resizing-pytorch-tensors-b88d3f34a76b
Expert Guide to Resizing PyTorch Tensors | by Amit Yadav | Medium
February 9, 2025 - Purpose and Use Cases: Use padding when resizing requires adding values (like padding sequences in NLP) and trimming when the tensor is too large for the target shape. Zero-padding is crucial for creating uniform input sizes in batches. ... def zero_pad(tensor, target_shape): padded_tensor = torch.zeros(target_shape) padded_tensor[:tensor.shape[0], :tensor.shape[1]] = tensor return padded_tensor x = torch.randn(3, 3) padded_x = zero_pad(x, (5, 5)) print("Zero-Padded Tensor:\n", padded_x)
🌐
Mlverse
torchvision.mlverse.org › reference › transform_resize
Resize the input image to the given size — transform_resize • torchvision
transform_resize(img, size, interpolation = 2) img · A magick-image, array or torch_tensor. size · (sequence or int): Desired output size. If size is a sequence like c(h, w), output size will be matched to this. If size is an int, smaller edge of the image will be matched to this number.
🌐
Readthedocs
torcheeg.readthedocs.io › en › latest › _modules › torcheeg › transforms › torch › resize.html
torcheeg.transforms.torch.resize — torcheeg 1.1.3 documentation
torcheeg.transforms.torch.resize ... ..base_transform import EEGTransform · [docs]class Resize(EEGTransform): r''' Use an interpolation algorithm to scale a grid-like EEG signal at the spatial dimension....
🌐
PyTorch
docs.pytorch.org › vision › 0.22 › generated › torchvision.transforms.Resize.html
Resize — Torchvision 0.22 documentation
Resize the input image to the given size. If the image is torch Tensor, it is expected to have […, H, W] shape, where …
🌐
GitHub
github.com › assafshocher › ResizeRight
GitHub - assafshocher/ResizeRight: The correct way to resize images or tensors. For Numpy or Pytorch (differentiable). · GitHub
resize_right.resize(input, scale_factors=None, out_shape=None, interp_method=interp_methods.cubic, support_sz=None, antialiasing=True, by_convs=False, scale_tolerance=None, max_numerator=10, pad_mode='constant'): input : the input image/tensor, a Numpy or Torch tensor.
Author: assafshocher
Top answer
1 of 3
16

In order to automatically resize your input images you need to define a preprocessing pipeline all your images go through. This can be done with torchvision.transforms.Compose() (Compose docs). To resize Images you can use torchvision.transforms.Scale() (Scale docs) from the torchvision package.

See the documentation: Note, in the documentation it says that .Scale() is deprecated and .Resize() should be used instead. Resize docs

This would be a minimal working example:

import torch
from torchvision import transforms

p = transforms.Compose([transforms.Scale((48,48))])

from PIL import Image

img = Image.open('img.jpg')

img.size
# (224, 224) <-- This will be the original dimensions of your image

p(img).size
# (48, 48) <-- This will be the rescaled/resized dimensions of your image
2 of 3
2

1)

If you are using transform you can simply use resize. For example, this code will convert MNIST dataloading into a 32*32 shape (in the resize line)

train_loader = torch.utils.data.DataLoader(
  torchvision.datasets.MNIST('/files/', train=True, download=True,
                             transform=torchvision.transforms.Compose([
                               torchvision.transforms.ToTensor(),
                               torchvision.transforms.Resize(32), # This line
                               torchvision.transforms.Normalize(
                                 (0.1307,), (0.3081,))
                             ])),
  batch_size=batch_size_train, shuffle=True)

2)

If you only want a function you can use torchvision.transforms.functional.resize(image, size, ...). The other answers seem deprecated.

In the Resize Docs is written

Resize the input image to the given size.

Parameters:

  • img (PIL Image or Tensor) – Image to be resized.
  • size Desired output size. If size is a sequence like (h, w), the output size will be matched to this. If size is an int, the smaller edge of the image will be matched to this number maintaining the aspect ratio

Return type:

  • PIL Image or Tensor