Bird
Raised Fist0
PyTorchml~5 mins

Data augmentation with transforms in PyTorch

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using transforms.Compose in PyTorch data augmentation?
easy
A. To combine multiple image transformations into one pipeline
B. To train the model faster by skipping data loading
C. To convert images into numpy arrays
D. To save the augmented images to disk automatically

Solution

  1. Step 1: Understand the role of transforms.Compose

    transforms.Compose is used to chain several image transformations so they apply sequentially to the input image.
  2. Step 2: Identify the correct purpose

    It does not speed up training directly, convert images to numpy, or save images. Its main job is combining transformations.
  3. Final Answer:

    To combine multiple image transformations into one pipeline -> Option A
  4. Quick Check:

    transforms.Compose = combine transforms [OK]
Hint: Remember Compose chains transforms in order [OK]
Common Mistakes:
  • Thinking Compose speeds up training
  • Confusing Compose with image saving
  • Assuming Compose converts image formats
2. Which of the following is the correct way to apply a horizontal flip and convert an image to a tensor using PyTorch transforms?
easy
A. transforms.Compose(transforms.RandomHorizontalFlip(), transforms.ToTensor())
B. transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()])
C. transforms.ToTensor(transforms.RandomHorizontalFlip())
D. transforms.RandomHorizontalFlip(transforms.ToTensor())

Solution

  1. Step 1: Check the syntax for combining transforms

    PyTorch requires transforms to be passed as a list inside transforms.Compose([]).
  2. Step 2: Validate each option

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()]) correctly uses a list inside Compose. The other options misuse function calls or pass arguments incorrectly.
  3. Final Answer:

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()]) -> Option B
  4. Quick Check:

    Compose needs list of transforms [OK]
Hint: Use Compose with a list of transforms inside brackets [OK]
Common Mistakes:
  • Passing transforms as separate arguments instead of a list
  • Calling transforms inside each other incorrectly
  • Forgetting to convert images to tensor
3. Given the following code, what will be the shape of the output tensor after applying the transforms to a 3-channel 64x64 image?
transform = transforms.Compose([
    transforms.RandomRotation(90),
    transforms.ToTensor()
])
output = transform(image)
medium
A. [1, 64, 64]
B. [64, 64, 3]
C. [3, 64, 64]
D. [64, 3, 64]

Solution

  1. Step 1: Understand the transform effects

    RandomRotation rotates the image but does not change its size or channels. ToTensor converts the image to a tensor with shape [channels, height, width].
  2. Step 2: Determine output shape

    Input image is 3 channels, 64x64 pixels. After ToTensor, shape is [3, 64, 64]. Rotation keeps size same.
  3. Final Answer:

    [3, 64, 64] -> Option C
  4. Quick Check:

    ToTensor output shape = [channels, height, width] [OK]
Hint: ToTensor outputs [channels, height, width] shape [OK]
Common Mistakes:
  • Confusing channel position in tensor shape
  • Assuming rotation changes image size
  • Thinking output is a numpy array shape
4. Identify the error in this PyTorch transform pipeline:
transform = transforms.Compose([
    transforms.RandomCrop(32),
    transforms.ToTensor,
    transforms.Normalize((0.5,), (0.5,))
])
medium
A. transforms.ToTensor is missing parentheses to call it
B. Normalize mean and std should be lists, not tuples
C. RandomCrop size should be a tuple, not an integer
D. Compose should not be used with Normalize

Solution

  1. Step 1: Check each transform usage

    RandomCrop accepts an integer for size, so that is correct. Normalize accepts tuples for mean and std, so that is correct.
  2. Step 2: Identify the missing parentheses

    transforms.ToTensor is a class, but it must be called as transforms.ToTensor() to create the transform instance.
  3. Final Answer:

    transforms.ToTensor is missing parentheses to call it -> Option A
  4. Quick Check:

    Call ToTensor() with parentheses [OK]
Hint: Always call transforms with parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on transform classes
  • Thinking Normalize needs lists instead of tuples
  • Misunderstanding RandomCrop size argument
5. You want to augment your training images by randomly flipping horizontally, rotating by up to 30 degrees, and normalizing with mean=0.5 and std=0.5 for each channel. Which transform pipeline correctly applies these steps in PyTorch?
hard
A. transforms.Compose([transforms.ToTensor(), transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.Normalize((0.5,), (0.5,))])
B. transforms.Compose([transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor()])
C. transforms.Compose([transforms.RandomRotation(30), transforms.RandomHorizontalFlip(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), transforms.ToTensor()])
D. transforms.Compose([transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

Solution

  1. Step 1: Order of transforms matters

    Data augmentation like flipping and rotation must happen before converting to tensor. Normalization happens after ToTensor.
  2. Step 2: Check each option's order and parameters

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) applies flip and rotation first, then ToTensor, then Normalize with correct mean/std for 3 channels. Others have wrong order or missing steps.
  3. Final Answer:

    transforms.Compose([transforms.RandomHorizontalFlip(), transforms.RandomRotation(30), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) -> Option D
  4. Quick Check:

    Augment before ToTensor, normalize after [OK]
Hint: Augment first, then ToTensor, then Normalize [OK]
Common Mistakes:
  • Normalizing before ToTensor
  • Applying augmentations after ToTensor
  • Using wrong mean/std shapes for Normalize