Explanation of TensoeFlow model saving as pb file, how to use Save and Restore of pb file/model

1.pb file

pb is the abbreviation of protocol (protocol) buffer (buffer). The pb file saved after TensorFlow trains the model is a binary file representing the structure of the model (neural network), without source code, and generally cannot be mapped into source code . It's a bit like the machine code produced by C compilation generally cannot be mapped back to the source code.

pb files as part of a SavedModel that can be loaded back into TensorFlow for deployment or further training.

Author: Cai Shanqing
Link : https://www.zhihu.com/question/271666487/answer/365519609
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.


The above is the answer of the author of Zhihu when I was confused. I always wanted to open this pb file to see the source code. Later, I realized that there is no source code at all. If you want to find the code, go to the author of github to find the code, and don't try to open the pb again. file.

2. How to save and restore the tensorflow model?

This part includes saving of variables and saving of models

Reference official website: tensorflow

The tf.train.saver program class provides methods for saving and restoring models. The tf.saved_model.simple_save function is a simple way to build a saved model suitable for serving. Estimators automatically save and restore variables in model_dir.

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

2.1 Saving and restoring variables

Save: Create a Saver with tf.train.Saver() to manage all variables in the model. For example, the following code snippet demonstrates how to call tf.train.Saver. The save method saves the variable to a 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))

Restoring: Note that when you restore variables, you don't need to pre-initialize them. For example, the following code snippet demonstrates how to call tf.train.Saver. Revert method to restore variables from checkpoint file:

tf.reset_default_graph()
# clear the previous 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())

Note: There is not a single physics file called model.ckpt. It is the prefix of the filename created for the checkpoint. The user interacts only with the prefix, not with the physical checkpoint file. In other words, the file you saved does not have a model.ckpt file, but a file called checkpoint


Choose which variables to store and restore

If you don't pass any arguments to tf.train.Saver(), then saver will process all variables in the graph. Each of these variables is saved with the name passed in when the variable was created. Sometimes it is useful to explicitly define the names of variables in the checkpoint file. For example, you may have trained a model with a variable named "weights" and you want to restore its value to a new variable "params".

Sometimes it is useful to save and restore only a subset of the variables of the model. For another example, you may have trained a 5-layer neural network, and now want to train a new 6-layer model, you can import the parameters of the previous 5-layer model into the first 5 layers of the new model.
You can easily define the variables to keep and their corresponding names by passing a Python dictionary to the tf.train.Saver() constructor: the keys correspond to the names used, and the values ​​correspond to the managed variables.

Note:
1. If you need to save and restore different subsets of model variables, you can create as many saver objects as you want. The same variable can be listed in multiple saver objects, and its value will only change when the saver's restore() function is run.

2. If you restore only a subset of model variables at the beginning of the session, you need to perform an initialization op on the remaining variables. See tf.initialize_variables() for details. 3. To inspect variables in checkpoint, you can use inspect_checkpoint library , in particular print_tensors_in_checkpoint_file function

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}) # Save a variable to create a dictionary, the key is the name, and the value is the value

# Use the saver object normally after that.
with tf.Session() as sess:
  # Initialize v1 since the saver will not.
  v1.initializer.run() # The saved variable v2 does not need to be initialized, just like doing migration learning and directly using the saved value as the initial value, so don't initialize it again
  saver.restore(sess, "model.ckpt")

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

The following demonstrates using print_tensors_in_checkpoint_file() to check variables in 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 Save and restore models (models)

Use SavedModel to save and load your model variables, graphs, and graph metadata. This is a language-agnostic, recoverable, and recoverable serialization format that can support higher-level systems and tools for generating, consuming, and transforming TensorFlow models. TensorFlow provides several ways to interact with SavedModel, including the tf.saved_model api, tf.estimator.Estimator, and the command line interface.

Build and load the SavedModel:

2.2.1 Simple save

The easiest way to create a SavedModel is to use the tf.saved_model.simple_save function 

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

2.2.2 Manually build a SavedModel

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

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

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

export_dir = ... # Fill in your save address
...
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()
Loading SavedModels in Python
The Python version of the SavedModel loader provides loading and restoring functionality for SavedModels. The load operation requires the following information:
. Session to restore graph definitions and variables.
The .tag is used to identify the MetaGraphDef to load.
The location (directory) of the .SavedModel.
On load, a subset of variables, assets and signatures provided as part of a specific MetaGraphDef will be restored to the provided session.
export_dir = ...
...
with tf.Session(graph=tf.Graph()) as sess:
  tf.saved_model.loader.load(sess, [tag_constants.TRAINING], export_dir)
  ...









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520149&siteId=291194637