Skip to content

Instantly share code, notes, and snippets.

@hongta
Last active April 28, 2020 23:54
Show Gist options
  • Select an option

  • Save hongta/c08079314bd0b0120c054b36a1d3052a to your computer and use it in GitHub Desktop.

Select an option

Save hongta/c08079314bd0b0120c054b36a1d3052a to your computer and use it in GitHub Desktop.
[Convert the Keras HDF5 model into TensorFlow SavedModel] #tensorflow #keras
# source: https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/census/keras/trainer/model.py
from keras import backend as K
import tensorflow as tf
from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def
def to_savedmodel(model, export_path):
"""Convert the Keras HDF5 model into TensorFlow SavedModel."""
builder = saved_model_builder.SavedModelBuilder(export_path)
signature = predict_signature_def(inputs={'input': model.inputs[0]},
outputs={'income': model.outputs[0]})
with K.get_session() as sess:
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature}
)
builder.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment