Skip to content

Instantly share code, notes, and snippets.

@Taehun
Created January 29, 2021 01:04
Show Gist options
  • Save Taehun/7ccd04fdb6552531f5fff901c3a5b0f8 to your computer and use it in GitHub Desktop.
Save Taehun/7ccd04fdb6552531f5fff901c3a5b0f8 to your computer and use it in GitHub Desktop.
DeepLab frozen graph to saved model
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants
export_dir = './saved'
graph_pb = 'frozen_inference_graph.pb'
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.gfile.GFile(graph_pb, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
sigs = {}
with tf.Session(graph=tf.Graph()) as sess:
# name="" is important to ensure we don't get spurious prefixing
tf.import_graph_def(graph_def, name="")
g = tf.get_default_graph()
inp = g.get_tensor_by_name("ImageTensor:0")
out = g.get_tensor_by_name("SemanticPredictions:0")
sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
tf.saved_model.signature_def_utils.predict_signature_def(
{"in": inp}, {"out": out})
builder.add_meta_graph_and_variables(sess,
[tag_constants.SERVING],
signature_def_map=sigs)
builder.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment