tensorflowobject-detectiongoogle-cloud-mlgoogle-cloud-ml-engine

Convert graph (pb) to SavedModel for gcloud ml-engine predict


I trained an object detector using Cloud Machine Learning Engine according to the recent post by Google’s Derek Chow on the Google Cloud Big Data And Machine Learning Blog and now want to predict using Cloud Machine Learning Engine.

The instructions include code to export the Tensorflow graph as output_inference_graph.pb but not how to convert protobuf format (pb) into the SavedModel format required for gcloud ml-engine predict.

I reviewed the answer by Google’s @rhaertel80 for how to convert a “Tensorflow For Poets” image classification model and the answer provided by Google’s @MarkMcDonald for how to convert a “Tensorflow For Poets 2” image classification model but neither appears to work for the object detector graph (pb) described in the blog post.

How does one convert that object detector graph (pb) so it can be used or gcloud ml-engine predict, please?


Solution

  • SavedModel contains a MetaGraphDef inside its structure. To create a SavedModel from a GraphDef in python you may want to use builder as described in the link.

    export_dir = ...
    ...
    builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
    with tf.Session(graph=tf.Graph()) as sess:
      ...
      builder.add_meta_graph_and_variables(sess,
                                           [tag_constants.TRAINING],
                                           signature_def_map=foo_signatures,
                                           assets_collection=foo_assets)
    ...
    with tf.Session(graph=tf.Graph()) as sess:
      ...
      builder.add_meta_graph(["bar-tag", "baz-tag"])
    ...
    builder.save()