1
votes

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 = Image.open(single_image_path)
        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.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

batch_size = 4

trainset = cows_train
trainloader =    torch.utils.data.DataLoader(dataset = trainset, batch_size=10,

shuffle=True, num_workers=2)

classes = ('cow_left', 'cow_other')

As Output I receive:

TypeError Traceback (most recent call last)
<ipython-input-6-54702f98a725> in <module>()
      6 
      7 trainset = cows_train
----> 8 trainloader = torch.utils.data.DataLoader(dataset = trainset, batch_size=10, shuffle=True, num_workers=2)
      9 
     10 testset = cows_test

2 frames
/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in __init__(self, dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn, multiprocessing_context, generator, prefetch_factor, persistent_workers)
    264                     # Cannot statically verify that dataset is Sized
    265                     # Somewhat related: see NOTE [ Lack of Default `__len__` in Python Abstract Base Classes ]
--> 266                     sampler = RandomSampler(dataset, generator=generator)  # type: ignore
    267                 else:
    268                     sampler = SequentialSampler(dataset)

/usr/local/lib/python3.7/dist-packages/torch/utils/data/sampler.py in __init__(self, data_source, replacement, num_samples, generator)
    100                              "since a random permute will be performed.")
    101 
--> 102         if not isinstance(self.num_samples, int) or self.num_samples <= 0:
    103             raise ValueError("num_samples should be a positive integer "
    104                              "value, but got num_samples={}".format(self.num_samples))

/usr/local/lib/python3.7/dist-packages/torch/utils/data/sampler.py in num_samples(self)
    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
    112 

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
1
You should post the code that causes the actual error or it will be very hard to debug. In this case, you need to post the code block that contains return len(self.data_source) - DerekG
Might be this one: (?) def num_samples(self) -> int: if self._num_samples is None: return len(self.data_source) return self._num_samples - Fredrik
Code didn't display. Please add to original question - DerekG

1 Answers

1
votes

You are not creating your dataset object correctly. Currently, you do:

trainset = cows_train

This only assigns the class type to trainset. To create an object of the class, you need to use:

folder_path = '/path/to/dataset/'
trainset = cows_train(folder_path)