PyTorch DataLoader 错误:“类型”类型的对象没有 len()

Posted

技术标签:

【中文标题】PyTorch DataLoader 错误:“类型”类型的对象没有 len()【英文标题】:PyTorch DataLoader Error: object of type 'type' has no len() 【发布时间】:2021-08-03 19:30:04 【问题描述】:

我对编程很陌生,现在知道我的错误来自哪里。

我得到以下代码来设置我的数据集以训练我的分类器:

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 没有长度,在我眼里它是被定义的...有人请帮忙?

添加:这是代码中出现“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

【问题讨论】:

您应该发布导致实际错误的代码,否则将很难调试。在这种情况下,您需要发布包含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 代码未显示。请添加到原始问题 【参考方案1】:

您没有正确创建数据集对象。目前,您可以:

trainset = cows_train

这只会将类类型分配给trainset。要创建该类的对象,您需要使用:

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

【讨论】:

以上是关于PyTorch DataLoader 错误:“类型”类型的对象没有 len()的主要内容,如果未能解决你的问题,请参考以下文章

通过 DataLoader (PyTorch) 迭代:RuntimeError: 标量类型 unsigned char 的预期对象但序列元素 9 的标量类型浮点数

pytorch中data.DataLoader用法

pytorch中data.DataLoader用法

pytorch中data.DataLoader用法

不使用多处理但在使用 PyTorch DataLoader 时在 google colab 上出现 CUDA 错误

pytorch COCO2017 目标检测 DataLoader