OpenMMLab多库推理问题:KeyError: ‘XXX is not in the XXX registry.

本文所讨论的问题限定在OpenMMLab多库推理过程中,即一个py程序需要调用多个repository的推理器Inferencers(如MMPretrain的ImageClassificationInferencer、MMDet的inference_detector等)的情况。本文的解决方案或许并不适用于其他情况下的类似报错(如:直接下载解压别人源码后无法运行使用register_all_modules注册所有模块解决使用MMDet推理器推理MMYolo模型报错YOLOv5KeepRatioResize is not in the transform registry的问题

问题背景

场景一:同时使用MMYolo和MMPretrain的推理器——KeyError: 'ResizeEdge is not in the mmyolo::transform registry

Traceback (most recent call last):
  File "/www/wwwroot/backend/model/Model6/test_runtime_model6.py", line 71, in <module>
    pretrain_inferencer = ImageClassificationInferencer(model=pretrain_config,
  File "/www/wwwroot/backend/model/Model6/Model6_2/mmpretrain/mmpretrain/apis/image_classification.py", line 66, in __init__
    super().__init__(
  File "/www/wwwroot/backend/model/Model6/Model6_2/mmpretrain/mmpretrain/apis/base.py", line 121, in __init__
    self.pipeline = self._init_pipeline(self.config)
  File "/www/wwwroot/backend/model/Model6/Model6_2/mmpretrain/mmpretrain/apis/image_classification.py", line 117, in _init_pipeline
    [TRANSFORMS.build(t) for t in test_pipeline_cfg])
  File "/www/wwwroot/backend/model/Model6/Model6_2/mmpretrain/mmpretrain/apis/image_classification.py", line 117, in <listcomp>
    [TRANSFORMS.build(t) for t in test_pipeline_cfg])
  File "/root/miniconda3/lib/python3.8/site-packages/mmengine/registry/registry.py", line 570, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/root/miniconda3/lib/python3.8/site-packages/mmengine/registry/build_functions.py", line 100, in build_from_cfg
    raise KeyError(
KeyError: 'ResizeEdge is not in the mmyolo::transform registry. Please check whether the value of `ResizeEdge` is correct or it was registered as expected. More details can be found at https://mmengine.readthedocs.io/en/latest/advanced_tutorials/config.html#import-the-custom-module'

场景二:同时使用MMYolo和MMPose推理器——KeyError: 'YOLOv5KeepRatioResize is not in the mmpose::transform registry.

Traceback (most recent call last):
  File "/www/wwwroot/backend/model/Model6/test_runtime_model6.py", line 230, in <module>
    main('data-test/')
  File "/www/wwwroot/backend/model/Model6/test_runtime_model6.py", line 187, in main
    result, runtime_mmyolo = mmyolo_inference(img, yolo_inferencer)
  File "/www/wwwroot/backend/model/Model6/test_runtime_model6.py", line 118, in mmyolo_inference
    result = inference_detector(model, img)
  File "/root/miniconda3/lib/python3.8/site-packages/mmdet/apis/inference.py", line 152, in inference_detector
    test_pipeline = Compose(test_pipeline)
  File "/root/miniconda3/lib/python3.8/site-packages/mmcv/transforms/wrappers.py", line 66, in __init__
    transform = TRANSFORMS.build(transform)
  File "/root/miniconda3/lib/python3.8/site-packages/mmengine/registry/registry.py", line 570, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/root/miniconda3/lib/python3.8/site-packages/mmengine/registry/build_functions.py", line 100, in build_from_cfg
    raise KeyError(
KeyError: 'YOLOv5KeepRatioResize is not in the mmpose::transform registry. Please check whether the value of `YOLOv5KeepRatioResize` is correct or it was registered as expected. More details can be found at https://mmengine.readthedocs.io/en/latest/advanced_tutorials/config.html#import-the-custom-module'

问题产生原因(猜想)

这只是猜想,博主能力和精力有限,没有进行验证

在生成多个库的推理器时,MMEngine只会记住后来者的scope(比如我先调用MMDet的inference_detector生成了MMYolo的推理器,再调用ImageClassificationInferencer生成了MMPretrain的推理器,但MMEngine只会记住mmpretrain这个一个scope),如果先来者的cfg中存在后来者的scope没有注册的模块,这就会导致在调用先来者的推理器进行模型推理时,MMEngine无法找到先来者的模块,从而报错KeyError:XXX is not in the XXX registry

解决方案

根据报错信息,在先来者的config里,将在后来scope中未注册的模块加上前缀——先来者的scope。
以场景一中的报错为例:'ResizeEdge is not in the mmyolo::transform registry表明scopemmpose中的模块ResizeEdge没有在scopemmyolo中注册,因此在mmpose对应的cfg中将所有的ResizeEdge改为mmpose.ResizeEdge

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值