open-mmlab / mmdeploy

OpenMMLab Model Deployment Framework
https://mmdeploy.readthedocs.io/en/latest/
Apache License 2.0
2.75k stars 631 forks source link

[Bug]python mmdeploy/tools/deploy.py , convert onnx from pth,AttributeError: module 'mmcv' has no attribute 'Config' #1959

Closed likecv closed 1 year ago

likecv commented 1 year ago

Checklist

Describe the bug

(mmdet)@:/home/**/MMLab$ python mmdeploy/tools/deploy.py mmdeploy/configs/mmdet/detection/detection_onnxruntime_static.py mmdetection/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py mmdetection/ssdlite_mobilenetv2_scratch_600e_coco_20210629_110627-974d9307.pth mmdetection/demo/demo.jpg --work-dir onnx/SSD_MbV3 --device cpu --dump-info Traceback (most recent call last): File "mmdeploy/tools/deploy.py", line 15, in from mmdeploy.apis import (create_calib_input_data, extract_model, File "/home/lk/MMLab/mmdeploy/mmdeploy/init.py", line 4, in from mmdeploy.utils import get_root_logger File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/init.py", line 18, in from .config_utils import (cfg_apply_marks, get_backend, File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/config_utils.py", line 10, in def load_config(args) -> List[mmcv.Config]: AttributeError: module 'mmcv' has no attribute 'Config'

Reproduction

(mmdet)@:/home/**/MMLab$ python mmdeploy/tools/deploy.py mmdeploy/configs/mmdet/detection/detection_onnxruntime_static.py mmdetection/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py mmdetection/ssdlite_mobilenetv2_scratch_600e_coco_20210629_110627-974d9307.pth mmdetection/demo/demo.jpg --work-dir onnx/SSD_MbV3 --device cpu --dump-info Traceback (most recent call last): File "mmdeploy/tools/deploy.py", line 15, in from mmdeploy.apis import (create_calib_input_data, extract_model, File "/home/lk/MMLab/mmdeploy/mmdeploy/init.py", line 4, in from mmdeploy.utils import get_root_logger File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/init.py", line 18, in from .config_utils import (cfg_apply_marks, get_backend, File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/config_utils.py", line 10, in def load_config(args) -> List[mmcv.Config]: AttributeError: module 'mmcv' has no attribute 'Config'

Environment

(mmdet)***@***:/home/***/MMLab$ python mmdeploy/tools/deploy.py     mmdeploy/configs/mmdet/detection/detection_onnxruntime_static.py     mmdetection/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py     mmdetection/ssdlite_mobilenetv2_scratch_600e_coco_20210629_110627-974d9307.pth     mmdetection/demo/demo.jpg     --work-dir onnx/SSD_MbV3     --device cpu    --dump-info
Traceback (most recent call last):
  File "mmdeploy/tools/deploy.py", line 15, in <module>
    from mmdeploy.apis import (create_calib_input_data, extract_model,
  File "/home/lk/MMLab/mmdeploy/mmdeploy/__init__.py", line 4, in <module>
    from mmdeploy.utils import get_root_logger
  File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/__init__.py", line 18, in <module>
    from .config_utils import (cfg_apply_marks, get_backend,
  File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/config_utils.py", line 10, in <module>
    def load_config(*args) -> List[mmcv.Config]:
AttributeError: module 'mmcv' has no attribute 'Config'

Error traceback

(mmdet)***@***:/home/***/MMLab$ python mmdeploy/tools/deploy.py     mmdeploy/configs/mmdet/detection/detection_onnxruntime_static.py     mmdetection/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py     mmdetection/ssdlite_mobilenetv2_scratch_600e_coco_20210629_110627-974d9307.pth     mmdetection/demo/demo.jpg     --work-dir onnx/SSD_MbV3     --device cpu    --dump-info
Traceback (most recent call last):
  File "mmdeploy/tools/deploy.py", line 15, in <module>
    from mmdeploy.apis import (create_calib_input_data, extract_model,
  File "/home/lk/MMLab/mmdeploy/mmdeploy/__init__.py", line 4, in <module>
    from mmdeploy.utils import get_root_logger
  File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/__init__.py", line 18, in <module>
    from .config_utils import (cfg_apply_marks, get_backend,
  File "/home/lk/MMLab/mmdeploy/mmdeploy/utils/config_utils.py", line 10, in <module>
    def load_config(*args) -> List[mmcv.Config]:
AttributeError: module 'mmcv' has no attribute 'Config'
irexyc commented 1 year ago

Please check your environment.

MMDeploy master or tag v0.xx is target to original openmmlab algorithm repo and MMDeploy dev-1.x and tag v1.xx is target to newly openmmlab algorithm repo.

Since you installed mmengine, I doubt you are using newly openmmlab algorithm repo, so you should use MMDeploy dev-1.x branch or tag v1.xx

likecv commented 1 year ago

Please check your environment.

MMDeploy master or tag v0.xx is target to original openmmlab algorithm repo and MMDeploy dev-1.x and tag v1.xx is target to newly openmmlab algorithm repo.

Since you installed mmengine, I doubt you are using newly openmmlab algorithm repo, so you should use MMDeploy dev-1.x branch or tag v1.xx

mmdeploy/tools/torch2onnx.py, hello, Do you have README.md for torch2onnx

likecv commented 1 year ago

python3 mmdeploy/tools/pytorch2onnx.py home/mmdetection/ssdlite_mobilenetv2_scratch_600e_coco.py home/mmdetection/ssdlite_mobilenetv2_scratch_600e_coco_20210629_110627-974d9307.pth --output-file=onnx/ssdlite_mobilenetv2.onnx --shape 320 python3: can't open file 'mmdeploy/tools/pytorch2onnx.py': [Errno 2] No such file or directory (mmdet)@/home/MMLab$ python3 mmdeploy/tools/torch2onnx.py \

home/mmdetection/ssdlite_mobilenetv2_scratch_600e_coco.py \ home/mmdetection/ssdlite_mobilenetv2_scratch_600e_coco_20210629_110627-974d9307.pth \ --output-file=onnx/ssdlite_mobilenetv2.onnx \ --shape 320 Traceback (most recent call last): File "mmdeploy/tools/torch2onnx.py", line 7, in from mmdeploy.apis import (extract_model, get_predefined_partition_cfg, File "/home/MMLab/mmdeploy/mmdeploy/init.py", line 4, in from mmdeploy.utils import get_root_logger File "/home/MMLab/mmdeploy/mmdeploy/utils/init.py", line 18, in from .config_utils import (cfg_apply_marks, get_backend, File "/home/MMLab/mmdeploy/mmdeploy/utils/config_utils.py", line 10, in def load_config(*args) -> List[mmcv.Config]: AttributeError: module 'mmcv' has no attribute 'Config'

likecv commented 1 year ago

pip list: mmcv 2.0.0 mmcv-full 1.2.4 mmdet 3.0.0rc5 mmengine 0.7.0 mmpycocotools 12.0.3

irexyc commented 1 year ago

You installed the wrong version of mmdeploy. You should use dev-1.x branch of mmdeploy or tag wtih v1.xxx

As I said, mmdeploy master support old openmmlab algorithm branch and dev-1.x branch support openmmlab newly branch.

The master branch of config_utils.py (see your error log)

The dev-1.x branch of config_utils.py

likecv commented 1 year ago

python mmdeploy/tools/deploy.py mmdeploy/configs/mmdet/detection/detection_tensorrt_dynamic-320x320-1344x1344.py mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth mmdetection/demo/demo.jpg --work-dir mmdeploy_model/faster-rcnn --device cpu --dump-info Traceback (most recent call last): File "mmdeploy/tools/deploy.py", line 16, in from mmdeploy.apis import (create_calib_input_data, extract_model, File "/home/MMLab/mmdeploy/mmdeploy/init.py", line 8, in importlib.import_module('mmdeploy.pytorch') File "/home/infname/miniconda3/envs/mmdeploy/lib/python3.8/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/MMLab/mmdeploy/mmdeploy/pytorch/init.py", line 2, in from . import functions # noqa: F401,F403 File "/home/MMLab/mmdeploy/mmdeploy/pytorch/functions/init.py", line 2, in from . import adaptive_pool # noqa: F401,F403 File "/home/MMLab/mmdeploy/mmdeploy/pytorch/functions/adaptive_pool.py", line 6, in from mmdeploy.core import FUNCTION_REWRITER File "/home/MMLab/mmdeploy/mmdeploy/core/init.py", line 2, in from .optimizers import * # noqa: F401,F403 File "/home/MMLab/mmdeploy/mmdeploy/core/optimizers/init.py", line 2, in from .extractor import create_extractor, parse_extractor_io_string File "/home/MMLab/mmdeploy/mmdeploy/core/optimizers/extractor.py", line 48, in def create_extractor(model: onnx.ModelProto) -> onnx.utils.Extractor: AttributeError: module 'onnx' has no attribute 'ModelProto'

The official samples:https://github.com/open-mmlab/mmdeploy/blob/v1.0.0rc3/docs/zh_cn/get_started.md

The official samples are not working, and the environment is configured according to the official settings

likecv commented 1 year ago

克隆 mmdeploy 仓库。转换时,需要使用 mmdeploy 仓库中的配置文件,建立转换流水线, --recursive 不是必须的

git clone -b dev-1.x --recursive https://github.com/open-mmlab/mmdeploy.git

安装 mmdetection。转换时,需要使用 mmdetection 仓库中的模型配置文件,构建 PyTorch nn module

git clone -b 3.x https://github.com/open-mmlab/mmdetection.git cd mmdetection mim install -v -e . cd ..

下载 Faster R-CNN 模型权重

wget -P checkpoints https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth

执行转换命令,实现端到端的转换

python mmdeploy/tools/deploy.py \ mmdeploy/configs/mmdet/detection/detection_tensorrt_dynamic-320x320-1344x1344.py \ mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py \ checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \ mmdetection/demo/demo.jpg \ --work-dir mmdeploy_model/faster-rcnn \ --device cuda \ --dump-info

irexyc commented 1 year ago

AttributeError: module 'onnx' has no attribute 'ModelProto'

The reason is the process import a wrong onnx module. I have no clue what caused it for now.

Could you print the result of python tools/check_env.py

likecv commented 1 year ago

python tools/check_env.py

Traceback (most recent call last): File "tools/check_env.py", line 4, in from mmcv.utils import get_git_hash ImportError: cannot import name 'get_git_hash' from 'mmcv.utils' (/home/infname/miniconda3/envs/mmdeploy/lib/python3.8/site-packages/mmcv/utils/init.py)

irexyc commented 1 year ago

The line4 of tools/check_env.py in dev-1.x branch is from mmengine.utils import get_git_hash, not 'mmcv.utils' https://github.com/open-mmlab/mmdeploy/blob/dev-1.x/tools/check_env.py#L1-L4

And mmcv-full is for old openmmlab algorithm repos.

I think you'd better remove the mmdeploy conda environment, create a new one and reinstall everything.

likecv commented 1 year ago

Converting to an onnx model cannot be visualized using the netron tool. Note: The onnx model cannot be loaded. What happens

irexyc commented 1 year ago

I can't reproduce your problem with mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py.

likecv commented 1 year ago

python mmdeploy/tools/deploy.py mmdeploy/configs/mmdet/detection/detection_onnxruntime_static.py mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth mmdetection/demo/demo.jpg --work-dir mmdeploy_model/faster-rcnn --device cpu --dump-info

hello,Could you please give this a try and see if the resulting model can be visualized using Netron tools

irexyc commented 1 year ago

I have tried.

likecv commented 1 year ago

Can the converted model be visualized using Netron tools

irexyc commented 1 year ago

I can't reproduce your problem with mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py.

Yes, I can visualize the onnx model with netron.