0
0
PyTorchml~10 mins

Albumentations integration 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 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'
A2
B0.5
C-0.5
D1.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a probability greater than 1 causes an error.
Using a negative probability is invalid.
2fill in blank
medium

Complete 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'
Aimage.numpy()
Bimage.toarray()
Cnp.asarray(image.convert())
Dnp.array(image)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling non-existent methods like toarray() or numpy() on PIL images.
Using np.asarray with incomplete arguments.
3fill in blank
hard

Fix 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'
Anp.array(image)
Bimage
Cimage.numpy()
Dimage.to_tensor()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing PIL images directly without conversion.
Using non-existent methods like to_tensor() or numpy() on images.
4fill in blank
hard

Fill 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'
Atransform
Bimages
Cnp.array(image)
Dlabels
Attempts:
3 left
💡 Hint
Common Mistakes
Not storing the transform in __init__.
Passing the wrong variable to the transform.
5fill in blank
hard

Fill 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'
A0.3
Btransform
C32
Dimages
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid probability values.
Passing wrong variables to dataset or DataLoader.