{
  "id": "opencv/dnn-readnet-from-tensorflow-failed",
  "signature": "cv2.error: OpenCV(4.8.0) /modules/dnn/src/tensorflow/tf_io.cpp:89: error: (-2:Unspecified error) FAILED to read .pb file: Invalid GraphDef",
  "signature_zh": "cv2.error: OpenCV(4.8.0) /modules/dnn/src/tensorflow/tf_io.cpp:89: error: (-2:未指定错误) 无法读取 .pb 文件：无效的 GraphDef",
  "regex": "cv2\\.error: OpenCV\\([\\d.]+\\).*tf_io\\.cpp:\\d+: error: \\(-2:Unspecified error\\) FAILED to read \\.pb file: Invalid GraphDef",
  "domain": "opencv",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "The TensorFlow .pb model file is corrupted, incompatible with OpenCV's protobuf version, or exported with a different opset.",
  "root_cause_type": "generic",
  "root_cause_zh": "TensorFlow .pb 模型文件损坏、与 OpenCV 的 protobuf 版本不兼容，或使用不同的操作集导出。",
  "versions": [
    {
      "version": "4.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "4.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "If the model was exported with an incompatible TensorFlow version, re-downloading won't change the GraphDef format.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "ONNX conversion may fail if the original GraphDef is malformed or contains unsupported operations.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "OpenCV's bundled protobuf may still be incompatible with newer TensorFlow exports; the error persists.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "# In TensorFlow:\nimport tensorflow as tf\nfrom tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2\n# Freeze and save as frozen graph\nmodel = tf.saved_model.load('model_dir')\nfrozen_func = convert_variables_to_constants_v2(model.signatures['serving_default'])\ntf.io.write_graph(frozen_func.graph, '.', 'frozen_model.pb', as_text=False)",
      "success_rate": 0.8,
      "how": "# In TensorFlow:\nimport tensorflow as tf\nfrom tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2\n# Freeze and save as frozen graph\nmodel = tf.saved_model.load('model_dir')\nfrozen_func = convert_variables_to_constants_v2(model.signatures['serving_default'])\ntf.io.write_graph(frozen_func.graph, '.', 'frozen_model.pb', as_text=False)",
      "condition": "",
      "sources": []
    },
    {
      "action": "net = cv2.dnn.readNetFromTensorflow('model.pb', 'model.pbtxt')  # Provide text protobuf for graph structure",
      "success_rate": 0.75,
      "how": "net = cv2.dnn.readNetFromTensorflow('model.pb', 'model.pbtxt')  # Provide text protobuf for graph structure",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "# In TensorFlow:\nimport tensorflow as tf\nfrom tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2\n# Freeze and save as frozen graph\nmodel = tf.saved_model.load('model_dir')\nfrozen_func = convert_variables_to_constants_v2(model.signatures['serving_default'])\ntf.io.write_graph(frozen_func.graph, '.', 'frozen_model.pb', as_text=False)",
    "net = cv2.dnn.readNetFromTensorflow('model.pb', 'model.pbtxt')  # Provide text protobuf for graph structure"
  ],
  "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": "-2",
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-06-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}