【问题标题】:Tensorflow: How to convert .meta, .data and .index model files into one graph.pb fileTensorflow:如何将 .meta、.data 和 .index 模型文件转换为一个 graph.pb 文件
【发布时间】:2018-02-02 11:57:39
【问题描述】:

在 tensorflow 中,从头开始的训练产生了以下 6 个文件:

  1. events.out.tfevents.1503494436.06L7-BRM738
  2. model.ckpt-22480.meta
  3. 检查点
  4. model.ckpt-22480.data-00000-of-00001
  5. model.ckpt-22480.index
  6. graph.pbtxt

我想将它们(或仅需要的)转换为一个文件 graph.pb,以便能够将其传输到我的 Android 应用程序。

我尝试了脚本freeze_graph.py,但它已经需要我没有的 input.pb 文件作为输入。 (我只有前面提到的这 6 个文件)。如何继续获取这个 freezed_graph.pb 文件?我看到了几个线程,但没有一个对我有用。

【问题讨论】:

  • 你是怎么得到graph.pbtxt的?如果它是您的模型图,您可以使用freeze.py 将其冻结。 .pbtxt.
  • 训练结束后在训练日志中找到的graph.pbtxt。然而,它在训练完成之前被保存了。在先前保存的图表状态中检查它。对于从头开始的训练,我使用了脚本:train_image_classifier.py。为了训练,我使用了自己的图片 (.jpg),在使用脚本 build_image_data.py 之前,我必须将其转换为 .tfrecord 文件

标签: graph tensorflow model meta checkpoint


【解决方案1】:

您可以使用这个简单的脚本来执行此操作。但您必须指定输出节点的名称。

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())

如果不知道输出节点的名称,有两种方法

  1. 您可以使用Netron 或控制台summarize_graph 实用程序浏览图表并找到名称。

  2. 您可以使用所有节点作为输出节点,如下所示。

output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]

(请注意,您必须将此行放在convert_variables_to_constants 调用之前。)

但我认为这是不寻常的情况,因为如果您不知道输出节点,您实际上无法使用图形。

【讨论】:

  • 有没有简单的方法来获取输出节点名称?
  • 我收到此错误,可能是因为我不确定我的 output_node_names 是否正确。 File "/path/to/saver.py", line 1796, in restore raise ValueError("Can't load save_path when it is None.")
  • @craq 看起来在当前目录中找不到任何检查点(路径名称为.)尝试明确设置检查点的路径:saver.restore(sess, 'path/to/model.ckpt')
  • 如果有人来到这里并遇到与我相同的问题,也就是尝试冻结图形时,它将失败并显示“尝试使用未初始化的值”,只需在加载后添加 init=tf.global_variables_initializer() sess.run(init)重量。
  • @Juan 通常并非所有可训练变量都是您需要的输出节点。此外,输出节点可能根本不是变量。例如,在表达式a * x + 1 的图中,输出节点是add
【解决方案2】:

由于可能对其他人有帮助,我也在github上回答后在这里回答;-)。 我认为您可以尝试这样的事情(使用 tensorflow/python/tools 中的 freeze_graph 脚本):

python freeze_graph.py --input_graph=/path/to/graph.pbtxt --input_checkpoint=/path/to/model.ckpt-22480 --input_binary=false --output_graph=/path/to/frozen_graph.pb --output_node_names="the nodes that you want to output e.g. InceptionV3/Predictions/Reshape_1 for Inception V3 "

这里的重要标志是 --input_binary=false,因为文件 graph.pbtxt 是文本格式。我认为它对应于所需的graph.pb,它是二进制格式的等价物。

关于 output_node_names,这对我来说真的很困惑,因为我在这部分仍然有一些问题,但是您可以在 tensorflow 中使用 summarise_graph 脚本,它可以将 pb 或 pbtxt 作为输入。

问候,

斯蒂芬

【讨论】:

  • 我应该用什么来代替 -out_node_name 的 ssd_mobilnet_v1_coco
  • @PratikKhadloya 你能回答我上面的评论吗?
  • 用法示例:python freeze_graph.py --input_graph=some_graph_def.pb --input_checkpoint=model.ckpt-8361242 --output_graph=/tmp/frozen_graph.pb --output_node_names=softmax
【解决方案3】:

我尝试了 freezed_graph.py 脚本,但 output_node_name 参数完全令人困惑。作业失败。

所以我尝试了另一个:export_inference_graph.py。 它按预期工作!

python -u /tfPath/models/object_detection/export_inference_graph.py \
  --input_type=image_tensor \
  --pipeline_config_path=/your/config/path/ssd_mobilenet_v1_pets.config \
  --trained_checkpoint_prefix=/your/checkpoint/path/model.ckpt-50000 \
  --output_directory=/output/path

我使用的tensorflow安装包来自这里: https://github.com/tensorflow/models

【讨论】:

  • 嗨@kennynut 这是什么--pipeline_config_path?这种文件里写了什么,你能给我举个例子吗?用了 tensorflow 有一段时间了,从来没用过这样的配置文件。
  • pipeline_config_path 提供了冻结图正常运行的基本配置。比如说,它通常带有默认名称-pipeline.config-在来自google git hub存储库的模型动物园之间的压缩包的默认根路径下。
【解决方案4】:

首先,使用以下代码生成graph.pb 文件。 使用 tf.Session() 作为 sess:

    # Restore the graph
    _ = tf.train.import_meta_graph(args.input)

    # save graph file
    g = sess.graph
    gdef = g.as_graph_def()
    tf.train.write_graph(gdef, ".", args.output, True)

然后,使用汇总图获取输出节点名称。 最后,使用

python freeze_graph.py --input_graph=/path/to/graph.pbtxt --input_checkpoint=/path/to/model.ckpt-22480 --input_binary=false --output_graph=/path/to/frozen_graph.pb --output_node_names="the nodes that you want to output e.g. InceptionV3/Predictions/Reshape_1 for Inception V3 "

生成冻结图。

【讨论】:

    猜你喜欢
    • 2019-06-18
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多