關(guān)于Tensorflow 模型持久化詳解
當我們使用 tensorflow 訓練神經(jīng)網(wǎng)絡的時候,模型持久化對于我們的訓練有很重要的作用。
如果我們的神經(jīng)網(wǎng)絡比較復雜,訓練數(shù)據(jù)比較多,那么我們的模型訓練就會耗時很長,如果在訓練過程中出現(xiàn)某些不可預計的錯誤,導致我們的訓練意外終止,那么我們將會前功盡棄。為了避免這個問題,我們就可以通過模型持久化(保存為CKPT格式)來暫存我們訓練過程中的臨時數(shù)據(jù)。
如果我們訓練的模型需要提供給用戶做離線的預測,那么我們只需要前向傳播的過程,只需得到預測值就可以了,這個時候我們就可以通過模型持久化(保存為PB格式)只保存前向傳播中需要的變量并將變量的值固定下來,這個時候只需用戶提供一個輸入,我們就可以通過模型得到一個輸出給用戶。
保存為 CKPT 格式的模型
定義運算過程
聲明并得到一個 Saver
通過 Saver.save 保存模型
# coding=UTF-8 支持中文編碼格式
import tensorflow as tf
import shutil
import os.path
MODEL_DIR = "model/ckpt"
MODEL_NAME = "model.ckpt"
# if os.path.exists(MODEL_DIR): 刪除目錄
# shutil.rmtree(MODEL_DIR)
if not tf.gfile.Exists(MODEL_DIR): #創(chuàng)建目錄
tf.gfile.MakeDirs(MODEL_DIR)
#下面的過程你可以替換成CNN、RNN等你想做的訓練過程,這里只是簡單的一個計算公式
input_holder = tf.placeholder(tf.float32, shape=[1], name="input_holder") #輸入占位符,并指定名字,后續(xù)模型讀取可能會用的
W1 = tf.Variable(tf.constant(5.0, shape=[1]), name="W1")
B1 = tf.Variable(tf.constant(1.0, shape=[1]), name="B1")
_y = (input_holder * W1) + B1
predictions = tf.greater(_y, 50, name="predictions") #輸出節(jié)點名字,后續(xù)模型讀取會用到,比50大返回true,否則返回false
init = tf.global_variables_initializer()
saver = tf.train.Saver() #聲明saver用于保存模型
with tf.Session() as sess:
sess.run(init)
print "predictions : ", sess.run(predictions, feed_dict={input_holder: [10.0]}) #輸入一個數(shù)據(jù)測試一下
saver.save(sess, os.path.join(MODEL_DIR, MODEL_NAME)) #模型保存
print("%d ops in the final graph." % len(tf.get_default_graph().as_graph_def().node)) #得到當前圖有幾個操作節(jié)點
for op in tf.get_default_graph().get_operations(): #打印模型節(jié)點信息
print (op.name, op.values())
運行后生成的文件如下:

checkpoint : 記錄目錄下所有模型文件列表
ckpt.data : 保存模型中每個變量的取值
ckpt.meta : 保存整個計算圖的結(jié)構(gòu)
保存為 PB 格式模型
定義運算過程
通過 get_default_graph().as_graph_def() 得到當前圖的計算節(jié)點信息
通過 graph_util.convert_variables_to_constants 將相關(guān)節(jié)點的values固定
通過 tf.gfile.GFile 進行模型持久化
# coding=UTF-8
import tensorflow as tf
import shutil
import os.path
from tensorflow.python.framework import graph_util
# MODEL_DIR = "model/pb"
# MODEL_NAME = "addmodel.pb"
# if os.path.exists(MODEL_DIR): 刪除目錄
# shutil.rmtree(MODEL_DIR)
#
# if not tf.gfile.Exists(MODEL_DIR): #創(chuàng)建目錄
# tf.gfile.MakeDirs(MODEL_DIR)
output_graph = "model/pb/add_model.pb"
#下面的過程你可以替換成CNN、RNN等你想做的訓練過程,這里只是簡單的一個計算公式
input_holder = tf.placeholder(tf.float32, shape=[1], name="input_holder")
W1 = tf.Variable(tf.constant(5.0, shape=[1]), name="W1")
B1 = tf.Variable(tf.constant(1.0, shape=[1]), name="B1")
_y = (input_holder * W1) + B1
# predictions = tf.greater(_y, 50, name="predictions") #比50大返回true,否則返回false
predictions = tf.add(_y, 10,name="predictions") #做一個加法運算
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print "predictions : ", sess.run(predictions, feed_dict={input_holder: [10.0]})
graph_def = tf.get_default_graph().as_graph_def() #得到當前的圖的 GraphDef 部分,通過這個部分就可以完成重輸入層到輸出層的計算過程
output_graph_def = graph_util.convert_variables_to_constants( # 模型持久化,將變量值固定
sess,
graph_def,
["predictions"] #需要保存節(jié)點的名字
)
with tf.gfile.GFile(output_graph, "wb") as f: # 保存模型
f.write(output_graph_def.SerializeToString()) # 序列化輸出
print("%d ops in the final graph." % len(output_graph_def.node))
print (predictions)
# for op in tf.get_default_graph().get_operations(): 打印模型節(jié)點信息
# print (op.name)
*GraphDef:這個屬性記錄了tensorflow計算圖上節(jié)點的信息。

add_model.pb : 里面保存了重輸入層到輸出層這個計算過程的計算圖和相關(guān)變量的值,我們得到這個模型后傳入一個輸入,既可以得到一個預估的輸出值
CKPT 轉(zhuǎn)換成 PB格式
通過傳入 CKPT 模型的路徑得到模型的圖和變量數(shù)據(jù)
通過 import_meta_graph 導入模型中的圖
通過 saver.restore 從模型中恢復圖中各個變量的數(shù)據(jù)
通過 graph_util.convert_variables_to_constants 將模型持久化
# coding=UTF-8
import tensorflow as tf
import os.path
import argparse
from tensorflow.python.framework import graph_util
MODEL_DIR = "model/pb"
MODEL_NAME = "frozen_model.pb"
if not tf.gfile.Exists(MODEL_DIR): #創(chuàng)建目錄
tf.gfile.MakeDirs(MODEL_DIR)
def freeze_graph(model_folder):
checkpoint = tf.train.get_checkpoint_state(model_folder) #檢查目錄下ckpt文件狀態(tài)是否可用
input_checkpoint = checkpoint.model_checkpoint_path #得ckpt文件路徑
output_graph = os.path.join(MODEL_DIR, MODEL_NAME) #PB模型保存路徑
output_node_names = "predictions" #原模型輸出操作節(jié)點的名字
saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=True) #得到圖、clear_devices :Whether or not to clear the device field for an `Operation` or `Tensor` during import.
graph = tf.get_default_graph() #獲得默認的圖
input_graph_def = graph.as_graph_def() #返回一個序列化的圖代表當前的圖
with tf.Session() as sess:
saver.restore(sess, input_checkpoint) #恢復圖并得到數(shù)據(jù)
print "predictions : ", sess.run("predictions:0", feed_dict={"input_holder:0": [10.0]}) # 測試讀出來的模型是否正確,注意這里傳入的是輸出 和輸入 節(jié)點的 tensor的名字,不是操作節(jié)點的名字
output_graph_def = graph_util.convert_variables_to_constants( #模型持久化,將變量值固定
sess,
input_graph_def,
output_node_names.split(",") #如果有多個輸出節(jié)點,以逗號隔開
)
with tf.gfile.GFile(output_graph, "wb") as f: #保存模型
f.write(output_graph_def.SerializeToString()) #序列化輸出
print("%d ops in the final graph." % len(output_graph_def.node)) #得到當前圖有幾個操作節(jié)點
for op in graph.get_operations():
print(op.name, op.values())
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("model_folder", type=str, help="input ckpt model dir") #命令行解析,help是提示符,type是輸入的類型,
# 這里運行程序時需要帶上模型ckpt的路徑,不然會報 error: too few arguments
aggs = parser.parse_args()
freeze_graph(aggs.model_folder)
# freeze_graph("model/ckpt") #模型目錄
以上這篇關(guān)于Tensorflow 模型持久化詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python中with語句結(jié)合上下文管理器操作詳解
這篇文章主要給大家介紹了關(guān)于python中with語句結(jié)合上下文管理器操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12
快速解釋如何使用pandas的inplace參數(shù)的使用
這篇文章主要介紹了快速解釋如何使用pandas的inplace參數(shù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Python Django 數(shù)據(jù)庫的相關(guān)操作詳解
下面小編就為大家?guī)硪黄猟jango數(shù)據(jù)庫的相關(guān)操作,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-11-11

