日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

TensorFlow 调用预训练好的模型—— Python 实现

發(fā)布時(shí)間:2024/4/17 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TensorFlow 调用预训练好的模型—— Python 实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 準(zhǔn)備預(yù)訓(xùn)練好的模型

  • TensorFlow 預(yù)訓(xùn)練好的模型被保存為以下四個(gè)文件

  • data 文件是訓(xùn)練好的參數(shù)值,meta 文件是定義的神經(jīng)網(wǎng)絡(luò)圖,checkpoint 文件是所有模型的保存路徑,如下所示,為簡單起見只保留了一個(gè)模型。
model_checkpoint_path: "/home/senius/python/c_python/test/model-40" all_model_checkpoint_paths: "/home/senius/python/c_python/test/model-40" 復(fù)制代碼

2. 導(dǎo)入模型圖、參數(shù)值和相關(guān)變量

import tensorflow as tf import numpy as npsess = tf.Session() X = None # input yhat = None # outputdef load_model():"""Loading the pre-trained model and parameters."""global X, yhatmodelpath = r'/home/senius/python/c_python/test/'saver = tf.train.import_meta_graph(modelpath + 'model-40.meta')saver.restore(sess, tf.train.latest_checkpoint(modelpath))graph = tf.get_default_graph()X = graph.get_tensor_by_name("X:0")yhat = graph.get_tensor_by_name("tanh:0")print('Successfully load the pre-trained model!')復(fù)制代碼
  • 通過 saver.restore 我們可以得到預(yù)訓(xùn)練的所有參數(shù)值,然后再通過 graph.get_tensor_by_name 得到模型的輸入張量和我們想要的輸出張量。

3. 運(yùn)行前向傳播過程得到預(yù)測值

def predict(txtdata):"""Convert data to Numpy array which has a shape of (-1, 41, 41, 41 3).Test a single example.Arg:txtdata: Array in C.Returns:Three coordinates of a face normal."""global X, yhatdata = np.array(txtdata)data = data.reshape(-1, 41, 41, 41, 3)output = sess.run(yhat, feed_dict={X: data}) # (-1, 3)output = output.reshape(-1, 1)ret = output.tolist()return ret復(fù)制代碼
  • 通過 feed_dict 喂入測試數(shù)據(jù),然后 run 輸出的張量我們就可以得到預(yù)測值。

4. 測試

load_model() testdata = np.fromfile('/home/senius/python/c_python/test/04t30t00.npy', dtype=np.float32) testdata = testdata.reshape(-1, 41, 41, 41, 3) # (150, 41, 41, 41, 3) testdata = testdata[0:2, ...] # the first two examples txtdata = testdata.tolist() output = predict(txtdata) print(output) # [[-0.13345889747142792], [0.5858198404312134], [-0.7211828231811523], # [-0.03778800368309021], [0.9978875517845154], [0.06522832065820694]] 復(fù)制代碼
  • 本例輸入是一個(gè)三維網(wǎng)格模型處理后的 [41, 41, 41, 3] 的數(shù)據(jù),輸出一個(gè)表面法向量坐標(biāo) (x, y, z)。

獲取更多精彩,請(qǐng)關(guān)注「seniusen」!

總結(jié)

以上是生活随笔為你收集整理的TensorFlow 调用预训练好的模型—— Python 实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。