0
0
PyTorchml~10 mins

Data transforms in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to apply a random horizontal flip to an image tensor using PyTorch transforms.

PyTorch
transform = torchvision.transforms.RandomHorizontalFlip(p=[1])
Drag options to blanks, or click blank then click option'
A0
B1.0
C0.5
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1.0 flips every image, which might be too much.
Using 0 means no flip happens.
2fill in blank
medium

Complete the code to convert a PIL image to a tensor using PyTorch transforms.

PyTorch
transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])])
image_tensor = transform([1])
Drag options to blanks, or click blank then click option'
Aimage_pil
Bimage_path
Cimage_tensor
Dimage_array
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a file path string instead of an image object.
Passing an already converted tensor.
3fill in blank
hard

Fix the error in the code to resize an image to 128x128 pixels using PyTorch transforms.

PyTorch
transform = torchvision.transforms.Resize([1])
Drag options to blanks, or click blank then click option'
A128
B128, 128
C[128, 128]
D(128, 128)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list instead of a tuple.
Passing two separate integers without parentheses.
4fill in blank
hard

Fill both blanks to create a transform that converts an image to grayscale and then to a tensor.

PyTorch
transform = torchvision.transforms.Compose([torchvision.transforms.[1](), torchvision.transforms.[2]()])
Drag options to blanks, or click blank then click option'
AGrayscale
BToTensor
CResize
DNormalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using Resize or Normalize instead of Grayscale.
Switching the order of transforms.
5fill in blank
hard

Fill all three blanks to create a transform pipeline that resizes an image to 64x64, converts it to a tensor, and normalizes it with mean 0.5 and std 0.5.

PyTorch
transform = torchvision.transforms.Compose([
    torchvision.transforms.[1]([2]),
    torchvision.transforms.[3](),
    torchvision.transforms.Normalize(mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5])
])
Drag options to blanks, or click blank then click option'
AResize
B(64, 64)
CToTensor
DGrayscale
Attempts:
3 left
💡 Hint
Common Mistakes
Using Grayscale instead of ToTensor.
Passing size as a single integer instead of a tuple.