- class torchvision.transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=InterpolationMode.BILINEAR, antialias: Optional[bool] = True)¶
Crop a random portion of image and resize it to a given size.
If the image is torch Tensor, it is expected to have […, H, W] shape, where … means an arbitrary number of leading dimensions
A crop of the original image is made: the crop has a random area (H * W) and a random aspect ratio. This crop is finally resized to the given size. This is popularly used to train the Inception networks.
size (int or sequence) –
expected output size of the crop, for each edge. If size is an int instead of sequence like (h, w), a square output size
(size, size)is made. If provided a sequence of length 1, it will be interpreted as (size, size).
In torchscript mode size as single int is not supported, use a sequence of length 1:
scale (tuple of python:float) – Specifies the lower and upper bounds for the random area of the crop, before resizing. The scale is defined with respect to the area of the original image.
ratio (tuple of python:float) – lower and upper bounds for the random aspect ratio of the crop, before resizing.
interpolation (InterpolationMode) – Desired interpolation enum defined by
torchvision.transforms.InterpolationMode. Default is
InterpolationMode.BILINEAR. If input is Tensor, only
InterpolationMode.BICUBICare supported. The corresponding Pillow integer constants, e.g.
PIL.Image.BILINEARare accepted as well.
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 to
Falsefor tensors and
Truefor 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
Truein v0.17, for the PIL and Tensor backends to be consistent.
RandomResizedCrop:Getting started with transforms v2Illustration of transformsHow to use CutMix and MixUpHow to write your own v2 transforms
img (PIL Image or Tensor) – Image to be cropped and resized.
Randomly cropped and resized image.
- Return type:
PIL Image or Tensor
- static get_params(img: Tensor, scale: List[float], ratio: List[float]) Tuple[int, int, int, int] ¶
Get parameters for
cropfor a random sized crop.