title: Adding a {serve} metagraph to existing Tensorflow model
body: <p>The situation:</p>
  
  <p>I've already created several models, trained over several days each, that we're ready to move from local testing to a serving environment.</p>
  
  <p>The models were saved using the function</p>
  
  <pre class="lang-py prettyprint-override"><code>def save_graph_to_file(sess, graph, graph_file_name):
      """Saves an graph to file, creating a valid quantized one if necessary."""
      output_graph_def = graph_util.convert_variables_to_constants(sess, graph.as_graph_def(), [final_tensor_name])
      with gfile.FastGFile(graph_file_name, 'wb') as f:
          f.write(output_graph_def.SerializeToString())
  </code></pre>
  
  <p>Now when attempting to deploy to a serving environment (Sagemaker, using a correct directory structure and file naming convention), the system returns</p>
  
  <pre><code>2019-06-04 22:38:53.794056: I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:54] Reading meta graph with tags { serve }
  2019-06-04 22:38:53.798096: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:259] SavedModel load for tags { serve }; Status: fail. Took 83297 microseconds.
  2019-06-04 22:38:53.798132: E tensorflow_serving/util/retrier.cc:37] Loading servable: {name: model version: 1} failed: Not found: Could not find meta graph def matching supplied tags: { serve }. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: `saved_model_cli`
  </code></pre>
  
  <p>All I have are the <code>*.pb</code> files and their label textfiles. These work lovely across multiple computers in local environments. </p>
  
  <pre class="lang-py prettyprint-override"><code>def load_graph(model_file):
      """
      Code from v1.6.0 of Tensorflow's label_image.py example
      """
      graph = tf.Graph()
      graph_def = tf.GraphDef()
      with open(model_file, "rb") as f:
          graph_def.ParseFromString(f.read())
      with graph.as_default():
          tf.import_graph_def(graph_def)
      return graph
  
  inputLayer = "Mul"
  outputLayer = "final_result"
  inputName = "import/" + inputLayer
  outputName = "import/" + outputLayer
  graph = load_graph(modelPath)
  inputOperation = graph.get_operation_by_name(inputName)
  outputOperation = graph.get_operation_by_name(outputName)
  with tf.Session(graph= graph) as sess:
      # ... make a tensor t
      results = sess.run(outputOperation.outputs[0], {
          inputOperation.outputs[0]: t
      })
      # lovely functional results here
  </code></pre>
  
  <p>All I want to do is to take these existing files, add the "serve" tag needed, and re-save them, but everything I see seems to be related to doing this from scratch.</p>
  
  <p>I tried to use the builder to append a graph to a model like so:</p>
  
  <pre class="lang-py prettyprint-override"><code># Load the graph
  graph = load_graph(modelPath)
  import shutil
  if os.path.exists(exportDir):
      shutil.rmtree(exportDir)
  # Add the serving metagraph tag
  builder = tf.saved_model.builder.SavedModelBuilder(exportDir)
  from tensorflow.saved_model import tag_constants
  with tf.Session(graph= graph) as sess:
      builder.add_meta_graph_and_variables(sess, [tag_constants.SERVING, tag_constants.GPU], strip_default_attrs= True)
  builder.save()
  print("Built a SavedModel")
  </code></pre>
  
  <p>but got the same error.</p>