code: def get_metagraph():
      """Constructs and returns a MetaGraphDef from the input file."""
      with gfile.GFile(FLAGS.input) as input_file:
          input_data = input_file.read()
          try:
              saved_model = saved_model_pb2.SavedModel()
              text_format.Merge(input_data, saved_model)
              meta_graph = saved_model.meta_graphs[0]
          except text_format.ParseError:
              try:
                  saved_model.ParseFromString(input_data)
                  meta_graph = saved_model.meta_graphs[0]
              except message.DecodeError:
                  try:
                      meta_graph = meta_graph_pb2.MetaGraphDef()
                      text_format.Merge(input_data, meta_graph)
                  except text_format.ParseError:
                      try:
                          meta_graph.ParseFromString(input_data)
                      except message.DecodeError:
                          try:
                              graph_def = graph_pb2.GraphDef()
                              text_format.Merge(input_data, graph_def)
                          except text_format.ParseError:
                              try:
                                  graph_def.ParseFromString(input_data)
                              except message.DecodeError:
                                  raise ValueError(
                                      f"Invalid input file: {FLAGS.input}."
                                  )
                          importer.import_graph_def(graph_def, name="")
                          graph = ops.get_default_graph()
                          meta_graph = saver.export_meta_graph(
                              graph_def=graph.as_graph_def(),
                              graph=graph,
                          )
      if FLAGS.fetch is not None:
          fetch_collection = meta_graph_pb2.CollectionDef()
          for fetch in FLAGS.fetch.split(","):
              fetch_collection.node_list.value.append(fetch)
          meta_graph.collection_def["train_op"].CopyFrom(
              fetch_collection
          )
      return meta_graph
  
file_path: tensorflow/tensorflow/python/grappler/cost_analyzer_tool.py
imports: import argparse
  import sys
  from absl import app
  from google.protobuf import message
  from google.protobuf import text_format
  from tensorflow.core.framework import graph_pb2
  from tensorflow.core.protobuf import config_pb2
  from tensorflow.core.protobuf import meta_graph_pb2
  from tensorflow.core.protobuf import saved_model_pb2
  from tensorflow.python.framework import importer
  from tensorflow.python.framework import ops
  from tensorflow.python.grappler import cost_analyzer
  from tensorflow.python.grappler import tf_optimizer
  from tensorflow.python.platform import gfile
  from tensorflow.python.training import saver
label_desc: Use image node_id 1 for calling a global function with example usage: get_metagraph() and returns: meta_graph