TypeError: 'NoneType' object cannot be interpreted as an integer
I want to classify cat and dog with pytorch. So I downloaded dataset from Kaggle, and separate train/validate set. I changed the file name from 00001.jpg to cat.00001.jpg..
But when I try to use enumerate(dataset), this error occurs:
My dataset code is:
class TrainImageFolder(Dataset):
    def __init__(self, path, transform=None):
        self.transform = transform
        self.path = path
        self.image = []
        self.label = []
        for i in os.listdir(self.path):
            self.image.append(i)
            if i.startswith(&quot;cat&quot;):
                self.label.append(0)
            elif i.startswith(&quot;dog&quot;):
                self.label.append(1)
        assert len(self.label) == len(self.image)

    def __len__(self):
        len(self.image)

    def __getitem__(self, index):
        label = self.label[index]
        img = Image.open(self.image[index]).convert(&quot;RGB&quot;)
        if self.transform:
            img = self.transform(img)
        return img, label

train_transform = transforms.Compose([transforms.Resize((224, 224)),
                                transforms.RandomHorizontalFlip(),
                                transforms.ToTensor(),
                                transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

train_dataset = TrainImageFolder('train', transform=train_transform)
train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=False)

for i, (imgs, labels) in tqdm(enumerate(train_dataloader)):
    print(labels)
    

and error is:
Traceback (most recent call last):
  File &quot;C:/Users/ge971/PycharmProjects/myVGG16/dataset.py&quot;, line 148, in &lt;module&gt;
    for i, (imgs, labels) in tqdm(enumerate(train_dataloader)):
  File &quot;C:\Users\ge971\miniconda3\envs\torch17\lib\site-packages\tqdm\std.py&quot;, line 1166, in __iter__
    for obj in iterable:
  File &quot;C:\Users\ge971\miniconda3\envs\torch17\lib\site-packages\torch\utils\data\dataloader.py&quot;, line 435, in __next__
    data = self._next_data()
  File &quot;C:\Users\ge971\miniconda3\envs\torch17\lib\site-packages\torch\utils\data\dataloader.py&quot;, line 474, in _next_data
    index = self._next_index()  # may raise StopIteration
  File &quot;C:\Users\ge971\miniconda3\envs\torch17\lib\site-packages\torch\utils\data\dataloader.py&quot;, line 427, in _next_index
    return next(self._sampler_iter)  # may raise StopIteration
  File &quot;C:\Users\ge971\miniconda3\envs\torch17\lib\site-packages\torch\utils\data\sampler.py&quot;, line 227, in __iter__
    for idx in self.sampler:
  File &quot;C:\Users\ge971\miniconda3\envs\torch17\lib\site-packages\torch\utils\data\sampler.py&quot;, line 67, in __iter__
    return iter(range(len(self.data_source)))
TypeError: 'NoneType' object cannot be interpreted as an integer

Process finished with exit code 1

Could you let me know how to fix the error?

I just had the same error, you forgot to return the length of the data in len function.
