opencv / opencv

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

Inconsistent shape for ConcatLayer in function 'getMemoryShapes' while reading onnx model #25485

Closed gursimarsingh closed 2 weeks ago

gursimarsingh commented 3 weeks ago

System Information

Python 3.10.10 General configuration for OpenCV 4.6, 4.x and 5.x =====================================

Platform: Host: Linux (Ubuntu 22.04) x86_64 CMake: 3.22.1 Configuration: Debug Release

Detailed description

Detailed description

Issue came up while adding a new onnx model dexiened for edge detection sample in dnn

onnx model can be downloaded from here

Model works perfectly when inferred with onnx and opencv==4.6.0

I get the following error with opencv==4.9.0-dev and opencv==5.x during model loading:

ERROR:0@1.272] global onnx_importer.cpp:1032 handleNode DNN/ONNX: ERROR during processing node with 6 inputs and 1 outputs: [Concat]:(onnx_node!Concat_146) from domain='ai.onnx'
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.9.0-dev) opencv/modules/dnn/src/onnx/onnx_importer.cpp:1054: error: (-2:Unspecified error) in function 'handleNode'
> Node [Concat@ai.onnx]:(onnx_node!Concat_146) parse error: OpenCV(4.9.0-dev) opencv/modules/dnn/src/layers/concat_layer.cpp:108: error: (-201:Incorrect size of input array) Inconsistent shape for ConcatLayer in function 'getMemoryShapes'

Similar issues: https://github.com/opencv/opencv/issues/24030 , https://github.com/opencv/opencv/issues/19416

Steps to reproduce

Here is the inference snippet:

        session = cv.dnn.readNetFromONNX(args.onnxmodel)
        # Load and process the image using DexiNed model
        orig_img = cv.imread(cv.samples.findFile(args.input))
        # Prepocess the image
        img = preprocess(orig_img)

        inp = cv.dnn.blobFromImage(img, swapRB=False, crop=False)

        session.setInput(inp)

        out = session.forward()

Issue submission checklist

LaurentBerger commented 2 weeks ago

No error : you must simplify model :

onnxsim model(1).onnx a.sim.onnx image opencv onnx importer does not process dynamic shape. You have to substitue w and h

image

python -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param w --dim_value 640 a.sim.onnx a1.onnx

python -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param h --dim_value 480 a1.onnx a2.onnx

then python

Python 3.10.10 (tags/v3.10.10:aad5f6a, Feb  7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> x = cv.dnn.readNet("a2.onnx")
>>> import numpy as np
>>> img =  np.zeros((1, 3, 480,640))
>>> x.setInput(img)
>>> w = x.forward()
>>>
WanliZhong commented 2 weeks ago

This bug should be fixed if possible. dexiened is used to replace hed_pretrained_bsds model for edge detection. Do we have other public onnx models without manual modification for edge detection? Use public model link will save our traffic.

LaurentBerger commented 2 weeks ago

This bug should be fixed if possible.

https://github.com/opencv/opencv/issues/19347#issuecomment-1868227401

WanliZhong commented 2 weeks ago

This bug should be fixed if possible.

#19347 (comment)

OK, Thanks! Replaced model PR is crested https://github.com/opencv/opencv/pull/25515

LaurentBerger commented 2 weeks ago

This bug should be fixed if possible.

bug fixed