pythoncomputer-visionpytorchimagenet

'DataLoader' object does not support indexing


I have downloaded the ImageNet dataset via this pytorch api by setting download=True. But I cannot iterate through the dataloader.

The error says "'DataLoader' object does not support indexing"

trainset = torch.utils.data.DataLoader(
    datasets.ImageNet('/media/farshid/DataStore/temp/Imagenet/', split='train',
                      download=False))
trainloader = torch.utils.data.DataLoader(trainset, batch_size=1, shuffle=False, num_workers=1)

I tried a simple approach I just tried to run the following,

trainloader[0]

In the root directory, the pattern is

root/  
    train/  
          n01440764/
          n01443537/ 
                   n01443537_2.jpg

The docs in the official website doesnt say anything else. https://pytorch.org/docs/stable/torchvision/datasets.html#imagenet

What am I doing wrong ?


Solution

  • Solution

    input_transform = standard_transforms.Compose([
        transforms.Resize((255,255)), # to Make sure all the 
        transforms.CenterCrop(224),   # imgs are at the same size 
        transforms.ToTensor()
    ])  
    
    
    # torch.utils.data.Dataset object
    trainset = datasets.ImageNet('/media/farshid/DataStore/temp/Imagenet/',
                                 split='train', download=False, transform = input_transform)
    # torch.utils.data.DataLoader object
    trainloader =torch.utils.data.DataLoader(trainset, batch_size=2, shuffle=False)
    
    
    for batch_idx, data in enumerate(trainloader, 0):
        x, y = data 
        break