Tensorflow Lite 编译

Google最近发布了Tensorflow Lite,并且提供了demo,虽然该demo可以使用bazel build –cxxopt=’–std=c++11’ //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo命令成功编译出来,但是文档中并没有提及如何纯粹的编译出动态库,参考之前的一篇文章《当 Android 开发者遇见 TensorFlow》,这篇文章就简单介绍一下如何编译动态库

clone 代码

 
     
1
 
     

修改TensorFlow项目根下的WROKSPACE文件

将以下代码反注释

 
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
     
# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
name = "androidsdk",
api_level = 23,
# Ensure that you have the build_tools_version below installed in the
# SDK manager as it updates periodically.
build_tools_version = "26.0.1",
# Replace with path to Android SDK on your system
path = "/Users/lizhangqu/AndroidSDK",
)
#
android_ndk_repository(
name= "androidndk",
path= "/Users/lizhangqu/AndroidNDK/android-ndk-r14b",
# This needs to be 14 or higher to compile TensorFlow.
# Please specify API level to >= 21 to build for 64-bit
# archtectures or the Android NDK will automatically select biggest
# API level that it supports without notice.
# Note that the NDK version is not the API level.
api_level=14)

然后修改android_sdk_repository中的path为自己电脑中的android sdk目录,修改android_ndk_repository中的path为自己电脑的android ndk目录。

值得注意的是,ndk的版本,官方建议使用大于r14的版本,下载地址android-ndk-r14b-darwin-x86_64.zip

编译

确保必要的工具已经安装,如bazel

 
     
1
2
3
4
 
     
bazel build --cxxopt='--std=c++11' //tensorflow/contrib/lite/java:tensorflowlite \
- -crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=armeabi

编译其他ABI请修改cpu参数,分别为

 
     
1
2
3
4
5
6
7
 
     
--cpu=armeabi
--cpu=armeabi-v7a
--cpu=arm64-v8a
--cpu=mips
--cpu=mips64
--cpu=x86
--cpu=x 86_64

产物位于

 
     
1
2
 
     
bazel-bin /tensorflow/contrib /lite/java/libtensorflowlite_jni.so
bazel-bin /tensorflow/contrib /lite/java/libtensorflowlitelib.jar

注意编译mips和mips64的时候,需要将构建脚本稍微修改一下,删除一部分代码,否则会报错

找到/tensorflow/contrib/lite/build_def.bzl文件,找到如下代码

 
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
     
def tflite_linkopts_unstripped():
"""Defines linker flags to reduce size of TFLite binary.
These are useful when trying to investigate the relative size of the
symbols in TFLite.
Returns:
a select object with proper linkopts
"""
return select({
"//tensorflow:android": [
"-Wl,--no-export-dynamic", # Only inc syms referenced by dynamic obj.
"-Wl,--exclude-libs,ALL", # Exclude syms in all libs from auto export.
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],
"//tensorflow/contrib/lite:mips": [],
"//tensorflow/contrib/lite:mips64": [],
"//conditions:default": [
"-Wl,--icf=all", # Identical code folding.
],
})
def tflite_jni_linkopts_unstripped():
"""Defines linker flags to reduce size of TFLite binary with JNI.
These are useful when trying to investigate the relative size of the
symbols in TFLite.
Returns:
a select object with proper linkopts
"""
return select({
"//tensorflow:android": [
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],
"//tensorflow/contrib/lite:mips": [],
"//tensorflow/contrib/lite:mips64": [],
"//conditions:default": [
"-Wl,--icf=all", # Identical code folding.
],
})

将上面代码中的下面这段删除

 
     
1
2
3
4
5
6
 
     
"//tensorflow:android": [
"-Wl,--no-export-dynamic", # Only inc syms referenced by dynamic obj.
"-Wl,--exclude-libs,ALL", # Exclude syms in all libs from auto export.
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],

以及这一段也删除

 
     
1
2
3
4
 
     
"//tensorflow:android": [
"-Wl,--gc-sections", # Eliminate unused code and data.
"-Wl,--as-needed", # Don't link unused libs.
],

临时删除后,编译完mips和mips64还原即可,不然会报如下错误

 
     
1
2
3
4
5
6
7
8
9
10
 
     
ERROR: /Users/lizhangqu /Desktop/tensorflow /tensorflow/contrib /lite/java /BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in / /tensorflow/contrib /lite/ java:libtensorflowlite_jni. so:
//tensorflow/contrib/lite:mips
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.
ERROR: Analysis of target '//tensorflow/contrib/lite/java:tensorflowlite' failed; build aborted:
/Users/lizhangqu /Desktop/tensorflow /tensorflow/contrib /lite/java /BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in / /tensorflow/contrib /lite/ java:libtensorflowlite_jni. so:
//tensorflow/contrib/lite:mips
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.
 
     
1
2
3
4
5
6
7
8
9
10
 
     
ERROR: /Users/lizhangqu /Desktop/tensorflow /tensorflow/contrib /lite/java /BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in / /tensorflow/contrib /lite/ java:libtensorflowlite_jni. so:
//tensorflow/contrib/lite:mips64
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.
ERROR: Analysis of target '//tensorflow/contrib/lite/java:tensorflowlite' failed; build aborted:
/Users/lizhangqu /Desktop/tensorflow /tensorflow/contrib /lite/java /BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in / /tensorflow/contrib /lite/ java:libtensorflowlite_jni. so:
//tensorflow/contrib/lite:mips64
//tensorflow:android
Multiple matches are not allowed unless one is unambiguously more specialized.

http://fucknmb.com/2017/11/17/Tensorflow-Lite%E7%BC%96%E8%AF%91/
### TensorFlow Lite 交叉编译指南 TensorFlow Lite 是一种轻量级的机器学习框架,专为移动设备和嵌入式系统设计。为了在 ARM 平台(如树莓派或其他基于 ARM 的硬件)上部署模型,通常需要进行交叉编译。以下是针对 TensorFlow Lite 交叉编译的相关说明: #### 1. 准备工作 确保开发环境中已安装必要的工具链和依赖项。这包括但不限于 CMake、Bazel 和目标平台的交叉编译器。对于 ARM 架构的目标设备,推荐使用 `aarch64-linux-gnu` 工具链。 - 安装基础依赖: ```bash sudo apt-get update && sudo apt-get install -y \ build-essential \ cmake \ git \ wget \ unzip \ python3-dev \ python3-pip ``` - 下载并配置交叉编译工具链[^4]: ```bash wget https://developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz tar xf gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz -C /opt/ export PATH=/opt/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu/bin:$PATH ``` #### 2. 获取 TensorFlow 源码 克隆 TensorFlow Git 仓库,并切换至支持 TensorFlow Lite 的版本(例如 v2.3.1)。此操作可以通过以下命令完成: ```bash git clone https://github.com/tensorflow/tensorflow.git cd tensorflow git checkout tags/v2.3.1 ``` #### 3. 配置交叉编译环境 创建一个自定义工具链文件用于描述交叉编译器路径和其他必要参数。例如,可以命名为 `toolchain.cmake`: ```cmake set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR aarch64) set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc) set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) ``` 随后,在构建过程中传递该工具链文件给 CMake。 #### 4. 使用 Bazel 或 CMake 进行交叉编译 ##### 方法一:通过 Bazel 跨平台编译 如果选择使用 Bazel 来执行交叉编译,则需调整 `.bazelrc` 文件中的配置以适配目标架构。具体步骤如下: ```bash echo "build --crosstool_top=@local_config_arm_compiler//:toolchain" >> ~/.tf_configure_bazelrc echo "build --cpu=armeabi-v7a" >> ~/.tf_configure_bazelrc ``` 接着运行标准的 Bazel 命令来生成动态链接库或静态库[^3]: ```bash bazel build --config=opt //tensorflow/lite:libtensorflowlite.so ``` ##### 方法二:借助 CMake 实现更灵活控制 另一种方式是利用 CMake 执行手动配置与构建流程。这种方法允许开发者更加精细地定制化选项。示例脚本如下所示: ```bash mkdir -p tflite_build && cd tflite_build cmake .. \ -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake \ -DTFLITE_ENABLE_RUY=ON \ -DTFLITE_WITH_XNNPACK=OFF \ -DANDROID_ABI="arm64-v8a" make -j$(nproc) ``` 以上两种方法均可实现 TensorFlow Lite 对于 ARM 设备的支持,但实际应用中可能还需要额外考虑特定硬件加速单元(比如 GPU 或 NPU)所需的驱动程序及其集成问题[^1]。 --- ### 总结 综上所述,无论是采用 Bazel 自动化的解决方案还是依靠 CMake 提供的高度可配置能力,都可以顺利完成 TensorFlow Lite 向 ARM 架构迁移的任务。然而需要注意的是整个过程涉及较多细节处理,尤其是当引入第三方扩展组件时更是如此。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值