PyTorch DataLoader Error: object of type ‘type’ has no len()

  dataloader, python, pytorch

I’m quite new to programming and have now clue where my error comes from.

I got the following code to set up my dataset for training my classifier:

class cows_train(Dataset):

def __init__(self, folder_path):
    self.image_list = glob.glob(folder_path+'/content/cows/train')
    self.data_len = len(self.image_list)

def __getitem__(self, index):
    single_image_path = self.image_list[index]
    im_as_im =
    im_as_np = np.asarray(im_as_im)/255
    im_as_np = np.expand_dims(im_as_np, 0)
    im_as_ten = torch.from_numpy(im_as_np).float()
    class_indicator_location = single_image_path.rfind('/content/cows/train/_annotations.csv')
    label = int(single_image_path[class_indicator_location+2:class_indicator_location+3])
    return (im_as_ten, label)

def __len__(self):
    return self.data_len

And this for the DataLoader:

transform = transforms.Compose(
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

batch_size = 4

trainset = cows_train trainloader = = trainset, batch_size=10,
shuffle=True, num_workers=2)

classes = (‘cow_left’, ‘cow_other’)

As Output I receive:

————————————————————————— TypeError Traceback (most recent call
last) in ()
7 trainset = cows_train
—-> 8 trainloader = = trainset, batch_size=10, shuffle=True, num_workers=2)
10 testset = cows_test

2 frames
/usr/local/lib/python3.7/dist-packages/torch/utils/data/ in
108 # dataset size might change at runtime
109 if self._num_samples is None:
–> 110 return len(self.data_source)
111 return self._num_samples

TypeError: object of type ‘type’ has no len()

Problem is: I don’t understand why typ has no length, in my eyes it’s defined… Someone please help?

Add: This is where "return len(self.data_source)" shows up in the code

def num_samples(self) -> int:
    if self._num_samples is None:
        return len(self.data_source)
    return self._num_samples

Source: Python Questions