Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The onnx model was loadable in 4.5.4 but not in 4.6.0 #22139

Closed
iHaibaraAi opened this issue Jun 21, 2022 · 9 comments
Closed

The onnx model was loadable in 4.5.4 but not in 4.6.0 #22139

iHaibaraAi opened this issue Jun 21, 2022 · 9 comments
Assignees
Labels
category: dnn (onnx) ONNX suport issues in DNN module

Comments

@iHaibaraAi
Copy link

opencv 4.5.4 and 4.6.0
Operating System / Platform => Windows 11 64 bit
Compiler => Visual Studio 2022

onnx
https://github.com/afterdm/onnx/blob/master/yunet.onnx

[ INFO:0@20.685] global C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp (797) cv::dnn::dnn4_v20220524::ONNXImporter::populateNet DNN/ONNX: loading ONNX v6 model produced by 'pytorch':1.9. Number of nodes = 111, initializers = 92, inputs = 1, outputs = 3
[ INFO:0@20.685] global C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp (713) cv::dnn::dnn4_v20220524::ONNXImporter::parseOperatorSet DNN/ONNX: ONNX opset version = 9
[ERROR:0@20.708] global C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp (2845) cv::dnn::dnn4_v20220524::ONNXImporter::parseShape DNN/ONNX(Shape): dynamic 'zero' shapes are not supported, input 243 [ 0 0 0 51 ]
OpenCV(4.6.0) Error: Assertion failed (!isDynamicShape) in cv::dnn::dnn4_v20220524::ONNXImporter::parseShape, file C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp, line 2846
[ERROR:0@20.716] global C:\build\opencv-4.6.0\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: [Shape]:(onnx_node!Shape_70) from domain='ai.onnx'
[ INFO:0@20.716] global C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp (1025) cv::dnn::dnn4_v20220524::ONNXImporter::handleNode Input[0] = '243'
[ INFO:0@20.716] global C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp (1029) cv::dnn::dnn4_v20220524::ONNXImporter::handleNode Output[0] = '265'
OpenCV(4.6.0) Error: Unspecified error (> Node [Shape@ai.onnx]:(onnx_node!Shape_70) parse error: OpenCV(4.6.0) C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp:2846: error: (-215:Assertion failed) !isDynamicShape in function 'cv::dnn::dnn4_v20220524::ONNXImporter::parseShape'

) in cv::dnn::dnn4_v20220524::ONNXImporter::handleNode, file C:\build\opencv-4.6.0\modules\dnn\src\onnx\onnx_importer.cpp, line 1040

@asmorkalov asmorkalov added category: dnn (onnx) ONNX suport issues in DNN module bug labels Jun 21, 2022
@zihaomu zihaomu self-assigned this Jun 21, 2022
@zihaomu
Copy link
Member

zihaomu commented Jun 21, 2022

Thanks @afterdm, this bug is caused by the shape layer. I'll try to fix it now.

@fengyuentau
Copy link
Member

fengyuentau commented Jun 21, 2022

OpenCV DNN does not support ONNX models with dynamic input shape. However, you can load a ONNX model with fixed input shape and infer with other input shapes using OpenCV DNN. You can download YuNet with fixed size input and demos from https://github.com/fengyuentau/opencv_zoo/tree/master/models/face_detection_yunet.

@fisakhan
Copy link

fisakhan commented Sep 8, 2022

@fengyuentau thanks. How can I convert a model to fixed size input?
I convert a variable size input model (RetinaFace) from SavedModel (.pb) format to fixed.onnx and by loading it using net = cv2.dnn.readNet("fixed.onnx") I get the following error:

[ERROR:0@0.051] global /opt/conda/conda-bld/opencv-suite_1659973592936/work/modules/dnn/src/onnx/onnx_importer.cpp (2516) parseShape DNN/ONNX(Shape): dynamic 'zero' shapes are not supported, input input_image [ 0 0 0 3 ]
[ERROR:0@0.051] global /opt/conda/conda-bld/opencv-suite_1659973592936/work/modules/dnn/src/onnx/onnx_importer.cpp (906) handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Squeeze]:(StatefulPartitionedCall/RetinaFaceModel/tf_op_layer_strided_slice_14/strided_slice_14__128:0) from domain='ai.onnx'

@fengyuentau
Copy link
Member

[ERROR:0@0.051] global /opt/conda/conda-bld/opencv-suite_1659973592936/work/modules/dnn/src/onnx/onnx_importer.cpp (2516) parseShape DNN/ONNX(Shape): dynamic 'zero' shapes are not supported, input input_image [ 0 0 0 3 ]

@fisakhan This error is exactly caused by trying to load a dynamic shape ONNX model with OpenCV DNN. You can have a try with https://github.com/daquexian/onnx-simplifier for your purpose.

@fisakhan
Copy link

@fengyuentau After conversion using onnx-simplifier, I still get the same error. You can find both the models (savedmodel and onnx) at https://github.com/fisakhan/Face_Pose/tree/master/models. netron.app shows this for .onnx model
Screenshot from 2022-09-10 20-40-35

@fengyuentau
Copy link
Member

@fisakhan Obviously the input shape is still dynamic. You need to set it fixed using onnx-simplifier.

@fisakhan
Copy link

Now I receive this error:
Screenshot from 2022-09-11 16-07-03

[ERROR:0@114.883] global /opt/conda/conda-bld/opencv-suite_1659973592936/work/modules/dnn/src/onnx/onnx_importer.cpp (906) handleNode DNN/ONNX: ERROR during processing node with 3 inputs and 1 outputs: [Clip]:(StatefulPartitionedCall/RetinaFaceModel/MobileNetV2_extrator/Conv1_relu/Relu6:0) from domain='ai.onnx'
Traceback (most recent call last):
  File "/media/Disk/Projects/opencv/scr/image.py", line 147, in <module>
    net = cv2.dnn.readNet("/media/Disk/model_conversion/models/retinaface_mbv2_sim.onnx")

error: OpenCV(4.5.5) /opt/conda/conda-bld/opencv-suite_1659973592936/work/modules/dnn/src/onnx/onnx_importer.cpp:928: error: (-2:Unspecified error) in function 'handleNode'
> Node [Clip@ai.onnx]:(StatefulPartitionedCall/RetinaFaceModel/MobileNetV2_extrator/Conv1_relu/Relu6:0) parse error: OpenCV(4.5.5) /opt/conda/conda-bld/opencv-suite_1659973592936/work/modules/dnn/src/onnx/onnx_importer.cpp:1613: error: (-2:Unspecified error) in function 'void cv::dnn::dnn4_v20211220::ONNXImporter::parseClip(cv::dnn::dnn4_v20211220::LayerParams&, const opencv_onnx::NodeProto&)'
> >  (expected: 'node_proto.input_size() == 1'), where
> >     'node_proto.input_size()' is 3
> > must be equal to
> >     '1' is 1
> 

@fengyuentau
Copy link
Member

@fisakhan Please have a try with the latest OpenCV (for python: pip install opencv-python-rolling). This issue should have been resolved in #22100.

@fisakhan
Copy link

fisakhan commented Nov 4, 2022

Now I get the following error:

model = cv2.dnn.readNetFromONNX('./models/rf640_simplified.onnx')

error: OpenCV(4.6.0) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'handleNode'

Node [Shape@ai.onnx]:(onnx_node!StatefulPartitionedCall/RetinaFaceModel/tf_op_layer_Shape_2/Shape_2) parse error: OpenCV(4.6.0) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:2846: error: (-215:Assertion failed) !isDynamicShape in function 'parseShape'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: dnn (onnx) ONNX suport issues in DNN module
Projects
None yet
Development

No branches or pull requests

5 participants