Challenge - 5 Problems
Custom Detection Dataset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Look at what the __getitem__ method returns explicitly.
✗ Incorrect
The __getitem__ method returns two objects as a tuple: the image and the target (annotation). This is a common pattern in detection datasets where targets are dictionaries with bounding boxes and labels.
❓ data_output
intermediate1: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))
Attempts:
2 left
💡 Hint
Check the __len__ method implementation.
✗ Incorrect
The __len__ method returns the length of the images list, which is 150 in this case.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Use a safe way to access dictionary keys that might be missing.
✗ Incorrect
Using target.get('boxes', torch.empty((0,4))) returns an empty tensor if 'boxes' key is missing, preventing KeyError.
🚀 Application
advanced3: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
Attempts:
2 left
💡 Hint
Check how image.flip and box coordinate updates correspond to horizontal flip.
✗ Incorrect
image.flip(-1) flips the image horizontally. Updating boxes by width - boxes[:, [2, 0]] correctly adjusts x coordinates after flip.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about dataset flexibility and formats.
✗ Incorrect
Custom datasets allow handling of unique data formats and annotations that built-in datasets do not support.