Skip to content

Instantly share code, notes, and snippets.

@ankitshekhawat
Created January 10, 2017 05:50
Show Gist options
  • Save ankitshekhawat/c003b4da201229c6bb548c12c3518a50 to your computer and use it in GitHub Desktop.
Save ankitshekhawat/c003b4da201229c6bb548c12c3518a50 to your computer and use it in GitHub Desktop.
Export models and weights are trained in Keras to be used in TensorFlow Serving library
from keras import backend as K
import tensorflow as tf
from tensorflow.contrib.session_bundle import exporter
sess = K.get_session()
export_path ="."
export_version = 1
saver = tf.train.Saver(sharded=True)
### Code if you want to only export .meta files
# model_exporter = exporter.Exporter(saver)
# signature = exporter.classification_signature(input_tensor=model.input,
# scores_tensor=model.output)
# model_exporter.init(sess.graph.as_graph_def(),
# default_graph_signature=signature)
# model_exporter.export(export_path, tf.constant(export_version), sess)
# Use a saver_def to get the "magic" strings to restore
### Code if you want to export .proto files
saver_def = saver.as_saver_def()
print saver_def.filename_tensor_name
print saver_def.restore_op_name
# write out 3 files
saver.save(sess, 'trained_model.sd')
tf.train.write_graph(sess.graph_def, '.', 'trained_model.proto', as_text=False)
tf.train.write_graph(sess.graph_def, '.', 'trained_model.txt', as_text=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment