You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow导入错误:无法导入model_fn,模型转TFLite格式失败求助

Fixing TFLite Conversion & ImportError Issues with TensorFlow 1.12.0

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:
    pip install tensorflow-gpu==1.12.0
    
    If you don't need GPU acceleration, stick to the CPU version:
    pip 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.pb is a frozen graph (no trainable variables left)—this is the default output of TensorFlow's official retrain.py script, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:57:43