opencv data_error ai_generated true

cv::error: (-2:Unspecified error) Failed to parse ONNX model: (node: 'Gemm_0') Input tensor 'A' has 3 dimensions but expected 2 in function 'cv::dnn::dnn4_v20231225::ONNXImporter::parseNode'

ID: opencv/dnn-readnet-from-onnx-failed

Also available as: JSON · Markdown · 中文
78%Fix Rate
82%Confidence
1Evidence
2024-01-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
opencv-4.8.0 active
opencv-4.9.0 active
opencv-4.10.0 active

Root Cause

The ONNX model contains a Gemm (general matrix multiply) node that expects 2D inputs, but the actual input tensor has 3 dimensions, often due to batch dimension mismatch or incorrect model export.

generic

中文

ONNX 模型包含一个 Gemm(通用矩阵乘法)节点,该节点期望 2D 输入,但实际输入张量有 3 维,通常是由于批次维度不匹配或模型导出错误。

Official Documentation

https://docs.opencv.org/4.x/d6/d0f/group__dnn.html

Workarounds

  1. 80% success Use ONNX Runtime (onnxruntime) to validate and fix the model: run 'python -c "import onnx; model = onnx.load('model.onnx'); onnx.checker.check_model(model)"'. If the model is valid, try simplifying it with onnx-simplifier: 'python -m onnxsim model.onnx simplified.onnx'. Then load simplified.onnx with OpenCV.
    Use ONNX Runtime (onnxruntime) to validate and fix the model: run 'python -c "import onnx; model = onnx.load('model.onnx'); onnx.checker.check_model(model)"'. If the model is valid, try simplifying it with onnx-simplifier: 'python -m onnxsim model.onnx simplified.onnx'. Then load simplified.onnx with OpenCV.
  2. 75% success Export the model from PyTorch with torch.onnx.export(model, dummy_input, 'model.onnx', opset_version=11, input_names=['input'], output_names=['output'], dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}). Ensure the model uses only supported operators by setting opset_version <= 12.
    Export the model from PyTorch with torch.onnx.export(model, dummy_input, 'model.onnx', opset_version=11, input_names=['input'], output_names=['output'], dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}). Ensure the model uses only supported operators by setting opset_version <= 12.
  3. 70% success Manually edit the ONNX graph using onnx_graphsurgeon: remove the problematic Gemm node and replace with MatMul + Add. Example: import onnx_graphsurgeon as gs; graph = gs.import_onnx(onnx.load('model.onnx')); for node in graph.nodes: if node.op == 'Gemm': node.op = 'MatMul'; # adjust inputs/outputs; onnx.save(gs.export_onnx(graph), 'fixed.onnx')
    Manually edit the ONNX graph using onnx_graphsurgeon: remove the problematic Gemm node and replace with MatMul + Add. Example: import onnx_graphsurgeon as gs; graph = gs.import_onnx(onnx.load('model.onnx')); for node in graph.nodes: if node.op == 'Gemm': node.op = 'MatMul'; # adjust inputs/outputs; onnx.save(gs.export_onnx(graph), 'fixed.onnx')

中文步骤

  1. Use ONNX Runtime (onnxruntime) to validate and fix the model: run 'python -c "import onnx; model = onnx.load('model.onnx'); onnx.checker.check_model(model)"'. If the model is valid, try simplifying it with onnx-simplifier: 'python -m onnxsim model.onnx simplified.onnx'. Then load simplified.onnx with OpenCV.
  2. Export the model from PyTorch with torch.onnx.export(model, dummy_input, 'model.onnx', opset_version=11, input_names=['input'], output_names=['output'], dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}). Ensure the model uses only supported operators by setting opset_version <= 12.
  3. Manually edit the ONNX graph using onnx_graphsurgeon: remove the problematic Gemm node and replace with MatMul + Add. Example: import onnx_graphsurgeon as gs; graph = gs.import_onnx(onnx.load('model.onnx')); for node in graph.nodes: if node.op == 'Gemm': node.op = 'MatMul'; # adjust inputs/outputs; onnx.save(gs.export_onnx(graph), 'fixed.onnx')

Dead Ends

Common approaches that don't work:

  1. Reinstall OpenCV with ONNX support enabled via -DWITH_ONNX=ON 95% fail

    The error is a parsing issue with the model structure, not a missing module. OpenCV's DNN module already supports ONNX by default.

  2. Convert the model to TensorFlow protobuf format and load with readNetFromTensorflow 70% fail

    The underlying layer structure issue (Gemm with wrong tensor shape) will likely persist after conversion, or the conversion itself may fail.

  3. Set the input blob with a different shape using blobFromImage with size 224x224 90% fail

    The error is at model parsing time, before any forward pass. Changing input blob shape doesn't affect model structure parsing.