0
0
PyTorchml~20 mins

Custom detection dataset in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Detection Dataset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Custom Dataset __getitem__ method
Given the following PyTorch custom dataset code snippet, what is the output type of the __getitem__ method when called with an index?
PyTorch
from torch.utils.data import Dataset

class CustomDetectionDataset(Dataset):
    def __init__(self, images, annotations):
        self.images = images
        self.annotations = annotations

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        image = self.images[idx]
        target = self.annotations[idx]
        return image, target
AA list containing image and target tensors
BOnly the image tensor at the given index
COnly the target dictionary at the given index
DA tuple containing (image, target) where image is a tensor and target is a dictionary
Attempts:
2 left
💡 Hint
Look at what the __getitem__ method returns explicitly.
data_output
intermediate
1:30remaining
Number of items in Custom Detection Dataset
If a CustomDetectionDataset is initialized with a list of 150 images and a list of 150 corresponding annotations, what will be the output of len(dataset)?
PyTorch
dataset = CustomDetectionDataset(images=[...150 images...], annotations=[...150 annotations...])
print(len(dataset))
A150
B0
CDepends on the length of annotations only
DRaises a TypeError
Attempts:
2 left
💡 Hint
Check the __len__ method implementation.
🔧 Debug
advanced
2:30remaining
Fixing a KeyError in Custom Detection Dataset
Consider this code snippet inside a custom dataset __getitem__ method: image = self.images[idx] target = self.annotations[idx] boxes = target['boxes'] Sometimes, running this code raises a KeyError: 'boxes'. Which option fixes this error by ensuring 'boxes' key exists in target?
PyTorch
def __getitem__(self, idx):
    image = self.images[idx]
    target = self.annotations[idx]
    boxes = target['boxes']
    return image, target
AUse boxes = target.get('boxes', torch.empty((0,4))) to provide an empty tensor if 'boxes' is missing
BReplace boxes = target['boxes'] with boxes = target['box']
CRemove the line boxes = target['boxes'] entirely
DChange target = self.annotations[idx] to target = self.annotations.get(idx)
Attempts:
2 left
💡 Hint
Use a safe way to access dictionary keys that might be missing.
🚀 Application
advanced
3:00remaining
Transforming Images in Custom Detection Dataset
You want to apply a random horizontal flip to images and their bounding boxes in your custom detection dataset. Which approach correctly applies the transform inside __getitem__?
PyTorch
def __getitem__(self, idx):
    image = self.images[idx]
    target = self.annotations[idx]
    if random.random() > 0.5:
        # Apply horizontal flip to image and boxes
        image = image.flip(-1)
        boxes = target['boxes']
        width = image.shape[-1]
        boxes[:, [0, 2]] = width - boxes[:, [2, 0]]
        target['boxes'] = boxes
    return image, target
AThe boxes coordinates are not updated correctly after flip
BThe image.flip(-1) should be image.flip(0) to flip horizontally
CThis code correctly flips both image and bounding boxes horizontally
DThe target dictionary should not be modified inside __getitem__
Attempts:
2 left
💡 Hint
Check how image.flip and box coordinate updates correspond to horizontal flip.
🧠 Conceptual
expert
2:00remaining
Why use a custom detection dataset in PyTorch?
Which of the following is the best reason to create a custom detection dataset class in PyTorch instead of using a built-in dataset?
ATo automatically generate synthetic images
BTo handle unique image and annotation formats not supported by built-in datasets
CTo avoid using DataLoader for batching
DBecause built-in datasets are slower to load images
Attempts:
2 left
💡 Hint
Think about dataset flexibility and formats.