TensoeFlow 模型保存为pb文件的解释,怎么使用pb文件/模型的Save and Restore

1.pb文件

pb是protocol(协议) buffer(缓冲)的缩写。TensorFlow训练模型后存成的pb文件,是一种表示模型(神经网络)结构的二进制文件,不带有源代码,也一般无法映射成源代码。这就有点像C 语言编译产生的机器码一般无法映射回源代码一样。

pb文件作为SavedModel的一部分,可以加载回TensorFlow进行部署或进一步训练。

作者:蔡善清
链接:https://www.zhihu.com/question/271666487/answer/365519609
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


以上是在我困惑时,知乎作者的回答,我一直想打开这个pb文件看源代码,后来才知道这里根本没有源代码,要找代码还是去github的作者下找代码,不要再试图打开pb文件了。

2.tensorflow模型的保存与恢复是怎样的呢?

这一部分包括变量的保存和模型的保存

参考官网:tensorflow

tf.train.saver 程序类提供了保存和恢复模型的方法。tf.saved_model.simple_save 函数是构建适合服务的保存模型的简单方法。估计器在model_dir中自动保存和恢复变量。

------------------------------------------------------------------------------------------------------

2.1 保存和恢复变量

保存:用tf.train.Saver()创建一个Saver来管理模型中的所有变量。例如,下面的代码片段演示了如何调用tf.train.Saver。保存方法,将变量保存到检查点文件(checkpoint file).

import tensorflow as tf

# Create some variables.
v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", shape=[5], initializer = tf.zeros_initializer)

inc_v1 = v1.assign(v1+1)
dec_v2 = v2.assign(v2-1)

# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, and save the
# variables to disk.
with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  inc_v1.op.run()
  dec_v2.op.run()
  # Save the variables to disk.
  save_path = saver.save(sess, "model.ckpt")
  print("Model saved in path {}".format(save_path))

恢复:注意,当您恢复变量时,您不需要预先初始化它们。例如,下面的代码片段演示了如何调用tf.train.Saver。恢复从检查点文件中恢复变量的方法:

tf.reset_default_graph()
# 将以前的graph清零
# Create some variables.
v1 = tf.get_variable("v1", shape=[3])
v2 = tf.get_variable("v2", shape=[5])

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
  # Restore variables from disk.
  saver.restore(sess, "model.ckpt")
  print("Model restored.")
  # Check the values of the variables
  print("v1 : %s" % v1.eval())
  print("v2 : %s" % v2.eval())

注意:没有一个物理文件叫做model.ckpt。它是为检查点创建的文件名的前缀。用户只与前缀交互,而不与物理检查点文件交互。换句话说,你保存的文件没有model.ckpt这个文件,而是一个叫checkpoint的文件


选择存储和恢复哪些变量

如果你不给tf.train.Saver()传入任何参数,那么saver将处理graph中的所有变量。其中每一个变量都以变量创建时传入的名称被保存。有时候在检查点文件中明确定义变量的名称很有用。举个例子,你也许已经训练得到了一个模型,其中有个变量命名为"weights",你想把它的值恢复到一个新的变量"params"中。

有时候仅保存和恢复模型的一部分变量很有用。再举个例子,你也许训练得到了一个5层神经网络,现在想训练一个6层的新模型,可以将之前5层模型的参数导入到新模型的前5层中。
你可以通过给tf.train.Saver()构造函数传入Python字典,很容易地定义需要保持的变量及对应名称:键对应使用的名称,值对应被管理的变量。

注意:
1.如果需要保存和恢复模型变量的不同子集,可以创建任意多个saver对象。同一个变量可被列入多个saver对象中,只有当saver的restore()函数被运行时,它的值才会发生改变。

2.如果你仅在session开始时恢复模型变量的一个子集,你需要对剩下的变量执行初始化op。详情请见tf.initialize_variables()。3.要检查检查点中的变量,您可以使用inspect_checkpoint库,特别是print_tensors_in_checkpoint_file 函数

tf.reset_default_graph()
# Create some variables.
v1 = tf.get_variable("v1", [3], initializer = tf.zeros_initializer)
v2 = tf.get_variable("v2", [5], initializer = tf.zeros_initializer)

# Add ops to save and restore only `v2` using the name "v2"
saver = tf.train.Saver({"v2": v2}) # 单独保存一个变量创建字典,键为名,值为值

# Use the saver object normally after that.
with tf.Session() as sess:
  # Initialize v1 since the saver will not.
  v1.initializer.run() # 保存的变量 v2 不用初始化,就像是做迁移学习直接用保存的值作为初始值,所以不要再初始化
  saver.restore(sess, "model.ckpt")

  print("v1 : %s" % v1.eval())
  print("v2 : %s" % v2.eval())

下面演示使用print_tensors_in_checkpoint_file()检查在checkpoint中的变量

# import the inspect_checkpoint library
from tensorflow.python.tools import inspect_checkpoint as chkp

# print all tensors in checkpoint file
chkp.print_tensors_in_checkpoint_file("model.ckpt", tensor_name='', all_tensors=True)

# tensor_name:  v1
# [ 1.  1.  1.]
# tensor_name:  v2
# [-1. -1. -1. -1. -1.]

# print only tensor v1 in checkpoint file
chkp.print_tensors_in_checkpoint_file("model.ckpt", tensor_name='v1', all_tensors=False)

# tensor_name:  v1
# [ 1.  1.  1.]

# print only tensor v2 in checkpoint file
chkp.print_tensors_in_checkpoint_file("model.ckpt", tensor_name='v2', all_tensors=False)

# tensor_name:  v2
# [-1. -1. -1. -1. -1.]

2.2 保存和恢复模型(models)

使用SavedModel来保存和加载您的模型变量、图和图的元数据。这是一种与语言无关的、可恢复的、可恢复的序列化格式,可以支持更高级别的系统和工具来生成、使用和转换TensorFlow模型。TensorFlow提供了几种与SavedModel交互的方法,包括tf.saved_model api、tf.estimator.Estimator和命令行界面。

构建并加载SavedModel:

2.2.1 简单保存

创建SavedModel的最简单方法是使用tf.saved_model.simple_save 函数 

simple_save(session,
            export_dir,
            inputs={"x": x, "y": y},
            outputs={"z": z})

2.2.2 手工构建一个SavedModel

使用 tf.saved_model.builder.SavedModelBuilder 创建一个SavedModel。

如果资产需要保存并写入或复制到磁盘,则可以在首次MetaGraphDef添加时提供资产。如果多个MetaGraphDefs与同名的资产相关联,则只保留第一个版本。
MetaGraphDef添加到SavedModel中的每个都必须用用户指定的标签进行注释。标签提供了一种识别特定 MetaGraphDef于加载和恢复的方法,以及共享的一组变量和资产。这些标签通常MetaGraphDef用其功能(例如,服务或培训)以及可选地具有硬件特定方面(例如,GPU)来注释。

例如,以下代码建议使用一种典型的 SavedModelBuilder构建SavedModel的方法:

export_dir = ... # 填写你的保存地址
...
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.Session(graph=tf.Graph()) as sess:
  ...
  builder.add_meta_graph_and_variables(sess,
                                       [tag_constants.TRAINING],
                                       signature_def_map=foo_signatures,
                                       assets_collection=foo_assets)
...
# Add a second MetaGraphDef for inference.
with tf.Session(graph=tf.Graph()) as sess:
  ...
  builder.add_meta_graph([tag_constants.SERVING])
...
builder.save()
在Python中加载SavedModel
SavedModel 加载器的Python版本为SavedModel 提供加载和恢复功能。该load操作需要以下信息:
.恢复图形定义和变量的会话。
.标签用于标识要加载的MetaGraphDef。
.SavedModel的位置(目录)。
在加载时,作为特定MetaGraphDef的一部分提供的变量,资产和签名的子集将被还原到提供的会话中。
export_dir = ...
...
with tf.Session(graph=tf.Graph()) as sess:
  tf.saved_model.loader.load(sess, [tag_constants.TRAINING], export_dir)
  ...









猜你喜欢

转载自blog.csdn.net/u014264373/article/details/79943389