当前位置: 代码迷 >> 综合 >> Tensor flow模型的保存和提取方法
  详细解决方案

Tensor flow模型的保存和提取方法

热度:33   发布时间:2023-11-19 15:33:25.0

一、Tensorflow模型的保存和提取方法

1、Tensorflow通过tf.train.Saver类实现神经网络模型的保存和提取。tf.train.Saver对象saver的save方法将Tensoreflow模型保存到指定的路径中,saver.save(sess,'model/model.ckpt).实际在这个文件目录下有4个文件:


checkpoint 文件保存了一个录下多有的模型文件列表,model.ckpt.meta保存了tensorflow计算图的结构信息,model.ckpt保存每个变量的取值,此处文件名的写入方式会因不同参数的设置不同,但加载restore时的文件路径名是以checkpoint文件的‘model_checkpoint_path’值决定的。

2.加载这个已保存的tensorflow模型的方法是saver.restore(sess,'./model/model.ckpt'),加载模型的代码中也要定义tensorflow计算图上的所有运算并声明一个tf.train.Saver类,不同的是加载模型时不需要进行变量的初始化,而是将变量的取值通过保存的模型加载进来,如果不希望重复定义计算图上的运算,可直接加载已经持久化的图,saver = tf.train.import_meta_graph('model/model.ckpt.meta')

3.tf.train.Saver类也支持在保存和加载时给变量重命名,声明Saver类对象的时候使用一个字典dict重命名变量即可,{已知保存的变量名:重命名变量名},saver.tf.train.Saver({v1:u1,v2:u2})即原来名称name为v1的变量现在加载到变量u1中。

4.第三条做的目的之一就是方便使用变量的滑动平均值。如果在加载模型时直接将影子变量映射到变量自身,则在使用训练好的模型时就不需要再调用函数来获取变量的滑动平均值了。载入时,声明Saver类对象时通过一个字典将滑动平均值直接加载到新的变量中,saver = tf.train.Saver({v/ExponentialMovingAverage:v}) 另通过tf.train.ExponentialMovingAverage的variables_to_restore()函数获取变量重命名字典。通过convert_variables_to_constants函数将计算图中的变量以及其取值痛殴常量的方式保存于一个文件中

Tensorflow程序实现


    
  1. import tensorflow as tf
  2. v1 = tf.Variable(tf.constant( 1.0, shape=[ 1]), name= "v1")
  3. v2 = tf.Variable(tf.constant( 2.0, shape=[ 1]), name= "v2")
  4. result = v1 + v2
  5. saver = tf.train.Saver()
  6. with tf.Session() as sess:
  7. sess.run(tf.global_variables_initializer())
  8. saver.save(sess, "Model/model.ckpt")
  9. # Part2: 加载TensorFlow模型的方法
  10. import tensorflow as tf
  11. v1 = tf.Variable(tf.constant( 1.0, shape=[ 1]), name= "v1")
  12. v2 = tf.Variable(tf.constant( 2.0, shape=[ 1]), name= "v2")
  13. result = v1 + v2
  14. saver = tf.train.Saver()
  15. with tf.Session() as sess:
  16. saver.restore(sess, "./Model/model.ckpt") # 注意此处路径前添加"./"
  17. print(sess.run(result)) # [ 3.]
  18. # Part3: 若不希望重复定义计算图上的运算,可直接加载已经持久化的图
  19. import tensorflow as tf
  20. saver = tf.train.import_meta_graph( "Model/model.ckpt.meta")
  21. with tf.Session() as sess:
  22. saver.restore(sess, "./Model/model.ckpt") # 注意路径写法
  23. print(sess.run(tf.get_default_graph().get_tensor_by_name( "add:0"))) # [ 3.]
  24. # Part4: tf.train.Saver类也支持在保存和加载时给变量重命名
  25. import tensorflow as tf
  26. # 声明的变量名称name与已保存的模型中的变量名称name不一致
  27. u1 = tf.Variable(tf.constant( 1.0, shape=[ 1]), name= "other-v1")
  28. u2 = tf.Variable(tf.constant( 2.0, shape=[ 1]), name= "other-v2")
  29. result = u1 + u2
  30. # 若直接生命Saver类对象,会报错变量找不到
  31. # 使用一个字典dict重命名变量即可,{"已保存的变量的名称name": 重命名变量名}
  32. # 原来名称name为v1的变量现在加载到变量u1(名称name为other-v1)中
  33. saver = tf.train.Saver({ "v1": u1, "v2": u2})
  34. with tf.Session() as sess:
  35. saver.restore(sess, "./Model/model.ckpt")
  36. print(sess.run(result)) # [ 3.]
  37. # Part5: 保存滑动平均模型
  38. import tensorflow as tf
  39. v = tf.Variable( 0, dtype=tf.float32, name= "v")
  40. for variables in tf.global_variables():
  41. print(variables.name) # v:0
  42. ema = tf.train.ExponentialMovingAverage( 0.99)
  43. maintain_averages_op = ema.apply(tf.global_variables())
  44. for variables in tf.global_variables():
  45. print(variables.name) # v:0
  46. # v/ExponentialMovingAverage:0
  47. saver = tf.train.Saver()
  48. with tf.Session() as sess:
  49. sess.run(tf.global_variables_initializer())
  50. sess.run(tf.assign(v, 10))
  51. sess.run(maintain_averages_op)
  52. saver.save(sess, "Model/model_ema.ckpt")
  53. print(sess.run([v, ema.average(v)])) # [10.0, 0.099999905]
  54. # Part6: 通过变量重命名直接读取变量的滑动平均值
  55. import tensorflow as tf
  56. v = tf.Variable( 0, dtype=tf.float32, name= "v")
  57. saver = tf.train.Saver({ "v/ExponentialMovingAverage": v})
  58. with tf.Session() as sess:
  59. saver.restore(sess, "./Model/model_ema.ckpt")
  60. print(sess.run(v)) # 0.0999999
  61. # Part7: 通过tf.train.ExponentialMovingAverage的variables_to_restore()函数获取变量重命名字典
  62. import tensorflow as tf
  63. v = tf.Variable( 0, dtype=tf.float32, name= "v")
  64. # 注意此处的变量名称name一定要与已保存的变量名称一致
  65. ema = tf.train.ExponentialMovingAverage( 0.99)
  66. print(ema.variables_to_restore())
  67. # {'v/ExponentialMovingAverage': <tf.Variable 'v:0' shape=() dtype=float32_ref>}
  68. # 此处的v取自上面变量v的名称name="v"
  69. saver = tf.train.Saver(ema.variables_to_restore())
  70. with tf.Session() as sess:
  71. saver.restore(sess, "./Model/model_ema.ckpt")
  72. print(sess.run(v)) # 0.0999999
  73. # Part8: 通过convert_variables_to_constants函数将计算图中的变量及其取值通过常量的方式保存于一个文件中
  74. import tensorflow as tf
  75. from tensorflow.python.framework import graph_util
  76. v1 = tf.Variable(tf.constant( 1.0, shape=[ 1]), name= "v1")
  77. v2 = tf.Variable(tf.constant( 2.0, shape=[ 1]), name= "v2")
  78. result = v1 + v2
  79. with tf.Session() as sess:
  80. sess.run(tf.global_variables_initializer())
  81. # 导出当前计算图的GraphDef部分,即从输入层到输出层的计算过程部分
  82. graph_def = tf.get_default_graph().as_graph_def()
  83. output_graph_def = graph_util.convert_variables_to_constants(sess,
  84. graph_def, [ 'add'])
  85. with tf.gfile.GFile( "Model/combined_model.pb", 'wb') as f:
  86. f.write(output_graph_def.SerializeToString())
  87. # Part9: 载入包含变量及其取值的模型
  88. import tensorflow as tf
  89. from tensorflow.python.platform import gfile
  90. with tf.Session() as sess:
  91. model_filename = "Model/combined_model.pb"
  92. with gfile.FastGFile(model_filename, 'rb') as f:
  93. graph_def = tf.GraphDef()
  94. graph_def.ParseFromString(f.read())
  95. result = tf.import_graph_def(graph_def, return_elements=[ "add:0"])
  96. print(sess.run(result)) # [array([ 3.], dtype=float32)]

    
  1. '''
  2. Save and Restore a model using TensorFlow.
  3. This example is using the MNIST database of handwritten digits
  4. (http://yann.lecun.com/exdb/mnist/)
  5. Author: Aymeric Damien
  6. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  7. '''
  8. from __future__ import print_function
  9. # Import MNIST data
  10. from tensorflow.examples.tutorials.mnist import input_data
  11. mnist = input_data.read_data_sets( "MNIST_data/", one_hot= True)
  12. import tensorflow as tf
  13. # Parameters
  14. learning_rate = 0.001
  15. batch_size = 100
  16. display_step = 1
  17. model_path = "/tmp/model.ckpt"
  18. # Network Parameters
  19. n_hidden_1 = 256 # 1st layer number of features
  20. n_hidden_2 = 256 # 2nd layer number of features
  21. n_input = 784 # MNIST data input (img shape: 28*28)
  22. n_classes = 10 # MNIST total classes (0-9 digits)
  23. # tf Graph input
  24. x = tf.placeholder( "float", [ None, n_input])
  25. y = tf.placeholder( "float", [ None, n_classes])
  26. # Create model
  27. def multilayer_perceptron(x, weights, biases):
  28. # Hidden layer with RELU activation
  29. layer_1 = tf.add(tf.matmul(x, weights[ 'h1']), biases[ 'b1'])
  30. layer_1 = tf.nn.relu(layer_1)
  31. # Hidden layer with RELU activation
  32. layer_2 = tf.add(tf.matmul(layer_1, weights[ 'h2']), biases[ 'b2'])
  33. layer_2 = tf.nn.relu(layer_2)
  34. # Output layer with linear activation
  35. out_layer = tf.matmul(layer_2, weights[ 'out']) + biases[ 'out']
  36. return out_layer
  37. # Store layers weight & bias
  38. weights = {
  39. 'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
  40. 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
  41. 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
  42. }
  43. biases = {
  44. 'b1': tf.Variable(tf.random_normal([n_hidden_1])),
  45. 'b2': tf.Variable(tf.random_normal([n_hidden_2])),
  46. 'out': tf.Variable(tf.random_normal([n_classes]))
  47. }
  48. # Construct model
  49. pred = multilayer_perceptron(x, weights, biases)
  50. # Define loss and optimizer
  51. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
  52. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
  53. # Initializing the variables
  54. init = tf.global_variables_initializer()
  55. # 'Saver' op to save and restore all the variables
  56. saver = tf.train.Saver()
  57. # Running first session
  58. print( "Starting 1st session...")
  59. with tf.Session() as sess:
  60. # Initialize variables
  61. sess.run(init)
  62. # Training cycle
  63. for epoch in range( 3):
  64. avg_cost = 0.
  65. total_batch = int(mnist.train.num_examples/batch_size)
  66. # Loop over all batches
  67. for i in range(total_batch):
  68. batch_x, batch_y = mnist.train.next_batch(batch_size)
  69. # Run optimization op (backprop) and cost op (to get loss value)
  70. _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
  71. y: batch_y})
  72. # Compute average loss
  73. avg_cost += c / total_batch
  74. # Display logs per epoch step
  75. if epoch % display_step == 0:
  76. print( "Epoch:", '%04d' % (epoch+ 1), "cost=", \
  77. "{:.9f}".format(avg_cost))
  78. print( "First Optimization Finished!")
  79. # Test model
  80. correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
  81. # Calculate accuracy
  82. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  83. print( "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
  84. # Save model weights to disk
  85. save_path = saver.save(sess, model_path)
  86. print( "Model saved in file: %s" % save_path)
  87. # Running a new session
  88. print( "Starting 2nd session...")
  89. with tf.Session() as sess:
  90. # Initialize variables
  91. sess.run(init)
  92. # Restore model weights from previously saved model
  93. saver.restore(sess, model_path)
  94. print( "Model restored from file: %s" % save_path)
  95. # Resume training
  96. for epoch in range( 7):
  97. avg_cost = 0.
  98. total_batch = int(mnist.train.num_examples / batch_size)
  99. # Loop over all batches
  100. for i in range(total_batch):
  101. batch_x, batch_y = mnist.train.next_batch(batch_size)
  102. # Run optimization op (backprop) and cost op (to get loss value)
  103. _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
  104. y: batch_y})
  105. # Compute average loss
  106. avg_cost += c / total_batch
  107. # Display logs per epoch step
  108. if epoch % display_step == 0:
  109. print( "Epoch:", '%04d' % (epoch + 1), "cost=", \
  110. "{:.9f}".format(avg_cost))
  111. print( "Second Optimization Finished!")
  112. # Test model
  113. correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
  114. # Calculate accuracy
  115. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
  116. print( "Accuracy:", accuracy.eval(
  117. {x: mnist.test.images, y: mnist.test.labels}))

    
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Jun 4 10:29:48 2017
  4. @author: Administrator
  5. """
  6. import tensorflow as tf
  7. from tensorflow.examples.tutorials.mnist import input_data
  8. mnist = input_data.read_data_sets( "MNIST_data/", one_hot= False)
  9. x = tf.placeholder(tf.float32, [ None, 784])
  10. y_=tf.placeholder(tf.int32,[ None,])
  11. dense1 = tf.layers.dense(inputs=x,
  12. units= 1024,
  13. activation=tf.nn.relu,
  14. kernel_initializer=tf.truncated_normal_initializer(stddev= 0.01),
  15. kernel_regularizer=tf.nn.l2_loss)
  16. dense2= tf.layers.dense(inputs=dense1,
  17. units= 512,
  18. activation=tf.nn.relu,
  19. kernel_initializer=tf.truncated_normal_initializer(stddev= 0.01),
  20. kernel_regularizer=tf.nn.l2_loss)
  21. logits= tf.layers.dense(inputs=dense2,
  22. units= 10,
  23. activation= None,
  24. kernel_initializer=tf.truncated_normal_initializer(stddev= 0.01),
  25. kernel_regularizer=tf.nn.l2_loss)
  26. loss=tf.losses.sparse_softmax_cross_entropy(labels=y_,logits=logits)
  27. train_op=tf.train.AdamOptimizer(learning_rate= 0.001).minimize(loss)
  28. correct_prediction = tf.equal(tf.cast(tf.argmax(logits, 1),tf.int32), y_)
  29. acc= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  30. sess=tf.InteractiveSession()
  31. sess.run(tf.global_variables_initializer())
  32. saver=tf.train.Saver(max_to_keep= 1)
  33. for i in range( 100):
  34. batch_xs, batch_ys = mnist.train.next_batch( 100)
  35. sess.run(train_op, feed_dict={x: batch_xs, y_: batch_ys})
  36. val_loss,val_acc=sess.run([loss,acc], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
  37. print( 'epoch:%d, val_loss:%f, val_acc:%f'%(i,val_loss,val_acc))
  38. saver.save(sess, 'ckpt/mnist.ckpt',global_step=i+ 1)
  39. sess.close()

代码中红色部分就是保存模型的代码,虽然我在每训练完一代的时候,都进行了保存,但后一次保存的模型会覆盖前一次的,最终只会保存最后一次。因此我们可以节省时间,将保存代码放到循环之外(仅适用max_to_keep=1,否则还是需要放在循环内).

在实验中,最后一代可能并不是验证精度最高的一代,因此我们并不想默认保存最后一代,而是想保存验证精度最高的一代,则加个中间变量和判断语句就可以了。


    
  1. saver=tf.train.Saver(max_to_keep=1)
  2. max_acc=0
  3. for i in range(100):
  4. batch_xs, batch_ys = mnist.train.next_batch(100)
  5. sess.run(train_op, feed_dict={x: batch_xs, y_: batch_ys})
  6. val_loss,val_acc=sess.run([loss,acc], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
  7. print('epoch:%d, val_loss:%f, val_acc:%f'%(i,val_loss,val_acc))
  8. if val_acc>max_acc:
  9. max_acc=val_acc
  10. saver.save(sess,'ckpt/mnist.ckpt',global_step=i+1)
  11. sess.close()


saver=tf.train.Saver(max_to_keep=3)
max_acc=0
f=open('ckpt/acc.txt','w')
for i in range(100):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_op, feed_dict={x: batch_xs, y_: batch_ys})
  val_loss,val_acc=sess.run([loss,acc], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
  print('epoch:%d, val_loss:%f, val_acc:%f'%(i,val_loss,val_acc))
  f.write(str(i+1)+', val_acc: '+str(val_acc)+'\n')
  if val_acc>max_acc:
      max_acc=val_acc
      saver.save(sess,'ckpt/mnist.ckpt',global_step=i+1)
f.close()

sess.close()


    
  1. sess=tf.InteractiveSession()
  2. sess.run(tf.global_variables_initializer())
  3. is_train= False
  4. saver=tf.train.Saver(max_to_keep= 3)
  5. #训练阶段
  6. if is_train:
  7. max_acc= 0
  8. f=open( 'ckpt/acc.txt', 'w')
  9. for i in range( 100):
  10. batch_xs, batch_ys = mnist.train.next_batch( 100)
  11. sess.run(train_op, feed_dict={x: batch_xs, y_: batch_ys})
  12. val_loss,val_acc=sess.run([loss,acc], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
  13. print( 'epoch:%d, val_loss:%f, val_acc:%f'%(i,val_loss,val_acc))
  14. f.write(str(i+ 1)+ ', val_acc: '+str(val_acc)+ '\n')
  15. if val_acc>max_acc:
  16. max_acc=val_acc
  17. saver.save(sess, 'ckpt/mnist.ckpt',global_step=i+ 1)
  18. f.close()
  19. #验证阶段
  20. else:
  21. model_file=tf.train.latest_checkpoint( 'ckpt/')
  22. saver.restore(sess,model_file)
  23. val_loss,val_acc=sess.run([loss,acc], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
  24. print( 'val_loss:%f, val_acc:%f'%(val_loss,val_acc))
  25. sess.close()



  相关解决方案