Last active
November 5, 2018 14:57
-
-
Save adekunleba/a147d1df892014f37624d5e4c699556f to your computer and use it in GitHub Desktop.
Convert a frozen tensorflow model to Tensorflow serving format
This file contains hidden or 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
| import tensorflow as tf | |
| from tensorflow.python.saved_model import signature_constants | |
| from tensorflow.python.saved_model import tag_constants | |
| import argparse | |
| export_dir = 'models/1' | |
| builder = tf.saved_model.builder.SavedModelBuilder(export_dir) | |
| def convertFrozenModel(model_protobuf): | |
| with tf.gfile.GFile(model_protobuf, 'rb') as f: | |
| graph_def = tf.GraphDef() | |
| graph_def.ParseFromString(f.read()) | |
| with tf.Session(graph=tf.Graph()) as sess: | |
| tf.import_graph_def(graph_def, input_map=None, return_elements=None, name="") | |
| graph = tf.get_default_graph() | |
| predTensor = graph.get_tensor_by_name("ImageTensor:0") | |
| inputTensor = graph.get_tensor_by_name("SemanticPredictions:0") | |
| tensor_input = tf.saved_model.utils.build_tensor_info(inputTensor) | |
| tensorf_output = tf.saved_model.utils.build_tensor_info(predTensor) | |
| pred_signature = ( | |
| tf.saved_model.signature_def_utils.build_signature_def( | |
| inputs={"input" : tensor_input}, | |
| outputs= {"outputs": tensorf_output}, | |
| method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME | |
| ) | |
| ) | |
| builder.add_meta_graph_and_variables(sess, [tag_constants.SERVING], signature_def_map={ | |
| "prediction":pred_signature | |
| }) | |
| builder.save() | |
| convertFrozenModel("path/to/.pb/file") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment