0
0
PyTorchml~5 mins

Data augmentation with transforms in PyTorch

Choose your learning style9 modes available
Introduction

Data augmentation helps your model learn better by creating new, slightly changed versions of your images. This makes the model stronger and less likely to make mistakes.

When you have a small number of images and want to make your dataset bigger.
When you want your model to recognize objects even if they are rotated or flipped.
When you want to reduce overfitting by showing the model varied examples.
When training models for tasks like image classification or object detection.
When you want to improve model accuracy without collecting more data.
Syntax
PyTorch
from torchvision import transforms

transform = transforms.Compose([
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomRotation(degrees=30),
    transforms.ToTensor()
])

Compose lets you combine many transforms to apply one after another.

Transforms like RandomHorizontalFlip and RandomRotation change images randomly to create variety.

Examples
This flips images vertically 70% of the time.
PyTorch
transform = transforms.RandomVerticalFlip(p=0.7)
This randomly changes brightness and contrast to make images look different.
PyTorch
transform = transforms.ColorJitter(brightness=0.5, contrast=0.5)
This crops a random part of the image and resizes it to 224x224 pixels, then converts it to a tensor.
PyTorch
transform = transforms.Compose([
    transforms.RandomResizedCrop(224),
    transforms.ToTensor()
])
Sample Model

This program loads an image from the internet, applies horizontal flip and random rotation, then converts it to a tensor. It prints the original image size, the shape of the tensor, and the pixel value range.

PyTorch
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 data augmentation transforms
transform = transforms.Compose([
    transforms.RandomHorizontalFlip(p=1.0),  # Always flip horizontally
    transforms.RandomRotation(degrees=45),   # Rotate randomly up to 45 degrees
    transforms.ToTensor()                      # Convert image to tensor
])

# Apply transform
augmented_img = transform(img)

# Show original and augmented image sizes and tensor shape
print(f'Original image size: {img.size}')
print(f'Augmented tensor shape: {augmented_img.shape}')

# Check pixel value range
print(f'Pixel value range: min={augmented_img.min():.3f}, max={augmented_img.max():.3f}')
OutputSuccess
Important Notes

Always convert images to tensors after augmentation to use them in PyTorch models.

Random transforms add variety but can make training results different each time.

You can combine many transforms to create powerful data augmentation pipelines.

Summary

Data augmentation creates new image versions to help models learn better.

Use transforms.Compose to combine multiple changes like flips and rotations.

Always convert images to tensors before feeding them to your model.