Bird
Raised Fist0
PyTorchml~20 mins

Data augmentation with transforms in PyTorch - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Data Augmentation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a composed transform on an image tensor
Given the following PyTorch code applying data augmentation transforms, what is the shape of the output tensor after the transforms?
PyTorch
import torch
from torchvision import transforms
from PIL import Image

transform = transforms.Compose([
    transforms.Resize((64, 64)),
    transforms.RandomHorizontalFlip(p=1.0),
    transforms.ToTensor()
])

# Simulate a PIL image as a tensor with shape (3, 100, 100)
# Normally transforms expect PIL images, so convert tensor to PIL Image
image = Image.fromarray((torch.rand(3, 100, 100).permute(1, 2, 0).mul(255).byte().numpy()))

output = transform(image)

print(output.shape)
Atorch.Size([1, 3, 64, 64])
Btorch.Size([64, 64, 3])
Ctorch.Size([3, 64, 64])
Dtorch.Size([3, 100, 100])
Attempts:
2 left
💡 Hint
Remember that Resize changes the image size and ToTensor converts to a tensor with channels first.
Model Choice
intermediate
1:30remaining
Choosing the best augmentation for rotation invariance
You want to train a model to recognize objects regardless of their orientation. Which data augmentation transform should you add to your training pipeline to help the model learn rotation invariance?
Atransforms.RandomRotation(degrees=45)
Btransforms.RandomHorizontalFlip(p=0.5)
Ctransforms.ColorJitter(brightness=0.5)
Dtransforms.RandomCrop(size=32)
Attempts:
2 left
💡 Hint
Think about which transform changes the orientation of the image.
Hyperparameter
advanced
1:00remaining
Effect of probability parameter in RandomHorizontalFlip
In the transform transforms.RandomHorizontalFlip(p=0.7), what does the parameter p=0.7 control?
AThe probability that the image will be converted to grayscale
BThe probability that the image will be flipped horizontally during augmentation
CThe angle in degrees to flip the image
DThe scale factor for resizing the image
Attempts:
2 left
💡 Hint
Think about what 'p' usually means in random transforms.
🔧 Debug
advanced
2:00remaining
Debugging a transform pipeline error
You have this transform pipeline: transform = transforms.Compose([ transforms.ToTensor(), transforms.RandomCrop(32), transforms.Normalize(mean=[0.5], std=[0.5]) ]) When you run it on a PIL image of size 28x28, you get an error. What is the cause?
ARandomCrop(32) fails because the image is smaller than the crop size
BToTensor() must be after Normalize()
CNormalize expects 3 channels but the image has 1 channel
DTransforms.Compose requires a list of functions, not a list of transforms
Attempts:
2 left
💡 Hint
Check the image size compared to the crop size.
🧠 Conceptual
expert
1:30remaining
Why use data augmentation in training deep learning models?
Which of the following best explains why data augmentation is important when training deep learning models?
AIt removes noisy data points from the training set
BIt reduces the training time by simplifying the dataset
CIt guarantees the model will achieve 100% accuracy on the training set
DIt artificially increases the size and diversity of the training data to reduce overfitting and improve generalization
Attempts:
2 left
💡 Hint
Think about how data augmentation affects the training data and model learning.

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