Phase 4: Data Pipeline
Module 12: Data Augmentation
What You Will Learn
In this module, you will learn:
- What is Data Augmentation?
- Why Data Augmentation is Important
- torchvision.transforms
- Resize
- Normalize
- RandomCrop
- RandomFlip
- RandomRotation
- ColorJitter
- RandomErasing
- Building an Image Augmentation Pipeline
- Best Practices
What is Data Augmentation?
Data Augmentation is the process of creating new training samples by applying random transformations to existing images.
Instead of collecting thousands of new images, we generate new variations from the existing dataset.
For example:
Original Image
1🐱
After Augmentation
1🐱 (Flipped) 2 3🐱 (Rotated) 4 5🐱 (Cropped) 6 7🐱 (Brighter) 8 9🐱 (Zoomed)
Although the image changes, its label remains the same.
Why Do We Need Data Augmentation?
Deep learning models often memorize training data, leading to overfitting.
Without augmentation:
1Training Accuracy → 99% 2 3Validation Accuracy → 72%
The model performs well on training data but poorly on unseen data.
With augmentation:
1Training Accuracy → 95% 2 3Validation Accuracy → 92%
The model learns more robust features and generalizes better.
Image Processing Pipeline
1Original Image 2 │ 3 ▼ 4Data Augmentation 5 │ 6 ▼ 7Tensor Conversion 8 │ 9 ▼ 10Normalization 11 │ 12 ▼ 13Neural Network
torchvision.transforms
PyTorch provides image transformations through:
1from torchvision import transforms
Most image preprocessing and augmentation tasks use this module.
Creating a Transform Pipeline
1from torchvision import transforms 2 3transform = transforms.Compose([ 4 transforms.Resize((224, 224)), 5 transforms.ToTensor() 6])
Compose() applies multiple transformations sequentially.
1. Resize
What is Resize?
Images often have different sizes.
Neural networks require all images to have the same dimensions.
Example:
1Image 1 → 500×300 2 3Image 2 → 640×480 4 5Image 3 → 1024×768
After resizing:
1224×224 2 3224×224 4 5224×224
Resize Example
1from torchvision import transforms 2 3transform = transforms.Resize((224, 224))
Using Compose:
1transform = transforms.Compose([ 2 transforms.Resize((224,224)), 3 transforms.ToTensor() 4])
2. Normalize
What is Normalization?
Pixel values range from:
10 → 255
After ToTensor():
10.0 → 1.0
Normalization shifts the pixel distribution to improve training stability.
Formula
1Normalized = (Image - Mean) / Std
For ImageNet pretrained models:
1transforms.Normalize( 2 mean=[0.485,0.456,0.406], 3 std=[0.229,0.224,0.225] 4)
Example
1transform = transforms.Compose([ 2 transforms.ToTensor(), 3 transforms.Normalize( 4 mean=[0.485,0.456,0.406], 5 std=[0.229,0.224,0.225] 6 ) 7])
Why Normalize?
Benefits:
- Faster convergence
- Stable gradients
- Better optimization
- Required for pretrained models
3. RandomCrop
What is RandomCrop?
RandomCrop randomly crops part of an image.
Original
1+----------------------+ 2| | 3| Entire Image | 4| | 5+----------------------+
Random Crop
1+-----------+ 2| Selected | 3| Region | 4+-----------+
The model learns to recognize objects even when only part of the object is visible.
Example
1transform = transforms.RandomCrop(224)
With padding:
1transform = transforms.RandomCrop( 2 224, 3 padding=10 4)
Advantages
- Improves robustness
- Prevents memorization
- Encourages spatial invariance
4. RandomHorizontalFlip
What is Random Flip?
Randomly flips the image horizontally.
Before
1🐶 →
After
1← 🐶
Useful for:
- Animals
- Faces
- Vehicles
- Everyday objects
Example
1transform = transforms.RandomHorizontalFlip( 2 p=0.5 3)
p=0.5 means there is a 50% chance that the image will be flipped.
RandomVerticalFlip
1transform = transforms.RandomVerticalFlip( 2 p=0.5 3)
Useful for aerial or satellite images, but usually not recommended for natural image classification tasks.
5. RandomRotation
What is Random Rotation?
Randomly rotates the image.
1Original 2 3□ 4 5↓ 6 715° 8 9↗ 10 11↓ 12 13-20° 14 15↘
The model learns rotational invariance.
Example
1transform = transforms.RandomRotation( 2 degrees=30 3)
Random rotation between:
1-30° 2 3to 4 5+30°
Advantages
- Prevents overfitting
- Handles tilted objects
- Improves generalization
6. ColorJitter
What is ColorJitter?
Randomly changes:
- Brightness
- Contrast
- Saturation
- Hue
Different lighting conditions improve robustness.
Example
1transform = transforms.ColorJitter( 2 3 brightness=0.3, 4 5 contrast=0.3, 6 7 saturation=0.3, 8 9 hue=0.1 10)
Effect
Original
1Normal Lighting
Augmented
1Brighter 2 3Darker 4 5Higher Contrast 6 7Lower Contrast
Advantages
- Better lighting robustness
- Helps outdoor datasets
- Useful for mobile images
7. RandomErasing
What is RandomErasing?
Randomly removes a rectangular region from the image.
Original
1+--------------+ 2| | 3| Object | 4| | 5+--------------+
After
1+--------------+ 2| | 3| █████ | 4| | 5+--------------+
The model learns to recognize partially occluded objects.
Example
1transform = transforms.Compose([ 2 3 transforms.ToTensor(), 4 5 transforms.RandomErasing( 6 p=0.5 7 ) 8])
Advantages
- Handles occlusions
- Improves robustness
- Reduces overfitting
Combining Transformations
Most real-world projects combine multiple augmentations.
Example:
1from torchvision import transforms 2 3train_transform = transforms.Compose([ 4 5 transforms.Resize((256,256)), 6 7 transforms.RandomCrop(224), 8 9 transforms.RandomHorizontalFlip(), 10 11 transforms.RandomRotation(15), 12 13 transforms.ColorJitter( 14 15 brightness=0.2, 16 17 contrast=0.2, 18 19 saturation=0.2, 20 21 hue=0.05 22 ), 23 24 transforms.ToTensor(), 25 26 transforms.Normalize( 27 28 mean=[0.485,0.456,0.406], 29 30 std=[0.229,0.224,0.225] 31 ), 32 33 transforms.RandomErasing( 34 p=0.5 35 ) 36])
Validation Transform
Do not apply random augmentations during validation or testing.
1val_transform = transforms.Compose([ 2 3 transforms.Resize((224,224)), 4 5 transforms.ToTensor(), 6 7 transforms.Normalize( 8 9 mean=[0.485,0.456,0.406], 10 11 std=[0.229,0.224,0.225] 12 ) 13])
Validation data should remain consistent to provide reliable performance metrics.
Using Transform in Dataset
1from torchvision.datasets import ImageFolder 2 3train_dataset = ImageFolder( 4 5 root="train", 6 7 transform=train_transform 8) 9 10val_dataset = ImageFolder( 11 12 root="validation", 13 14 transform=val_transform 15)
Creating DataLoader
1from torch.utils.data import DataLoader 2 3train_loader = DataLoader( 4 5 train_dataset, 6 7 batch_size=32, 8 9 shuffle=True, 10 11 num_workers=4, 12 13 pin_memory=True 14) 15 16val_loader = DataLoader( 17 18 val_dataset, 19 20 batch_size=32, 21 22 shuffle=False 23)
Complete Image Augmentation Pipeline
1import torch 2from torchvision import datasets, transforms 3from torch.utils.data import DataLoader 4 5# Training transforms 6train_transform = transforms.Compose([ 7 transforms.Resize((256, 256)), 8 transforms.RandomCrop(224), 9 transforms.RandomHorizontalFlip(p=0.5), 10 transforms.RandomRotation(15), 11 transforms.ColorJitter( 12 brightness=0.2, 13 contrast=0.2, 14 saturation=0.2, 15 hue=0.05 16 ), 17 transforms.ToTensor(), 18 transforms.Normalize( 19 mean=[0.485, 0.456, 0.406], 20 std=[0.229, 0.224, 0.225] 21 ), 22 transforms.RandomErasing(p=0.3) 23]) 24 25# Validation transforms 26val_transform = transforms.Compose([ 27 transforms.Resize((224,224)), 28 transforms.ToTensor(), 29 transforms.Normalize( 30 mean=[0.485,0.456,0.406], 31 std=[0.229,0.224,0.225] 32 ) 33]) 34 35# Datasets 36train_dataset = datasets.ImageFolder( 37 "train", 38 transform=train_transform 39) 40 41val_dataset = datasets.ImageFolder( 42 "validation", 43 transform=val_transform 44) 45 46# DataLoaders 47train_loader = DataLoader( 48 train_dataset, 49 batch_size=32, 50 shuffle=True, 51 num_workers=4, 52 pin_memory=True 53) 54 55val_loader = DataLoader( 56 val_dataset, 57 batch_size=32, 58 shuffle=False 59) 60 61# Iterate through one batch 62for images, labels in train_loader: 63 print(images.shape) 64 print(labels.shape) 65 break
Output
1torch.Size([32, 3, 224, 224]) 2 3torch.Size([32])
Common Transformations
| Transform | Purpose |
|---|---|
| Resize | Resize image |
| CenterCrop | Crop image from center |
| RandomCrop | Random crop |
| RandomHorizontalFlip | Flip horizontally |
| RandomVerticalFlip | Flip vertically |
| RandomRotation | Rotate image |
| ColorJitter | Adjust brightness, contrast, saturation, and hue |
| RandomAffine | Apply affine transformations |
| RandomPerspective | Simulate perspective distortion |
| GaussianBlur | Blur the image |
| RandomGrayscale | Convert to grayscale randomly |
| ToTensor | Convert image to tensor |
| Normalize | Normalize pixel values |
| RandomErasing | Randomly erase image regions |
Best Practices
- Resize all images to a consistent size before training.
- Apply random augmentations only to the training dataset.
- Normalize images using the statistics expected by your pretrained model (such as ImageNet mean and standard deviation).
- Combine multiple augmentations using
transforms.Compose(). - Avoid excessive augmentation that changes the semantic meaning of the image.
- Use
RandomErasingto improve robustness against partial occlusions. - Keep validation and test preprocessing deterministic for fair evaluation.
Practice Project: Image Augmentation Pipeline
Objective: Build a robust image preprocessing pipeline for an image classification task.
Steps
- Load images using
ImageFolder. - Resize images to
256×256. - Apply
RandomCrop(224). - Apply
RandomHorizontalFlip(). - Apply
RandomRotation(20). - Apply
ColorJitter(). - Convert images to tensors using
ToTensor(). - Normalize using ImageNet statistics.
- Create a
DataLoader. - Display one batch of augmented images and labels.
Module Summary
In this module, you learned:
- ✅ What Data Augmentation is and why it improves model generalization.
- ✅ How to use torchvision.transforms for preprocessing and augmentation.
- ✅ How Resize, Normalize, RandomCrop, RandomHorizontalFlip, RandomRotation, ColorJitter, and RandomErasing work.
- ✅ How to combine multiple transformations using
transforms.Compose(). - ✅ Why training, validation, and test datasets should use different preprocessing pipelines.
- ✅ How to build a complete Image Augmentation Pipeline using
ImageFolderandDataLoader.
By mastering data augmentation, you can significantly reduce overfitting and build computer vision models that perform better on unseen real-world data.