Save and Restore Tensorflow Model

Sanpreet Singh
2 min readJun 30, 2019

--

Save and Restore Tensorflow Model
Save and Restore Tensorflow Model

INTRODUCTION

The main reason behind writing this article is to help others to restore the tensorflow model but it is important and vital to understand why to learn about restoring the model. Suppose you are training for 2 days and suddenly light goes off. You need to start the training from the scratch. If you need to restore the model from the last checkpoint, it would be great and your computational cost and time will be saved. This is is the basic tutorial to restore the model using checkpoints and meta graph

I would also suggest you go through the following articles as it will make concepts of frozen file and TensorFlow files more clear

Freezing Tensorflow Model for knowing about tensorflow saved model

Road Map for Custom Object Detection with Tensorflow API for knowing about frozen file. This is protocol buffer and is very important file if you are working with tensorflow

Tensor-flow checkpoint and Meta Graph for tensorflow checkpoints and other files

Code to create the tensorflow model

import tensorflow as tf
# creating the variables
w1 = tf.Variable([1,2], name='w1')
w2 = tf.Variable([3,4], name='w2')
# saver object to save the variables
saver = tf.train.Saver()
# session is run
sess = tf.Session()
#initialize the variables
sess.run(tf.global_variables_initializer())
# save the variables
saver.save(sess, './simple_model')

Code to restore the model using checkpoints and meta graph

import tensorflow as tf# session is created
sess=tf.Session()
# one need to load the meta graph
# one can visit https://ersanpreet.wordpress.com/2019/06/24/freezing-tensorflow-model/
# to know more about meta graph
saver = tf.train.import_meta_graph('simple_model.meta')
# restoring the latest checkpoints
saver.restore(sess.tf.train.latest_checkpoint('./'))
# if the model is successfully loaded,
# one will see the output of variable w1 which is being saved
print("Output of the variable w1", sess.run('w1:0'))
print("Success")
print("Output of the another variable w2", sess.run('w2:0'))
print("Sucessfully loaded both the variables")

Conclusion

In a nutshell, it can be said that it is important to learn how to restore the model while working for any deep learning framework such as tensorflow, pytorch, keras and any other. It is key to understand the basics of each framework to load and restore the variables and weights. It is the basics for the transfer learning and is proved to be very beneficial when one is playing with deep learning. If you want more updates about deep learning frameworks and models, please stay tuned to website. Thanks for sparing some time for reading this article. Feel free to ask questions in the comment section.

Originally published at http://ersanpreet.wordpress.com on June 30, 2019.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response