I. TensorFlow model saving and extraction methods
1. TensorFlow implements the saving and extraction of neural network models through classes. The save method of the object saver saves the TensorFlow model to the specified path.(sess,"Model/") , the actual 4 person file will be generated in this file directory:
The checkpoint file saves a list of all the model files under a record, the structure of the TensorFlow computation graph, and the values of each variable. The way the filename is written here varies depending on the parameter settings, but the pathname of the file when loading restore is determined by the value of " model_checkpoint_path" value in the checkpoint file.
2. The way to load this saved TensorFlow model is to(sess,"./Model/") The code that loads the model also defines all the operations on the TensorFlow computational graph and declares a class, the difference is that when loading the model there is no need to initialize the variables, but rather load the values of the variables through the saved model, paying attention to the way the loading path is written. If you do not want to repeat the definition of operations on the computational graph, you can directly load the already persistent graph.saver =.import_meta_graph("Model/") 。
Classes also support renaming variables at save and load time. When declaring an object of the Saver class it is sufficient to rename the variable using a dictionary dict, {"Name of the saved variable name": the name of the renamed variable}.saver = ({"v1":u1, "v2": u2})That is, the variable whose name name was v1 is now loaded into the variable u1 (whose name name is other-v1).
4. One of the purposes of the previous article is to facilitate the use of sliding averages of variables. If the shadow variable is mapped directly to the variable itself when loading the model, there is no need to call the function to get the sliding average of the variable when using the trained model. When loading, declaring the Saver class object loads the sliding average directly into the new variable via a dictionary, saver = ({"v/ExponentialMovingAverage": v}), and the other via thevariables_to_restore()function gets the variable renaming dictionary.
In addition, the variables and their values in the computational graph are saved in a file by means of constants through the convert_variables_to_constants function.
Second, TensorFlow program implementation
# The procedures in this document are designed to be progressive and in line with the teaching materials and learning progress, please follow the notes in sections. # When executing, pay attention to the IDE's current work over the path, it is best to restart the controller once per segment, the output results are more accurate # Part1: Saving and Loading Neural Network Models via Class Implementation # Take note of the current working path when executing this program. import tensorflow as tf v1 = ((1.0, shape=[1]), name="v1") v2 = ((2.0, shape=[1]), name="v2") result = v1 + v2 saver = () with () as sess: (tf.global_variables_initializer()) (sess, "Model/") # Part2: Methods for loading TensorFlow models import tensorflow as tf v1 = ((1.0, shape=[1]), name="v1") v2 = ((2.0, shape=[1]), name="v2") result = v1 + v2 saver = () with () as sess: (sess, "./Model/") # Note that the path is preceded by ". /" print((result)) # [ 3.] # Part3: If you don't want to duplicate the definition of operations on the computational graph, you can load the already-persistent graph directly. import tensorflow as tf saver = .import_meta_graph("Model/") with () as sess: (sess, "./Model/") # Note the way the path is written print((tf.get_default_graph().get_tensor_by_name("add:0"))) # [ 3.] # Part4: Classes also support renaming variables at save and load time import tensorflow as tf # The name of the declared variable name does not match the name of the variable name in the saved model u1 = ((1.0, shape=[1]), name="other-v1") u2 = ((2.0, shape=[1]), name="other-v2") result = u1 + u2 # If the Saver class object is lifted directly, it will report that the variable was not found. # Just rename the variable using a dictionary dict, {"name of saved variable name": rename variable name} # The variable whose name was v1 is now loaded into the variable u1 (whose name is other-v1) saver = ({"v1": u1, "v2": u2}) with () as sess: (sess, "./Model/") print((result)) # [ 3.] # Part5: Preservation of the sliding average model import tensorflow as tf v = (0, dtype=tf.float32, name="v") for variables in tf.global_variables(): print() # v:0 ema = (0.99) maintain_averages_op = (tf.global_variables()) for variables in tf.global_variables(): print() # v:0 # v/ExponentialMovingAverage:0 saver = () with () as sess: (tf.global_variables_initializer()) ((v, 10)) (maintain_averages_op) (sess, "Model/model_ema.ckpt") print(([v, (v)])) # [10.0, 0.099999905] # Part6: Read sliding averages of variables directly by renaming them import tensorflow as tf v = (0, dtype=tf.float32, name="v") saver = ({"v/ExponentialMovingAverage": v}) with () as sess: (sess, "./Model/model_ema.ckpt") print((v)) # 0.0999999 # Part7: Getting the variable renaming dictionary via the variables_to_restore() function import tensorflow as tf v = (0, dtype=tf.float32, name="v") # Note that the variable name here must be the same as the name of the saved variable. ema = (0.99) print(ema.variables_to_restore()) # {'v/ExponentialMovingAverage': < 'v:0' shape=() dtype=float32_ref>} # v here is taken from the name of the variable v above name="v" saver = (ema.variables_to_restore()) with () as sess: (sess, "./Model/model_ema.ckpt") print((v)) # 0.0999999 # Part8: Save the variables and their values in the computed graph as constants in a file with the convert_variables_to_constants function. import tensorflow as tf from import graph_util v1 = ((1.0, shape=[1]), name="v1") v2 = ((2.0, shape=[1]), name="v2") result = v1 + v2 with () as sess: (tf.global_variables_initializer()) # Export the GraphDef part of the current calculation diagram, i.e. the part of the calculation process from the input layer to the output layer graph_def = tf.get_default_graph().as_graph_def() output_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, ['add']) with ("Model/combined_model.pb", 'wb') as f: (output_graph_def.SerializeToString()) # Part9: Load a model with variables and their values. import tensorflow as tf from import gfile with () as sess: model_filename = "Model/combined_model.pb" with (model_filename, 'rb') as f: graph_def = () graph_def.ParseFromString(()) result = tf.import_graph_def(graph_def, return_elements=["add:0"]) print((result)) # [array([ 3.], dtype=float32)]
This is the whole content of this article.