Sarvagya Gupta
Sarvagya Gupta

Reputation: 907

TypeError: 'tuple' object cannot be interpreted as an integer while creating data generators PyTorch

I am new to PyTorch and I am learning to create batches of data for segmentation. The code is shown below:

class NumbersDataset(Dataset):
    def __init__(self):
        self.X = list(df['input_img'])
        self.y = list(df['mask_img'])

    def __len__(self):
        return len(self.X), len(self.y)

    def __getitem__(self, idx):
        return self.X[idx], self.y[idx]


if __name__ == '__main__':
    dataset = NumbersDataset()
    dataloader = DataLoader(dataset, batch_size=50, shuffle=True, num_workers=2)
    # print(len(dataset))
    # plt.imshow(dataset[100])
    # plt.show()
    print(next(iter(dataloader)))

where df['input_img'] column contains the location of the image ('/path/to/pic/480p/boxing-fisheye/00010.jpg') and df['mask_img'] contains the location of all the mask images. I am trying to load the images but I get the error:

TypeError: 'tuple' object cannot be interpreted as an integer

However, if I don't use DataLoader and just do the following:

     dataset = NumbersDataset()
     print(len(dataset))
     print(dataset[10:20])

then I get what I expect. Can someone tell me what am I doing wrong?

Upvotes: 1

Views: 1948

Answers (1)

JBirdVegas
JBirdVegas

Reputation: 11403

You cannot return a tuple for the __len__ method. The expected type is int

# perhaps you can add the list length's for the total length
# but no matter how you choose to implement the method you can
# only return on value of type integer `int`
def __len__(self):
    return len(self.X) + len(self.y)

Upvotes: 2

Related Questions