• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Python quickstart
2
3Using TensorFlow Lite with Python is great for embedded devices based on Linux,
4such as [Raspberry Pi](https://www.raspberrypi.org/){:.external} and
5[Coral devices with Edge TPU](https://coral.withgoogle.com/){:.external},
6among many others.
7
8This page shows how you can start running TensorFlow Lite models with Python in
9just a few minutes. All you need is a TensorFlow model [converted to TensorFlow
10Lite](../convert/). (If you don't have a model converted yet, you can experiment
11using the model provided with the example linked below.)
12
13## About the TensorFlow Lite runtime package
14
15To quickly start executing TensorFlow Lite models with Python, you can install
16just the TensorFlow Lite interpreter, instead of all TensorFlow packages. We
17call this simplified Python package `tflite_runtime`.
18
19The `tflite_runtime` package is a fraction the size of the full `tensorflow`
20package and includes the bare minimum code required to run inferences with
21TensorFlow Lite—primarily the
22[`Interpreter`](https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter)
23Python class. This small package is ideal when all you want to do is execute
24`.tflite` models and avoid wasting disk space with the large TensorFlow library.
25
26Note: If you need access to other Python APIs, such as the
27[TensorFlow Lite Converter](../convert/), you must install the
28[full TensorFlow package](https://www.tensorflow.org/install/).
29
30## Install TensorFlow Lite for Python
31
32To install the TensorFlow Lite runtime package, run this command:
33
34<pre class="devsite-terminal devsite-click-to-copy">
35pip3 install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtime
36</pre>
37
38If you're on a Raspberry Pi, this command might fail due to a known issue with
39the `extra-index-url` option
40([#4011](https://github.com/raspberrypi/linux/issues/4011)). So we suggest you
41specify one of the
42[`tflite_runtime` wheels](https://github.com/google-coral/pycoral/releases/)
43that matches your system. For example, if you're running Raspberry Pi OS 10
44(which has Python 3.7), instead use this command:
45
46<pre class="devsite-terminal devsite-click-to-copy">
47pip3 install https://github.com/google-coral/pycoral/releases/download/release-frogfish/tflite_runtime-2.5.0-cp37-cp37m-linux_armv7l.whl
48</pre>
49
50Note: If you're on Debian Linux and using TensorFlow Lite with a Coral ML
51accelerator, using pip to install `tflite_runtime` may not be compatible with
52other Coral libraries. To ensure all your libraries are compatible, instead
53install `tflite_runtime` as a
54[Debian package from Coral](https://coral.ai/software/#debian-packages).
55
56## Run an inference using tflite_runtime
57
58Instead of importing `Interpreter` from the `tensorflow` module, you now need to
59import it from `tflite_runtime`.
60
61For example, after you install the package above, copy and run the
62[`label_image.py`](
63https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/python/)
64file. It will (probably) fail because you don't have the `tensorflow` library
65installed. To fix it, edit this line of the file:
66
67```python
68import tensorflow as tf
69```
70
71So it instead reads:
72
73```python
74import tflite_runtime.interpreter as tflite
75```
76
77And then change this line:
78
79```python
80interpreter = tf.lite.Interpreter(model_path=args.model_file)
81```
82
83So it reads:
84
85```python
86interpreter = tflite.Interpreter(model_path=args.model_file)
87```
88
89Now run `label_image.py` again. That's it! You're now executing TensorFlow Lite
90models.
91
92## Learn more
93
94For more details about the `Interpreter` API, read
95[Load and run a model in Python](inference.md#load-and-run-a-model-in-python).
96
97If you have a Raspberry Pi, try the
98[classify_picamera.py example](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/raspberry_pi)
99to perform image classification with the Pi Camera and TensorFlow Lite.
100
101If you're using a Coral ML accelerator, check out the
102[Coral examples on GitHub](https://github.com/google-coral/tflite/tree/master/python/examples).
103
104To convert other TensorFlow models to TensorFlow Lite, read about the
105the [TensorFlow Lite Converter](../convert/).
106