Environment Setup
Cross-compilation SDK Installation
Install the cross-compilation tooclhain specific to the i.MX6 platform. Different chips require corresponding toolchains.
TensorFlow Source Retrieval
Clone the TensorFlow repository:
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r1.10
Ensure compatibility between TensorFlow version and Bazel compiler. For r1.10, use Python 2.7 or 3.6 with Bazel 0.18.0-0.19.2.
Bazel Compiler Installation
Install required dependencies:
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
Download and install Bazel:
wget https://github.com/bazelbuild/bazel/releases/download/0.18.1/bazel-0.18.1-installer-linux-x86_64.sh
chmod +x bazel-0.18.1-installer-linux-x86_64.sh
./bazel-0.18.1-installer-linux-x86_64.sh --user
Add to environment variables:
export PATH=$PATH:~/bin
Configuration and Build Process
Configure Build Settings
In the TensorFlow root directory:
./configure
Set appropriate flags for ARM architecture when targeting i.MX6:
--config=opt --copt=-march=armv7-a --copt=-mfpu=neon --copt=-mfloat-abi=hard
Compile Python Package
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
Generate wheel package:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
Install package:
pip install /tmp/tensorflow_pkg/tensorflow-*.whl
Download Dependencies
./tensorflow/contrib/lite/tools/make/download_dependencies.sh
ARM Platform Compilation
Create Custom Build Script
Create a build script for i.MX6 platform:
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/../../.."
CC_PREFIX=arm-poky-linux-gnueabi- make -j 3 -f tensorflow/contrib/lite/Makefile TARGET=imx6 TARGET_ARCH=armv7-a
The resulting static library will be located at:
tensorflow/contrib/lite/tools/make/gen/rpi_arm7l/lib/libtensorflow-lite.a
Alternative Build Method Using Bazel
Modify BUILD Configuration
Edit tensorflow/contrib/lite/kernels/internal/BUILD to include ARMv7a optimizations:
config_setting(
name = "armv7a",
values = {"cpu": "armv7a"},
)
"armv7a": [
"-O3",
"-mfpu=vfpv3",
"-mfloat-abi=hard",
],
Create Compilation Script
Create build_armv7_tflite.sh:
#!/bin/bash
bazel build --copt="-fPIC" \
--copt="-march=armv6" \
--copt="-mfloat-abi=hard" \
--copt="-mfpu=vfpv3" \
--copt="-funsafe-math-optimizations" \
--copt="-Wno-unused-function" \
--copt="-Wno-sign-compare" \
--copt="-ftree-vectorize" \
--copt="-fomit-frame-pointer" \
--cxxopt='--std=c++11' \
--cxxopt="-Wno-maybe-uninitialized" \
--cxxopt="-Wno-narrowing" \
--cxxopt="-Wno-unused" \
--cxxopt="-Wno-comment" \
--cxxopt="-Wno-unused-function" \
--cxxopt="-Wno-sign-compare" \
--cxxopt="-funsafe-math-optimizations" \
--linkopt="-lstdc++" \
--linkopt="-mfloat-abi=hard" \
--linkopt="-mfpu=vfpv3" \
--linkopt="-funsafe-math-optimizations" \
--verbose_failures \
--strip=always \
--crosstool_top=//armv6-compiler:toolchain \
--cpu=armv7 \
--config=opt \
tensorflow/contrib/lite/examples/label_image
Fix linker issues by removing --icf=all from build_def.bzl.
Model Compilation
Modify Makefile to include label_image binary:
LABEL_IMAGE_SRCS := \
tensorflow/contrib/lite/examples/label_image/label_image.cc \
tensorflow/contrib/lite/examples/label_image/bitmap_helpers.cc
LABEL_IMAGE_OBJS := $(addprefix $(OBJDIR), \
$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(LABEL_IMAGE_SRCS))))
all: $(LIB_PATH) $(MINIMAL_PATH) $(LABEL_IMAGE_PATH) $(BENCHMARK_BINARY)
Execute build script:
./tensorflow/contrib/lite/tools/make/build_imx6_lib.sh
Testing on Host System
Compile for host platform:
bazel build tensorflow/examples/label_image:label_image
Copy necessary files:
# Copy executable and shared library
cp bazel-bin/tensorflow/examples/label_image/label_image .
cp bazel-bin/tensorflow/libtensorflow_framework.so .
Run test:
./label_image -v 1 -m ./mobilenet_v1_1.0_224.tflite -i ./grace_hopper.jpg -l ./imagenet_slim_labels.txt
Deployment on Target Device
Transfer files to ARM board:
- label_image (1.7MB)
- grace_hopper.jpg
- mobilenet_quant_v1_224.tflite
- imagenet_slim_labels.txt
- libtensorflow_framework.so (14MB)
Mount and copy files:
mount /dev/sda1 /mnt/usb
ls /mnt/usb
cp /mnt/usb/* .
Execute application:
./label_image
If execution fails with "not found", ensure proper architecture compatibility and correct dynamic linker setup.