PyTorch数据加载器错误:类型“type”的对象没有len()

kh212irz  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(202)

我对编程很陌生,现在知道我的错误是从哪里来的了。
我得到了下面的代码来设置我的数据集,以训练我的分类器:

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

下面是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')

作为输出,我收到:

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()

问题是:我不明白为什么typ没有长度,在我眼里它的定义是......有人能帮帮忙吗?
Add:这是代码中出现“return len(self.data_source)”的地方

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

enxuqcxy1#

您没有正确创建数据集对象。当前,您执行以下操作:

trainset = cows_train

这只是将类类型赋给trainset,要创建该类的对象,需要用途:

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

kmpatx3s2#

我之前也有过类似的错误,去掉shuffle=True就解决了,不知道为什么。

相关问题