opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
75.95k stars 55.62k forks source link

OpenCV fails to import ONNX model: Error in ReduceMean #22458

Closed ukoehler closed 1 year ago

ukoehler commented 1 year ago
Detailed description

I converted the following DNN from PyTorch to ONNX and tried to load it using OpenCV: https://docs.openvino.ai/latest/omz_models_model_nfnet_f0.html#doxid-omz-models-model-nfnet-f0 The resulting ONNX file is 272 MB and can be provided, if needed.

I checked the ONNX using Pythin and onnx, as well as onnxruntime. Both claim that the model is OK. OpenCV, however, reports: `[ERROR:0@0.844] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp (1021) cv::dnn::dnn4_v20220524::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 1 inputs and 1 outputs: [ReduceMean]:(onnx_node!ReduceMean_37) from domain='ai.onnx' Traceback (most recent call last): File "d:\Local\devel\Python\OpenCV\image_classification_nfnet_f0\inference.py", line 83, in model = cv2.dnn.readNet(model_config) cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'cv::dnn::dnn4_v20220524::ONNXImporter::handleNode'

Node [ReduceMean@ai.onnx]:(onnx_node!ReduceMean_37) parse error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\include\opencv2/dnn/shape_utils.hpp:216: error: (-2:Unspecified error) in function 'int __cdecl cv::dnn::dnn4_v20220524::normalize_axis(int,int)'

: 'axis >= -dims && axis < dims' where 'axis' is 1 `

Steps to reproduce

model = cv2.dnn.readNet(model_file)

Issue submission checklist
fengyuentau commented 1 year ago

Hello @ukoehler , thanks for the feedback! Could you upload the model somewhere accessible for us?

ukoehler commented 1 year ago

Dear @fengyuentau,

thanks for the quick reply. I would quickly only know to upload it here. I have never tried to upload a file that big.

Cheers

Uwe

ukoehler commented 1 year ago

Sorry, wrong button.

zihaomu commented 1 year ago

Google drive is available.

ukoehler commented 1 year ago

OK, fist time using Google drive for something like this. The file should be available here: https://drive.google.com/file/d/1IXCkZbdQzcgZ3f2dAXQ5mZ67xo92Bvtz/view?usp=sharing

zihaomu commented 1 year ago

Hi, @ukoehler, your model is very complex.

Please try the following script to simplify your model first

import onnx
import onnxsim
import numpy as np

from onnx import load_model, save_model

path = "model.onnx"

path_saved = "model_sim.onnx"
onnx_model = onnx.load(path)
input_shape = np.array([1, 3, 224, 224])
input = dict()
input["x.1"] = input_shape
out = onnxsim.simplify(onnx_model, dynamic_input_shape=False, input_shapes=input)
onnx.save(out[0], path_saved)

And after that, everything should work fine.

There is a simplified model based on your google drive link. dm_nfnet_f0_sim.onnx

ukoehler commented 1 year ago

Dear @zihaomu,

many thanks for your help. I am only using, not creating the model. So far I did not know about the simplification possibility. I will try it and it might help with other models, as well.

Cheers

Uwe

ukoehler commented 1 year ago

Dear @zihaomu,

I tried you simplified model and it does load and produces reasonable output (I still have to find some undocumented parameters). That does give me a workaround (I am really grateful for that), but is still leaves an issue with OpenCV.

Cheers

Uwe

zihaomu commented 1 year ago

Hi @ukoehler, thanks for your report. The error was due to the reduce node cannot process the constant input. I'm considering whether to support this case.

ukoehler commented 1 year ago

Using opencv-python-rolling 4.6.0.20220924 this model can now be executed.

ANH2018 commented 2 weeks ago

Dear @zihaomu, how to install dm_nfnet_f0_sim.onnx vscode Thanks