Shortcuts

RandomResizedCrop

class torchvision.transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=InterpolationMode.BILINEAR, antialias: Optional[bool] = None)[source]

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.

Parameters:
  • 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[0], size[0]).

    Note

    In torchscript mode size as single int is not supported, use a sequence of length 1: [size, ].

  • 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.NEAREST, InterpolationMode.NEAREST_EXACT, InterpolationMode.BILINEAR and InterpolationMode.BICUBIC are supported. For backward compatibility integer values (e.g. PIL.Image[.Resampling].NEAREST) are still accepted, but deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum.

  • antialias (bool, optional) – antialias flag. If img is PIL Image, the flag is ignored and anti-alias is always used. If img is Tensor, the flag is False by default and can be set to True for InterpolationMode.BILINEAR and InterpolationMode.BICUBIC modes. This can help making the output for PIL images and tensors closer.

Examples using RandomResizedCrop:

Illustration of transforms

Illustration of transforms

Illustration of transforms
forward(img)[source]
Parameters:

img (PIL Image or Tensor) – Image to be cropped and resized.

Returns:

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][source]

Get parameters for crop for a random sized crop.

Parameters:
  • img (PIL Image or Tensor) – Input image.

  • scale (list) – range of scale of the origin size cropped

  • ratio (list) – range of aspect ratio of the origin aspect ratio cropped

Returns:

params (i, j, h, w) to be passed to crop for a random sized crop.

Return type:

tuple

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources