我正在尝试建立一个ML模型来检测卡通图像脸部的地标。当我将图像数据集分成训练集和验证集时,我得到了以下错误。在这里,我使用pytorch来构建模型。那么这个错误是什么意思呢?
这就是我拆分数据集的方式。
# split the dataset into validation and test sets
len_valid_set = int(0.2*len(dataset))
len_train_set = len(dataset) - len_valid_set
print("The length of Train set is {}".format(len_train_set))
print("The length of Valid set is {}".format(len_valid_set))
train_dataset , valid_dataset, = torch.utils.data.random_split(dataset , [len_train_set, len_valid_set])
# shuffle and batch the datasets
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4)
valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=8, shuffle=True, num_workers=4)
images, landmarks = next(iter(train_loader))
这是我得到的错误。
The length of Train set is 105
The length of Valid set is 26
AttributeError Traceback (most recent call last)
<ipython-input-61-ffb86a628e37> in <module>()
----> 1 images, landmarks = next(iter(train_loader))
2
3 print(images.shape)
4 print(landmarks.shape)
3 frames
/usr/local/lib/python3.6/dist-packages/torch/_utils.py in reraise(self)
426 # have message field
427 raise self.exc_type(message=msg)
--> 428 raise self.exc_type(msg)
429
430
AttributeError: Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 198, in _worker_loop
data = fetcher.fetch(index)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataset.py", line 272, in __getitem__
return self.dataset[self.indices[idx]]
File "<ipython-input-12-5595ac89d75d>", line 38, in __getitem__
image, landmarks = self.transform(image, landmarks, self.crops[index])
File "<ipython-input-9-e38df55ee0d4>", line 46, in __call__
image = Image.fromarray(image)
File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 2670, in fromarray
arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'
发布于 2020-12-02 20:39:19
基本上,它说明在执行行image = Image.fromarray(image)
时,Image.fromarray
函数期望image
是一个数组,而image
实现了一个名为__array_interface__
的函数,该函数会将自身转换为图像。然而,在执行过程中,image
实际上是None
(一个空的python对象类型)。当然,你不能把None
变成一个图像。
您的数据可能有问题。我建议不要先进行随机拆分,检查数据集中的每个项目是否不是None
。
https://stackoverflow.com/questions/65091965
复制相似问题