n __getattr__ raise AttributeError(name) AttributeError: __array_interface__

今天写一个类读取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)把这一行删掉就好了,也不知道为什么会报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值