Skip to content

Instantly share code, notes, and snippets.

@sdcubber
Last active July 30, 2018 15:38
Show Gist options
  • Save sdcubber/96ef92656b9996c8e1aa02d70d886b15 to your computer and use it in GitHub Desktop.
Save sdcubber/96ef92656b9996c8e1aa02d70d886b15 to your computer and use it in GitHub Desktop.
import tensorflow as tf
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def
from tensorflow.python.saved_model import tag_constants
tf.keras.backend.set_learning_phase(0)
# The export path contains the name and the version of the model
export_path = './PlanetModel/1'
model = tf.keras.models.load_model('./model.h5')
builder = saved_model_builder.SavedModelBuilder(export_path)
# Specify the signature definition
signature = predict_signature_def(inputs={'images': model.input},
outputs={t.name:t for t in model.outputs})
# This defaults to 'serving_default'
key = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
# Feth the current session and save the model
with tf.keras.backend.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={key: signature})
builder.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment