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

조회 수: 114(최근 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?
  댓글 수: 3

댓글을 달려면 로그인하십시오.

채택된 답변

Bill Chou
Bill Chou 2022년 12월 5일
Use on Windows targets (For Release 2022b and newer)
To generate and run C++ code that performs inference with TensorFlow Lite models on Windows targets, you must have the TensorFlow Lite library on the Windows hardware. To build the TensorFlow Lite library version 2.4.1 for Windows targets on your host Windows platform, execute the following steps.
Requirements:
  • To build the TensorFlow Lite dynamic library, you must install bazel (version 3.1.0 to 3.99.0) on the host Windows computer. See this link for more information: Installing Bazel on Windows
  • You may need to add PYTHON_BIN_PATH to the bazel command if Bazel is not able to find the python paths
  • Start the process of bazel-build from the Developer Command Prompt terminals provided with Visual Studio installations
Build Instructions:
1. Open Command Prompt
2. Execute the following commands:
% clone the git repo
git clone https://github.com/tensorflow/tensorflow.git
% git clone will automatically download the code into a folder named `tensorflow`
cd tensorflow
% checkout version 2.4.1
git checkout v2.4.1
% install dependencies
pip --no-cache-dir install numpy future
% Download flatbuffers and other dependent files by running the following command from within the command prompt in Windows:
./tensorflow/lite/tools/make/download_dependencies.sh
% configure tensorflowlite installation
python ./configure.py
% build the dynamic library
bazel build -c opt //tensorflow/lite:tensorflowlite.dll
% it might be necessary to provide python path if it's not picked up automatically
bazel build -c opt //tensorflow/lite:tensorflowlite.dll --action_env PYTHON_BIN_PATH=<python-path>
% Create a directory named lib and copy the contents of the bazel-bin directory into it.
cd bazel-bin
cp -r ../lib/
% To configure the MATLAB environment for TensorFlow Lite code generation, set the environment variables TFLITE_PATH
% <TFLite Root folder> is the full-path to `tensorflow` folder where you have downloaded the source files
setenv('TFLITE_PATH’, ‘<TFLite Root folder>’);

추가 답변(2개)

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']) ;

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

범주

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by