0
0
PyTorchml~20 mins

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

Choose your learning style9 modes available
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.