0
0
PytorchHow-ToBeginner · 3 min read

How to Use Random Crop in PyTorch for Image Augmentation

Use torchvision.transforms.RandomCrop to randomly crop images during training in PyTorch. It takes the crop size as input and can be applied as part of a transform pipeline to augment image data.
📐

Syntax

The RandomCrop transform in PyTorch is used to crop a random region from an image. It is commonly used for data augmentation to improve model robustness.

Key parts:

  • size: The output size of the crop. Can be an int or tuple (height, width).
  • padding: Optional padding added before cropping.
  • pad_if_needed: If true, pads image if smaller than crop size.
  • fill: Pixel fill value for padding.
  • padding_mode: Mode for padding (e.g., 'constant', 'reflect').
python
torchvision.transforms.RandomCrop(size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant')
💻

Example

This example shows how to apply RandomCrop to an image tensor. It crops a random 100x100 patch from a sample image and displays the original and cropped sizes.

python
import torch
from torchvision import transforms
from PIL import Image
import requests
from io import BytesIO

# Load an example image from the web
url = 'https://pytorch.org/assets/images/deeplab1.png'
response = requests.get(url)
img = Image.open(BytesIO(response.content))

# Define RandomCrop transform with crop size 100x100
random_crop = transforms.RandomCrop((100, 100))

# Apply the transform
cropped_img = random_crop(img)

print(f'Original image size: {img.size}')
print(f'Cropped image size: {cropped_img.size}')
Output
Original image size: (500, 333) Cropped image size: (100, 100)
⚠️

Common Pitfalls

  • Crop size larger than image: If the crop size is bigger than the image, RandomCrop will raise an error unless pad_if_needed=True is set.
  • Not converting images to PIL: RandomCrop expects PIL images or tensors in specific formats; passing raw numpy arrays may cause errors.
  • Applying RandomCrop during validation: Random cropping is for training augmentation; avoid using it during validation or testing to keep evaluation consistent.
python
from torchvision import transforms

# Wrong: crop size larger than image without padding
try:
    transform = transforms.RandomCrop((600, 600))
    cropped = transform(img)  # img size is smaller
except Exception as e:
    print(f'Error: {e}')

# Right: use pad_if_needed=True to avoid error
transform = transforms.RandomCrop((600, 600), pad_if_needed=True)
cropped = transform(img)
print(f'Cropped with padding size: {cropped.size}')
Output
Error: Requested crop size (600, 600) is bigger than input image size (500, 333) Cropped with padding size: (600, 600)
📊

Quick Reference

Tips for using RandomCrop effectively:

  • Use it as part of transforms.Compose for chaining multiple augmentations.
  • Set pad_if_needed=True if images vary in size to avoid errors.
  • Use consistent crop sizes matching your model input.
  • Apply only during training, not validation/testing.

Key Takeaways

Use torchvision.transforms.RandomCrop to randomly crop images for data augmentation in PyTorch.
Specify the crop size as an int or tuple (height, width) when creating RandomCrop.
Set pad_if_needed=True to handle images smaller than the crop size without errors.
Apply RandomCrop only during training to improve model generalization.
Combine RandomCrop with other transforms using transforms.Compose for flexible pipelines.