如何將tensorflow模型打包成PB文件-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)如何將tensorflow模型打包成PB文件,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元尉犁做網(wǎng)站,已為上家服務(wù),為尉犁各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):13518219792

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)

關(guān)于“如何將tensorflow模型打包成PB文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)頁(yè)題目:如何將tensorflow模型打包成PB文件-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://bm7419.com/article22/hdpcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、虛擬主機(jī)網(wǎng)站營(yíng)銷(xiāo)、軟件開(kāi)發(fā)網(wǎng)頁(yè)設(shè)計(jì)公司、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)