opencv / opencv

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

global onnx_importer.cpp:1051 handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Add]:(onnx_node!/Add_1) from domain='ai.onnx' #23231

Open Neo-Abhishek opened 1 year ago

Neo-Abhishek commented 1 year ago

System Information

OpenCV python version: 4.7.0 Operating System / Platform: Ubuntu 20.04 Python version: 3.8.10

Detailed description

The error happens when reading the .onnx model using cv2.dnn.readNet().

error: OpenCV(4.7.0) /home/astra2/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1073: error: (-2:Unspecified error) in function 'handleNode' 
Node [Add@ai.onnx]:(onnx_node!/Add_1) parse error: OpenCV(4.7.0) /home/astra2/opencv/modules/dnn/src/layers/nary_eltwise_layers.cpp:139: error: (-215:Assertion failed) shape[i] == 1 || outShape[i] == 1 in function 'findCommonShape' 

Whereas onnxruntime doesn't throw any error. Also this error seems similar to #23227 but with a different layer

Steps to reproduce

Model link - https://drive.google.com/file/d/10hET3jvdCBWyIHZR13lIXaKsQUiN2Wyz/view?usp=share_link

import cv2
model_path = 'clip_visual_dynamic_batch.onnx'
net = cv2.dnn.readNet(model_path)

Issue submission checklist

Neo-Abhishek commented 1 year ago

Another interesting observation: The above error occurs when exporting the model for dynamic batch input using this syntax -

torch.onnx.export(model.visual, 
                  image_input, 
                  "clip-visual.onnx", 
                  export_params=True, 
                  opset_version=14, 
                  do_constant_folding=True, 
                  input_names = ['input'], 
                  output_names = ['output'], 
                  dynamic_axes={'input' : {0 : 'batch_size'}, 'output' : {0 : 'batch_size'}})

but when exporting for single image input by making the 'dynamic_axes' parameter above empty cv2.dnn.readNet() is successful in reading the onnx model

dynamic_axes = {}

but this time the model output dimension is wrong - [50,512] whereas it should be [1,512].

WanliZhong commented 1 year ago

@Neo-Abhishek Thanks for your reporting. ConstantOfShape doesn't support batch_size until OpenCV 5.0 released. I think you can set batch_size to 1 then use single image input model.

Neo-Abhishek commented 1 year ago

but this time the model output dimension is wrong - [50,512] whereas it should be [1,512].

@WanliZhong I have already tried that and the dnn.readNet() is a success with single image input but the model output is incorrect as mentioned above. Of all the 50 vectors only the first index vector is the correct output and remaining are not useful.

WanliZhong commented 1 year ago

but this time the model output dimension is wrong - [50,512] whereas it should be [1,512].

@WanliZhong I have already tried that and the dnn.readNet() is a success with single image input but the model output is incorrect as mentioned above. Of all the 50 vectors only the first index vector is the correct output and remaining are not useful.

Could you put this model on this issue?

Neo-Abhishek commented 1 year ago

but this time the model output dimension is wrong - [50,512] whereas it should be [1,512].

@WanliZhong I have already tried that and the dnn.readNet() is a success with single image input but the model output is incorrect as mentioned above. Of all the 50 vectors only the first index vector is the correct output and remaining are not useful.

Could you put this model on this issue?

Model with single image input - https://drive.google.com/file/d/1DSA-0GWEA7WuCyCIO_LK_3cfaqTSgj-q/view?usp=share_link

Samorodowv commented 1 year ago

I faced the same problem, trying to run this project: https://github.com/hpc203/YOLOP-opencv-dnn, which is based on https://github.com/hustvl/YOLOP

yuandanfei commented 1 year ago

can you try to add --opset 12 https://forum.opencv.org/t/error-when-reading-yolo5-as-onnx-using-cv2/11507/3

fti-sbargule commented 1 year ago

Model run to python. cv2.dnn.readNetFromONNX(model_yaw) Same problem to c++ language. cv::dnn::readNetFromONNX(model_yaw)

Please help

[ERROR:0] global ./modules/dnn/src/onnx/onnx_importer.cpp (718) handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Squeeze]:(model/block1a_se_squeeze/Mean_Squeeze__1241:0)
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.4) ./modules/dnn/src/onnx/onnx_importer.cpp:739: error: (-2:Unspecified error) in function 'handleNode'
> Node [Squeeze]:(model/block1a_se_squeeze/Mean_Squeeze__1241:0) parse error: OpenCV(4.5.4) ./modules/dnn/src/onnx/onnx_importer.cpp:1824: error: (-215:Assertion failed) node_proto.input_size() == 1 in function 'parseSqueeze'
> 
joewong00 commented 11 months ago

Hi, I have the same issue. Is there any fix to this?

nordic-style commented 10 months ago

You need to export the model using opset 12 as already mentioned above. If you use the ultralytics convert.py it would be:

from ultralytics import YOLO

model = YOLO("yolov5m.pt") model.export(format="onnx", imgsz=[640,640], opset=12)

bleachman08 commented 9 months ago

You need to export the model using opset 12 as already mentioned above. If you use the ultralytics convert.py it would be:

from ultralytics import YOLO

model = YOLO("yolov5m.pt") model.export(format="onnx", imgsz=[640,640], opset=12)

Hello, i tried using opset 12 but i am still receiving the same issue, is there any other solution to this?

hmkamran83 commented 6 months ago

I am also facing the same issue, I am getting the following issue. cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.02s Runningtarget/debug/onnx_cv [ERROR:0@0.052] global ./modules/dnn/src/onnx/onnx_importer.cpp (1018) handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Add]:(onnx_node!/model.22/Add) from domain='ai.onnx' Invalid ONNX model.

then I check the model using Check Model, and says "The model is valid!",

then I check my model at Netron, the result is Screenshot_20240425_161031