Skip to content

Instantly share code, notes, and snippets.

@jainxy
Created April 16, 2020 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jainxy/d561347bce354c38ee8a5d35f21910ad to your computer and use it in GitHub Desktop.
Save jainxy/d561347bce354c38ee8a5d35f21910ad to your computer and use it in GitHub Desktop.
Tensorflow snippets
## Save tf model under graph-model
saver = tf.train.Saver() # general saver object
#saves a model every 2 hours and maximum 4 latest models are saved.
saver = tf.train.Saver(max_to_keep=4, keep_checkpoint_every_n_hours=2)
saver = tf.train.Saver([w1,w2]) # save a list of variables
saver.save(sess, 'my-test-model') / saver.save(sess, 'my_test_model',global_step=1000)
saver.save(sess, 'my-model', global_step=step,write_meta_graph=False) # dont save the model graph
## Recreate network
new_saver = tf.train.import_meta_graph('my_test_model-1000.meta')
with tf.Session() as sess:
new_saver.restore(sess, tf.train.latest_checkpoint('./'))
# Access the graph after loading the meta
graph = tf.get_default_graph()
# freeze gradient computation upto fc7
fc7 = tf.stop_gradient(fc7) # It's an identity function
# makes use of the Keras learning phase before tf-serving => https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html
from keras import backend as K
K.set_learning_phase(0) # all new operations will be in test mode from now on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment