冻结Tensorflow模型由session_bundle.exporter

时间:2016-11-28 15:27:34

标签: tensorflow tensorflow-serving

我目前正在关注Train And Export TensorFlow Model

model_exporter = exporter.Exporter(saver)
...
model_exporter.export(...)

为每一步产生:

  173 Nov 26 17:26 checkpoint
  31M Nov 26 17:26 export-00000-of-00001
 1.5M Nov 26 17:26 export.meta

如何获取这些文件并创建frozen model(例如使用freeze_graph.py)?

看起来freeze_graph.py需要GraphDef,但我拥有的只是MetaGraph文件。我需要先提取这个吗?

可以将export-00000-of-00001文件用于“要加载的TensorFlow变量文件”吗?

尝试冻结模型时是否还有其他标志?

1 个答案:

答案 0 :(得分:0)

这似乎对我有用:

from tensorflow.python.framework import graph_util
from tensorflow.contrib.session_bundle import session_bundle
import tensorflow as tf

export_dir = '/tf_files/00000170/'
output_graph = '/tf_files/00000170/frozen.pb'
clear_devices = True

sess, meta_graph_def = session_bundle.load_session_bundle_from_path(export_dir)

input_graph_def = meta_graph_def.graph_def
if clear_devices:
    for node in input_graph_def.node:
        node.device = ''

output_graph_def = graph_util.convert_variables_to_constants(sess, input_graph_def, ['flatten5/Reshape'])

with tf.gfile.GFile(output_graph, "wb") as f:
    f.write(output_graph_def.SerializeToString())