Python uses tensorflow to save, load, and use models.
When using Tensorflow for deep learning training, you need to save the trained network model and various parameters for further training or use. There are many blogs about this. I found that the best one is this official English introduction:
Http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/
I have summarized and summarized this article.
First, save the model. Directly run the Code:
#!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: tut1_save.py #Author: Wang #Mail: wang19920419@hotmail.com #Created Time:2017-08-30 11:04:25 ############################ import tensorflow as tf # prepare to feed input, i.e. feed_dict and placeholders w1 = tf.Variable(tf.random_normal(shape = [2]), name = 'w1') # name is very important in restoration w2 = tf.Variable(tf.random_normal(shape = [2]), name = 'w2') b1 = tf.Variable(2.0, name = 'bias1') feed_dict = {w1:[10,3], w2:[5,5]} # define a test operation that will be restored w3 = tf.add(w1, w2) # without name, w3 will not be stored w4 = tf.multiply(w3, b1, name = "op_to_restore") #saver = tf.train.Saver() saver = tf.train.Saver(max_to_keep = 4, keep_checkpoint_every_n_hours = 1) sess = tf.Session() sess.run(tf.global_variables_initializer()) print sess.run(w4, feed_dict) #saver.save(sess, 'my_test_model', global_step = 100) saver.save(sess, 'my_test_model') #saver.save(sess, 'my_test_model', global_step = 100, write_meta_graph = False)
Note the following:
1. When creating a saver, you can specify the tensor to be stored. If this parameter is not specified, all tensor instances are saved. You can also specify the maximum storage quantity and checkpoint record time. For more information, see the English blog.
2. saver. you can set global_step and write_meta_graph in the save () function. meta stores the network structure only once when you start running the program. You can set write_meta_graph = False to limit this.
3. after the program is executed, four files are generated in the program Directory, which are. meta (storage network structure ),. data and. index (stores trained parameters) and checkpoint (records the latest model ).
The following describes how to load a saved network model. There are two methods. The first one is saver. restore (sess, 'aaaa. ckpt '). The essence of this method is to read all the parameters and load them to the defined network structure. Therefore, it is equivalent to assigning values to weights and biases of the network and executing tf. global_variables_initializer (). The disadvantage of this method is that the network structure must be overwritten before use, and the network structure must be exactly the same as the saved parameter. The second type is relatively high-end. The network structure is directly loaded into (. meta) and the Code is as follows:
#!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: tut2_import.py #Author: Wang #Mail: wang19920419@hotmail.com #Created Time:2017-08-30 14:16:38 ############################ import tensorflow as tf sess = tf.Session() new_saver = tf.train.import_meta_graph('my_test_model.meta') new_saver.restore(sess, tf.train.latest_checkpoint('./')) print sess.run('w1:0')
Use the loaded model to input new data, compute and output, or directly upload the Code:
#!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: tut3_reuse.py #Author: Wang #Mail: wang19920419@hotmail.com #Created Time:2017-08-30 14:33:35 ############################ import tensorflow as tf sess = tf.Session() # First, load meta graph and restore weights saver = tf.train.import_meta_graph('my_test_model.meta') saver.restore(sess, tf.train.latest_checkpoint('./')) # Second, access and create placeholders variables and create feed_dict to feed new data graph = tf.get_default_graph() w1 = graph.get_tensor_by_name('w1:0') w2 = graph.get_tensor_by_name('w2:0') feed_dict = {w1:[-1,1], w2:[4,6]} # Access the op that want to run op_to_restore = graph.get_tensor_by_name('op_to_restore:0') print sess.run(op_to_restore, feed_dict) # ouotput: [6. 14.]
After the network has been attached, continue to join the new network layer:
import tensorflow as tf sess=tf.Session() #First let's load meta graph and restore weights saver = tf.train.import_meta_graph('my_test_model-1000.meta') saver.restore(sess,tf.train.latest_checkpoint('./')) # Now, let's access and create placeholders variables and # create feed-dict to feed new data 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} #Now, access the op that you want to run. op_to_restore = graph.get_tensor_by_name("op_to_restore:0") #Add more to the current graph add_on_op = tf.multiply(op_to_restore,2) print sess.run(add_on_op,feed_dict) #This will print 120.
Perform local modification and processing on the loaded Network (this is the most troublesome one. I have not made it clear yet, and I will continue to add it later ):
...... ...... saver = tf.train.import_meta_graph('vgg.meta') # Access the graph graph = tf.get_default_graph() ## Prepare the feed_dict for feeding data for fine-tuning #Access the appropriate output for fine-tuning fc7= graph.get_tensor_by_name('fc7:0') #use this if you only want to change gradients of the last layer fc7 = tf.stop_gradient(fc7) # It's an identity function fc7_shape= fc7.get_shape().as_list() new_outputs=2 weights = tf.Variable(tf.truncated_normal([fc7_shape[3], num_outputs], stddev=0.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()
With this method, whether it is self-training, Loading Model for continuous training, using the classic model, finetune classic model, or loading the network before running, the effect is superb.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.