{
  "id": "opencv/dnn-readnet-from-onnx-failed",
  "signature": "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'",
  "signature_zh": "cv::error: (-2:未指定错误) 解析 ONNX 模型失败: (节点: 'Gemm_0') 输入张量 'A' 有 3 维但预期 2 维 在函数 'cv::dnn::dnn4_v20231225::ONNXImporter::parseNode' 中",
  "regex": "Failed to parse ONNX model.*Gemm.*Input tensor.*has \\d+ dimensions but expected \\d+",
  "domain": "opencv",
  "category": "data_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "ONNX 模型包含一个 Gemm（通用矩阵乘法）节点，该节点期望 2D 输入，但实际输入张量有 3 维，通常是由于批次维度不匹配或模型导出错误。",
  "versions": [
    {
      "version": "opencv-4.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "opencv-4.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "opencv-4.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Reinstall OpenCV with ONNX support enabled via -DWITH_ONNX=ON",
      "why_fails": "The error is a parsing issue with the model structure, not a missing module. OpenCV's DNN module already supports ONNX by default.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Convert the model to TensorFlow protobuf format and load with readNetFromTensorflow",
      "why_fails": "The underlying layer structure issue (Gemm with wrong tensor shape) will likely persist after conversion, or the conversion itself may fail.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Set the input blob with a different shape using blobFromImage with size 224x224",
      "why_fails": "The error is at model parsing time, before any forward pass. Changing input blob shape doesn't affect model structure parsing.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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.",
      "success_rate": 0.8,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "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.",
      "success_rate": 0.75,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "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')",
      "success_rate": 0.7,
      "how": "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')",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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.",
    "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.",
    "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')"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://docs.opencv.org/4.x/d6/d0f/group__dnn.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.78,
  "resolvable": "true",
  "first_seen": "2024-01-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}