On this page
torch.nn.functional.interpolate
torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False)[source]-
Down/up samples the input to either the given
sizeor the givenscale_factorThe algorithm used for interpolation is determined by
mode.Currently temporal, spatial and volumetric sampling are supported, i.e. expected inputs are 3-D, 4-D or 5-D in shape.
The input dimensions are interpreted in the form:
mini-batch x channels x [optional depth] x [optional height] x width.The modes available for resizing are:
nearest,linear(3D-only),bilinear,bicubic(4D-only),trilinear(5D-only),area,nearest-exact- Parameters
-
- input (Tensor) – the input tensor
- size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) – output spatial size.
- scale_factor (float or Tuple[float]) – multiplier for spatial size. If
scale_factoris a tuple, its length has to match the number of spatial dimensions;input.dim() - 2. - mode (str) – algorithm used for upsampling:
'nearest'|'linear'|'bilinear'|'bicubic'|'trilinear'|'area'|'nearest-exact'. Default:'nearest' - align_corners (bool, optional) – Geometrically, we consider the pixels of the input and output as squares rather than points. If set to
True, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels. If set toFalse, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of input size whenscale_factoris kept the same. This only has an effect whenmodeis'linear','bilinear','bicubic'or'trilinear'. Default:False - recompute_scale_factor (bool, optional) – recompute the scale_factor for use in the interpolation calculation. If
recompute_scale_factorisTrue, thenscale_factormust be passed in andscale_factoris used to compute the outputsize. The computed outputsizewill be used to infer new scales for the interpolation. Note that whenscale_factoris floating-point, it may differ from the recomputedscale_factordue to rounding and precision issues. Ifrecompute_scale_factorisFalse, thensizeorscale_factorwill be used directly for interpolation. Default:None. - antialias (bool, optional) – flag to apply anti-aliasing. Default:
False. Using anti-alias option together withalign_corners=False, interpolation result would match Pillow result for downsampling operation. Supported modes:'bilinear','bicubic'.
- Return type
Note
With
mode='bicubic', it’s possible to cause overshoot, in other words it can produce negative values or values greater than 255 for images. Explicitly callresult.clamp(min=0, max=255)if you want to reduce the overshoot when displaying the image.Note
Mode
mode='nearest-exact'matches Scikit-Image and PIL nearest neighbours interpolation algorithms and fixes known issues withmode='nearest'. This mode is introduced to keep backward compatibility. Modemode='nearest'matches buggy OpenCV’sINTER_NEARESTinterpolation algorithm.Note
The gradients for the dtype
float16on CUDA may be inaccurate in the upsample operation when using modes['linear', 'bilinear', 'bicubic', 'trilinear', 'area']. For more details, please refer to the discussion in issue#104157.Note
This operation may produce nondeterministic gradients when given tensors on a CUDA device. See Reproducibility for more information.
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.nn.functional.interpolate.html