code: def keras_model_to_graph_def(keras_layer):
      """Returns a GraphDef representation of the Keras model in a dict form.
  
      Note that it only supports models that implemented to_json().
  
      Args:
        keras_layer: A dict from Keras model.to_json().
  
      Returns:
        A GraphDef representation of the layers in the model.
      """
      input_to_layer = {}
      model_name_to_output = {}
      g = GraphDef()
  
      # Sequential model layers do not have a field "inbound_nodes" but
      # instead are defined implicitly via order of layers.
      prev_node_name = None
  
      for name_scope, layer in _walk_layers(keras_layer):
          if _is_model(layer):
              (
                  input_to_layer,
                  model_name_to_output,
                  prev_node_name,
              ) = _update_dicts(
                  name_scope,
                  layer,
                  input_to_layer,
                  model_name_to_output,
                  prev_node_name,
              )
              continue
  
          layer_config = layer.get("config")
          node_name = _scoped_name(name_scope, layer_config.get("name"))
  
          node_def = g.node.add()
          node_def.name = node_name
  
          if layer.get("class_name") is not None:
              keras_cls_name = layer.get("class_name").encode("ascii")
              node_def.attr["keras_class"].s = keras_cls_name
  
          dtype_or_policy = layer_config.get("dtype")
          # Skip dtype processing if this is a dict, since it's presumably a instance of
          # tf/keras/mixed_precision/Policy rather than a single dtype.
          # TODO(#5548): parse the policy dict and populate the dtype attr with the variable dtype.
          if dtype_or_policy is not None and not isinstance(
              dtype_or_policy, dict
          ):
              tf_dtype = dtypes.as_dtype(layer_config.get("dtype"))
              node_def.attr["dtype"].type = tf_dtype.as_datatype_enum
          if layer.get("inbound_nodes") is not None:
              for maybe_inbound_node in layer.get("inbound_nodes"):
                  inbound_nodes = _norm_to_list_of_layers(
                      maybe_inbound_node
                  )
                  for [name, size, index, _] in inbound_nodes:
                      inbound_name = _scoped_name(name_scope, name)
                      # An input to a layer can be output from a model. In that case, the name
                      # of inbound_nodes to a layer is a name of a model. Remap the name of the
                      # model to output layer of the model. Also, since there can be multiple
                      # outputs in a model, make sure we pick the right output_layer from the model.
                      inbound_node_names = model_name_to_output.get(
                          inbound_name, [inbound_name]
                      )
                      # There can be multiple inbound_nodes that reference the
                      # same upstream layer. This causes issues when looking for
                      # a particular index in that layer, since the indices
                      # captured in `inbound_nodes` doesn't necessarily match the
                      # number of entries in the `inbound_node_names` list. To
                      # avoid IndexErrors, we just use the last element in the
                      # `inbound_node_names` in this situation.
                      # Note that this is a quick hack to avoid IndexErrors in
                      # this situation, and might not be an appropriate solution
                      # to this problem in general.
                      input_name = (
                          inbound_node_names[index]
                          if index < len(inbound_node_names)
                          else inbound_node_names[-1]
                      )
                      node_def.input.append(input_name)
          elif prev_node_name is not None:
              node_def.input.append(prev_node_name)
  
          if node_name in input_to_layer:
              node_def.input.append(input_to_layer.get(node_name))
  
          prev_node_name = node_def.name
  
      return g
  
file_path: tensorboard/tensorboard/plugins/graph/keras_util.py
imports: from tensorboard.compat.proto.graph_pb2 import GraphDef
  from tensorboard.compat.tensorflow_stub import dtypes
label_desc: Use image node_id 6 for calling a global function with example usage: keras_model_to_graph_def(keras_layer) and returns: g