【问题标题】:Conversion of .pb file to .ckpt (tensorflow)将 .pb 文件转换为 .ckpt (tensorflow)
【发布时间】:2018-01-11 11:57:23
【问题描述】:

我已成功使用此脚本将预训练的 .ckpt 模型转换为 .pb(protobuf)格式:

import os
import tensorflow as tf

# Get the current directory
dir_path = os.path.dirname(os.path.realpath(__file__))
print "Current directory : ", dir_path
save_dir = dir_path + '/Protobufs'

graph = tf.get_default_graph()

# Create a session for running Ops on the Graph.
sess = tf.Session()

print("Restoring the model to the default graph ...")
saver = tf.train.import_meta_graph(dir_path + '/model.ckpt.meta')
saver.restore(sess,tf.train.latest_checkpoint(dir_path))
print("Restoring Done .. ")

print "Saving the model to Protobuf format: ", save_dir

#Save the model to protobuf  (pb and pbtxt) file.
tf.train.write_graph(sess.graph_def, save_dir, "Binary_Protobuf.pb", False)
tf.train.write_graph(sess.graph_def, save_dir, "Text_Protobuf.pbtxt", True)
print("Saving Done .. ")

现在,我想要的是反之亦然的程序。如何加载 protobuf 文件并将其转换为 .ckpt(检查点)格式?

我正在尝试使用以下脚本执行此操作,但它总是失败:

import tensorflow as tf
import argparse 

# Pass the filename as an argument
parser = argparse.ArgumentParser()
parser.add_argument("--frozen_model_filename", default="/path-to-pb-file/Binary_Protobuf.pb", type=str, help="Pb model file to import")
args = parser.parse_args()

    # We load the protobuf file from the disk and parse it to retrieve the 
    # unserialized graph_def
with tf.gfile.GFile(args.frozen_model_filename, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

    #saver=tf.train.Saver()
    with tf.Graph().as_default() as graph:

        tf.import_graph_def(
            graph_def,
            input_map=None,
            return_elements=None,
            name="prefix",
            op_dict=None,
            producer_op_list=None
        )
        sess = tf.Session(graph=graph)
        saver=tf.train.Saver()
        save_path = saver.save(sess, "path-to-ckpt/model.ckpt")
         print("Model saved to chkp format")

我相信拥有这些转换脚本会很有帮助。

P.S : 权重已经嵌入到 .pb 文件中。

谢谢。

【问题讨论】:

    标签: python tensorflow protocol-buffers


    【解决方案1】:

    您似乎只在两个文件中获得了图形定义,而不是冻结模型。

    # This two lines only save the graph as proto file; it doesn't save the variables and their values. 
    tf.train.write_graph(sess.graph_def, save_dir, "Binary_Protobuf.pb", False)
    tf.train.write_graph(sess.graph_def, save_dir, "Text_Protobuf.pbtxt", True)
    

    使用freeze_graph file获得冻结图

    【讨论】:

    • 模型是从检查点(带有权重)所在的 restore(sess,tf.train.latest_checkpoint(dir_path)) 加载的。
    • 好吧!在第二个脚本中,您没有加载任何模型,您只是导入了图表。即使您在第一个脚本中加载模型,它也不会将变量写入 pb 文件。
    • 好的,我在 tf.train.write_graph 函数中将 sess.graph_def 更改为 sess.graph,但运气相同。
    • 你已经使用了我之前写的 freeze_graph 脚本,write_graph 不写变量和它的值。
    • 如何在我的脚本中使用它?作为一个 python 脚本,它需要我没有的输入。 out.pb、in.pb 等我只有检查点文件。
    【解决方案2】:

    如果您在 pb 文件中加载模型,是否可以将其重新训练为预训练模型。 我使用“tf.global_variables()”来获取可以训练的变量,但是当我加载 pb 模型时没有 vars 返回。

    tf.global_variables()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多