在tensorflow中,从头开始训练产生以下6个文件:
- events.out.tfevents.1503494436.06L7-BRM738
- model.ckpt-22480.Meta
- checkpoint
- model.ckpt-22480.data-00000-of-00001
- model.ckpt-22480.index
- graph.pbtxt
我想将它们(或仅需要的)转换为一个文件graph.pb,以便能够将其转移到我的Android应用程序.
我尝试了脚本freeze_graph.py但它需要输入我还没有的input.pb文件. (我之前只提到过这6个文件).如何获得这个freezed_graph.pb文件?我看到几个线程,但没有一个为我工作.
解决方法
您可以使用此简单脚本来执行此操作.但是您必须指定输出节点的名称.
import tensorflow as tf
Meta_path = 'model.ckpt-22480.Meta' # Your .Meta file
with tf.Session() as sess:
# Restore the graph
saver = tf.train.import_Meta_graph(Meta_path)
# Load weights
saver.restore(sess,tf.train.latest_checkpoint('.'))
# Output nodes
output_node_names =[n.name for n in tf.get_default_graph().as_graph_def().node]
# Freeze the graph
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
sess,sess.graph_def,output_node_names)
# Save the frozen graph
with open('output_graph.pb','wb') as f:
f.write(frozen_graph_def.SerializetoString())