Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to apply Albumentations transformations to an image tensor.
PyTorch
import albumentations as A from albumentations.pytorch import ToTensorV2 transform = A.Compose([ A.HorizontalFlip(p=[1]), ToTensorV2() ])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a probability greater than 1 causes an error.
Using a negative probability is invalid.
✗ Incorrect
The probability parameter p in Albumentations must be between 0 and 1, so 0.5 is valid.
2fill in blank
mediumComplete the code to convert a PIL image to a numpy array before applying Albumentations.
PyTorch
from PIL import Image import numpy as np image = Image.open('image.jpg') image_np = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling non-existent methods like toarray() or numpy() on PIL images.
Using np.asarray with incomplete arguments.
✗ Incorrect
To convert a PIL Image to a numpy array, use np.array(image).
3fill in blank
hardFix the error in applying Albumentations transform to a batch of images.
PyTorch
batch_transformed = [transform(image=[1]) for image in batch_images]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing PIL images directly without conversion.
Using non-existent methods like to_tensor() or numpy() on images.
✗ Incorrect
Albumentations expects images as numpy arrays, so convert each image with np.array(image).
4fill in blank
hardFill both blanks to create a custom PyTorch Dataset that applies Albumentations transforms.
PyTorch
from torch.utils.data import Dataset class CustomDataset(Dataset): def __init__(self, images, labels, transform=None): self.images = images self.labels = labels self.transform = [1] def __getitem__(self, idx): image = self.images[idx] label = self.labels[idx] if self.transform: image = self.transform(image=[2])['image'] return image, label
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not storing the transform in __init__.
Passing the wrong variable to the transform.
✗ Incorrect
The transform is stored in self.transform and applied to the numpy array of the image.
5fill in blank
hardFill all three blanks to correctly integrate Albumentations with PyTorch DataLoader.
PyTorch
import torch from torch.utils.data import DataLoader transform = A.Compose([ A.RandomBrightnessContrast(p=[1]), ToTensorV2() ]) dataset = CustomDataset(images, labels, transform=[2]) dataloader = DataLoader(dataset, batch_size=[3], shuffle=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid probability values.
Passing wrong variables to dataset or DataLoader.
✗ Incorrect
Set probability p=0.3, pass the transform object, and use batch size 32 for DataLoader.