Bird
Raised Fist0
PyTorchml~12 mins

Data augmentation with transforms in PyTorch - Model Pipeline Trace

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
Model Pipeline - Data augmentation with transforms

This pipeline shows how data augmentation uses image transforms to create varied training images. This helps the model learn better by seeing different versions of the same image.

Data Flow - 3 Stages
1Original Dataset
1000 images x 3 channels x 32 height x 32 widthLoad raw images from dataset1000 images x 3 channels x 32 height x 32 width
Image of a cat with size 32x32 pixels and 3 color channels
2Apply Data Augmentation Transforms
1000 images x 3 channels x 32 height x 32 widthRandom horizontal flip, random rotation, random crop1000 images x 3 channels x 32 height x 32 width
Original cat image flipped horizontally and slightly rotated
3Batch Preparation
1000 images x 3 channels x 32 height x 32 widthGroup images into batches of 10010 batches x 100 images x 3 channels x 32 height x 32 width
Batch 1 contains 100 augmented cat and dog images
Training Trace - Epoch by Epoch

Loss
1.2 |*       
1.0 | *      
0.8 |  *     
0.6 |   *    
0.4 |    *   
    +---------
     1 2 3 4 5
     Epochs
EpochLoss ↓Accuracy ↑Observation
11.20.45Model starts learning with high loss and low accuracy
20.90.60Loss decreases and accuracy improves as model sees augmented data
30.70.72Model learns better features due to varied augmented images
40.550.80Loss continues to drop, accuracy rises steadily
50.450.85Model converges with good accuracy on augmented data
Prediction Trace - 5 Layers
Layer 1: Input Image
Layer 2: Random Horizontal Flip
Layer 3: Random Rotation
Layer 4: Random Crop
Layer 5: Normalized Tensor
Model Quiz - 3 Questions
Test your understanding
Why do we apply random horizontal flips during data augmentation?
ATo convert images to grayscale
BTo reduce the image size
CTo help the model learn to recognize objects flipped left to right
DTo increase the number of color channels
Key Insight
Data augmentation creates varied versions of images, helping the model learn more robust features. This leads to better accuracy and lower loss during training because the model sees many different views of the same objects.

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