WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Intermediate · question 36 of 100

How do you implement a custom DataLoader in PyTorch for a dataset that is not natively supported?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

If you have a dataset that is not natively supported in PyTorch, you can implement a custom DataLoader by creating a subclass of the ‘torch.utils.data.Dataset‘ class that defines the logic for loading and preprocessing your data. Here is an example implementation of a custom DataLoader for a fictitious dataset of images that are stored in separate folders based on their labels:

import os
from PIL import Image
from torch.utils.data import Dataset, DataLoader

class CustomDataset(Dataset):
    def __init__(self, root_dir, transform=None):
        self.root_dir = root_dir
        self.transform = transform
        self.labels = os.listdir(root_dir)
        
    def __len__(self):
        return sum(len(os.listdir(os.path.join(self.root_dir, label))) for label in self.labels)
        
    def __getitem__(self, idx):
        label = self.labels[idx % len(self.labels)]
        label_path = os.path.join(self.root_dir, label)
        img_path = os.path.join(label_path, os.listdir(label_path)[idx // len(self.labels)])
        img = Image.open(img_path)
        if self.transform:
            img = self.transform(img)
        return img, label

# Example usage:
custom_dataset = CustomDataset('path/to/data', transform=transforms.ToTensor())
custom_dataloader = DataLoader(custom_dataset, batch_size=32, shuffle=True)

In this example, the ‘CustomDataset‘ class takes in the dataset root directory and an optional image transformation (e.g. converting the image to a tensor) in its ‘__init__‘ method. The ‘_len__‘ method returns the length of the dataset, which is the sum of the number of images in each label folder. The ‘__getitem__‘ method loads an image and its corresponding label based on the provided index ‘idx‘. Finally, we create an instance of ‘CustomDataset‘ with the appropriate arguments and use it to create a ‘DataLoader‘ instance, which allows us to iterate over minibatches of the data during training.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics