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
class torchvision.transforms.Resize(size, interpolation=InterpolationMode.BILINEAR, max_size=None, antialias=True)[source]¶
🌐
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 - This image is used as the input file in all the following examples. The input image is resized to (300, 350). The original image is of size (700,700) # import the required libraries import torch import torchvision.transforms as T from PIL import ...
🌐
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 - Here’s how to do it efficiently: ... ImageFolder( root='path/to/image/directory', transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor() ]) ) # Create a DataLoader for batch processing dataloader = ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-resize-a-tensor-in-pytorch
How to resize a tensor in PyTorch? - GeeksforGeeks
March 23, 2022 - Example 2: The following program is to resize the 2D tensor in PyTorch using view(). Python · # 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) Output: This method is also used to resize the tensors.
🌐
Python Guides
pythonguides.com › pytorch-resize-images
How To Resize Images In PyTorch - Python Guides
June 19, 2025 - 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. ... Now, I will explain methods to resize images in PyTorch. 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}")
🌐
ProjectPro
projectpro.io › recipes › crop-and-resize-image-pytorch
How to resize image using Pytorch? - Projectpro
December 19, 2022 - Step 4 - Resize the Image · import ... Image · img = Image.open("/content/Pytorch_Exercise_46_Crop_and_Resize_image.jpg") print("This is size of original image:",img.size, "\n") img · This is size of original image: (1200, 602) crop = fn.center_crop(img, ...
🌐
PyTorch
docs.pytorch.org › vision › 0.16 › generated › torchvision.transforms.Resize.html
Resize — Torchvision 0.16 documentation
In torchscript mode size as single int is not supported, use a sequence of length 1: [size, ].
Find elsewhere
🌐
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]¶
🌐
Readthedocs
torcheeg.readthedocs.io › en › v1.1.0 › _modules › torcheeg › transforms › torch › resize.html
torcheeg.transforms.torch.resize — torcheeg 1.1.0 documentation
''' return super().__call__(*args, eeg=eeg, baseline=baseline, **kwargs) def apply(self, eeg: torch.Tensor, **kwargs) -> torch.Tensor: assert eeg.dim() == 3, f'The Resize only allows to input a 3-d tensor, but the input has dimension {eeg.dim()}' eeg = eeg.unsqueeze(0) align_corners = False if self.interpolation in ["bilinear", "bicubic"] else None interpolated_x = interpolate(eeg, size=self.size, mode=self.interpolation, align_corners=align_corners) return interpolated_x.squeeze(0) @property def repr_body(self) -> Dict: return dict(super().repr_body, **{'size': self.size, 'interpolation': self.interpolation})
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
🌐
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]¶
🌐
ProgramCreek
programcreek.com › python › example › 104834 › torchvision.transforms.Resize
Python Examples of torchvision.transforms.Resize
def load_training(root_path, dir, batch_size, kwargs): transform = transforms.Compose( [transforms.Resize([256, 256]), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor()]) data = datasets.ImageFolder(root=root_path + dir, transform=transform) train_loader = ...
🌐
PyTorch
docs.pytorch.org › vision › 0.18 › generated › torchvision.transforms.Resize.html
Resize — Torchvision 0.18 documentation
class torchvision.transforms.Resize(size, interpolation=InterpolationMode.BILINEAR, max_size=None, antialias=True)[source]¶
🌐
Medium
medium.com › @amit25173 › expert-guide-to-resizing-pytorch-tensors-b88d3f34a76b
Expert Guide to Resizing PyTorch Tensors | by Amit Yadav | Medium
February 9, 2025 - ... import torch # Example of memory-efficient resizing x = torch.randn(1000, 1000) # Adding a dimension with unsqueeze (no new data created) x_unsqueezed = x.unsqueeze(0) # Alternative using reshape (may create a new copy if non-contiguous) ...
🌐
DEV Community
dev.to › hyperkai › resize-in-pytorch-47j7
Resize in PyTorch - DEV Community
May 16, 2025 - root="data", transform=Resize(size=1000) # transform=Resize(size=[1000]) # transform=Resize(size=[1000, 1000]) ) s500_data = OxfordIIITPet( root="data", transform=Resize(size=500) ) s100_data = OxfordIIITPet( root="data", transform=Resize(size=100) ...
🌐
PyTorch
docs.pytorch.org › vision › 0.28 › generated › torchvision.transforms.v2.Resize.html
Resize — Torchvision 0.28 documentation
Resize the input to the given size. If the input is a torch.Tensor or a TVTensor (e.g. Image, Video, BoundingBoxes etc.) it can have arbitrary number of leading batch dimensions. For example, the image can have [..., C, H, W] shape.
🌐
PyTorch
docs.pytorch.org › vision › 0.11 › transforms.html
torchvision.transforms — Torchvision 0.11.0 documentation
In torchscript mode size as single int is not supported, use a sequence of length 1: [size, ]. scale (tuple of python:float) – Specifies the lower and upper bounds for the random area of the crop, before resizing.
🌐
Readthedocs
torchaug.readthedocs.io › en › v0.5.0 › generated › torchaug.transforms.Resize.html
Resize - Torchaug - Read the Docs
Resize the input to the given size. If the input is a torch.Tensor or a TATensor (e.g. Image, Video, BoundingBoxes etc.) it can have arbitrary number of leading batch dimensions. For example, the image can have [..., C, H, W] shape.