Complete the code to import the PyTorch Dataset class.
from torch.utils.data import [1]
We import Dataset to create a custom dataset class in PyTorch.
Complete the code to initialize the dataset with image paths and annotations.
class CustomDataset(Dataset): def __init__(self, [1], annotations): self.images = images self.annotations = annotations
The constructor takes images and annotations as inputs.
Fix the error in the __len__ method to return dataset size.
def __len__(self): return [1](self.images)
The len() function returns the number of items in a list.
Fill both blanks to load an image and its annotation in __getitem__.
def __getitem__(self, idx): image = Image.open(self.images[[1]]) annotation = self.annotations[[2]] return image, annotation
We use the index idx to get the image path and annotation at the same position.
Fill all three blanks to apply a transform to the image if provided.
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
We check if self.transform exists, then apply it to the image, and return the transformed image.
