问题描述
在 tensorflow 中,从头开始的训练产生了以下 6 个文件:
- events.out.tfevents.1503494436.06L7-BRM738
- model.ckpt-22480.meta
- 检查点
- 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 文件?我看到了几个线程,但没有一个对我有用.
您可以使用这个简单的脚本来做到这一点.但您必须指定输出节点的名称.
将 tensorflow 导入为 tfmeta_path = 'model.ckpt-22480.meta' # 你的 .meta 文件output_node_names = ['output:0'] # 输出节点使用 tf.Session() 作为 sess:# 恢复图形保护程序 = tf.train.import_meta_graph(meta_path)# 加载权重saver.restore(sess,tf.train.latest_checkpoint('path/of/your/.meta/file'))# 冻结图形frozen_graph_def = tf.graph_util.convert_variables_to_constants(评估,sess.graph_def,output_node_names)# 保存冻结图with open('output_graph.pb', 'wb') as f:f.write(frozen_graph_def.SerializeToString())
如果不知道输出节点的名字,有两种方法
您可以使用 Netron 或控制台 实用程序.
您可以将所有节点用作输出节点,如下所示.
output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]
(请注意,您必须将此行放在 convert_variables_to_constants
调用之前.)
但我认为这是不寻常的情况,因为如果您不知道输出节点,则实际上无法使用该图.
In tensorflow the training from the scratch produced following 6 files:
I would like to convert them (or only the needed ones) into one file graph.pb to be able to transfer it to my Android application.
I tried the script freeze_graph.py
but it requires as an input already the input.pb file which I do not have. (I have only these 6 files mentioned before). How to proceed to get this one freezed_graph.pb file? I saw several threads but none was working for me.
You can use this simple script to do that. But you must specify the names of the output nodes.
import tensorflow as tf
meta_path = 'model.ckpt-22480.meta' # Your .meta file
output_node_names = ['output:0'] # Output nodes
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('path/of/your/.meta/file'))
# 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())
If you don't know the name of the output node or nodes, there are two ways
You can explore the graph and find the name with Netron or with console summarize_graph utility.
You can use all the nodes as output ones as shown below.
output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]
(Note that you have to put this line just before convert_variables_to_constants
call.)
But I think it's unusual situation, because if you don't know the output node, you cannot use the graph actually.
这篇关于Tensorflow:如何将 .meta、.data 和 .index 模型文件转换为一个 graph.pb 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!