0
0
PyTorchml~20 mins

Data transforms in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Data Transform Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output shape after applying this transform?
Consider a PyTorch tensor representing an image of shape (3, 64, 64). We apply the following transform:

transform = torchvision.transforms.Compose([torchvision.transforms.Resize(32), torchvision.transforms.CenterCrop(28)])

What will be the shape of the resulting tensor after applying transform?
PyTorch
import torch
import torchvision.transforms as T

img = torch.randn(3, 64, 64)
transform = T.Compose([T.Resize(32), T.CenterCrop(28)])
result = transform(img)
print(result.shape)
Atorch.Size([3, 64, 64])
Btorch.Size([3, 32, 32])
Ctorch.Size([1, 28, 28])
Dtorch.Size([3, 28, 28])
Attempts:
2 left
💡 Hint
Resize changes the smaller edge to 32, then CenterCrop cuts a 28x28 square from the center.
Model Choice
intermediate
2:00remaining
Which transform is best to normalize image pixels to [0,1] range?
You have image tensors with pixel values from 0 to 255. You want to normalize them to the range [0,1] before feeding into a model. Which PyTorch transform should you use?
Atorchvision.transforms.Normalize(mean=[0.5], std=[0.5])
Btorchvision.transforms.ToTensor()
Ctorchvision.transforms.Lambda(lambda x: x / 255)
Dtorchvision.transforms.Resize(256)
Attempts:
2 left
💡 Hint
One transform converts PIL images or numpy arrays to tensors and scales pixels to [0,1].
Hyperparameter
advanced
2:00remaining
Choosing mean and std for Normalize transform
You want to normalize RGB images using torchvision.transforms.Normalize. Which values for mean and std are correct if your images have pixel values in [0,1] and you want zero mean and unit variance per channel?
Amean=[0, 0, 0], std=[1, 1, 1]
Bmean=[1, 1, 1], std=[0, 0, 0]
Cmean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
Dmean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]
Attempts:
2 left
💡 Hint
Zero mean and unit variance means subtract 0 and divide by 1.
🔧 Debug
advanced
2:00remaining
Why does this transform code raise an error?
This code raises an error when applied to a tensor image. Why?

transform = torchvision.transforms.Compose([torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor()])

Assume input is a PyTorch tensor image.
PyTorch
import torch
import torchvision.transforms as T
img = torch.randn(3, 64, 64)
transform = T.Compose([T.RandomHorizontalFlip(), T.ToTensor()])
result = transform(img)
ARandomHorizontalFlip expects a PIL image or numpy array, not a tensor
BToTensor must be applied before RandomHorizontalFlip
CRandomHorizontalFlip requires the tensor to be 4D, but input is 3D
DThe code runs without error
Attempts:
2 left
💡 Hint
Check the expected input types for RandomHorizontalFlip.
🧠 Conceptual
expert
2:00remaining
Why use data transforms during training but not always during testing?
In machine learning, why do we often apply random data transforms (like flips, crops) during training but use fixed transforms during testing?
ARandom transforms are only compatible with GPUs, fixed transforms work on CPUs
BRandom transforms reduce training time, fixed transforms speed up testing
CRandom transforms increase training data diversity to improve generalization, while testing uses fixed transforms for consistent evaluation
DRandom transforms normalize data, fixed transforms augment data
Attempts:
2 left
💡 Hint
Think about how randomness affects learning and evaluation.