code: def get_activations(
      model,
      x,
      layer_names=None,
      nodes_to_evaluate=None,
      output_format="simple",
      nested=False,
      auto_compile=True,
  ):
      """
      Fetch activations (nodes/layers outputs as Numpy arrays) for a Keras model and an input X.
      By default, all the activations for all the layers are returned.
      :param model: Keras compiled model or one of ['vgg16', 'vgg19', 'inception_v3', 'inception_resnet_v2',
      'mobilenet_v2', 'mobilenetv2', ...].
      :param x: Model input (Numpy array). In the case of multi-inputs, x should be of type List.
      :param layer_names: (optional) Single name of a layer or list of layer names for which activations should be
      returned. It is useful in very big networks when it is computationally expensive to evaluate all the layers/nodes.
      :param nodes_to_evaluate: (optional) List of Keras nodes to be evaluated. Useful when the nodes are not
      in model.layers.
      :param output_format: Change the output dictionary key of the function.
      - 'simple': output key will match the names of the Keras layers. For example Dense(1, name='d1') will
      return {'d1': ...}.
      - 'full': output key will match the full name of the output layer name. In the example above, it will
      return {'d1/BiasAdd:0': ...}.
      - 'numbered': output key will be an index range, based on the order of definition of each layer within the model.
      :param nested: If specified, will move recursively through the model definition to retrieve nested layers.
                  Recursion ends at leaf layers of the model tree or at layers with their name specified in layer_names.
  
                  E.g., a model with the following structure
  
                  -layer1
                      -conv1
                      ...
                      -fc1
                  -layer2
                      -fc2
  
                  ... yields a dictionary with keys 'layer1/conv1', ..., 'layer1/fc1', 'layer2/fc2'.
                  If layer_names = ['layer2/fc2'] is specified, the dictionary will only hold one key 'layer2/fc2'.
  
                  The layer names are generated by joining all layers from top level to leaf level with the separator '/'.
      :param auto_compile: If set to True, will auto-compile the model if needed.
      :return: Dict {layer_name (specified by output_format) -> activation of the layer output/node (Numpy array)}.
      """
      layer_names = (
          [layer_names] if isinstance(layer_names, str) else layer_names
      )
      # print('Layer names:', layer_names)
      nodes = OrderedDict()
      if nodes_to_evaluate is None:
          _get_nodes(model, nodes, output_format, nested, layer_names)
      else:
          if layer_names is not None:
              raise ValueError(
                  "Do not specify a [layer_name] with [nodes_to_evaluate]. It will not be used."
              )
          nodes = OrderedDict(
              [(n_(node, "full"), node) for node in nodes_to_evaluate]
          )
  
      if len(nodes) == 0:
          if layer_names is not None:
              network_layers = ", ".join(
                  [layer.name for layer in model.layers]
              )
              raise KeyError(
                  "Could not find a layer with name: [{}]. "
                  "Network layers are [{}]".format(
                      ", ".join(layer_names), network_layers
                  )
              )
          else:
              raise ValueError(
                  "Nodes list is empty. Or maybe the model is empty."
              )
  
      # The placeholders are processed later (Inputs node in Keras). Due to a small bug in tensorflow.
      input_layer_outputs = []
      layer_outputs = OrderedDict()
  
      for key, node in nodes.items():
          if isinstance(node, list):
              for nod in node:
                  if not is_placeholder(nod):
                      if key not in layer_outputs:
                          layer_outputs[key] = []
                      layer_outputs[key].append(nod)
          else:
              if not is_placeholder(node):
                  layer_outputs.update({key: node})
      if (
          nodes_to_evaluate is None
          or (layer_names is not None)
          and any([n.name in layer_names for n in model.inputs])
      ):
          input_layer_outputs = list(model.inputs)
  
      if len(layer_outputs) > 0:
          activations = _evaluate(
              model,
              layer_outputs.values(),
              x,
              y=None,
              auto_compile=auto_compile,
          )
      else:
          activations = {}
  
      def craft_output(output_format_):
          inputs = [x] if not isinstance(x, list) else x
          activations_inputs_dict = OrderedDict(
              zip(
                  [
                      n_(output, output_format_)
                      for output in input_layer_outputs
                  ],
                  inputs,
              )
          )
          activations_dict = OrderedDict(
              zip(layer_outputs.keys(), activations)
          )
          result_ = activations_inputs_dict.copy()
          result_.update(activations_dict)
  
          if output_format_ == "numbered":
              result_ = OrderedDict(
                  [(i, v) for i, (k, v) in enumerate(result_.items())]
              )
          return result_
  
      result = craft_output(output_format)
      if layer_names is not None:  # extra check.
          result = {k: v for k, v in result.items() if k in layer_names}
      if nodes_to_evaluate is not None and len(result) != len(
          nodes_to_evaluate
      ):
          result = craft_output(
              output_format_="full"
          )  # collision detected in the keys.
  
      return result
  
imports: import json
  import os
  from collections import OrderedDict
  import numpy
  import tensorflow
  import tensorflow.keras.backend
  from tensorflow.keras import Sequential
  from tensorflow.keras.models import Model
label_desc: Use image node_id 9 for calling a global function with example usage: get_activations(model, x, layer_names, nodes_to_evaluate, output_format, nested, auto_compile) and returns: result, result_