Install TensorFlow Lite 2 on Raspberry Pi 4
Last updated: December 13, 2021
TensorFlow Lite will be installed on your Raspberry Pi 4 with a 32-bit operating system, along with some examples. TensorFlow evolves over time. Models generated in an older version of TensorFlow may have compatibility issues with a newer version of TensorFlow Lite. Or vice versa. This manual describes the latest version of TensorFlow Lite. You can always install an older version by changing the version number in the download command. For example, $ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.2.1.zip, will download version 2.2.1 on your Raspberry Pi.
To compile Tensorflow Lite, you need enough memory onboard. Make sure you have at least a 1.5 GByte of memory available. In the case of the Raspberry Pi Zero (2), with its 500 MByte RAM, you need to expand the swap space to a comforting 1024 MByte. Please follow the instructions given on this page on how to do so.
If you want to run the TensorFlow Lite examples we provide, please make sure you have OpenCV 4.5 installed on your Raspberry Pi according to our guide.
Besides OpenCV, you also needed 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 copy the GitHub repository and run two scripts. The commands are listed below.
When everything is done, you end up with a screen like this.
The TensorFlow Lite flat buffers are also needed. If you have the latest Bullseye OS running on your Raspberry Pi, you will get errors during the compilation of the flatbuffers. It has to do with the gcc version 10.2.1 giving some warnings now the 'error' status. The flatbuffers team has already fixed these warnings. However, TensorFlow still uses the 'old' version of the flatbuffers.
Please use the following commands.
If everything went well, you should have the two libraries and two folders with header files as shown in the slide show.
As of version 2.3.0, Tensorflow Lite uses dynamic linking. At runtime libraries are copied to RAM and pointers are relocated before TF Lite can run. This strategy gives greater flexibility. It all means that TensorFlow Lite now requires glibc 2.28 or higher to run. From now on, link the libdl library when building your application, otherwise, you get undefined reference to symbol dlsym@@GLIBC_2.17 linker errors. The symbolic link can be found at /lib/aarch64-linux-gnu/libdl.so.2 on a 64-bit Linux OS, or /lib/arm-linux-gnueabihf/libdl.so.2 on a Raspberry Pi 32-bit OS. Please see our examples on GitHub.
Install TensorFlow Lite on Raspberry Pi Zero.
The installation above includes all Raspberry Pi with an ARMv7l chip (RPi 2, RPi 3) or an ARMv8-a (RPi 4). However, the Raspberry Pi Zero ships with an ARMv6. It is possible to install TensorFlow on the Raspberry Pi Zero. Just replace the definition TARGET:=armv7l with TAGRET:=armv6 in the file rpi_makefile.inc. Then follow the same steps as above as if you were dealing with a Raspberry Pi 4.
The compilation will take approximately 4½ hours. Remember that with only one core, clocked at 1GHz and no NEON registers, you are entering the zen zone. On our GitHub page you will find an example of TF-Lite running on a Raspberry Pi Zero, which classifies objects.
The most well known is, of course, the classifications of objects. Google hosts a wide range of TensorFlow Lite models, the so-called quantized models in their zoo. The models are capable of detecting 1000 different objects. All models are trained with square images. Therefore, the best results are given when your input image is also square-like. All models are supported on GitHub by our C ++ software samples for both the 32-bit and 64-bit Raspberry and Ubuntu 18.04 or 20.04 operating system.
This neural network tries to estimate a person pose in a scene. It recognizes certain key features like elbows, knees, ankles in an image. TensorFlow Lite supports two models, a single person and a multi-person version. We have only used the single person model because it gives reasonable good results when the person is centred and in full view in a square-like image. Please, find the 32-bit Raspbain C++ example at our GitHub page.
With semantic image segmentation, a neural network attempts to associate every pixel in the scene with a particular object. You could say, it tries to detect the outline of objects. Tensorflow Lite has one segmentation model capable of classifying 20 different objects. Keep in mind that only reasonable sized objects can be recognized, not a scene of a highway with lots of tiny cars. The C++ examples can be found here for 32-bit.
Here, some frame rates are given of the several TensorFlow Lite models tested on a bare Raspberry Pi 4. The overclock frequencies are indications. Ubuntu always crashes above 1950 MHz when running deep learning models with the 4 cores simultaneous. Some models could run at 1950 MHz, others not higher than 1825 MHz. The 32-bit Raspbian is capable of a clock rate of 1950 MHz for all the examples. Frame rates are only based on model run time (interpreter->Invoke()). Grabbing and preprocessing of a image are not taken into account. Also noteworthy is the higher frame rate on a Raspbian for the MobileNet models compared to Ubuntu. The guide to installing Ubuntu along with OpenCV and TensorFlow Lite can be found here. Overclocking is covered here. This is the guide to install the Raspberry Pi 64-bit operating system. Installing OpenCV 4.3.0 on the 64-bit Raspberry OS is explained here.
Raspberry Pi 4
64 bit OS
Raspberry Pi 4
64 bit OS
|Raspberry Pi 4 64 bit Ubuntu 1850 MHz|
Raspberry Pi 4
64 bit Ubuntu
Raspberry Pi 4
32 bit OS
|Raspberry Pi 4|
32 bit OS
|21.5 FPS||24.0 FPS||17.2 FPS||17.0 FPS||13.8 FPS|
|38.5 FPS||32.2 FPS||22.9 FPS||22.5 FPS||33.0 FPS||22.2 FPS|
|45.5 FPS||37 FPS||19,7 FPS||19,5 FPS||36.2 FPS||28.0 FPS|
|9.5 FPS||10.0 FPS||8.7 FPS||8.9 FPS||6.9 FPS|
|1.7 FPS||2.0 FPS||1.8 FPS||1.6 FPS||1.3 FPS|
|7.5 FPS||6.8 FPS||7.2 FPS|
|4.0 FPS||3.6 FPS|
|10.3 FPS||9.2 FPS||9.4 FPS||8.7 FPS||5.0 FPS||4.3 FPS|
Python installation of TensorFlow Lite.
For completeness, the Pythons installation of TensorFlow Lite 2.1.0 is given here. It is only one command. There are no wheels for TensorFlow Lite version 2.2.0, 2.3.0 and 2.3.1. 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