# tf.lite.Interpreter

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/lite/Interpreter) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L173-L456) |

Interpreter interface for TensorFlow Lite Models.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.lite.Interpreter`](https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter)

<br />

    tf.lite.Interpreter(
        model_path=None, model_content=None, experimental_delegates=None
    )

This makes the TensorFlow Lite interpreter accessible in Python.
It is possible to use this interpreter in a multithreaded Python environment,
but you must be sure to call functions of a particular instance from only
one thread at a time. So if you want to have 4 threads running different
inferences simultaneously, create an interpreter for each one as thread-local
data. Similarly, if you are calling invoke() in one thread on a single
interpreter but you want to use tensor() on another thread once it is done,
you must use a synchronization primitive between the threads to ensure invoke
has returned before calling tensor().

<br />

<br />

<br />

| ## 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(). |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If the interpreter was unable to create. |

<br />

## Methods

### `allocate_tensors`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L242-L244)

    allocate_tensors()

### `get_input_details`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L322-L330)

    get_input_details()

Gets model input details.

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of input details. ||

<br />

### `get_output_details`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L365-L373)

    get_output_details()

Gets model output details.

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of output details. ||

<br />

### `get_tensor`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L375-L388)

    get_tensor(
        tensor_index
    )

Gets the value of the input tensor (get a copy).

If you wish to avoid the copy, use `tensor()`. This function cannot be used
to read intermediate results.

<br />

<br />

<br />

| Args ||
|---|---|
| `tensor_index` | Tensor index of tensor to get. This value can be gotten from the 'index' field in get_output_details. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| a numpy array. ||

<br />

### `get_tensor_details`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L305-L320)

    get_tensor_details()

Gets tensor details for every tensor with valid tensor details.

Tensors where required information about the tensor is not found are not
added to the list. This includes temporary tensors without a name.

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of dictionaries containing tensor information. ||

<br />

### `invoke`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L440-L453)

    invoke()

Invoke the interpreter.

Be sure to set the input sizes, allocate tensors and fill values before
calling this. Also, note that this function releases the GIL so heavy
computation can be done in the background while the Python interpreter
continues. No other function on this object should be called while the
invoke() call has not finished.

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | When the underlying interpreter fails raise ValueError. |

<br />

### `reset_all_variables`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L455-L456)

    reset_all_variables()

### `resize_tensor_input`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L348-L363)

    resize_tensor_input(
        input_index, tensor_size
    )

Resizes an input tensor.

<br />

<br />

<br />

| Args ||
|---|---|
| `input_index` | Tensor index of input to set. This value can be gotten from the 'index' field in get_input_details. |
| `tensor_size` | The tensor_shape to resize the input to. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If the interpreter could not resize the input tensor. |

<br />

### `set_tensor`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L332-L346)

    set_tensor(
        tensor_index, value
    )

Sets the value of the input tensor. Note this copies data in `value`.

If you want to avoid copying, you can use the `tensor()` function to get a
numpy buffer pointing to the input buffer in the tflite interpreter.

<br />

<br />

<br />

| Args ||
|---|---|
| `tensor_index` | Tensor index of tensor to set. This value can be gotten from the 'index' field in get_input_details. |
| `value` | Value of tensor to set. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If the interpreter could not set the tensor. |

<br />

### `tensor`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/lite/python/interpreter.py#L390-L438)

    tensor(
        tensor_index
    )

Returns function that gives a numpy view of the current tensor buffer.

This allows reading and writing to this tensors w/o copies. This more
closely mirrors the C++ Interpreter class interface's tensor() member, hence
the name. Be careful to not hold these output references through calls
to `allocate_tensors()` and `invoke()`. This function cannot be used to read
intermediate results.

#### Usage:

    interpreter.allocate_tensors()
    input = interpreter.tensor(interpreter.get_input_details()[0]["index"])
    output = interpreter.tensor(interpreter.get_output_details()[0]["index"])
    for i in range(10):
      input().fill(3.)
      interpreter.invoke()
      print("inference %s" % output())

Notice how this function avoids making a numpy array directly. This is
because it is important to not hold actual numpy views to the data longer
than necessary. If you do, then the interpreter can no longer be invoked,
because it is possible the interpreter would resize and invalidate the
referenced tensors. The NumPy API doesn't allow any mutability of the
the underlying buffers.

#### WRONG:

    input = interpreter.tensor(interpreter.get_input_details()[0]["index"])()
    output = interpreter.tensor(interpreter.get_output_details()[0]["index"])()
    interpreter.allocate_tensors()  # This will throw RuntimeError
    for i in range(10):
      input.fill(3.)
      interpreter.invoke()  # this will throw RuntimeError since input,output

<br />

<br />

<br />

| Args ||
|---|---|
| `tensor_index` | Tensor index of tensor to get. This value can be gotten from the 'index' field in get_output_details. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A function that can return a new numpy array pointing to the internal TFLite tensor state at any point. It is safe to hold the function forever, but it is not safe to hold the numpy array forever. ||

<br />