Install TensorFlow Lite 2.1.0 on Raspberry Pi 4
Tensorflow Lite will be installed together with some amazing examples.
Before downloading the TensorFlow Lite libraries, please make sure you have OpenCV 4.3 installed on your Raspberry Pi according to our guide.
Besides OpenCV, you need also Code::Blocks installed. It would be comforting if you have already run our first C++ example, James.mp4, on your Pi.
Install TensorFlow Lite.
To build a very fast deep learning application, we need to install the C++ API libraries. The procedure is very simple. Just clone the GitHub repository and run two scripts. The commands are listed below.
$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
The TensorFlow Lite flat buffers are also needed. Please use the following commands.
$ cd ~/tensorflow/tensorflow/lite/tools/make/downloads/flatbuffers
$ mkdir build
$ cd build
$ cmake ..
$ make -j4
$ sudo make install
$ sudo ldconfig
If everything went well, you should have the two libraries and two folders with header files as shown in the slide show.
Now the libraries are installed, only the TensorFlow Lite models are missing. You cannot run normal TensorFlow models on the Lite software, they must be converted before use. The paragraph at the bottom of this page explains how to do this. Google has some ready-made models available on the net here.
Please note, each model has its unique input and output characteristics that will affect the C++ code used.
Building the TensorFlow Lite app.
To build the application, please follow the next steps. See also our OpenCV examples page where some Code::Block aspects are treated in more detail.
First, download the software from our GitHub page. Please be sure it's the 32-bits Raspbian version, not the 64-bits version for Ubuntu.
Once all the files are placed in a folder (we used ~/software/tensorflow_lite) on your Raspberry Pi, start Code::Blocks and load the TestTensorFlow_Lite.cbp project file. Or simply double click on the TestTensorFlow_Lite.cbp in your File Manager.
- Once the project is loaded, Code::Blocks looks like this. First, select the release mode. We are not going to debug the code now.
- Open the Build option dialog with menu option Project → Build options... Notice the search directories in this dialog. For both Debug and Release there are four directories given. The OpenCV folder (/usr/local/include/opencv4) and the tensor library (/home/pi/tensorflow). Indeed, the one we just created. The two other directories are pointing at the location where some flat buffer software is located.
- In the linker settings, there are references to two libraries who will be statical loaded into the code; libtensorflow-lite.a and libflatbuffers.a. There are two additional options given in the Other linker options list. -pthread enables the POSIX thread engine for TensorFlow. The second flag is the well known OpenCV package load.
- Check the Release mode optimalisation on the Compiler Flags tab; -O3 must be selected.
- Close the dialog, build and run the program.
- The output is a single file located in the Release folder. Before you can run the application here, you need to copy the associated files to this location.
If you want to use a camera, simply alter line 115 in MobileNetV1.cpp and you have a live object detector.
Your app will now run around 13 FPS (Frames Per Second). To squeeze the last bits of speed out of your Raspberry Pi, you have to overclock. This means that the system clock on your board is running at a higher frequency then it normally does. If the Raspberry Pi is running at a higher clock rate, the CPU will get hot, so proper cooling is needed. Please refer to our overclock page for detailed information. We could overclock this application to 2 GHz which gives you a nice 17 FPS.
Python installation of TensorFlow Lite.
For completeness, the Pythons installation of TensorFlow Lite is given here. It is only one command. Python examples can be found everywhere on the net. Google has also made an example here.
$ pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl