In this guide, we will be showing you how to install TensorFlow Lite on the Raspberry Pi.
TensorFlow is an open-source framework developed by Google for machine learning and artificial intelligence. You can use this for various tasks such as classifying an image, detecting the bounding box of objects in an image, or even estimating the pose of people.
TensorFlow Lite is a lightweight version of “
TensorFlow” designed for low-powered devices such as the Raspberry Pi.
You can not use the Lite version of TensorFlow to train models. You can only use it to run pre-trained models that have been made compatible with the “Lite” version.
Models designed for TensorFlow Lite need to be lightweight and less computationally expensive.
To show you how this all works, we will also show you how to use the example “
Image Classification” model with your Pi camera or webcam.
Below is the equipment we used when installing the TensorFlow Lite software onto the Raspberry Pi.
Preparing your Raspberry Pi for TensorFlow
Before you can install TensorFlow, we need to complete some preparation work. Unfortunately, TensorFlow Lite isn’t available through the included repositories. Instead, we must rely on Google’s package repository.
1. Our first step is to perform an update of our Raspberry Pi’s package list and upgrade any existing package on your system.
To perform both of these updates, you will need to run the following two commands within the terminal.
sudo apt update sudo apt upgrade -y
2. Once the update completes, we will need to add the Google package repository containing TensorFlow Lite to our Raspberry Pi.
We can start this process by adding the repository to our sources list by using the following command.
echo "deb [signed-by=/usr/share/keyrings/coral-edgetpu-archive-keyring.gpg] https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list
3. Although we have added the repository, we still need to add its GPG key into our keychains directory.
The package manager will use this key to help ensure that the file did, in fact, come from this repository.
Download and save the GPG key to our keyrings directory by using the following command on your Raspberry Pi.
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo tee /usr/share/keyrings/coral-edgetpu-archive-keyring.gpg >/dev/null
4. Since we modified our Raspberry Pi’s package sources, we need to update our package list to scan the newly added repository.
Perform an update of the package list by using the command below.
sudo apt update
Installing TensorFlow Lite to your Raspberry Pi
Now that we have prepared the Raspberry Pi, we can install the TensorFlow Lite runtime to our Raspberry Pi.
1. To install Tensorflow Lite, all you need to do is run the command below on your device.
This will install the latest TensorFlow Lite runtime from Google’s package repository.
sudo apt install python3-tflite-runtime
2. Now that we have installed the package, we can verify that TensorFlow Lite is now working by importing it.
You can start the Python command-line interface (CLI) on your Raspberry Pi by typing in the command below.
3. Within the Python CLI, it is straightforward to verify that TensorFlow Lite is installed.
All we need to do is use the following line within the interface. All this line is doing is importing the interpreter library.
from tflite_runtime.interpreter import Interpreter
If everything has worked so far, you should see no further messages within the command line. You can now run your TensorFlow Lite models on your Raspberry Pi.
Running a TensorFlow Lite Model on the Raspberry Pi
There are various pre-trained TensorFlow Lite example models on the official TensorFlow website.
You can find examples with guides for the Raspberry Pi by looking for the “
Try it on Raspberry Pi” text.
1. To start this off, we will be cloning the examples directly from the TensorFlow GitHub.
However, to clone the software, we will need to install the “
git” software to our Raspberry Pi.
sudo apt install git
2. With “
git” installed, clone the example repository using the following command.
Using the “
--depth 1” option ensures we don’t clone any other repositories referenced by the one we are cloning.
git clone https://github.com/tensorflow/examples --depth 1
3. We can now change into the “
image_classification” example directory.
This is where the Python script sits as well as a setup script that will download the model that we need.
4. To run this script, we need to modify the file’s permissions to give us execute privileges.
Use the chmod command below in your Raspberry Pi’s terminal.
sudo chmod +x setup.sh
5. We can run the included setup script with everything now ready.
Using this script will install any dependencies required by Python and download the pre-trained TensorFlow model.
6. Now, run the image classifier using the following command on your Raspberry Pi.
Upon running the script, it will open a window on your device. You will see a video feed from your camera displayed here.
In the top-left of the window, you will see some text. It offers three guesses on what that image could be. Each guess has a probability associated with it. All probability values will add up to “
1” in total.
If you see an even spread across these values, it indicates that the model can’t confidently recognize the image.
At this point, you should now have successfully installed TensorFlow Lite to your Raspberry Pi.
During this tutorial, you will also have had a chance to test a pre-trained image classification model.
If you have run into any issues with getting TensorFlow Lite installed and running on your device.