恩施市建设银行网站,网上做彩票网站排名,做任务 送科比网站,用jsp sqlserver做的购物网站使用tensorflow过程中#xff0c;训练结束后我们需要用到模型文件。有时候#xff0c;我们可能也需要用到别人训练好的模型#xff0c;并在这个基础上再次训练。这时候我们需要掌握如何操作这些模型数据。看完本文#xff0c;相信你一定会有收获#xff01;
一、Tensorfl…使用tensorflow过程中训练结束后我们需要用到模型文件。有时候我们可能也需要用到别人训练好的模型并在这个基础上再次训练。这时候我们需要掌握如何操作这些模型数据。看完本文相信你一定会有收获
一、Tensorflow模型文件
我们在checkpoint_dir目录下保存的文件结构如下
|--checkpoint_dir
| |--checkpoint
| |--MyModel.meta
| |--MyModel.data-00000-of-00001
| |--MyModel.index
1.1 meta文件
MyModel.meta文件保存的是图结构meta文件是pbprotocol buffer格式文件包含变量、op、集合等。
1.2 ckpt文件
ckpt文件是二进制文件保存了所有的weights、biases、gradients等变量。在tensorflow 0.11之前保存在.ckpt文件中。0.11后通过两个文件保存,如下
MyModel.data-00000-of-00001
MyModel.index
1.3 checkpoint文件
我们还可以看checkpoint_dir目录下还有checkpoint文件该文件是个文本文件里面记录了保存的最新的checkpoint文件以及其它checkpoint文件列表。在inference时可以通过修改这个文件指定使用哪个model
二、保存Tensorflow模型
tensorflow 提供了 tf.train.Saver 类来保存模型值得注意的是在tensorflow中变量是存在于 Session 环境中也就是说只有在 Session 环境下才会存有变量值因此保存模型时需要传入session
saver tf.train.Saver()
saver.save(sess,./checkpoint_dir/MyModel)
看一个简单例子
import tensorflow as tfw1 tf.Variable(tf.random_normal(shape[2]), namew1)
w2 tf.Variable(tf.random_normal(shape[5]), namew2)saver tf.train.Saver()
sess tf.Session()
sess.run(tf.global_variables_initializer())
saver.save(sess, ./checkpoint_dir/MyModel)
执行后在checkpoint_dir目录下创建模型文件如下
checkpoint
MyModel.data-00000-of-00001
MyModel.index
MyModel.meta
另外如果想要在1000次迭代后再保存模型只需设置global_step参数即可。保存的模型文件名称会在后面加-1000,如下
checkpoint
MyModel-1000.data-00000-of-00001
MyModel-1000.index
MyModel-1000.meta
在实际训练中我们可能会在每1000次迭代中保存一次模型数据但是由于图是不变的没必要每次都去保存可以通过如下方式指定不保存图
saver.save(sess, ./checkpoint_dir/MyModel,global_stepstep,write_meta_graphFalse)
另一种比较实用的是如果你希望每2小时保存一次模型并且只保存最近的5个模型文件
tf.train.Saver(max_to_keep5, keep_checkpoint_every_n_hours2) 注意tensorflow默认只会保存最近的5个模型文件如果你希望保存更多可以通过max_to_keep来指定 如果我们不对tf.train.Saver指定任何参数默认会保存所有变量。如果你不想保存所有变量而只保存一部分变量可以通过指定variables/collections。在创建tf.train.Saver实例时通过将需要保存的变量构造list或者dictionary传入到Saver中
import tensorflow as tfw1 tf.Variable(tf.random_normal(shape[2]), namew1)
w2 tf.Variable(tf.random_normal(shape[5]), namew2)saver tf.train.Saver([w1,w2])
sess tf.Session()
sess.run(tf.global_variables_initializer())
saver.save(sess, ./checkpoint_dir/MyModel,global_step1000)
三、导入训练好的模型
在第1小节中我们介绍过tensorflow将图和变量数据分开保存为不同的文件。因此在导入模型时也要分为2步构造网络图和加载参数。
3.1 构造网络图
一个比较笨的方法是手敲代码实现跟模型一模一样的图结构。其实我们既然已经保存了图那就没必要在去手写一次图结构代码。下面一行代码就把图加载进来了
savertf.train.import_meta_graph(./checkpoint_dir/MyModel-1000.meta)
3.2 加载参数
仅仅有图并没有用更重要的是我们需要前面训练好的模型参数即weights、biases等本文第2节提到过变量值需要依赖于Session因此在加载参数时先要构造好Session
import tensorflow as tfwith tf.Session() as sess:saver tf.train.import_meta_graph(./checkpoint_dir/MyModel-1000.meta)saver.restore(sess,tf.train.latest_checkpoint(./checkpoint_dir))print(sess.run(w1:0))# Model has been restored. Above statement will print the saved value
此时W1和W2加载进了图并且可以被访问,执行后打印如下
[ 0.51480412 -0.56989086]
四、使用恢复的模型
前面我们理解了如何保存和恢复模型很多时候我们希望使用一些已经训练好的模型如prediction、fine-tuning以及进一步训练等。这时候我们可能需要获取训练好的模型中的一些中间结果值可以通过 graph.get_tensor_by_name(w1:0) 来获取注意w1:0是tensor的name。
假设我们有一个简单的网络模型代码如下
import tensorflow as tfw1 tf.placeholder(float, namew1)
w2 tf.placeholder(float, namew2)
b1 tf.Variable(2.0,namebias) # 定义一个op用于后面恢复
w3 tf.add(w1, w2)
w4 tf.multiply(w3, b1, nameop_to_restore)
sess tf.Session()
sess.run(tf.global_variables_initializer())# 创建一个Saver对象用于保存所有变量
saver tf.train.Saver()# 通过传入数据执行op
print(sess.run(w4, feed_dict {w1: 4, w2: 8})) # 打印 24.0 (w1w2)*b1# 保存模型
saver.save(sess, ./checkpoint_dir/MyModel,global_step1000)
接下来我们使用 graph.get_tensor_by_name() 方法来操纵这个保存的模型。
import tensorflow as tfsesstf.Session()
# 先加载图和参数变量
saver tf.train.import_meta_graph(./checkpoint_dir/MyModel-1000.meta)
saver.restore(sess, tf.train.latest_checkpoint(./checkpoint_dir))# 访问placeholders变量并且创建feed-dict来作为placeholders的新值
graph tf.get_default_graph()
# w1 graph.get_operation_by_name(w1) # 这个只是获取了operation 至于有什么用还不知道
w1 graph.get_tensor_by_name(w1:0)
w2 graph.get_tensor_by_name(w2:0)
feed_dict {w1:13.0,w2:17.0}# 接下来访问你想要执行的op
op_to_restore graph.get_tensor_by_name(op_to_restore:0)print(sess.run(op_to_restore,feed_dict)) # 打印结果为60.0(1317)*2 注意保存模型时只会保存变量的值placeholder里面的值不会被保存 如果你不仅仅是用训练好的模型还要加入一些op或者说加入一些layers并训练新的模型可以通过一个简单例子来看如何操作
import tensorflow as tfsess tf.Session()
# 先加载图和变量
saver tf.train.import_meta_graph(my_test_model-1000.meta)
saver.restore(sess, tf.train.latest_checkpoint(./))# 访问placeholders变量并且创建feed-dict来作为placeholders的新值
graph tf.get_default_graph()
w1 graph.get_tensor_by_name(w1:0)
w2 graph.get_tensor_by_name(w2:0)
feed_dict {w1: 13.0, w2: 17.0}#接下来访问你想要执行的op
op_to_restore graph.get_tensor_by_name(op_to_restore:0)# 在当前图中能够加入op
add_on_op tf.multiply(op_to_restore, 2)print (sess.run(add_on_op, feed_dict)) # 打印120.0(1317)*2*2
如果只想恢复图的一部分并且再加入其它的op用于fine-tuning。只需通过graph.get_tensor_by_name()方法获取需要的op并且在此基础上建立图看一个简单例子假设我们需要在训练好的VGG网络使用图并且修改最后一层将输出改为2用于fine-tuning新数据
......
......
saver tf.train.import_meta_graph(vgg.meta)
# 访问图
graph tf.get_default_graph() # 访问用于fine-tuning的output
fc7 graph.get_tensor_by_name(fc7:0)# 如果你想修改最后一层梯度需要如下
fc7 tf.stop_gradient(fc7) # Its an identity function
fc7_shape fc7.get_shape().as_list()new_outputs 2
weights tf.Variable(tf.truncated_normal([fc7_shape[3], num_outputs], stddev0.05))
biases tf.Variable(tf.constant(0.05, shape[num_outputs]))
output tf.matmul(fc7, weights) biases
pred tf.nn.softmax(output)# Now, you run this with fine-tuning data in sess.run()
六、tensorflow从已经训练好的模型中恢复指定权重(构建新变量、网络)并继续训练(finetuning)
该部分转载自https://blog.csdn.net/ying86615791/article/details/76215363
假如要保存或者恢复指定tensor并且把保存的graph恢复(插入)到当前的graph中呢
总的来说目前我会的是两种方法命名都是很关键 两种方式保存模型 1.保存所有tensor即整张图的所有变量 2.只保存指定scope的变量 两种方式恢复模型 1.导入模型的graph用该graph的saver来restore变量 2.在新的代码段中写好同样的模型变量名称及scope的name要对应用默认的graph的saver来restore指定scope的变量
两种保存方式1.保存整张图所有变量 ... init tf.global_variables_initializer() saver tf.train.Saver() config tf.ConfigProto() config.gpu_options.allow_growthTrue with tf.Session(configconfig) as sess: sess.run(init) ... writer.add_graph(sess.graph) ... saved_path saver.save(sess,saver_path) ...
2.保存图中的部分变量 ... init tf.global_variables_initializer() vgg_ref_vars tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scopevgg_feat_fc)#获取指定scope的tensor saver tf.train.Saver(vgg_ref_vars)#初始化saver时传入一个var_list的参数 config tf.ConfigProto() config.gpu_options.allow_growthTrue with tf.Session(configconfig) as sess: sess.run(init) ... writer.add_graph(sess.graph) ... saved_path saver.save(sess,saver_path) ...
两种恢复方式 1.导入graph来恢复 ... vgg_meta_path params[vgg_meta_path] # 后缀是.ckpt.meta的文件 vgg_graph_weight params[vgg_graph_weight] # 后缀是.ckpt的文件里面是各个tensor的值 saver_vgg tf.train.import_meta_graph(vgg_meta_path) # 导入graph到当前的默认graph中返回导入graph的saver x_vgg_feat tf.get_collection(inputs_vgg)[0] #placeholder, [None, 4096]获取输入的placeholder feat_decode tf.get_collection(feat_encode)[0] #[None, 1024]获取要使用的tensor 以上两个获取tensor的方式也可以为: graph tf.get_default_graph() centers graph.get_tensor_by_name(loss/intra/center_loss/centers:0) 当然前提是有tensor的名字 ... init tf.global_variables_initializer() saver tf.train.Saver() # 这个是当前新图的saver config tf.ConfigProto() config.gpu_options.allow_growthTrue with tf.Session(configconfig) as sess: sess.run(init) ... saver_vgg.restore(sess, vgg_graph_weight)#使用导入图的saver来恢复 ...
2.重写一样的graph然后恢复指定scope的变量 def re_build():#重建保存的那个graph with tf.variable_scope(vgg_feat_fc): #没错这个scope要和需要恢复模型中的scope对应 ... return ... ... vgg_ref_vars tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scopevgg_feat_fc) # 既然有这个scope其实第1个方法中导入graph后可以不用返回的vgg_saver再新建一个指定var_list的vgg_saver就好了恩需要传入一个var_list的参数 ... init tf.global_variables_initializer() saver_vgg tf.train.Saver(vgg_ref_vars) # 这个是要恢复部分的saver saver tf.train.Saver() # 这个是当前新图的saver config tf.ConfigProto() config.gpu_options.allow_growthTrue with tf.Session(configconfig) as sess: sess.run(init) ... saver_vgg.restore(sess, vgg_graph_weight)#使用导入图的saver来恢复 ...
总结一下这里的要点就是在restore的时候saver要和模型对应如果直接用当前graph的saver tf.train.Saver(),来恢复保存模型的权重saver.restore(vgg_graph_weight),就会报错提示key/tensor ... not found之类的错误; 写graph的时候一定要注意写好scope和tensor的name合理插入variable_scope;
最方便的方式还是用第1种方式来保存模型这样就不用重写代码段了然后第1种方式恢复不过为了稳妥最好还是通过获取var_list指定saver的var_list妥妥的
最新发现用第1种方式恢复时要记得当前的graph和保存的模型中没有重名的tensor否则当前graph的tensor name可能不是那个name可能在后面加了_1....-_-||
在恢复图基础上构建新的网络变量并训练(finetuning)
恢复模型graph和weights在上面已经说了这里的关键点是怎样只恢复原图的权重 并且使optimizer只更新新构造变量指定层、变量。
以下code与上面没联系 1.Get input, output , saver and graph#从导入图中获取需要的东西 meta_path_restore model_dir /model_model_version.ckpt.meta model_path_restore model_dir /model_model_version.ckpt saver_restore tf.train.import_meta_graph(meta_path_restore) #获取导入图的saver便于后面恢复 graph_restore tf.get_default_graph() #此时默认图就是导入的图 #从导入图中获取需要的tensor #1. 用collection来获取 input_x tf.get_collection(inputs)[0] input_is_training tf.get_collection(is_training)[0] output_feat_fused tf.get_collection(feat_fused)[0] #2. 用tensor的name来获取 input_y graph_restore.get_tensor_by_name(label_exp:0) print(Get tensors...) print(inputs shape: {}.format(input_x.get_shape().as_list())) print(input_is_training shape: {}.format(input_is_training.get_shape().as_list())) print(output_feat_fused shape: {}.format(output_feat_fused.get_shape().as_list())) 2.Build new variable for fine tuning#构造新的variables用于后面的finetuning graph_restore.clear_collection(feat_fused) #删除以前的集合假如finetuning后用新的代替原来的 graph_restore.clear_collection(prob) #添加新的东西 if F_scale is not None and F_scale!0: print(F_scale is not None, value{}.format(F_scale)) feat_fused Net_normlize_scale(output_feat_fused, F_scale) tf.add_to_collection(feat_fused,feat_fused)#重新添加到新集合 logits_fused last_logits(feat_fused,input_is_training,7) # scope name是final_logits 3.Get acc and loss#构造损失 with tf.variable_scope(accuracy): accuracy,prediction ... with tf.variable_scope(loss): loss ... 4.Build op for fine tuning global_step tf.Variable(0, trainableFalse,nameglobal_step) learning_rate tf.train.exponential_decay(initial_lr, global_stepglobal_step, decay_stepsdecay_steps, staircaseTrue, decay_rate0.1) update_ops tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): var_list tf.contrib.framework.get_variables(final_logits)#关键获取指定scope下的变量 train_op tf.train.MomentumOptimizer(learning_ratelearning_rate,momentum0.9).minimize(loss,global_stepglobal_step,var_listvar_list) #只更新指定的variables 5.Begin training init tf.global_variables_initializer() saver tf.train.Saver() config tf.ConfigProto() config.gpu_options.allow_growthTrue with tf.Session(configconfig) as sess: sess.run(init) saver_restore.restore(sess, model_path_restore) #这里saver_restore对应导入图的saver 如果用上面新的saver的话会报错 因为多出了一些新的变量 在保存的模型中是没有那些权值的 sess.run(train_op, feed_dict) .......
再说明下两个关键点
1. 如何在新图的基础上 只恢复 导入图的权重
用导入图的saver: saver_restore
2. 如何只更新指定参数
用var_list tf.contrib.framework.get_variables(scope_name)获取指定scope_name下的变量
然后optimizer.minimize()时传入指定var_list
附如何知道tensor名字以及获取指定变量
1.获取某个操作之后的输出
用graph.get_operations()获取所有op
比如tf.Operation common_conv_xxx_net/common_conv_net/flatten/Reshape typeReshape,
那么output_pool_flatten graph_restore.get_tensor_by_name(common_conv_xxx_net/common_conv_net/flatten/Reshape:0)就是那个位置经过flatten后的输出了
2.获取指定的var的值
用GraphKeys获取变量
tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)返回指定集合的变量
比如 tf.Variable common_conv_xxx_net/final_logits/logits/biases:0 shape(7,) dtypefloat32_ref
那么var_logits_biases graph_restore.get_tensor_by_name(common_conv_xxx_net/final_logits/logits/biases:0)就是那个位置的biases了
3.获取指定scope的collection
tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES,scopecommon_conv_xxx_net.final_logits)
注意后面的scope是xxx.xxx不是xxx/xxx
Reference
http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/