TypeError: _open() got an unexpected keyword argument 'pilmode'

TypeError: _open() got an unexpected keyword argument 'pilmode'

我正在 COCO 数据集上训练 CNN 模型,在几次迭代后出现此错误。错误不一致,因为我在 1100 次迭代中得到这个错误,一次在 4500 次迭代中,一次在 8900 次迭代中(所有这些都在 1 个 epoch 中)。

我认为这个错误可能是新版imageio的一个bug,所以我将版本更改为2.3.0,但在1个epoch 8900次迭代后,我仍然收到这个错误。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-4b33bec4a89e> in <module>()
     52 
     53     # train for one epoch
---> 54     train_loss = train(train_loader, model, [criterion1, criterion2], optimizer)
     55     print('train_loss: ',train_loss)
     56 

4 frames
/usr/local/lib/python3.7/dist-packages/torch/_utils.py in reraise(self)
    432             # instantiate since we don't know how to
    433             raise RuntimeError(msg) from None
--> 434         raise exception
    435 
    436 

TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py", line 49, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "<ipython-input-34-4c8722b5b16b>", line 143, in __getitem__
    image = imageio.imread(img_path, pilmode='RGB')
  File "/usr/local/lib/python3.7/dist-packages/imageio/core/functions.py", line 206, in imread
    reader = read(uri, format, 'i', **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/imageio/core/functions.py", line 129, in get_reader
    return format.get_reader(request)
  File "/usr/local/lib/python3.7/dist-packages/imageio/core/format.py", line 168, in get_reader
    return self.Reader(self, request)
  File "/usr/local/lib/python3.7/dist-packages/imageio/core/format.py", line 217, in __init__
    self._open(**self.request.kwargs.copy())
TypeError: _open() got an unexpected keyword argument 'pilmode'

我以前遇到过这个错误。 TLDR 是您不能假设所有数据都是干净的并且能够被解析。据我所知,您没有按顺序加载数据。您甚至可以启用数据混洗。考虑到所有这些,您不应该期望它在迭代 100 或 102 或任何时候确定性地失败。

问题归结为 COCO 数据集中的一个(或多个)文件已损坏或格式不同。你可以按batchsize为1的顺序处理图片,然后打印出文件名看看是哪个。

要“修复”此问题,您可以执行以下操作之一:

  1. 将加载图像的调用包装在 try-except 块中,然后跳过它。
  2. 自己将图片转换为另一种合适的格式。
  3. 尝试使用不同的方式在 pytorch 中加载图像。

使用 imageio 加载图像时,请参阅 here 作为示例失败场景。