微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tensorflow:如何将.meta,.data和.index模型文件转换为一个graph.pb文件

在tensorflow中,从头开始训练产生以下6个文件
  1. events.out.tfevents.1503494436.06L7-BRM738
  2. model.ckpt-22480.Meta
  3. checkpoint
  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文件?我看到几个线程,但没有一个为我工作.

解决方法

您可以使用此简单脚本来执行此操作.但是您必须指定输出节点的名称.
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())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。