將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文件讀取方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 矢量數(shù)據(jù)轉(zhuǎn)柵格數(shù)據(jù)代碼實(shí)例
這篇文章主要介紹了python 矢量數(shù)據(jù)轉(zhuǎn)柵格數(shù)據(jù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Anaconda配置各版本Pytorch的實(shí)現(xiàn)
本文是整理目前全版本pytorch深度學(xué)習(xí)環(huán)境配置指令,以下指令適用Windows操作系統(tǒng),在Anaconda Prompt中運(yùn)行,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08pygame學(xué)習(xí)筆記(3):運(yùn)動(dòng)速率、時(shí)間、事件、文字
這篇文章主要介紹了pygame學(xué)習(xí)筆記(3):運(yùn)動(dòng)速率、時(shí)間、事件、文字,本文講解了運(yùn)動(dòng)速率、事件、字體及字符顯示等內(nèi)容,需要的朋友可以參考下2015-04-04Python 格式化打印json數(shù)據(jù)方法(展開(kāi)狀態(tài))
今天小編就為大家分享一篇Python 格式化打印json數(shù)據(jù)方法(展開(kāi)狀態(tài)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python數(shù)據(jù)可視化使用pyfinance分析證券收益示例詳解
這篇文章主要為大家介紹了python數(shù)據(jù)可視化使用pyfinance分析證券收益的示例詳解及pyfinance中returns模塊的應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11python中字符串比較使用is、==和cmp()總結(jié)
在Python中比較字符串最好是使用簡(jiǎn)單邏輯操作符,今天為大家講解一下is、==和cmp()使用總結(jié)2018-03-03python使用python-pptx刪除ppt某頁(yè)實(shí)例
今天小編就為大家分享一篇python使用python-pptx刪除ppt某頁(yè)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python實(shí)現(xiàn)爬蟲(chóng)設(shè)置代理IP和偽裝成瀏覽器的方法分享
今天小編就為大家分享一篇Python實(shí)現(xiàn)爬蟲(chóng)設(shè)置代理IP和偽裝成瀏覽器的方法分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05