將tensorflow模型打包成PB文件及PB文件讀取方式
更新時間:2020年01月23日 17:54:47 作者:zchenack
今天小編就為大家分享一篇將tensorflow模型打包成PB文件及PB文件讀取方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
1. tensorflow模型文件打包成PB文件
import tensorflow as tf from tensorflow.python.tools import freeze_graph with tf.Graph().as_default(): with tf.device("/cpu:0"): config = tf.ConfigProto(allow_soft_placement=True) with tf.Session(config=config).as_default() as sess: model = Your_Model_Name() model.build_graph() sess.run(tf.initialize_all_variables()) saver = tf.train.Saver() ckpt_path = "/your/model/path" saver.restore(sess, ckpt_path) graphdef = tf.get_default_graph().as_graph_def() tf.train.write_graph(sess.graph_def,"/your/save/path/","save_name.pb",as_text=False) frozen_graph = tf.graph_util.convert_variables_to_constants(sess,graphdef,['output/node/name']) frozen_graph_trim = tf.graph_util.remove_training_nodes(frozen_graph) freeze_graph.freeze_graph('/your/save/path/save_name.pb','',True, ckpt_path,'output/node/name','save/restore_all','save/Const:0','frozen_name.pb',True,"")
2. PB文件讀取使用
output_graph_def = tf.GraphDef() with open("your_name.pb","rb") as f: output_graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(output_graph_def, name="") node_in = sess.graph.get_tensor_by_name("input_node_name") model_out = sess.graph.get_tensor_by_name("out_node_name") feed_dict = {node_in:in_data} pred = sess.run(model_out, feed_dict)
以上這篇將tensorflow模型打包成PB文件及PB文件讀取方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 矢量數(shù)據(jù)轉(zhuǎn)柵格數(shù)據(jù)代碼實例
這篇文章主要介紹了python 矢量數(shù)據(jù)轉(zhuǎn)柵格數(shù)據(jù)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09Python 格式化打印json數(shù)據(jù)方法(展開狀態(tài))
今天小編就為大家分享一篇Python 格式化打印json數(shù)據(jù)方法(展開狀態(tài)),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python數(shù)據(jù)可視化使用pyfinance分析證券收益示例詳解
這篇文章主要為大家介紹了python數(shù)據(jù)可視化使用pyfinance分析證券收益的示例詳解及pyfinance中returns模塊的應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11python中字符串比較使用is、==和cmp()總結(jié)
在Python中比較字符串最好是使用簡單邏輯操作符,今天為大家講解一下is、==和cmp()使用總結(jié)2018-03-03Python實現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享
今天小編就為大家分享一篇Python實現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05