Resize¶
- class torchvision.transforms.v2.Resize(size: Optional[Union[int, Sequence[int]]], interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR, max_size: Optional[int] = None, antialias: Optional[bool] = True)[source]¶
Resize the input to the given size.
If the input is a
torch.Tensoror aTVTensor(e.g.Image,Video,BoundingBoxesetc.) it can have arbitrary number of leading batch dimensions. For example, the image can have[..., C, H, W]shape. A bounding box can have[..., 4]shape.- Parameters:
size (sequence, int, or None) –
Desired output size.
If size is a sequence like (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. i.e, if height > width, then image will be rescaled to (size * height / width, size).
If size is None, the output shape is determined by the
max_sizeparameter.
Note
In torchscript mode size as single int is not supported, use a sequence of length 1:
[size, ].interpolation (InterpolationMode, optional) – Desired interpolation enum defined by
torchvision.transforms.InterpolationMode. Default isInterpolationMode.BILINEAR. If input is Tensor, onlyInterpolationMode.NEAREST,InterpolationMode.NEAREST_EXACT,InterpolationMode.BILINEARandInterpolationMode.BICUBICare supported. The corresponding Pillow integer constants, e.g.PIL.Image.BILINEARare accepted as well.max_size (int, optional) –
The maximum allowed for the longer edge of the resized image.
If
sizeis an int: if the longer edge of the image is greater thanmax_sizeafter being resized according tosize,sizewill be overruled so that the longer edge is equal tomax_size. As a result, the smaller edge may be shorter thansize. This is only supported ifsizeis an int (or a sequence of length 1 in torchscript mode).If
sizeis None: the longer edge of the image will be matched to max_size. i.e, if height > width, then image will be rescaled to (max_size, max_size * width / height).
This should be left to
None(default) whensizeis a sequence.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(default): 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 toFalsefor tensors andTruefor 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 default value changed from
NonetoTruein v0.17, for the PIL and Tensor backends to be consistent.
Examples using
Resize: