Build TensorFlow Lite Static Library on Raspberry Pi 4
I found a very useful resource about installing TensorFlow Lite 2.* on Raspberry Pi 4 as follows: https://qengineering.eu/install-tensorflow-2-lite-on-raspberry-pi-4.html
It's about to build TensorFlow Lite static library in version 2.4 :
pi@raspberrypi:~/Downloads/tensorflow-2.4.0/tensorflow/lite/tools/make/gen$ tree -L 3
.
└── rpi_armv7l
├── bin
│ ├── benchmark_model
│ ├── benchmark_model_performance_options
│ └── minimal
├── lib
│ ├── benchmark-lib.a
│ └── libtensorflow-lite.a
└── obj
└── tensorflow
Step 1. Install CMake tool
$ sudo apt-get install cmake
Step 2. Clone TensorFlow repository
$ git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
Note: If you're using the TensorFlow Docker image, the repo is already provided in /tensorflow_src/.
Step 3. Create CMake build directory
$ mkdir tflite_build
$ cd tflite_build
After that, run the following commands.
$ cmake -DTFLITE_ENABLE_XNNPACK=OFF ../tensorflow_src/tensorflow/lite
$ cmake --build . -j4
Build TensorFlow Lite Shared Library on Raspberry Pi 4 using Bazel-on-ARM project
Bazel is an open-source build tool from Google, used to build projects such as TensorFlow. Raspberry Pi OS (Raspbian) doesn't have a package for Bazel, and the Bazel project doesn't provide a binary for armhf.
Please install bazel first based on Bazel on ARM on Raspberry Pi
$ git clone https://github.com/koenvervloesem/bazel-on-arm
$ cd bazel-on-arm
$ sudo make requirements
$ make bazel
#or If you want to build a specific Bazel version, run the build script with the version number as an argument:
$ env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./scripts/build_bazel.sh 3.7.2
#You can install it to /usr/local/bin with:
sudo make install
There are pre-build bazel binary files in Release: https://github.com/koenvervloesem/bazel-on-arm/releases
Build TensorFlow Lite ( is not successful )
$ bazel build -c opt //tensorflow/lite/c:libtensorflowlite_c.so
Prebuilt binary for TensorflowLite
TensorflowLite-bin (Important)
Prebuilt binary for TensorflowLite's standalone installer. For RaspberryPi. I provide a FlexDelegate, MediaPipe Custom OP, XNNPACK and XNNPACK Multi-Thread PythonAPI enabled binary.
If you want the best performance with RaspberryPi4/3, install Ubuntu 18.04+ aarch64 (64bit) instead of Raspbian armv7l (32bit). The official Tensorflow Lite is performance tuned for aarch64. On aarch64 OS, performance is about 4 times higher than on armv7l OS
Python3.7 - Buster
$ wget https://github.com/PINTO0309/TensorflowLite-bin/releases/download/v2.8.0/tflite_runtime-2.8.0-cp37-none-linux_armv7l.whl
$ sudo pip3 install tflite_runtime-2.8.0-cp37-none-linux_armv7l.whl
The successful way to build and install TensorFlow Lite on Raspberry Pi 4:
Use Bazelisk to install Bazel
https://bazel.build/install/bazelisk
#First, we need to install bazelisk, and make sure you get the correct CPU arch and 64/32 bit for node.js
$ sudo npm install -g @bazel/bazelisk
#We can directly build bazel from source: ( it works )
$ cd tensorflow
$ ./configure
$ bazel build -c opt //tensorflow/lite/c:libtensorflowlite_c.so
$ sudo cp -rf bazel-bin/
$ sudo cp -rf bazel-bin/tensorflow/lite/c/* /usr/local/lib
$ sudo mkdir -p /usr/local/include/tensorflow/lite/c
$
$ sudo mkdir -p /usr/local/include/tensorflow/lite/c
$ sudo cp -rf tensorflow/lite/c/* /usr/local/include/tensorflow/lite/c
$ sudo ldconfig
Further Reading:
Building Bazel and TensorFlow 2.x on aarch64 ( ARM Board )
https://community.arm.com/arm-community-blogs/b/ai-and-ml-blog/posts/building-bazel-and-tensorflow-2-x-on-aarch64
No comments:
Post a Comment