运行开源库CCPD-RPnet代码,提示「KeyError: Caught KeyError in replica 0 on device 0」错误

文章目录

    • 问题描述
    • 问题排查
    • Solution
    • Other

问题描述

通过python3 demo.py -i ./demo -m ./models/fh02.pth运行车牌识别CCPD开源代码,提示「KeyError: Caught KeyError in replica 0 on device 0」和「KeyError: 」错误。

错误日志为:

True
/pytorch/torch/csrc/autograd/python_function.cpp:622: UserWarning: Legacy autograd function with non-static forward method is deprecated and will be removed in 1.3. Please use new-style autograd function with static forward method. (Example: https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)
Traceback (most recent call last):
  File "demo.py", line 273, in <module>
    fps_pred, y_pred = model_conv(x)
  File "/home/suy/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/suy/.local/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 152, in forward
    outputs = self.parallel_apply(replicas, inputs, kwargs)
  File "/home/suy/.local/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 162, in parallel_apply
    return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
  File "/home/suy/.local/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 85, in parallel_apply
    output.reraise()
  File "/home/suy/.local/lib/python3.6/site-packages/torch/_utils.py", line 385, in reraise
    raise self.exc_type(msg)
KeyError: Caught KeyError in replica 0 on device 0.
Original Traceback (most recent call last):
  File "/home/suy/.local/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 60, in _worker
    output = module(*input, **kwargs)
  File "/home/suy/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "demo.py", line 229, in forward
    roi1 = roi_pooling_ims(_x1, boxNew.mm(p1), size=(16, 8))
  File "/home/suy/data_disk/00_Project/01_LPR/CCPD/rpnet/roi_pooling.py", line 74, in roi_pooling_ims
    output.append(adaptive_max_pool(im, size))
  File "/home/suy/data_disk/00_Project/01_LPR/CCPD/rpnet/roi_pooling.py", line 36, in adaptive_max_pool
    return AdaptiveMaxPool2d(size[0], size[1])(input)
  File "/home/suy/data_disk/00_Project/01_LPR/CCPD/rpnet/roi_pooling.py", line 18, in forward
    self._backend = type2backend[type(input)] #运行有问题,根据issue 29 解决
  File "/home/suy/.local/lib/python3.6/site-packages/torch/_thnn/__init__.py", line 15, in __getitem__
    return self.backends[name].load()
KeyError: <class 'torch.Tensor'>

问题排查

在网上搜索该错误未找到解决相关信息,开始在CCPD的issue中查找,找到了一个类似的问题 issue-29

根据 CCPD-issue - 29 提出的解决方法:

You guys need to modify type(input) as input.type() in roi_pooling.py, line 18 ,then it works.
This error caused by api changes of different pytorch version。

参考该方法,将** roi_pooling.py**的第18行self._backend = type2backend[type(input)]进行修改,改为self._backend = type2backend[input.type()],再次运行,提示pytorch module 'torch._C' has no attribute '_THCUNN' ;

根据该错误提示,找到了一个相关问题 pytorch module torch._C has no attribute _THCUNN KeyError: class torch.Tensor,参考该博文进行修改,问题解决。

Solution

roi_pooling.py的第75行output.append(adaptive_max_pool(im, size))修改为output.append(F.adaptive_max_pool2d(im, size)),其中的F来自于引用库import torch.nn.functional as F

完整的修改方法为:

#output.append(adaptive_max_pool(im, size)) #原代码,以下两行为修改后的代码
import torch.nn.functional as F
output.append(F.adaptive_max_pool2d(im, size))

Other

下载文章中已经给定的权重模型,效果并不理想,下面是其中一两张的识别结果,效果较差。

运行开源库CCPD-RPnet代码,提示「KeyError: Caught KeyError in replica 0 on device 0」错误_第1张图片

运行开源库CCPD-RPnet代码,提示「KeyError: Caught KeyError in replica 0 on device 0」错误_第2张图片

你可能感兴趣的:(Python)