Python迭代DataLoader时出现TypeError: Caught TypeError in DataLoader worker process 0.错误。

Python迭代DataLoader时出现TypeError: Caught TypeError in DataLoader worker process 0.错误。

  • TypeError: Caught TypeError in DataLoader worker process 0.
  • TypeError: 'NoneType' object is not subscriptable
    • 2019.11.30更新
      • 解决方案:
    • 2019.12.1 更新
      • 解决方案
    • 更新

TypeError: Caught TypeError in DataLoader worker process 0.

TypeError: ‘NoneType’ object is not subscriptable

迭代 DataLoader时出现以下错误,暂时不知道怎么解决,向大家求救,是一个比较稀罕的错误,也分享给大家一个奇葩的问题一起讨论。

Traceback (most recent call last):
  File "/home/zero/blood_detect/PyTorch-YOLOv3/mTrain.py", line 96, in <module>
    for batch_i, info in enumerate(dataloader):
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 819, in __next__
    return self._process_data(data)
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 846, in _process_data
    data.reraise()
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/_utils.py", line 385, in reraise
    raise self.exc_type(msg)
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/zero/blood_detect/PyTorch-YOLOv3/utils/datasets.py", line 130, in __getitem__
    img, targets = horisontal_flip(img, targets)
  File "/home/zero/blood_detect/PyTorch-YOLOv3/utils/augmentations.py", line 8, in horisontal_flip
    targets[:, 2] = 1 - targets[:, 2]
TypeError: 'NoneType' object is not subscriptable

用pycharm查询过变量内容,dataloader里边的内容是没有问题的,就是迭代的时候有问题。

dataloader代码是这些

    dataset = ListDataset(train_path, augment=True, multiscale=opt.multiscale_training, normalized_labels=False)
    dataloader = torch.utils.data.DataLoader(
        dataset,
        batch_size=opt.batch_size,
        shuffle=True,
        num_workers=opt.n_cpu,
        pin_memory=True,
        collate_fn=dataset.collate_fn,
    )

静待有缘人。

2019.11.30更新

TypeError: Caught TypeError in DataLoader worker process 0.解决。

解决方案:

num_workers设置为0。

Error 现在为

Traceback (most recent call last):
  File "/home/zero/blood_detect/PyTorch-YOLOv3/mTrain.py", line 96, in <module>
    for batch_i, info in enumerate(dataloader):
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 346, in __next__
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/zero/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/zero/blood_detect/PyTorch-YOLOv3/utils/datasets.py", line 130, in __getitem__
    img, targets = horisontal_flip(img, targets)
  File "/home/zero/blood_detect/PyTorch-YOLOv3/utils/augmentations.py", line 8, in horisontal_flip
    targets[:, 2] = 1 - targets[:, 2]
TypeError: 'NoneType' object is not subscriptable

好像意思是info没有下标不可迭代。

2019.12.1 更新

TypeError: ‘NoneType’ object is not subscriptable. 解决。

解决方案

数据导入时少给了数据的标签,导致数据没有标签,也就没有下标。

给了标签后,上面那个Error也可以设置多个num_workers了。

更新

有个博主写的一个相关解决方案,感觉更详细一点,在这里贴上。
相关博客链接

你可能感兴趣的:(问题)