What is mean by (AttributeError: 'NoneType' object has no attribute '__array_interface__') error?

What is mean by (AttributeError: 'NoneType' object has no attribute '__array_interface__') error?

我正在尝试构建一个 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__'

基本上它说当执行行 image = Image.fromarray(image) 时,Image.fromarray 函数期望 image 是一个数组并且 image 实现一个名为 [=14 的函数=] 将自己变成图像。然而,在执行过程中 image 实际上是 None (一个 python 对象类型没有意义)。当然你不能把 None 变成图像。

您的数据可能有问题。我建议不要先进行随机拆分,然后检查数据集中的每个项目是否不是 None.