TensorFlow新手将.pb转TFLite遇错:Graph无SerializeToString属性
'Graph' object has no attribute 'SerializeToString' Error When Converting .pb to TFLite Hey there! Let's break down why you're seeing this error and how to fix it quickly.
What's Causing the Error?
The tf.contrib.lite.toco_convert function (or its TF 2.x equivalent) expects a GraphDef object (the serialized definition of your model graph) as input, but you're passing it a full tf.Graph instance (CD_graph in your code). The SerializeToString method is part of GraphDef, not the Graph class—hence the error.
Solution 1: For TensorFlow 1.x
Let's adjust your code to pass the correct GraphDef instead of the Graph object. You can either modify your load_graph function to return the GraphDef, or extract it from the loaded graph:
Option A: Modify the Loader to Return GraphDef
import numpy as np import tensorflow as tf from collections import OrderedDict def load_graph_def(frozen_graph_filename): with tf.gfile.GFile(frozen_graph_filename, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) return graph_def # Load the GraphDef instead of the full Graph CD_graph_def = load_graph_def("CD_Check_k.pb") # Define your input/output tensor names (match your model's nodes) input_tensor = "prefix/input_node:0" output_tensor = "prefix/output_node:0" # Convert to TFLite tflite_model = tf.contrib.lite.toco_convert( CD_graph_def, input_tensors=[input_tensor], output_tensors=[output_tensor] ) # Save the converted model with open('./CD_Check_k.tflite', "wb") as f: f.write(tflite_model)
Option B: Extract GraphDef from Your Existing Loaded Graph
If you want to keep using your original load_graph function:
CD_graph = load_graph("CD_Check_k.pb") CD_input = CD_graph.get_tensor_by_name('prefix/input_node:0') CD_output = CD_graph.get_tensor_by_name('prefix/output_node:0') # Extract the GraphDef from the loaded Graph graph_def = CD_graph.as_graph_def() with tf.Session(graph=CD_graph) as sess: tflite_model = tf.contrib.lite.toco_convert( graph_def, input_tensors=[CD_input], output_tensors=[CD_output] ) with open('./CD_Check_k.tflite', "wb") as f: f.write(tflite_model)
Solution 2: For TensorFlow 2.x
If you're using TF 2.x, the tf.contrib.lite module has been moved to tf.lite, and the API is cleaner with from_frozen_graph:
import tensorflow as tf # Directly convert the frozen .pb file converter = tf.lite.TFLiteConverter.from_frozen_graph( graph_def_file="CD_Check_k.pb", input_arrays=["prefix/input_node"], # Input node name (without the :0 suffix) input_shapes={"prefix/input_node": [1, 256, 256, 3]}, # Match your input shape output_arrays=["prefix/output_node"] # Output node name (without the :0 suffix) ) tflite_model = converter.convert() # Save the model with open('./CD_Check_k.tflite', "wb") as f: f.write(tflite_model)
Key Notes
- Make sure your input/output node names exactly match what's in your frozen graph (use tools like TensorBoard to verify if you're unsure).
- For TF 2.x, you don't need to manually load the graph—
from_frozen_graphhandles that for you.
内容的提问来源于stack exchange,提问作者Harshit Mishra

