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.
Data augmentation with transforms in 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.
transform = transforms.RandomVerticalFlip(p=0.7)transform = transforms.ColorJitter(brightness=0.5, contrast=0.5)
transform = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.ToTensor()
])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.
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}')
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.
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.