deepinsight / insightface

State-of-the-art 2D and 3D Face Analysis Project
https://insightface.ai
23.31k stars 5.41k forks source link

Error while converting SCRFD to onnx using scrfd2onnx.py #2054

Open YousefHesham7 opened 2 years ago

YousefHesham7 commented 2 years ago

Hello,

I'm getting the following error while trying to convert any of the SCRFD .pth models to onnx using scrfd2onnx.py

Here's the used command: python3 tools/scrfd2onnx.py configs/scrfd/scrfd_2.5g.py tools/models/SCRFD_2.5G.pth

Here's what I'm getting:

set to dynamic input with dummy shape: (1, 3, 640, 640) load checkpoint from local path: tools/models/SCRFD_2.5G.pth /home/yousef/Desktop/IN3/insightface/detection/scrfd/mmcv/mmcv/onnx/symbolic.py:481: UserWarning: DeprecationWarning: This function will be deprecated in future. Welcome to use the unified model deployment toolbox MMDeploy: https://github.com/open-mmlab/mmdeploy warnings.warn(msg) Traceback (most recent call last): File "tools/scrfd2onnx.py", line 198, in pytorch2onnx( File "tools/scrfd2onnx.py", line 84, in pytorch2onnx torch.onnx.export( File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/onnx/init.py", line 350, in export return utils.export( File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/onnx/utils.py", line 163, in export _export( File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/onnx/utils.py", line 1074, in _export graph, params_dict, torch_out = _model_to_graph( File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/onnx/utils.py", line 727, in _model_to_graph graph, params, torch_out, module = _create_jit_graph(model, args) File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/onnx/utils.py", line 602, in _create_jit_graph graph, torch_out = _trace_and_get_graph_from_model(model, args) File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/onnx/utils.py", line 517, in _trace_and_get_graph_from_model trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph( File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/jit/_trace.py", line 1175, in _get_trace_graph outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, *kwargs) File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl return forward_call(input, **kwargs) File "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/jit/_trace.py", line 95, in forward in_vars, in_desc = _flatten(args) RuntimeError: Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: numpy.ndarray

nttstar commented 2 years ago

What is the version of your mmdet and mmcv libraries?

YannikSchaefer commented 2 years ago

I have the same issue using scrfd_34g and mmcv-full 1.3.5 and mmdet 2.7.0

Z-Xiong commented 2 years ago

same issue, did you solve it ?

Z-Xiong commented 2 years ago

lib/python3.7/site-packages/torch/onnx/utils.py # args = _decide_input_format(model, args) sovled it

ZubairKhan001 commented 1 year ago

using mmcv 1.7.1 @Z-Xiong Commented out at three locations got error /lib/python3.9/site-packages/torch/nn/modules/batchnorm.py", line 410, in _check_input_dim raise ValueError("expected 4D input (got {}D input)".format(input.dim())) ValueError: expected 4D input (got 3D input)

quanqigu commented 1 year ago

@Z-Xiong it worked for me. need comment the _decide_input_format in the calling function.

args = _decide_input_format(model, args)

For python 3.8, we need comment the _decide_input_format at line 1069 as the error stacktrace hint. "/home/yousef/Desktop/IN3/insightface/detection/scrfd/venv/lib/python3.8/site-packages/torch/onnx/utils.py", line 1074, in _export"

ZubairKhan001 commented 1 year ago

@quanqigu mdet/models/dense_heads/scrfd_head.py", line 802, in _get_bboxes_single scores = cls_score.permute(1, 2, 0).reshape( RuntimeError: number of dims don't match in permute

in-onnx-export torch.Size([1, 2, 80, 80]) torch.Size([1, 8, 80, 80]) in-onnx-export torch.Size([1, 2, 40, 40]) torch.Size([1, 8, 40, 40]) in-onnx-export torch.Size([1, 2, 20, 20]) torch.Size([1, 8, 20, 20])

cls_score shape is torch.Size([12800, 1])

shape has issue but why? can help? Thankyou