Random Sized Crop

If you have ever worked on a Computer Vision project, you might know that using augmentations to diversify the dataset is the best practice. On this page, we will:

  • Сover the Random Sized Crop augmentation;

  • Check out its parameters;

  • See how Random Sized Crop affects an image;

  • And check out how to work with Random Sized Crop using Python through the Albumentations library.

Let's get into it!

To define the term, Random Sized Crop is a data augmentation technique that helps researchers to crop an image to any size within a certain specified bound.

Please make sure that the original image size is larger than the requested crop size.
  • Min max height - sets the crop size limits;

  • Height - sets the height of the desired resized and cropped image in pixels;

  • Width - sets the width of the desired resized and cropped image in pixels.

First, an image is cropped to any size within the given crop size limits. Then, it is resized to correspond to the Height and Width parameters.For example, the range is set from 1500 to 2000, and the height and width are set to 200. Then an image is cropped anywhere between 1500 to 2000 pixels, and the cropped image is then resized to 200x200 pixels. This is why some of the objects in the image might look distorted.

Advanced options

  • Width to height ratio - defines an aspect ratio of the crop;
  • Interpolation - used to specify the interpolation algorithm:
Original image
Image after Random Sized Crop (Min max height [128; 500], Height and Width equal to 200) is applied
    import albumentations as albu
from PIL import Image

transform = albu.RandomSizedCrop([1500,2000], 100, 100, 1.0, cv2.INTER_NEAREST, 1)
augmented_image = transform(image=figure)['image']

# We have our required cropped image in augmented_image.

# The first argument defines the crop size limits;
# Second and third are the height and width of the image after crop and resize;
# Fourth is the aspect ratio of crop;
# Fifth is the interpolation technique, and the last is the probability of the transform.
Last updated on Dec 21, 2022

Get to production reliably.

Hasty is a unified agile ML platform for your entire Vision AI pipeline — with minimal integration effort for you.

Start for free Request a demo