open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
28.66k stars 9.32k forks source link

How to convert mmdetection models to onnx? #7254

Closed LLsmile closed 2 years ago

LLsmile commented 2 years ago

I'm trying to convert Varifocal model to onnx. Even the model provided by anthor in https://github.com/hyz-xmaster/VarifocalNet/tree/master could not be converted correctly. My script is python tools/deployment/pytorch2onnx.py configs/vfnet/vfnet_r50_fpn_1x_coco.py vfnet_r50_1x_41.6.pth --input-img ./demo/demo.jpg --test-img ./demo/demo.jpg --show --verify Error message: 022-02-25 15:03:38.643520294 [W:onnxruntime:, graph.cc:1237 Graph] Initializer 6419 appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py. Traceback (most recent call last): File "tools/deployment/pytorch2onnx.py", line 230, in <module> do_simplify=args.simplify) File "tools/deployment/pytorch2onnx.py", line 107, in pytorch2onnx sess = rt.InferenceSession(output_file, session_options) File "/home/xyz/anaconda3/envs/openmmlab/lib/python3.7/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 335, in __init__ self._create_inference_session(providers, provider_options, disabled_optimizers) File "/home/xyz/anaconda3/envs/openmmlab/lib/python3.7/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 368, in _create_inference_session sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model) onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from tmp.onnx failed:Fatal error: MMCVDeformConv2d is not a registered function/op

LLsmile commented 2 years ago

Related warning message: /home/xyz/anaconda3/envs/openmmlab/lib/python3.7/site-packages/mmcv/ops/nms.py:159: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! assert boxes.size(1) == 4 /home/xyz/anaconda3/envs/openmmlab/lib/python3.7/site-packages/mmcv/ops/nms.py:160: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! assert boxes.size(0) == scores.size(0) WARNING: The shape inference of mmcv::MMCVDeformConv2d type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. Model and codes come from the official implementation in [https://github.com/hyz-xmaster/VarifocalNet/tree/master](https://github.com/open-mmlab/mmdetection/issues/url). So you can reproduce it directly.

LLsmile commented 2 years ago

Trained model by vfnet_r50_fpn_1x_coco.py show the same error.

ZwwWayne commented 2 years ago

See documentation here https://mmdetection.readthedocs.io/en/latest/tutorials/pytorch2onnx.html. VFNet is not supported due to its special operators. You can create a feature request issue in https://github.com/open-mmlab/mmdeploy