0

I have a keras model trained and saved it in h5 format. I want to host this model on google cloud ml engine for prediction. How can i convert keras model .h5 file to saved model.

1 Answers1

0

The post where I have seen this code(although I have seen this solution in many other posts) is this: https://www.dlology.com/blog/how-to-convert-trained-keras-model-to-tensorflow-and-make-prediction/

    import tensorflow as tf
    from keras import backend as K
    # This line must be executed before loading Keras model.
    K.set_learning_phase(0)

    from keras.models import load_model
    model = load_model('./model/keras_model.h5')

    def freeze_session(session, keep_var_names=None, output_names=None,clear_devices=True):
        """
        Freezes the state of a session into a pruned computation graph.

        Creates a new computation graph where variable nodes are replaced by
        constants taking their current value in the session. The new graph will be
        pruned so subgraphs that are not necessary to compute the requested
        outputs are removed.
        @param session The TensorFlow session to be frozen.
        @param keep_var_names A list of variable names that should not be frozen,
                              or None to freeze all the variables in the graph.
        @param output_names Names of the relevant graph outputs.
        @param clear_devices Remove the device directives from the graph for better portability.
        @return The frozen graph definition.
        """
        from tensorflow.python.framework.graph_util import convert_variables_to_constants
        graph = session.graph
        with graph.as_default():
            freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
            output_names = output_names or []
            output_names += [v.op.name for v in tf.global_variables()]
            # Graph -> GraphDef ProtoBuf
            input_graph_def = graph.as_graph_def()
            if clear_devices:
                for node in input_graph_def.node:
                    node.device = ""
            frozen_graph = convert_variables_to_constants(session, input_graph_def,
                                                          output_names, freeze_var_names)
            return frozen_graph

frozen_graph = freeze_session(K.get_session(),
output_names=[out.op.name for out in model.outputs])

tf.train.write_graph(frozen_graph, "model", "tf_model.pb", as_text=False)
Timbus Calin
  • 13,809
  • 5
  • 41
  • 59