This paper will introduce the TensorFlow save and restore model, mainly explaining the Saver class save and restore restoration methods. We can solve how to save and restore a trained neural network model for inference and prediction of realistic needs, and can also assist in viewing and analyzing the performance of a long-trained model. The most important thing is that we can prevent the failure of training due to power failure, downtime, error exit and other problems in the middle of the long training! It can be seen that mastering tensorFlow’s method of saving and restoring models is of great help to our engineering applications. Meanwhile, it is also a basic skill we must master. Saving and restoring models in TensorFlow is mainly through tb.train.saver (), as follows:

  • Save the model
  1. Saver = tf.train.saver () gets a file handle to save one of the snapshot states in the training to the file
  2. Saver.save (sess, os.path.join(model_DIR, ‘CKP -% 05D’ %(I +1))), save the trained model to a file
  3. Parameter 1: sess, session session, parameter 2: model save path
  • Restoring model
  1. Saver.restore (sess, model_path) restores the model from the file
  2. Parameter 1: sess, session Session. Parameter 2: model_path the model needs to be restored

Sample code:

With tf.session () as sess: sess.run(init) # Note: This step must be included! Train_writer = tf.summary.FileWriter(train_log_dir, train_log_dir, FileWriter(test_log_dir) fixed_test_batch_data, fixed_test_batch_labels = test_data.next_batch(batch_size) if os.path.exists(model_path + '.index'): saver.restore(sess, model_path) print('model restored from %s' % model_path) else: Print ('model %s dose not exist' % model_path) # train_steps: Next_batch (batch_size) eval_ops = [loss, accuracy, batch_labels = train_data.next_batch(batch_size) eval_ops = [loss, accuracy, train_op] should_output_summary = ((i+1) % output_summary_every_steps == 0) if should_output_summary: Append (merged_summary) # eval_val_results = sess.run(eval_ops, feed_dict={x:batch_data, y:batch_labels} ) loss_val, acc_val = eval_val_results[0:2] if should_output_summary: train_summary_str = eval_val_results[-1] train_writer.add_summary(train_summary_str, i+1) test_summary_str = sess.run([merged_summary_test], feed_dict = {x: fixed_test_batch_data,y: Fixed_test_batch_labels})[0] test_writer.add_summary(test_summary_str, I +1) # if (I +1) % 500 == 0: Print ('[Train] Step: %d, loss: %4.5f, acc: If (I +1) % 5000 == 0: if (I +1) % 5000 == 0: Test_data = CifarData(test_filename, False) all_test_ACC_val = [] for j in range(test_steps): test_batch_data, test_batch_labels = test_data.next_batch( batch_size ) test_acc_val = sess.run( [accuracy], feed_dict={ x:test_batch_data, y:test_batch_labels } ) all_test_acc_val.append( test_acc_val ) test_acc = np.mean( all_test_acc_val ) print('[Test ] Step: %d, acc: %4.5f' % ((I +1), test_ACC)) if (I +1) % output_model_every_steps == 0: # saver.save(sess, os.path.join(model_dir, 'ckp-%05d'%(i+1))) print('model saved to ckp-%05d' % (i+1))Copy the code

Reference:

Tensorflow saves and restores models