Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the PyTorch Dataset class.
PyTorch
from torch.utils.data import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing DataLoader instead of Dataset
Importing Tensor or Module which are unrelated here
✗ Incorrect
We import Dataset to create a custom dataset class in PyTorch.
2fill in blank
mediumComplete the code to initialize the dataset with image paths and annotations.
PyTorch
class CustomDataset(Dataset): def __init__(self, [1], annotations): self.images = images self.annotations = annotations
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' as a parameter name
Using 'labels' or 'data' which are not the parameter names here
✗ Incorrect
The constructor takes images and annotations as inputs.
3fill in blank
hardFix the error in the __len__ method to return dataset size.
PyTorch
def __len__(self): return [1](self.images)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which is not a built-in function for lists
Using size() or length() which are not valid Python functions
✗ Incorrect
The len() function returns the number of items in a list.
4fill in blank
hardFill both blanks to load an image and its annotation in __getitem__.
PyTorch
def __getitem__(self, idx): image = Image.open(self.images[[1]]) annotation = self.annotations[[2]] return image, annotation
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 instead of idx, which always returns the first or second item
Using 'self' which is invalid as an index
✗ Incorrect
We use the index idx to get the image path and annotation at the same position.
5fill in blank
hardFill all three blanks to apply a transform to the image if provided.
PyTorch
def __getitem__(self, idx): image = Image.open(self.images[idx]) annotation = self.annotations[idx] if [1] is not None: image = [2](image) return [3], annotation
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'transform' without self, which is undefined
Returning the original image variable name incorrectly
✗ Incorrect
We check if self.transform exists, then apply it to the image, and return the transformed image.