今天写一个类读取image时报错,代码如下:
class ARGSSL(datasets.AgriculturalDisease):
def __init__(self, data_folder, indexs,transform=None, target_transform=None,loader=default_loader):
super().__init__(data_folder,transform=transform,
target_transform=target_transform,loader=default_loader)
if indexs is not None:
self.image_paths = np.array(self.image_paths)[indexs]
self.labels = np.array(self.labels)[indexs]
def __getitem__(self, index):
image_path, label = self.image_paths[index], self.labels[index]
image = Image.open(image_path).convert('RGB')
image = Image.fromarray(image)
if self.transform is not None:
image = self.transform(image)
if self.target_transform is not None:
label = self.target_transform(label)
return image, label
报错如下:
Exception has occurred: AttributeError
Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/home/user-wsc/code/FixMatch_agr/train.py", line 346, in train
inputs_x, targets_x = labeled_iter.next()
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 652, in __next__
data = self._next_data()
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1347, in _next_data
return self._process_data(data)
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 1373, in _process_data
data.reraise()
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/torch/_utils.py", line 461, in reraise
raise exception
AttributeError: Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop
data = fetcher.fetch(index)
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 49, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/user-wsc/code/FixMatch_agr/dataset/cifar.py", line 194, in __getitem__
image = Image.fromarray(image)
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/PIL/Image.py", line 2803, in fromarray
arr = obj.__array_interface__
File "/home/user-wsc/anaconda3/envs/yjhtorch16/lib/python3.8/site-packages/PIL/Image.py", line 519, in __getattr__
raise AttributeError(name)
AttributeError: __array_interface_
在网上查了一下午也没有解决,后来 image = Image.fromarray(image)
把这一行删掉就好了,也不知道为什么会报错。