MATLAB Coder: How do I build TensorFlow Lite for Deep Learning C++ code generation and deployment?

조회 수: 69(최근 30일)
I see a few deep learning networks supported for code generation using MATLAB Coder:
I'm looking to generate code from my deep learning network (like ResNet, GoogLeNet, SqueezeNet, VGG-16/19, etc) to run on boards supported by TensorFlow Lite using MATLAB Coder. What are the steps to do this?

채택된 답변

Bill Chou
Bill Chou 2022년 3월 10일
Use with Linux host computer
To generate and run C++ code that performs inference with TensorFlow Lite models on your Linux host computer, you must have the TensorFlow Lite library. To build the TensorFlow Lite library version 2.4.1 on your host Linux platform, follow these steps.
Requirements
To build the TensorFlow Lite dynamic library, you must first install bazel (minimum version 3.1.0) on the host Linux computer. Note that these build steps were validated using bazel version 3.7.2:
1. Install bazel on Linux host (see Installing Bazel on Ubuntu - Bazel main for more information):
2. Install bazel dependencies:
sudo apt-get install curl gnupg
3. Install bazel from source on host Linux:
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo cp bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update && sudo apt install bazel
4. If the version of bazel is not compatible, install a compatible bazel version. Minimum bazel version needed is 3.1.0.
sudo apt install bazel-3.7.2
sudo ln -s /usr/bin/bazel-3.7.2 /usr/bin/bazel
bazel --version # 3.7.2
5. If you encounter any issues install:
sudo apt-get install python-pip python3-pip
pip --no-cache-dir install grpcio h5py keras_applications keras_preprocessing mock numpy requests future
(or)
pip3 --no-cache-dir install grpcio h5py keras_applications keras_preprocessing mock numpy requests future
Build instructions
1. Download TensorFlow source code for version 2.4.1 (available at GitHub - tensorflow/tensorflow at v2.4.1).
2. Unzip the source code to a folder, then:
a) Open Linux terminal
b) Change the directory to the TensorFlow Lite source folder by running the following command. <TFLite Root folder> is the location where you downloaded the source code:
cd <TFLite Root folder>
3. Download flatbuffers and other dependent files by running these commands.
chmod +x ./tensorflow/lite/tools/make/download_dependencies.sh
./tensorflow/lite/tools/make/download_dependencies.sh
4. To generate dynamic library, run:
bazel build -c opt //tensorflow/lite:libtensorflowlite.so
5. Create a directory named lib and copy the contents of the bazel-bin directory into it.
cd bazel-bin
cp -r ./ <TFLite Root folder>/lib/
6. To configure the MATLAB environment for TensorFlow Lite code generation, set the environment variables TFLITE_PATH and LD_LIBRARY_PATH at the MATLAB command window:
setenv('TFLITE_PATH’, ‘<TFLite Root folder>’);
setenv('LD_LIBRARY_PATH',[getenv('LD_LIBRARY_PATH'),':', ...
getenv('TFLITE_PATH'),'/lib/tensorflow/lite']) ;

추가 답변(1개)

Bill Chou
Bill Chou 2022년 3월 14일
Use on ARM targets
To generate and run C++ code that performs inference with TensorFlow Lite models on ARM targets, you must have the TensorFlow Lite library on the ARM hardware. To build the TensorFlow Lite library version 2.4.1 for ARM targets on your host Linux platform (by using a cross-compiler toolchain), execute the following steps.
Requirements:
  • The target device must have either armv7 (32-bit) or armv8 (64-bit) ARM architecture. To verify the architecture, run this command at your device terminal:
arch
  • You must have the Linaro AArch32 or AArch64 toolchain installed on the host Linux computer.
For armv7 target, install the GNU/GCC g++-arm-linux-gnueabihf toolchain:
sudo apt-get install g++-arm-linux-gnueabihf
For armv8 target, install the GNU/GCC g++-aarch64-linux-gnu toolchain:
sudo apt-get install g++-aarch64-linux-gnu
  • To build the TensorFlow Lite dynamic library, you must install bazel (minimum version 3.1.0) on the host Linux computer. To install bazel on Linux host (Ref: Installing Bazel on Ubuntu - Bazel main):
1. Install bazel dependencies:
sudo apt-get install curl gnupg
2. Install bazel from source on host Linux:
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo cp bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update && sudo apt install bazel
3. If the version of bazel is not compatible, install a compatible bazel version. Minimum bazel version needed is 3.1.0.
sudo apt install bazel-3.7.2
sudo ln -s /usr/bin/bazel-3.7.2 /usr/bin/bazel
bazel --version # 3.7.2
4. If you encounter any issues install:
sudo apt-get install python-pip python3-pip
pip --no-cache-dir install grpcio h5py keras_applications keras_preprocessing mock numpy requests future
(or)
pip3 --no-cache-dir install grpcio h5py keras_applications keras_preprocessing mock numpy requests future
We have validated the build steps on host Linux platform using the following software:
  • Build tool: bazel version 3.7.2
  • Toolchain:
  • GNU Arm Embedded Toolchain (g++-arm-linux-gnueabihf) version 8.3.0 for armv7 targets such as Raspberry Pi
  • AArch64 toolchain (g++-aarch64-linux-gnu) version 8.3.0 for armv8 targets such as Hikey960
Build instructions
1. Download the TensorFlow source code for version 2.4.1 (available at GitHub - tensorflow/tensorflow at v2.4.1) to your host Linux computer.
2. Unzip the source code to a folder. Then:
  • Open Linux terminal
  • Change directory to the TensorFlow Lite source folder by running this command. Here, <TFLite Root folder> points to the location where you downloaded the source code
cd <TFLite Root folder>
3. Download flatbuffers and other dependent files by running these commands:
chmod +x ./tensorflow/lite/tools/make/download_dependencies.sh
./tensorflow/lite/tools/make/download_dependencies.sh
4. Generate the TensorFlow Lite dynamic library by running these commands:
  • For armv8 architecture
bazel build --config=elinux_aarch64 -c
opt//tensorflow/lite:libtensorflowlite.so
  • For armv7 architecture
bazel build --config=elinux_armhf -c
opt//tensorflow/lite:libtensorflowlite.so
5. Create a directory lib. Copy the contents of bazel-bin into lib.
cd bazel-bin
cp -r ./ <TFLite Root folder>/lib/
6. Copy the <TFLite Root folder> directory to the target hardware.
7. On the target hardware, set the environment variables TFLITE_PATH and LD_LIBRARY_PATH as follows:
export TFLITE_PATH=<TFLite Root folder>
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:<TFLite Root folder>/lib/tensorflow/lite

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by