类型错误:_open() 获得意外的关键字参数“pilmode”;

发布于 2025-01-11 20:19:42 字数 2212 浏览 0 评论 0原文

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

我认为这个错误可能是新版本的 imageio 中的一个错误,所以我将版本更改为 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'

I am training a CNN model on the COCO dataset, and I am getting this error after a few iterations. The error is not consistent because I got this error in 1100 iterations, once in 4500 iterations and one time in 8900 iterations (all of them in 1 epoch).

I thought that this error might be a bug in the new version of imageio, so I changed the version to 2.3.0 but still, after 8900 iterations in 1 epoch, I am getting this error.

---------------------------------------------------------------------------
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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2025-01-18 20:19:42

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

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

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

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

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

I've had this error before. The TLDR is that you can't assume all of your data is clean and able to be parsed. You aren't loading the data in order as far as I can tell either. You may even have data shuffling enabled. With all of that in mind you should not expect it to fail determinisitically at iteration 100 or 102 or anything.

The issue comes down to one (or more) of the files in COCO dataset is either corrupted or is of a different format. You can process the images in order with a batchsize of 1 and print out the file name to see which one it is.

To "fix" this issue you can do one of several things:

  1. wrap the call to load the image in a try-except block and just skip it.
  2. Convert the image yourself to another appropriate format.
  3. Try a different way to load images in with pytorch.

See here as an example failure scenario when loading in images with imageio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文