This page describes how to build the TensorFlow Lite static and shared libraries for Raspberry Pi. If you just want to start using TensorFlow Lite to execute your models, the fastest option is to install the TensorFlow Lite runtime package as shown in the Python quickstart.
Cross-compile for Raspberry Pi with Make
The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64) and TensorFlow devel docker image tensorflow/tensorflow:devel.
To cross compile TensorFlow Lite follow the steps:
Step 1. Clone official Raspberry Pi cross-compilation toolchain
git clone https://github.com/raspberrypi/tools.git rpi_tools
Step 2. Clone TensorFlow repository
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
Step 3. Run following script at the root of the TensorFlow repository to download
all the build dependencies:
cd tensorflow_src && ./tensorflow/lite/tools/make/download_dependencies.sh
Step 4a. To build ARMv7 binary for Raspberry Pi 2, 3 and 4
PATH=../rpi_tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin:$PATH \
./tensorflow/lite/tools/make/build_rpi_lib.sh
You can add additional Make options or target names to the build_rpi_lib.sh
script since it's a wrapper of Make with TFLite
Makefile.
Here are some possible options:
./tensorflow/lite/tools/make/build_rpi_lib.sh clean # clean object files
./tensorflow/lite/tools/make/build_rpi_lib.sh -j 16 # run with 16 jobs to leverage more CPU cores
./tensorflow/lite/tools/make/build_rpi_lib.sh label_image # # build label_image binary
Step 4b. To build ARMv6 binary for Raspberry Pi Zero
PATH=../rpi_tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin:$PATH \
./tensorflow/lite/tools/make/build_rpi_lib.sh TARGET_ARCH=armv6
Compile natively on Raspberry Pi
The following instructions have been tested on Raspberry Pi Zero, Raspberry Pi OS GNU/Linux 10 (Buster), gcc version 8.3.0 (Raspbian 8.3.0-6+rpi1):
To natively compile TensorFlow Lite follow the steps:
Step 1. Log in to your Raspberry Pi and install the toolchain
sudo apt-get install build-essential
Step 2. Clone TensorFlow repository
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
Step 3. Run following script at the root of the TensorFlow repository to download all the build dependencies
cd tensorflow_src && ./tensorflow/lite/tools/make/download_dependencies.sh
Step 4. You should then be able to compile TensorFlow Lite with:
./tensorflow/lite/tools/make/build_rpi_lib.sh
Cross-compile for armhf with Bazel
You can use ARM GCC toolchains with Bazel to build an armhf shared library which is compatible with Raspberry Pi 2, 3 and 4.
The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64) and TensorFlow devel docker image tensorflow/tensorflow:devel.
To cross compile TensorFlow Lite with Bazel, follow the steps:
Step 1. Install Bazel
Bazel is the primary build system for TensorFlow. Install the latest version of the Bazel build system.
Step 2. Clone TensorFlow repository
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
Step 3. Build ARMv7 binary for Raspberry Pi 2, 3 and 4
C library
bazel build --config=elinux_armhf -c opt //tensorflow/lite/c:libtensorflowlite_c.so
Check TensorFlow Lite C API page for the detail.
C++ library
bazel build --config=elinux_armhf -c opt //tensorflow/lite:libtensorflowlite.so
You can find a shared library in:
bazel-bin/tensorflow/lite/libtensorflowlite.so
.
Currently, there is no straightforward way to extract all header files needed, so you must include all header files in tensorflow/lite/ from the TensorFlow repository. Additionally, you will need header files from FlatBuffers and Abseil.
Etc
You can also build other Bazel targets with the toolchain. Here are some useful targets.
- //tensorflow/lite/tools/benchmark:benchmark_model
- //tensorflow/lite/examples/label_image:label_image