学习过深度学习的同志们都知道在训练过程中,模型的重要性,训练好的模型可以用来做分类,因此,我们需要对经过千幸万苦训练好的模型进行保存。本人在学习深度学习的时候,只要使用两种方式,一种是tensorflow版的保存,还有一种是keras版的模型的保存。下面逐一介绍下:
一、TensorFlow模型保存与读取(该方式tensorflow只能保存变量而不是保存整个网络,所以在提取模型时,我们还需要重新第一网络结构。)
1、保存
import tensorflow as tf
import numpy as np
W = tf.Variable([[1,1,1],[2,2,2]],dtype = tf.float32,name='w')
b = tf.Variable([[0,1,2]],dtype = tf.float32,name='b')
init = tf.initialize_all_variables()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
save_path = saver.save(sess,"save/model.ckpt")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
2、加载
import tensorflow as tf
import numpy as np
W = tf.Variable(tf.truncated_normal(shape=(2,3)),dtype = tf.float32,name='w')
b = tf.Variable(tf.truncated_normal(shape=(1,3)),dtype = tf.float32,name='b')
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,"save/model.ckpt")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
二、TensorFlow模型保存与读取(该方式tensorflow保存整个网络)
转载http://www.jianshu.com/p/8487db911d9a
1、保存
import tensorflow as tf
# First, you design your mathematical operations
# We are the default graph scope
# Let's design a variable
v1 = tf.Variable(1. , name="v1")
v2 = tf.Variable(2. , name="v2")
# Let's design an operation
a = tf.add(v1, v2)
# Let's create a Saver object
# By default, the Saver handles every Variables related to the default graph
all_saver = tf.train.Saver()
# But you can precise which vars you want to save under which name
v2_saver = tf.train.Saver({"v2": v2})
# By default the Session handles the default graph and all its included variables
with tf.Session() as sess:
# Init v and v2
sess.run(tf.global_variables_initializer())
# Now v1 holds the value 1.0 and v2 holds the value 2.0
# We can now save all those values
all_saver.save(sess, 'data.chkp')
# or saves only v2
v2_saver.save(sess, 'data-v2.chkp')
模型的权重是保存在 .chkp 文件中,模型的图是保存在 .chkp.meta 文件中。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
2、加载
import tensorflow as tf
# Let's laod a previous meta graph in the current graph in use: usually the default graph
# This actions returns a Saver
saver = tf.train.import_meta_graph('results/model.ckpt-1000.meta')
# We can now access the default graph where all our metadata has been loaded
graph = tf.get_default_graph()
# Finally we can retrieve tensors, operations, etc.
global_step_tensor = graph.get_tensor_by_name('loss/global_step:0')
train_op = graph.get_operation_by_name('loss/train_op')
hyperparameters = tf.get_collection('hyperparameters')
恢复权重
请记住,在实际的环境中,真实的权重只能存在于一个会话中。也就是说,restore 这个操作必须在一个会话中启动,然后将数据权重导入到图中。理解恢复操作的最好方法是将它简单的看做是一种数据初始化操作。
with tf.Session() as sess:
# To initialize values with saved data
saver.restore(sess, 'results/model.ckpt-1000-00000-of-00001')
print(sess.run(global_step_tensor)) # returns 1000
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
三、keras模型保存和加载
model.save('my_model.h5')
model = load_model('my_model.h5')
前言:
tensorflow中有operation和tensor,前者表示 操作 ,后者表示 容器 ,每个operation都是有一个tensor来存放值的,比如y=f(x), operation是f(x), tensor存放的就是y,如果要获取y,就必须输入x
tensor的名字一般是 <operation>:<num>
可以通过 print(out.name) 来看看
假如之前的训练定义了如下图(模型),并保存:
[python]
view plain
copy
- ....
- bottom = layers.fully_connected(inputs=bottom, num_outputs=7, activation_fn=None, scope='logits_classifier')
- ......
- prediction = tf.nn.softmax(logits, name='prob')
- ......
- saver_path = './model/checkpoint/model.ckpt'
- saver = tf.train.Saver()
- config = tf.ConfigProto()
- config.gpu_options.allow_growth=True
- with tf.Session(config=config) as sess:
- sess.run(init)
- ...
- saved_path = saver.save(sess,saver_path) # 这个保存了三个东西, .meta是图的结构, 还有两个是模型中变量的值
- ...
要想图结构和模型(恢复图结构,没错,从空白的代码段中恢复一个graph,就不需要重新定义图了)
[python]
view plain
copy
- meta_path = './model/checkpoint/model.ckpt.meta'
- model_path = './model/checkpoint/model.ckpt'
- saver = tf.train.import_meta_graph(meta_path) # 导入图
-
- config = tf.ConfigProto()
- config.gpu_options.allow_growth=True
- with tf.Session(config=config) as sess:
- saver.restore(sess, model_path) # 导入变量值
- graph = tf.get_default_graph()
- prob_op = graph.get_operation_by_name('prob') # 这个只是获取了operation, 至于有什么用还不知道
- prediction = graph.get_tensor_by_name('prob:0') # 获取之前prob那个操作的输出,即prediction
- print( ress.run(prediciton, feed_dict={...})) # 要想获取这个值,需要输入之前的placeholder (这里我编辑文章的时候是在with里面的,不知道为什么查看的时候就在外面了...)
- print(sess.run(graph.get_tensor_by_name('logits_classifier/weights:0'))) # 这个就不需要feed了,因为这是之前train operation优化的变量,即模型的权重
关于获取保存的模型中的tensor或者输出,还有一种办法就是用tf.add_to_collection(),
假如上面每次定义一次运算后,可以在后面添加tf.add_to_collection():
[python]
view plain
copy
- ......
- bottom = layers.fully_connected(inputs=bottom, num_outputs=7, activation_fn=None, scope='logits_classifier')
- ### add collection
- tf.add_to_collection('logits',bottom)
- ......
- prediction = tf.nn.softmax(logits, name='prob')
- ### add collection
- tf.add_to_collection('prob',prediction)
- ......
恢复模型后,通过tf.get_collection()来获取tensor:
[python]
view plain
copy
- ......
- x = tf.get_collection('inputs')[0]
- prob = tf.get_collection('prob')[0]
- print(x)
- print(prob)
- .....
可以查看输出,效果是和上面get_tensor_by_name()一样的,注意get_collection(name)的name只是collection的name,tensor的名字还是原来的名字
得到了模型各个地方的tensor之后,要想获取该地方的参数或者输出的值,只需要通过sess.run()就可以了,参数可以直接run,中间的特征或者预测值需要通过feed_dict={}传递输入的值就行啦
具体就不费话了.....