Are dynamic input shapes a problem for Yolov8 models loaded with readNetFromONNX?

I am unsure about my understanding of the concept of dynamic input shapes and if this poses a problem for using Yolov8 models in OpenCV.

I managed to compile and execute the code of a pre-trained model MobileNet SSD in C++ and it gives the expected object detection results as in the “bikes” image ( see code here: OpenCV DNN Module and Deep Learning (A Definitive guide) ).

I am however a bit confused. I have heard that dynamic input shapes pose a problem for readNetFromONNX and wonder if models from Yolov8 should work or not.
If not, at what point should I expect errors? Should errors occur at:

  1. the level of loading the model at readNetFromOnnx or,
  2. the level of resulting bounding boxes from Mat output = model.forward();.
    Thank you very much in advance for your clarification!

dynamic input (or layer) shape does not work with opencv. Dynamic means at inference time.
But with onnx model if input is dynamic you can set input shape :

  1. simplify model
    onnxsim a.onnx a.sim.onnx
  2. use netron to know input and name shape

  1. fixed input shape using
python -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param batch --dim_value 1 a.sim.onnx a1.onnx
python -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param width --dim_value 640 a1.onnx a2.onnx
python -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param height --dim_value 480 a2.onnx a3.onnx

Sometimes data input layer are dynamic then post an issue

1 Like

Thank you for your kind help! Does that mean that even if I load an ONNX model with readNetFromONNX in OpenCV and I get results, that the results are (probably) wrong because of errors in the resulting array shapes?

I don’t understand. Have you got an example (link to model)?

For example discussed under the section Load the MobileNet SSD Model and Prepare the Input in OpenCV DNN Module and Deep Learning (A Definitive guide)

". It does use readNet("../../../input/frozen_inference_graph.pb",
"../../../input/ssd_mobilenet_v2_coco_2018_03_29.pbtxt.txt", "TensorFlow");

In this example there are no dynamic input shapes?

My problem is that I do not see in which cases object detection models can be read without problems in OpenCV and in which cases not.

I checked opencv code.
There is two importers
void TFImporter::populateNet()
and
void ONNXImporter::populateNet()

and behaviours seem different for dynamic shape.
Unfortunately tensorflow is in coma state all new models come from pytorch. Pytorch model must be convert in ONNX model because there is no graph in pytorch model

1 Like

@berak @crackwitz
Is my answer good?
With Tensorflow model dynamic shape is possible in opencv
With ONNX format dynamic shape is not possible in opencv