添加namenode
with tf.name_scope("output"): self.out = tf.add(tf.matmul(concat_input, self.weights["concat_projection"]), self.weights["concat_bias"]) if self.loss_type == "logloss": self.out = tf.nn.sigmoid(self.out, name="predictlabel")
from tensorflow.python import pywrap_tensorflow import tensorflow as tf from tensorflow.python.framework import graph_util def getAllNodes(checkpoint_path): reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map = reader.get_variable_to_shape_map() # Print tensor name and values for key in var_to_shape_map: print("tensor_name: ", key) #print(reader.get_tensor(key)) def freeze_graph(ckpt, output_graph): output_node_names = 'output/predictlabel' # saver = tf.train.import_meta_graph(ckpt+'.meta', clear_devices=True) saver = tf.compat.v1.train.import_meta_graph(ckpt+".meta", clear_devices=True) graph = tf.get_default_graph() input_graph_def = graph.as_graph_def() with tf.Session() as sess: saver.restore(sess, ckpt) output_graph_def = graph_util.convert_variables_to_constants( sess=sess, input_graph_def=input_graph_def, output_node_names=output_node_names.split(',') ) with tf.gfile.GFile(output_graph, 'wb') as fw: fw.write(output_graph_def.SerializeToString()) print('{} ops in the final graph.'.format(len(output_graph_def.node))) if __name__ == '__main__': ckpt_path = 'model' getAllNodes(ckpt_path) output_graph_path = 'res.pb' freeze_graph(ckpt_path, output_graph_path)
有兩個地方注意:
a: ckpt_path=「model」 是前綴,見圖片。 b: output_node_names = 'output/predictlabel' 跟第一步設置的同樣。python