resize¶
- torchvision.transforms.functional.resize(img: Tensor, size: List[int], interpolation: InterpolationMode = InterpolationMode.BILINEAR, max_size: Optional[int] = None, antialias: Optional[Union[str, bool]] = 'warn') Tensor [source]¶
Resize the input image to the given size. If the image is torch Tensor, it is expected to have […, H, W] shape, where … means an arbitrary number of leading dimensions
Warning
The output image might be different depending on its type: when downsampling, the interpolation of PIL images and tensors is slightly different, because PIL applies antialiasing. This may lead to significant differences in the performance of a network. Therefore, it is preferable to train and serve a model with the same input types. See also below the
antialias
parameter, which can help making the output of PIL images and tensors closer.- Parameters:
img (PIL Image or Tensor) – Image to be resized.
size (sequence or int) –
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. i.e, if height > width, then image will be rescaled to \(\left(\text{size} \times \frac{\text{height}}{\text{width}}, \text{size}\right)\).
Note
In torchscript mode size as single int is not supported, use a sequence of length 1:
[size, ]
.interpolation (InterpolationMode) – Desired interpolation enum defined by
torchvision.transforms.InterpolationMode
. Default isInterpolationMode.BILINEAR
. If input is Tensor, onlyInterpolationMode.NEAREST
,InterpolationMode.NEAREST_EXACT
,InterpolationMode.BILINEAR
andInterpolationMode.BICUBIC
are supported. The corresponding Pillow integer constants, e.g.PIL.Image.BILINEAR
are accepted as well.max_size (int, optional) – The maximum allowed for the longer edge of the resized image: if the longer edge of the image is greater than
max_size
after being resized according tosize
, then the image is resized again so that the longer edge is equal tomax_size
. As a result,size
might be overruled, i.e. the smaller edge may be shorter thansize
. This is only supported ifsize
is an int (or a sequence of length 1 in torchscript mode).antialias (bool, optional) –
Whether to apply antialiasing. It only affects tensors with bilinear or bicubic modes and it is ignored otherwise: on PIL images, antialiasing is always applied on bilinear or bicubic modes; on other modes (for PIL images and tensors), antialiasing makes no sense and this parameter is ignored. Possible values are:
True
: will apply antialiasing for bilinear or bicubic modes. Other mode aren’t affected. This is probably what you want to use.False
: will not apply antialiasing for tensors on any mode. PIL images are still antialiased on bilinear or bicubic modes, because PIL doesn’t support no antialias.None
: equivalent toFalse
for tensors andTrue
for PIL images. This value exists for legacy reasons and you probably don’t want to use it unless you really know what you are doing.
The current default is
None
but will change toTrue
in v0.17 for the PIL and Tensor backends to be consistent.
- Returns:
Resized image.
- Return type:
PIL Image or Tensor
Examples using
resize
:Optical Flow: Predicting movement with the RAFT model
Optical Flow: Predicting movement with the RAFT modelIllustration of transforms