TensorFlow导入错误:无法导入model_fn,模型转TFLite格式失败求助
Let's tackle your problems one by one—you're dealing with a version conflict that's causing import errors, plus a potentially incorrect approach to converting your frozen graph to TFLite. Here's how to get it working:
1. First, Fix the TensorFlow Version Mismatch
You've got tensorflow 1.12.0 and tensorflow-gpu 1.1.0 installed at the same time, which is a critical issue. These two packages are mutually exclusive (tensorflow-gpu is just the GPU-enabled variant of tensorflow), and having mismatched versions will wreak havoc with dependencies and imports.
To fix this:
- Uninstall both versions first:
pip uninstall -y tensorflow tensorflow-gpu - Reinstall a consistent version. If you need GPU support, go with the GPU-enabled 1.12.0:
If you don't need GPU acceleration, stick to the CPU version:pip install tensorflow-gpu==1.12.0pip install tensorflow==1.12.0
2. Resolve the ImportError: cannot import name model_fn
This error is almost certainly a side effect of the version conflict. But even beyond that, for converting a frozen .pb model (like your retrained_graph.pb) to TFLite, you don't need to use model_fn at all—that's part of the Estimator API, which isn't required here. You were likely using an incorrect conversion script that relied on Estimator, which was broken by the version mismatch.
3. Convert Your Frozen Graph to TFLite Correctly
In TensorFlow 1.12.0, you'll use tf.contrib.lite.TFLiteConverter for frozen graphs. Here's a complete, working script tailored to your retrained model:
import tensorflow as tf # Configure paths and node names FROZEN_GRAPH_PATH = 'retrained_graph.pb' TFLITE_OUTPUT_PATH = 'converted_model.tflite' # These are the default node names for models trained with TensorFlow's retrain.py INPUT_NODE_NAME = 'Placeholder' OUTPUT_NODE_NAME = 'final_result' # Initialize the converter from the frozen graph converter = tf.contrib.lite.TFLiteConverter.from_frozen_graph( graph_def_file=FROZEN_GRAPH_PATH, input_arrays=[INPUT_NODE_NAME], output_arrays=[OUTPUT_NODE_NAME] ) # Optional: Allow custom operations if your graph uses unsupported ops # converter.allow_custom_ops = True # Perform the conversion tflite_model = converter.convert() # Save the converted model with open(TFLITE_OUTPUT_PATH, 'wb') as f: f.write(tflite_model) print(f"Conversion complete! TFLite model saved to {TFLITE_OUTPUT_PATH}")
If You Don't Know Your Input/Output Node Names
If the default node names don't work (e.g., you modified the retrain script), run this quick snippet to list all nodes in your graph:
import tensorflow as tf with tf.gfile.GFile(FROZEN_GRAPH_PATH, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) with tf.Session() as sess: tf.import_graph_def(graph_def, name='') # Print all operation names in the graph for op in sess.graph.get_operations(): print(op.name)
Look for nodes that match your input (usually something like input or Placeholder) and output (like final_result or predictions).
Final Notes
- Ensure your
retrained_graph.pbis a frozen graph (no trainable variables left)—this is the default output of TensorFlow's officialretrain.pyscript, so you should be good here. - If you hit errors about unsupported operations, enable custom ops with
converter.allow_custom_ops = True(though you may need to handle those ops at inference time).
内容的提问来源于stack exchange,提问作者Manish Mahajan

