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
Recall & Review
beginner
What is data augmentation in machine learning?
Data augmentation is a technique to increase the diversity of training data by applying random transformations like rotation, flipping, or scaling. This helps models learn better and avoid overfitting.
Click to reveal answer
beginner
Name three common image transformations used in PyTorch for data augmentation.
Common transformations include RandomHorizontalFlip (flips image horizontally), RandomRotation (rotates image by a random angle), and ColorJitter (changes brightness, contrast, saturation).
Click to reveal answer
intermediate
How does RandomHorizontalFlip help improve model performance?
RandomHorizontalFlip flips images horizontally at random during training. This teaches the model to recognize objects regardless of left-right orientation, improving generalization.
Click to reveal answer
intermediate
Explain the role of Compose in PyTorch transforms.
Compose lets you chain multiple transformations together. For example, you can rotate, flip, and normalize images in one step by passing a list of transforms to Compose.
Click to reveal answer
intermediate
What is the difference between transforms applied during training and testing?
During training, random transforms are applied to augment data and improve robustness. During testing, deterministic transforms like resizing and normalization are used to keep evaluation consistent.
Click to reveal answer
Which PyTorch transform randomly flips an image horizontally?
ARandomHorizontalFlip
BRandomRotation
CColorJitter
DNormalize
✗ Incorrect
RandomHorizontalFlip flips images horizontally at random during training.
What does the Compose transform do in PyTorch?
AApplies a single transformation
BChains multiple transformations together
CNormalizes images
DLoads images from disk
✗ Incorrect
Compose chains multiple transforms so they apply sequentially.
Why do we apply data augmentation only during training?
ATo make the model robust by showing varied data
BTo speed up training
CTo increase test accuracy
DTo reduce dataset size
✗ Incorrect
Augmentation shows varied data to the model, helping it generalize better.
Which transform would you use to randomly change image brightness?
AToTensor
BRandomRotation
CRandomCrop
DColorJitter
✗ Incorrect
ColorJitter can randomly change brightness, contrast, and saturation.
What is a common transform applied during both training and testing?
ARandomHorizontalFlip
BRandomRotation
CNormalization
DColorJitter
✗ Incorrect
Normalization is applied during testing to keep input consistent.
Describe how you would set up data augmentation for training an image classifier using PyTorch transforms.
Think about the sequence of transformations to increase data variety.
You got /4 concepts.
Explain why data augmentation helps prevent overfitting in machine learning models.
Consider how seeing different versions of data affects learning.
You got /4 concepts.
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
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.
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.
Final Answer:
To combine multiple image transformations into one pipeline -> Option A
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
Step 1: Check the syntax for combining transforms
PyTorch requires transforms to be passed as a list inside transforms.Compose([]).
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.
Final Answer:
transforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()]) -> Option B
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?
RandomRotation rotates the image but does not change its size or channels. ToTensor converts the image to a tensor with shape [channels, height, width].
Step 2: Determine output shape
Input image is 3 channels, 64x64 pixels. After ToTensor, shape is [3, 64, 64]. Rotation keeps size same.
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
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.
Step 2: Identify the missing parentheses
transforms.ToTensor is a class, but it must be called as transforms.ToTensor() to create the transform instance.
Final Answer:
transforms.ToTensor is missing parentheses to call it -> Option A
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
Step 1: Order of transforms matters
Data augmentation like flipping and rotation must happen before converting to tensor. Normalization happens after ToTensor.
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.