Convert the proto protocol (pb / pbtxt) to SavedModel for use in the TensorFlow service or Cloud ML Engine

I followed TensorFlow for Poets 2 codelab on the model I trained and created a frozen, quantized graph with built-in weights. It is written to a single file - say my_quant_graph.pb.

Since I can use this graph for output with the TensorFlow Android output library , I thought I could do the same with the Cloud ML Engine, but it seems to work only with the SavedModel model.

How can I just convert a frozen / quantized graph into a single pb file for use on the ML engine?

0
source share
1 answer

It turns out SavedModel provides additional information around the saved chart. Assuming the frozen schedule does not need assets, then it only needs a serving signature.

Here the python code I was working with converted my chart to the format that the Cloud ML module accepted. Note. I have only one pair of input / output tensors.

import tensorflow as tf
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants

export_dir = './saved'
graph_pb = 'my_quant_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("real_A_and_B_images:0")
    out = g.get_tensor_by_name("generator/Tanh: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()
+2
source

Source: https://habr.com/ru/post/1679674/


All Articles