1. Save:
graph_util.convert_variables_to_constants can serialize the calculation graph of the current session into a byte stream (binary). This function contains three parameters: Parameter 1: the currently active session, which contains various variables
Parameter 2: GraphDef object, which describes the computing network
Parameter 3: List of names of nodes to be output in Graph graph
Return value: A simplified version of the GraphDef object, containing the network and variable information of the original input GraphDef and session. Its member function SerializeToString() can serialize this information into a byte stream and then write it to a file:
constant_graph = graph_util.convert_variables_to_constants( sess, sess.graph_def , ['sum_operation'] ) with open( pbName, mode='wb') as f: (constant_graph.SerializeToString())
It should be noted that if the original tensors (components included in parameters 1 and 2) do not participate in the tensor calculations specified in the output node list specified in parameter 3, these tensors will not exist in the returned GraphDef object and will not be serialized to the pb file.
2. Recovery:
When recovering, create a GraphDef, then load it from the above file, and then enter it to the current session:
graph0 = () with open( pbName, mode='rb') as f: ( () ) tf.import_graph_def( graph0 , name = '' )
3. Code:
import tensorflow as tf from import graph_util pbName = '' def graphCreate() : with () as sess : var1 = ( tf.int32 , name='var1' ) var2 = ( 20 , name='var2' )#The actual parameter name='var2' specifies the operation name, and the tensor name returned by the operation is #'var2' is followed by:0, that is, var2:0 is the returned tensor name, that is, the variable # The name of var2 is 'var2:0' var3 = ( 30 , name='var3' ) var4 = ( 40 , name='var4' ) var4op = ( var4 , 1000 , name = 'var4op1' ) sum = ( 4, name='sum' ) sum = ( var1 , var2, name = 'var1_var2' ) sum = ( sum , var3 , name='sum_var3' ) sumOps = ( sum , var4 , name='sum_operation' ) oper = tf.get_default_graph().get_operations() with open( '','wt' ) as f: s = 'name,type,output\n' ( s ) for o in oper: s = s += ','+ inp = oup = for iip in inp : s #s += ','+ str(iip) for iop in oup : s += ',' + str(iop) s += '\n' ( s ) for var in tf.global_variables(): print('variable=> ' , ) #Tensor is the result of operations like /, #The name of the tensor is expressed by adding: 0 to the operation name init = tf.global_variables_initializer() ( init ) ( var4op ) print('sum_operation result is Tensor ' , ( sumOps , feed_dict={var1:1}) ) constant_graph = graph_util.convert_variables_to_constants( sess, sess.graph_def , ['sum_operation'] ) with open( pbName, mode='wb') as f: (constant_graph.SerializeToString()) def graphGet() : print("start get:" ) with ().as_default(): graph0 = () with open( pbName, mode='rb') as f: ( () ) tf.import_graph_def( graph0 , name = '' ) with () as sess : init = tf.global_variables_initializer() (init) v1 = .get_tensor_by_name('var1:0' ) v2 = .get_tensor_by_name('var2:0' ) v3 = .get_tensor_by_name('var3:0' ) v4 = .get_tensor_by_name('var4:0' ) sumTensor = .get_tensor_by_name("sum_operation:0") print('sumTensor is : ' , sumTensor ) print( ( sumTensor , feed_dict={v1:1} ) ) graphCreate() graphGet()
4. Save the operation name/type/return tensor in the pb function code:
operation name | operation type | output | ||
var1 | Placeholder | Tensor("var1:0" | dtype=int32) | |
var2/initial_value | Const | Tensor("var2/initial_value:0" | shape=() | dtype=int32) |
var2 | VariableV2 | Tensor("var2:0" | shape=() | dtype=int32_ref) |
var2/Assign | Assign | Tensor("var2/Assign:0" | shape=() | dtype=int32_ref) |
var2/read | Identity | Tensor("var2/read:0" | shape=() | dtype=int32) |
var3/initial_value | Const | Tensor("var3/initial_value:0" | shape=() | dtype=int32) |
var3 | VariableV2 | Tensor("var3:0" | shape=() | dtype=int32_ref) |
var3/Assign | Assign | Tensor("var3/Assign:0" | shape=() | dtype=int32_ref) |
var3/read | Identity | Tensor("var3/read:0" | shape=() | dtype=int32) |
var4/initial_value | Const | Tensor("var4/initial_value:0" | shape=() | dtype=int32) |
var4 | VariableV2 | Tensor("var4:0" | shape=() | dtype=int32_ref) |
var4/Assign | Assign | Tensor("var4/Assign:0" | shape=() | dtype=int32_ref) |
var4/read | Identity | Tensor("var4/read:0" | shape=() | dtype=int32) |
var4op1/value | Const | Tensor("var4op1/value:0" | shape=() | dtype=int32) |
var4op1 | Assign | Tensor("var4op1:0" | shape=() | dtype=int32_ref) |
sum/initial_value | Const | Tensor("sum/initial_value:0" | shape=() | dtype=int32) |
sum | VariableV2 | Tensor("sum:0" | shape=() | dtype=int32_ref) |
sum/Assign | Assign | Tensor("sum/Assign:0" | shape=() | dtype=int32_ref) |
sum/read | Identity | Tensor("sum/read:0" | shape=() | dtype=int32) |
var1_var2 | Add | Tensor("var1_var2:0" | dtype=int32) | |
sum_var3 | Add | Tensor("sum_var3:0" | dtype=int32) | |
sum_operation | Add | Tensor("sum_operation:0" | dtype=int32) |
The above article about Tensorflow's calculation diagram and parameter examples using pb file to save (recover) the model is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.