class_docstr: Interpreter interface for running TensorFlow Lite models.
  
  Models obtained from `TfLiteConverter` can be run in Python with
  `Interpreter`.
  
  As an example, lets generate a simple Keras model and convert it to TFLite
  (`TfLiteConverter` also supports other input formats with `from_saved_model`
  and `from_concrete_function`)
  
  >>> x = np.array([[1.], [2.]])
  >>> y = np.array([[2.], [4.]])
  >>> model = tf.keras.models.Sequential([
  ...           tf.keras.layers.Dropout(0.2),
  ...           tf.keras.layers.Dense(units=1, input_shape=[1])
  ...         ])
  >>> model.compile(optimizer='sgd', loss='mean_squared_error')
  >>> model.fit(x, y, epochs=1)
  >>> converter = tf.lite.TFLiteConverter.from_keras_model(model)
  >>> tflite_model = converter.convert()
  
  `tflite_model` can be saved to a file and loaded later, or directly into the
  `Interpreter`. Since TensorFlow Lite pre-plans tensor allocations to optimize
  inference, the user needs to call `allocate_tensors()` before any inference.
  
  >>> interpreter = tf.lite.Interpreter(model_content=tflite_model)
  >>> interpreter.allocate_tensors()  # Needed before execution!
  
  Sample execution:
  
  >>> output = interpreter.get_output_details()[0]  # Model has single output.
  >>> input = interpreter.get_input_details()[0]  # Model has single input.
  >>> input_data = tf.constant(1., shape=[1, 1])
  >>> interpreter.set_tensor(input['index'], input_data)
  >>> interpreter.invoke()
  >>> interpreter.get_tensor(output['index']).shape
  (1, 1)
  
  Use `get_signature_runner()` for a more user-friendly inference API.
class_docstr_tok: ["Interpreter","interface","for","running","TensorFlow","Lite","models",".","Models","obtained","from","`","TfLiteConverter","`","can","be","run","in","Python","with","`","Interpreter","`",".","As","an","example",",","lets","generate","a","simple","Keras","model","and","convert","it","to","TFLite","(","`","TfLiteConverter","`","also","supports","other","input","formats","with","`","from_saved_model","`","and","`","from_concrete_function","`",")",">",">",">","x","=","np.array","(","[","[","1",".","]",",","[","2",".","]","]",")",">",">",">","y","=","np.array","(","[","[","2",".","]",",","[","4",".","]","]",")",">",">",">","model","=","tf.keras.models.Sequential","(","[","...","tf.keras.layers.Dropout","(","0.2",")",",","...","tf.keras.layers.Dense","(","units=1",",","input_shape=","[","1","]",")","...","]",")",">",">",">","model.compile","(","optimizer='sgd","'",",","loss='mean_squared_error","'",")",">",">",">","model.fit","(","x",",","y",",","epochs=1",")",">",">",">","converter","=","tf.lite.TFLiteConverter.from_keras_model","(","model",")",">",">",">","tflite_model","=","converter.convert","(",")","`","tflite_model","`","can","be","saved","to","a","file","and","loaded","later",",","or","directly","into","the","`","Interpreter","`",".","Since","TensorFlow","Lite","pre-plans","tensor","allocations","to","optimize","inference",",","the","user","needs","to","call","`","allocate_tensors","(",")","`","before","any","inference",".",">",">",">","interpreter","=","tf.lite.Interpreter","(","model_content=tflite_model",")",">",">",">","interpreter.allocate_tensors","(",")","#","Needed","before","execution","!","Sample","execution",":",">",">",">","output","=","interpreter.get_output_details","(",")","[","0","]","#","Model","has","single","output",".",">",">",">","input","=","interpreter.get_input_details","(",")","[","0","]","#","Model","has","single","input",".",">",">",">","input_data","=","tf.constant","(","1.",",","shape=","[","1",",","1","]",")",">",">",">","interpreter.set_tensor","(","input","[","'index","'","]",",","input_data",")",">",">",">","interpreter.invoke","(",")",">",">",">","interpreter.get_tensor","(","output","[","'index","'","]",")",".shape","(","1",",","1",")","Use","`","get_signature_runner","(",")","`","for","a","more","user-friendly","inference","API","."]
docstr: Constructor.
  
  Args:
    model_path: Path to TF-Lite Flatbuffer file.
    model_content: Content of model.
    experimental_delegates: Experimental. Subject to change. List of
      [TfLiteDelegate](https://www.tensorflow.org/lite/performance/delegates)
      objects returned by lite.load_delegate().
    num_threads: Sets the number of threads used by the interpreter and
      available to CPU kernels. If not set, the interpreter will use an
      implementation-dependent default number of threads. Currently, only a
      subset of kernels, such as conv, support multi-threading. num_threads
      should be >= -1. Setting num_threads to 0 has the effect to disable
      multithreading, which is equivalent to setting num_threads to 1. If set
      to the value -1, the number of threads used will be
      implementation-defined and platform-dependent.
    experimental_op_resolver_type: The op resolver used by the interpreter. It
      must be an instance of OpResolverType. By default, we use the built-in
      op resolver which corresponds to tflite::ops::builtin::BuiltinOpResolver
      in C++.
    experimental_preserve_all_tensors: If true, then intermediate tensors used
      during computation are preserved for inspection, and if the passed op
      resolver type is AUTO or BUILTIN, the type will be changed to
      BUILTIN_WITHOUT_DEFAULT_DELEGATES so that no Tensorflow Lite default
      delegates are applied. If false, getting intermediate tensors could
      result in undefined values or None, especially when the graph is
      successfully modified by the Tensorflow Lite default delegate.
    experimental_disable_delegate_clustering: If true, don't perform delegate
      clustering during delegate graph partitioning phase. Disabling delegate
      clustering will make the execution order of ops respect the
      explicitly-inserted control dependencies in the graph (inserted via
      `with tf.control_dependencies()`) since the TF Lite converter will drop
      control dependencies by default. Most users shouldn't turn this flag to
      True if they don't insert explicit control dependencies or the graph
      execution order is expected. For automatically inserted control
      dependencies (with `tf.Variable`, `tf.Print` etc), the user doesn't need
      to turn this flag to True since they are respected by default. Note that
      this flag is currently experimental, and it might be removed/updated if
      the TF Lite converter doesn't drop such control dependencies in the
      model. Default is False.
  
  Raises:
    ValueError: If the interpreter was unable to create.
code: def __init__(
      self,
      model_path=None,
      model_content=None,
      experimental_delegates=None,
      num_threads=None,
      experimental_op_resolver_type=OpResolverType.AUTO,
      experimental_preserve_all_tensors=False,
      experimental_disable_delegate_clustering=False,
  ):
      """Constructor.
  
      Args:
        model_path: Path to TF-Lite Flatbuffer file.
        model_content: Content of model.
        experimental_delegates: Experimental. Subject to change. List of
          [TfLiteDelegate](https://www.tensorflow.org/lite/performance/delegates)
          objects returned by lite.load_delegate().
        num_threads: Sets the number of threads used by the interpreter and
          available to CPU kernels. If not set, the interpreter will use an
          implementation-dependent default number of threads. Currently, only a
          subset of kernels, such as conv, support multi-threading. num_threads
          should be >= -1. Setting num_threads to 0 has the effect to disable
          multithreading, which is equivalent to setting num_threads to 1. If set
          to the value -1, the number of threads used will be
          implementation-defined and platform-dependent.
        experimental_op_resolver_type: The op resolver used by the interpreter. It
          must be an instance of OpResolverType. By default, we use the built-in
          op resolver which corresponds to tflite::ops::builtin::BuiltinOpResolver
          in C++.
        experimental_preserve_all_tensors: If true, then intermediate tensors used
          during computation are preserved for inspection, and if the passed op
          resolver type is AUTO or BUILTIN, the type will be changed to
          BUILTIN_WITHOUT_DEFAULT_DELEGATES so that no Tensorflow Lite default
          delegates are applied. If false, getting intermediate tensors could
          result in undefined values or None, especially when the graph is
          successfully modified by the Tensorflow Lite default delegate.
        experimental_disable_delegate_clustering: If true, don't perform delegate
          clustering during delegate graph partitioning phase. Disabling delegate
          clustering will make the execution order of ops respect the
          explicitly-inserted control dependencies in the graph (inserted via
          `with tf.control_dependencies()`) since the TF Lite converter will drop
          control dependencies by default. Most users shouldn't turn this flag to
          True if they don't insert explicit control dependencies or the graph
          execution order is expected. For automatically inserted control
          dependencies (with `tf.Variable`, `tf.Print` etc), the user doesn't need
          to turn this flag to True since they are respected by default. Note that
          this flag is currently experimental, and it might be removed/updated if
          the TF Lite converter doesn't drop such control dependencies in the
          model. Default is False.
  
      Raises:
        ValueError: If the interpreter was unable to create.
      """
      if not hasattr(self, "_custom_op_registerers"):
          self._custom_op_registerers = []
  
      actual_resolver_type = experimental_op_resolver_type
      if experimental_preserve_all_tensors and (
          experimental_op_resolver_type == OpResolverType.AUTO
          or experimental_op_resolver_type == OpResolverType.BUILTIN
      ):
          actual_resolver_type = (
              OpResolverType.BUILTIN_WITHOUT_DEFAULT_DELEGATES
          )
      op_resolver_id = _get_op_resolver_id(actual_resolver_type)
      if op_resolver_id is None:
          raise ValueError(
              "Unrecognized passed in op resolver type: {}".format(
                  experimental_op_resolver_type
              )
          )
  
      if model_path and not model_content:
          custom_op_registerers_by_name = [
              x
              for x in self._custom_op_registerers
              if isinstance(x, str)
          ]
          custom_op_registerers_by_func = [
              x
              for x in self._custom_op_registerers
              if not isinstance(x, str)
          ]
          self._interpreter = (
              _interpreter_wrapper.CreateWrapperFromFile(
                  model_path,
                  op_resolver_id,
                  custom_op_registerers_by_name,
                  custom_op_registerers_by_func,
                  experimental_preserve_all_tensors,
                  experimental_disable_delegate_clustering,
              )
          )
          if not self._interpreter:
              raise ValueError("Failed to open {}".format(model_path))
      elif model_content and not model_path:
          custom_op_registerers_by_name = [
              x
              for x in self._custom_op_registerers
              if isinstance(x, str)
          ]
          custom_op_registerers_by_func = [
              x
              for x in self._custom_op_registerers
              if not isinstance(x, str)
          ]
          # Take a reference, so the pointer remains valid.
          # Since python strings are immutable then PyString_XX functions
          # will always return the same pointer.
          self._model_content = model_content
          self._interpreter = (
              _interpreter_wrapper.CreateWrapperFromBuffer(
                  model_content,
                  op_resolver_id,
                  custom_op_registerers_by_name,
                  custom_op_registerers_by_func,
                  experimental_preserve_all_tensors,
                  experimental_disable_delegate_clustering,
              )
          )
      elif not model_content and not model_path:
          raise ValueError(
              "`model_path` or `model_content` must be specified."
          )
      else:
          raise ValueError(
              "Can't both provide `model_path` and `model_content`"
          )
  
      if num_threads is not None:
          if not isinstance(num_threads, int):
              raise ValueError("type of num_threads should be int")
          if num_threads < 1:
              raise ValueError("num_threads should >= 1")
          self._interpreter.SetNumThreads(num_threads)
  
      # Each delegate is a wrapper that owns the delegates that have been loaded
      # as plugins. The interpreter wrapper will be using them, but we need to
      # hold them in a list so that the lifetime is preserved at least as long as
      # the interpreter wrapper.
      self._delegates = []
      if experimental_delegates:
          self._delegates = experimental_delegates
          for delegate in self._delegates:
              self._interpreter.ModifyGraphWithDelegate(
                  delegate._get_native_delegate_pointer()
              )  # pylint: disable=protected-access
      self._signature_defs = self.get_signature_list()
  
      self._metrics = metrics.TFLiteMetrics()
      self._metrics.increase_counter_interpreter_creation()
  
imports: import ctypes
  import enum
  import os
  import platform
  import sys
  import numpy
label_desc: Use image node_id 1 to create a new Interpreter object  with example: obj = Interpreter(model_path, model_content, experimental_delegates, num_threads, experimental_op_resolver_type, experimental_preserve_all_tensors, experimental_disable_delegate_clustering)