Resize batched images to a fixed height and width.
Image preprocessing
PyTorch · Function
Resizes 3-D, 4-D, or 5-D tensors by upsampling or downsampling their spatial dimensions. You choose the output size or scale factor and an interpolation mode such as nearest, bilinear, bicubic, trilinear, area, or nearest-exact.
The function resizes a tensor by computing an output spatial shape from either an explicit size or scale multiplier, then applying the selected interpolation algorithm. It accepts temporal, spatial, and volumetric layouts with batch and channel dimensions followed by one to three spatial dimensions.
torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False)| Name | Type | Description |
|---|---|---|
input | Tensor | Input tensor to resize. |
size | int or tuple[int, ...], optional | Output spatial size, supplied as an integer or a tuple with one to three spatial dimensions. |
scale_factor | float or tuple[float, ...], optional | Multiplier applied to each spatial dimension; a tuple must match the number of spatial dimensions. |
mode | str, optional | Interpolation algorithm: nearest, linear, bilinear, bicubic, trilinear, area, or nearest-exact. |
align_corners | bool, optional | Whether to align corner pixels for supported linear-family modes. |
recompute_scale_factor | bool, optional | Whether to recompute the interpolation scale from the output size. |
antialias | bool, optional | Whether to apply anti-aliasing. |
A tensor resized along its spatial dimensions according to size or scale_factor and the selected mode.
import torch
import torch.nn.functional as F
x = torch.randn(1, 3, 8, 8)
y = F.interpolate(x, size=(16, 16), mode="bilinear", align_corners=False)
print(y.shape) # torch.Size([1, 3, 16, 16])This runnable example upsamples a 4-D image tensor to 16×16 using bilinear interpolation.
Image preprocessing
Feature pyramids
Video and volumetric data
It resizes a tensor using either an explicit output size or a scale factor. The supported input ranks are 3-D, 4-D, and 5-D, interpreted as batch, channels, and one to three spatial dimensions.
Follow-up: How would you resize a 2-D image batch? Use a 4-D tensor shaped N×C×H×W and select a compatible mode such as bilinear or nearest.
The size argument directly specifies the output spatial dimensions, while scale_factor multiplies the input spatial dimensions. Only one of size or scale_factor should determine the target shape.
Follow-up: What constraint applies to tuple scale_factor values? Their length must equal input.dim() minus 2, the number of spatial dimensions.
align_corners controls geometric alignment for linear-family interpolation and is only valid with linear, bilinear, bicubic, or trilinear modes. When enabled, the corner pixels of input and output are aligned; when disabled, scaling uses a different center-based geometry.
Follow-up: Which modes can use antialiasing according to the page? Bilinear and bicubic modes support it.
torch.nn.functional