Note: The following applies to R2022b and older releases of MATLAB Coder and TensorFlow Lite library version 2.4.1 as described here. For newer releases, see the answers above.
Use on Windows targets
To generate and run C++ code that performs inference with TensorFlow Lite models on Windows targets, you must have theTensorFlow 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:
git clone https://github.com/tensorflow/tensorflow.git
pip --no-cache-dir install numpy future
./tensorflow/lite/tools/make/download_dependencies.sh
bazel build -c opt //tensorflow/lite:tensorflowlite.dll
bazel build -c opt //tensorflow/lite:tensorflowlite.dll --action_env PYTHON_BIN_PATH=<python-path>
setenv('TFLITE_PATH’, ‘<TFLite Root folder>’);
Use on 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:
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
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
pip3 --no-cache-dir install grpcio h5py keras_applications keras_preprocessing mock numpy requests future
Build instructions
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:
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.
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']) ;
Use on ARM targets
To generate and run C++ code that performs inference with TensorFlow Lite models on ARM targets, you must have theTensorFlow 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:
- You must have the Linaro AArch32 or AArch64 toolchain installed on the host Linux computer.
For armv7 target, install the GNU/GCCg++-arm-linux-gnueabihftoolchain:
sudo apt-get install g++-arm-linux-gnueabihf
For armv8 target, install the GNU/GCCg++-aarch64-linux-gnutoolchain:
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
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
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
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
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:
bazel build --config=elinux_aarch64 -c
opt//tensorflow/lite:libtensorflowlite.so
bazel build --config=elinux_armhf -c
opt//tensorflow/lite:libtensorflowlite.so
5. Create a directory lib. Copy the contents of bazel-bin into lib.
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