Last active
August 17, 2020 11:40
-
-
Save bombdiggity/6001fa58c67bf96276bbafba3612089b to your computer and use it in GitHub Desktop.
Code to export Keras model to ProtoBuf format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras import backend as K | |
from keras.models import load_model | |
from tensorflow.python.saved_model import builder as saved_model_builder | |
from tensorflow.python.saved_model.signature_def_utils import predict_signature_def | |
from tensorflow.python.saved_model import tag_constants | |
# Function to export Keras model to Protocol Buffer format | |
# Inputs: | |
# path_to_h5: Path to Keras h5 model | |
# export_path: Path to store Protocol Buffer model | |
def export_h5_to_pb(path_to_h5, export_path): | |
# Set the learning phase to Test since the model is already trained. | |
K.set_learning_phase(0) | |
# Load the Keras model | |
keras_model = load_model(path_to_h5) | |
# Build the Protocol Buffer SavedModel at 'export_path' | |
builder = saved_model_builder.SavedModelBuilder(export_path) | |
# Create prediction signature to be used by TensorFlow Serving Predict API | |
signature = predict_signature_def(inputs={"images": keras_model.input}, | |
outputs={"scores": keras_model.output}) | |
with K.get_session() as sess: | |
# Save the meta graph and the variables | |
builder.add_meta_graph_and_variables(sess=sess, tags=[tag_constants.SERVING], | |
signature_def_map={"predict": signature}) | |
builder.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
raise ValueError('No model found in config file.')
ValueError: No model found in config file.