0
0
PyTorchml~20 mins

Compose transforms in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Compose Transforms Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
Atorch.Size([1, 32, 32])
Btorch.Size([3, 28, 28])
Ctorch.Size([3, 32, 32])
Dtorch.Size([1, 28, 28])
Attempts:
2 left
💡 Hint
Think about the number of channels in a grayscale image and what Resize and ToTensor do.
Model Choice
intermediate
2: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?
Atransforms.ToTensor(transforms.RandomHorizontalFlip())
Btransforms.Compose([transforms.ToTensor(), transforms.RandomHorizontalFlip()])
Ctransforms.RandomHorizontalFlip(transforms.ToTensor())
Dtransforms.Compose([transforms.RandomHorizontalFlip(), transforms.ToTensor()])
Attempts:
2 left
💡 Hint
Remember the order of transforms matters: image operations before tensor conversion.
Hyperparameter
advanced
2: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?
AImages will never be flipped horizontally during transform.
BImages will always be flipped horizontally during transform.
CImages will be flipped horizontally with 50% chance.
DTransforms will raise an error due to invalid probability.
Attempts:
2 left
💡 Hint
Probability p controls how often the flip happens.
🔧 Debug
advanced
2: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)
ANo error, code runs successfully.
BTypeError: img should be PIL Image or ndarray. Got Tensor instead.
CAttributeError: 'Tensor' object has no attribute 'rotate'.
DValueError: angle must be a number.
Attempts:
2 left
💡 Hint
Check the expected input types for RandomRotation.
🧠 Conceptual
expert
2: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?
ABecause transforms are applied in parallel, so order is irrelevant.
BBecause the order only affects training speed, not data correctness.
CBecause some transforms expect PIL images and others expect tensors, so wrong order causes errors or wrong data.
DBecause Compose automatically sorts transforms by type, so order does not matter.
Attempts:
2 left
💡 Hint
Think about input types each transform expects and produces.