1# Build TensorFlow Lite for Raspberry Pi 2 3This page describes how to build the TensorFlow Lite static and shared libraries 4for Raspberry Pi. If you just want to start using TensorFlow Lite to execute 5your models, the fastest option is to install the TensorFlow Lite runtime 6package as shown in the [Python quickstart](python.md). 7 8**Note:** This page shows how to compile the C++ static and shared libraries for 9TensorFlow Lite. Alternative install options include: 10[install just the Python interpreter API](python.md) (for inferencing only); 11[install the full TensorFlow package from pip](https://www.tensorflow.org/install/pip); 12or 13[build the full TensorFlow package](https://www.tensorflow.org/install/source_rpi). 14 15**Note:** This page only covers 32-bit builds. If you're looking for 64-bit 16builds, check [Build for ARM64](build_arm64.md) page. 17 18**Note:** Cross-compile ARM with CMake is available. Please check 19[this](https://www.tensorflow.org/lite/guide/build_cmake_arm). 20 21## Cross-compile for Raspberry Pi with Make 22 23The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64) 24and TensorFlow devel docker image 25[tensorflow/tensorflow:devel](https://hub.docker.com/r/tensorflow/tensorflow/tags/). 26 27To cross compile TensorFlow Lite follow the steps: 28 29#### Step 1. Clone official Raspberry Pi cross-compilation toolchain 30 31```sh 32git clone https://github.com/raspberrypi/tools.git rpi_tools 33``` 34 35#### Step 2. Clone TensorFlow repository 36 37```sh 38git clone https://github.com/tensorflow/tensorflow.git tensorflow_src 39``` 40 41**Note:** If you're using the TensorFlow Docker image, the repo is already 42provided in `/tensorflow_src/`. 43 44#### Step 3. Run following script at the root of the TensorFlow repository to download 45 46all the build dependencies: 47 48```sh 49cd tensorflow_src && ./tensorflow/lite/tools/make/download_dependencies.sh 50``` 51 52**Note:** You only need to do this once. 53 54#### Step 4a. To build ARMv7 binary for Raspberry Pi 2, 3 and 4 55 56```sh 57PATH=../rpi_tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin:$PATH \ 58 ./tensorflow/lite/tools/make/build_rpi_lib.sh 59``` 60 61**Note:** This should compile a static library in: 62`tensorflow/lite/tools/make/gen/rpi_armv7l/lib/libtensorflow-lite.a`. 63 64You can add additional Make options or target names to the `build_rpi_lib.sh` 65script since it's a wrapper of Make with TFLite 66[Makefile](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tools/make/Makefile). 67Here are some possible options: 68 69```sh 70./tensorflow/lite/tools/make/build_rpi_lib.sh clean # clean object files 71./tensorflow/lite/tools/make/build_rpi_lib.sh -j 16 # run with 16 jobs to leverage more CPU cores 72./tensorflow/lite/tools/make/build_rpi_lib.sh label_image # # build label_image binary 73``` 74 75#### Step 4b. To build ARMv6 binary for Raspberry Pi Zero 76 77```sh 78PATH=../rpi_tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin:$PATH \ 79 ./tensorflow/lite/tools/make/build_rpi_lib.sh TARGET_ARCH=armv6 80``` 81 82**Note:** This should compile a static library in: 83`tensorflow/lite/tools/make/gen/rpi_armv6/lib/libtensorflow-lite.a`. 84 85## Compile natively on Raspberry Pi 86 87The following instructions have been tested on Raspberry Pi Zero, Raspberry Pi 88OS GNU/Linux 10 (Buster), gcc version 8.3.0 (Raspbian 8.3.0-6+rpi1): 89 90To natively compile TensorFlow Lite follow the steps: 91 92#### Step 1. Log in to your Raspberry Pi and install the toolchain 93 94```sh 95sudo apt-get install build-essential 96``` 97 98#### Step 2. Clone TensorFlow repository 99 100```sh 101git clone https://github.com/tensorflow/tensorflow.git tensorflow_src 102``` 103 104#### Step 3. Run following script at the root of the TensorFlow repository to download all the build dependencies 105 106```sh 107cd tensorflow_src && ./tensorflow/lite/tools/make/download_dependencies.sh 108``` 109 110**Note:** You only need to do this once. 111 112#### Step 4. You should then be able to compile TensorFlow Lite with: 113 114```sh 115./tensorflow/lite/tools/make/build_rpi_lib.sh 116``` 117 118**Note:** This should compile a static library in: 119`tensorflow/lite/tools/make/gen/lib/rpi_armv6/libtensorflow-lite.a`. 120 121## Cross-compile for armhf with Bazel 122 123You can use 124[ARM GCC toolchains](https://github.com/tensorflow/tensorflow/tree/master/third_party/toolchains/embedded/arm-linux) 125with Bazel to build an armhf shared library which is compatible with Raspberry 126Pi 2, 3 and 4. 127 128Note: The generated shared library requires glibc 2.28 or higher to run. 129 130The following instructions have been tested on Ubuntu 16.04.3 64-bit PC (AMD64) 131and TensorFlow devel docker image 132[tensorflow/tensorflow:devel](https://hub.docker.com/r/tensorflow/tensorflow/tags/). 133 134To cross compile TensorFlow Lite with Bazel, follow the steps: 135 136#### Step 1. Install Bazel 137 138Bazel is the primary build system for TensorFlow. Install the latest version of 139the [Bazel build system](https://bazel.build/versions/master/docs/install.html). 140 141**Note:** If you're using the TensorFlow Docker image, Bazel is already 142available. 143 144#### Step 2. Clone TensorFlow repository 145 146```sh 147git clone https://github.com/tensorflow/tensorflow.git tensorflow_src 148``` 149 150**Note:** If you're using the TensorFlow Docker image, the repo is already 151provided in `/tensorflow_src/`. 152 153#### Step 3. Build ARMv7 binary for Raspberry Pi 2, 3 and 4 154 155##### C library 156 157```bash 158bazel build --config=elinux_armhf -c opt //tensorflow/lite/c:libtensorflowlite_c.so 159``` 160 161Check 162[TensorFlow Lite C API](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/c) 163page for the detail. 164 165##### C++ library 166 167```bash 168bazel build --config=elinux_armhf -c opt //tensorflow/lite:libtensorflowlite.so 169``` 170 171You can find a shared library in: 172`bazel-bin/tensorflow/lite/libtensorflowlite.so`. 173 174Currently, there is no straightforward way to extract all header files needed, 175so you must include all header files in tensorflow/lite/ from the TensorFlow 176repository. Additionally, you will need header files from FlatBuffers and 177Abseil. 178 179##### Etc 180 181You can also build other Bazel targets with the toolchain. Here are some useful 182targets. 183 184* //tensorflow/lite/tools/benchmark:benchmark_model 185* //tensorflow/lite/examples/label_image:label_image 186