轉換ckpt爲pb

轉換ckpt爲pb

使用tf.Session將Tensorflow模型轉爲Tensorflow Lite模型

import tensorflow as tf
from tensorflow import graph_util
def freeze_graph(ckpt, output_graph):
#     saver = tf.train.import_meta_graph(ckpt+'.meta', clear_devices=True)
    with tf.compat.v1.Session() as sess:
        saver = tf.compat.v1.train.import_meta_graph(ckpt+'.meta', clear_devices=True)
        saver.restore(sess, ckpt+'')
        graph = tf.compat.v1.get_default_graph()
        input_graph_def = graph.as_graph_def() 
        output_graph_def = tf.compat.v1.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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章