1

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?

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

5 Comments

What if I just return the length of self.X? Will it work? I guess it only needs the length so that it can iterate over the values. I know for a fact that length of X and y is the same
Then that should be fine. That's more of an implementation detail for you to decide. The error is your attempting to return two values when the Dataloader is treating it as a single int and failing if it's not.
Can you tell me why did it work without the DataLoader? Is there a difference there?
Because the DataLoader is expecting a single value of type int for the length property. Likely because it's adding or using the value from __len__ to do some math. If the value if not an integer then the math fails. For example running (1,2)+1 fails because you can't preform math on a type tuple
Makes sense. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.