將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面向?qū)ο蠡A(chǔ)入門之設(shè)置對象屬性
這篇文章主要給大家介紹了關(guān)于Python面向?qū)ο蠡A(chǔ)入門之設(shè)置對象屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Python中input()函數(shù)的用法實例小結(jié)
我們編寫的大部分程序,都需要讀取輸入并對其進(jìn)行處理,而基本的輸入操作是從鍵盤鍵入數(shù)據(jù),Python從鍵盤鍵入數(shù)據(jù),大多使用其內(nèi)置的input()函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python中input()函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2022-03-03

