Challenge - 5 Problems
Compose Transforms Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of composed image transforms
What is the output shape of the tensor after applying the composed transforms to a 28x28 grayscale image?
PyTorch
from torchvision import transforms from PIL import Image import torch # Create a dummy grayscale image of size 28x28 image = Image.new('L', (28, 28)) # Compose transforms: Resize to 32x32, convert to tensor, normalize transform = transforms.Compose([ transforms.Resize((32, 32)), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)) ]) transformed_image = transform(image) output_shape = transformed_image.shape
Attempts:
2 left
💡 Hint
Think about the number of channels in a grayscale image and what Resize and ToTensor do.
✗ Incorrect
The original image is grayscale, so it has 1 channel. Resize changes the spatial size to 32x32. ToTensor converts the image to a tensor with shape (channels, height, width). Normalize does not change the shape. So the output shape is (1, 32, 32).
❓ Model Choice
intermediate2:00remaining
Choosing the correct transform composition for data augmentation
Which composed transform will correctly apply random horizontal flip and convert the image to a tensor for training a model?
Attempts:
2 left
💡 Hint
Remember the order of transforms matters: image operations before tensor conversion.
✗ Incorrect
RandomHorizontalFlip expects a PIL image input, so it must come before ToTensor. Option D correctly composes the transforms in the right order. Options B, C, and D are incorrect because they either apply tensor transforms before image transforms or misuse the API.
❓ Hyperparameter
advanced2:00remaining
Effect of probability parameter in RandomHorizontalFlip
What is the effect of setting the probability parameter p=0.0 in transforms.RandomHorizontalFlip() inside a Compose?
Attempts:
2 left
💡 Hint
Probability p controls how often the flip happens.
✗ Incorrect
The parameter p in RandomHorizontalFlip controls the probability of flipping. p=0.0 means no flips occur, so images remain unchanged. p=1.0 would flip all images. p=0.5 is the default for random flipping.
🔧 Debug
advanced2:00remaining
Identify the error in this Compose transform code
What error will this code raise when applying the transform to a PIL image?
PyTorch
from torchvision import transforms transform = transforms.Compose([ transforms.ToTensor(), transforms.RandomRotation(30) ]) from PIL import Image image = Image.new('RGB', (64, 64)) transformed = transform(image)
Attempts:
2 left
💡 Hint
Check the expected input types for RandomRotation.
✗ Incorrect
RandomRotation expects a PIL Image or numpy ndarray as input. Since ToTensor converts the image to a tensor first, RandomRotation receives a tensor and raises a TypeError.
🧠 Conceptual
expert2:00remaining
Understanding Compose transform order impact on model input
Why is the order of transforms in torchvision.transforms.Compose critical when preparing data for a neural network?
Attempts:
2 left
💡 Hint
Think about input types each transform expects and produces.
✗ Incorrect
Transforms like RandomRotation or RandomHorizontalFlip expect PIL images or numpy arrays, while ToTensor converts images to tensors. Applying tensor-only transforms before image transforms causes errors or incorrect data. Hence, order matters to ensure correct data flow.