轉換ckpt爲pb

查看ckpt的各個節點

from tensorflow.python import pywrap_tensorflow
# checkpoint_path = 'model.ckpt-xxx'
checkpoint_path = './model'
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
for key in var_to_shape_map:
    print("tensor_name: ", key)

 

print(list(var_to_shape_map.keys()))
node_names=list(var_to_shape_map.keys())

 

轉換ckpt爲pb:

import tensorflow as tf
from tensorflow import graph_util
def freeze_graph(ckpt, output_graph):
#    saver = tf.train.Saver()
    saver = tf.train.import_meta_graph(ckpt+'.meta', clear_devices=True)
    with tf.Session() as sess:
#        saver = tf.compat.v1.train.import_meta_graph(ckpt+'.meta', clear_devices=True)
        saver.restore(sess, ckpt+'')
#         saver.save(sess, pb)
        graph = tf.get_default_graph()
        input_graph_def = graph.as_graph_def() 
        output_graph_def = graph_util.convert_variables_to_constants(
            sess=sess,
            input_graph_def=input_graph_def,
            output_node_names=node_names
        )
        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)))
 
ckpt = './model'
pb   = './model.pb'
 
if __name__ == '__main__':
    freeze_graph(ckpt, pb)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章