Install TensorFlow 2 Lite on Raspberry Pi 4 - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
TensorFlow Lite on Raspberry Pi

Install TensorFlow Lite 2.2.0 on Raspberry Pi 4

64-OS TensorFlow Lite

Introduction.

Tensorflow Lite will be installed on your Raspberry Pi 4 with a 32-bit operation system, together with some examples.

Preparations.

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.
# download the latest TensorFlow version (2.2.0)
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.2.0.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.2.0 tensorflow
$ cd tensorflow
# get the dependencies
$ ./tensorflow/lite/tools/make/download_dependencies.sh
# run the C++ installation
$ ./tensorflow/lite/tools/make/build_rpi_lib.sh
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.

TensorFlow Lite models.

You cannot run normal TensorFlow models on the Lite software, they must be converted before use. These TensorFlow pages explain how to do this. Google has some ready-made models available on the net here.
Object detection.
Another application is detecting objects in a scene. TensorFlow Lite host one model for now. COCO SSD MobileNet v1 recognize 80 different objects. It can detect up to ten objects in a scene. On GitHub we have a C++ example of the famous Skyfall intro running on a bare Raspberry Pi 4 for 32-bit.

James_YT_Large

Classification.
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.

Schoolbus
Pose estimation.

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.

PoseNet
Segmentation.
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.

Segmentation

Frame rate.

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.
Model
Raspberry Pi 4
64 bit OS
1950 MHz
Raspberry Pi 4
64 bit OS
1500 MHz
Raspberry Pi 4 64 bit Ubuntu  1850 MHz
Raspberry Pi 4
64 bit Ubuntu
1500 MHz
Raspberry Pi 4
32 bit OS
1950 MHz
Raspberry Pi 4
32 bit OS
1500 MHz
MobileNet-V1 SSD
(300x300)
26.8 FPS
21.5 FPS24.0 FPS17.2 FPS17.0 FPS13.8 FPS
MobileNet-V1
(224x224)
38.5 FPS32.2 FPS22.9 FPS22.5 FPS33.0 FPS22.2 FPS
MobileNet-V2
(224x224)
45.5 FPS37 FPS19,7 FPS19,5 FPS36.2 FPS28.0 FPS
Inception-V2
(224x224)
11.6 FPS
9.5 FPS10.0 FPS8.7 FPS8.9 FPS6.9 FPS
Inception-V4
(299x299)
2.1 FPS
1.7 FPS2.0 FPS1.8 FPS1.6 FPS1.3 FPS
Unet
(257x257)
7.5 FPS6.8 FPS7.2 FPS
6.6 FPS
4.0 FPS3.6 FPS
PoseNet
(257x257)
10.3 FPS9.2 FPS9.4 FPS8.7 FPS5.0 FPS4.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. 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
Deep learning examples for Raspberry Pi
Raspberry and alt
Install 32 OS
Raspberry Pi 4
Install 64 OS
Back to content