009Tensorflow模型的持久化

模型持久化
ckpt格式

使用saver保存和还原一个神经网络模型

import tensorflow as tf

v1 = tf.Variable(tf.constant(1.0,shape=[1],name="v1"))
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="v2")

result = v1 + v2

init_op = tf.global_variables_initializer()
saver = tf.train.Saver()#用于保存模型

with tf.Session() as sess:
    sess.run(init_op)
    saver.save(sess,"/Users/apple/Downloads/11人脸识别/projects/face/practice/MINIST/model/model.ckpt")

模型一般存在后缀为.ckpt的文件中,文件目录下有4个文件,其中model.ckpt.meta保存了tf计算图的结构,checkpoint保存了目录下所有模型的文件列表

import tensorflow as tf
v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="v1")
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="v2")
result = v1+v2
saver = tf.train.Saver()

#init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    #sess.run(init_op)
    saver.restore(sess,"/Users/apple/Downloads/11人脸识别/projects/face/practice/MINIST/model/model.ckpt")
    print(sess.run(result))#[3.]
'''
没有进行变量的初始化,而是将变量的值通过已经保存的模型加载进来
'''

如果不想重复定义上述图的计算,也可以直接加载已经持久化的图

import tensorflow as tf

saver = tf.train.import_meta_graph("/Users/apple/Downloads/11人脸识别/projects/face/practice/MINIST/model/model.ckpt.meta")

with tf.Session() as sess:
    saver.restore(sess,"/Users/apple/Downloads/11人脸识别/projects/face/practice/MINIST/model/model.ckpt")
    print(sess.run(tf.get_default_graph().get_tensor_by_name("add:0")))#[3.0]

这里默认加载了所有变量,如果只需要保存和加载部分变量:tf.train.Saver([v1]),则只有v1被加载进来,但是因为之前v2没有被保存,所以v2需要进行初始化。

tf.train.Saver类也支持在保存或加载时给变量重命名

v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="other_v1")
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="other_v2")
saver = tf.train.Saver({"v1":v1,"v2":v2})
'''将名称为v1的变量加载到变量v1中(名称为other_v1),将名称为v2的变量加载到变量v2中(名称为other_v2)
'''

在修改了变量v1和v2的名称后,如果直接通过tf.train.Saver默认的构造函数来加载保存的模型,那么程序会报变量找不到的错误,是因为变量的名称和加载时变量的名称不一致,可以通过字典将模型保存时的变量和需要加载的变量联系起来

pb格式

也可以将计算图中的变量和变量的取值通过常量的方式保存,使计算图存放在一个文件中:

import tensorflow as tf

from tensorflow.python.framework import graph_util

v1 = tf.Variable(tf.constant(1.0,shape=[1]),name="v1")
v2 = tf.Variable(tf.constant(2.0,shape=[1]),name="v2")
sum = v1+v2
init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)
    '''导出当前计算图的GraphDef部分,只需要这一部分就可以完成从输入层到输出层的计算过程'''
    graph_def = tf.get_default_graph().as_graph_def()
    #add节点给出了需要保存的节点名称,add节点时尚吗定义的两个变量相加的操作,这是名称,所以后面没有:0
    output_graph_def = tf.graph_util.convert_variables_to_constants(sess,graph_def,['add'])

#将导出的模型存入文件
    with tf.gfile.GFile("/Users/apple/Downloads/11人脸识别/projects/face/practice/MINIST/model.combined_model.pb","wb") as f:
        f.write(output_graph_def.SerializeToString())

'''
constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ["y_conv2"])

with tf.gfile.FastGFile('./Model_pb/grf.pb', mode='wb') as f:f.write(constant_graph.SerializeToString())
'''

解析pb格式文件:

import tensorflow as tf
from tensorflow.python.platform import gfile
with tf.Session() as sess:
    filename = "/Users/apple/Downloads/11人脸识别/projects/face/practice/MINIST/model.combined_model.pb"
    #读取保存的模型文件,并解析文件
    with gfile.FastGFile(filename,'rb') as f:
        graph_gef = tf.GraphDef()
        graph_gef.ParseFromString(f.read())

    result = tf.import_graph_def(graph_gef,return_elements=["add:0"])
    print(sess.run(result))#[array([3.], dtype=float32)]

'''
将graph_def中保存的图加载在当前的图中
return_elements=["add:0"]:给出了返回的张量名称
'''
持久化原理及数据格式(未看)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值