MacOS根据模型op编译Tensorflow-Lite生成.aar和动态链接库

本教程不适用于windows系统,windows系统编译问题比较多。请自行准备翻墙工具,否则部分依赖无法下载。

环境配置

  • Tensorflow: 2.5.0
  • 操作系统: macOS Big Sur 11.4
  • Python: 3.8.2
  • Bazel: 3.7.2
  • Terminal脚本:zsh

Android NDK:

  • NDK version: 20.1.5948944
  • API level: 23

Android SDK:

  • API level:23
  • Android build tools version: 28.0.3

1. 环境安装

以下指令均在Terminal完成

1.1 Homebrew

官方安装指令

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

1.2 Bazel

# 设置bazel版本
export BAZEL_VERSION=3.7.2
# 使用curl下载安装脚本
curl -fLO "https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh"
# 给予文件权限
chmod +x "bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh"
# 执行脚本
./bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh --user

在.zshrc文件添加以下代码

export PATH="$PATH:$HOME/bin"

检查是否生效

bazel --version
1.2.1 给bazel添加代理

ip和端口根据自己代理的设置进行填写

# 注意大小写
export HTTP_PROXY="http://127.0.0.1:1087";
export HTTPS_PROXY="http://127.0.0.1:1087";

1.3 Python

macOS 11.4系统自带两个版本的python

  • python 对应2.7
  • python3 对应3.8.2
1.3.1 安装需要的依赖
pip install -U --user pip numpy wheel
pip install -U --user keras_preprocessing --no-deps
1.3.2 修改python与pip指令的指向

个人把python和pip都指向python3和pip3

# 获得python3和pip3路径
where python3 # 本地结果 /usr/bin/python3
where pip3
# 编辑zsh配置文件
sudo vim ~/.zshrc

在.zshrc文件添加以下代码

alias python='/usr/bin/python3'
alias pip='/usr/bin/pip3'

使用source指令让其生效

source ~/.zshrc

1.4 Android SDK&NDK

下载Android Studio,打开SDK Manager,勾选右下角的Show Package Details,下载对应的版本
下载

1.5 Tensorflow源码

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
# 切换版本
git checkout v2.5.0

2. 编译流程

  1. 进入tensorflow源码根目录
  2. 执行配置命令
./configure

以下为配置流程

alexleung@liaozhipengdeMacBook-Pro tensorflow % ./configure 
You have bazel 3.7.2 installed.
Please specify the location of python. [Default is /Applications/Xcode.app/Contents/Developer/usr/bin/python3]: 


Found possible Python library paths:
  /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages
Please input the desired Python library path to use.  Default is [/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages]

Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.

Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.

Please specify the home path of the Android NDK to use. [Default is /Users/liaozhipeng/library/Android/Sdk/ndk-bundle]: /Users/liaozhipeng/Documents/SDK/ndk/20.1.5948944 


WARNING: The NDK version in /Users/liaozhipeng/Documents/SDK/ndk/20.1.5948944 is 20, which is not supported by Bazel (officially supported versions: [10, 11, 12, 13, 14, 15, 16, 17, 18]). Please use another version. Compiling Android targets may result in confusing errors.

Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29']] [Default is 21]: 23


Please specify the home path of the Android SDK to use. [Default is /Users/liaozhipeng/library/Android/Sdk]: /Users/liaozhipeng/Documents/SDK 


Please specify the Android SDK API level to use. [Available levels: ['23', '31']] [Default is 31]: 23


Please specify an Android build tools version to use. [Available versions: ['28.0.3', '31.0.0']] [Default is 31.0.0]: 28.0.3


Do you wish to build TensorFlow with iOS support? [y/N]: n
No iOS support will be enabled for TensorFlow.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
	--config=mkl         	# Build with MKL support.
	--config=mkl_aarch64 	# Build with oneDNN and Compute Library for the Arm Architecture (ACL).
	--config=monolithic  	# Config for mostly static monolithic build.
	--config=numa        	# Build with NUMA support.
	--config=dynamic_kernels	# (Experimental) Build kernels into separate shared objects.
	--config=v2          	# Build TensorFlow 2.x instead of 1.x.
Preconfigured Bazel build configs to DISABLE default on features:
	--config=noaws       	# Disable AWS S3 filesystem support.
	--config=nogcp       	# Disable GCP support.
	--config=nohdfs      	# Disable HDFS support.
	--config=nonccl      	# Disable NVIDIA NCCL support.
Configuration finished
  1. 执行编译指令

官方教程中文版的编译指令(已过时)

bazel build -c opt --fat_apk_cpu=x86,x86_64,arm64-v8a,armeabi-v7a \
  --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
  //tensorflow/lite/java:tensorflow-lite

英文版

输入你用到的模型,自动将你用到的op打包,如果有Tensorflow的op会额外生成一个tensorflow-lite-select-tf-ops.aar

bash tensorflow/lite/tools/build_aar.sh \
--input_models=/Users/alexleung/Downloads/yolov5.tflite \
--target_archs=arm64-v8a,armeabi-v7a
  1. 等待编译成功,中途会从google云盘下载很多依赖,需要确保网络通畅
  2. 最后在源码根目录下bazel-bin/temp/文件夹里面找到.aar文件

3. 将AAR发布到本地Maven仓库

tensorflow-lite.aar

mvn install:install-file \
  -Dfile=bazel-bin/tmp/tensorflow-lite.aar \
  -DgroupId=org.tensorflow \
  -DartifactId=tensorflow-lite -Dversion=0.1.100 -Dpackaging=aar

tensorflow-lite-select-tf-ops.aar

没有这个包可以忽略

mvn install:install-file \
  -Dfile=bazel-bin/tmp/tensorflow-lite-select-tf-ops.aar \
  -DgroupId=org.tensorflow \
  -DartifactId=tensorflow-lite-select-tf-ops -Dversion=0.1.100 -Dpackaging=aar

4. 在Android项目调用

gradle配置文件

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            name 'ossrh-snapshot'
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
        // 确保有以下这行  
        mavenLocal()
    }
}

在对应项目配置文件进行以下改动

dependencies {
	// 导入本地maven仓库的tensorflow-lite
    implementation 'org.tensorflow:tensorflow-lite:0.1.100'
    ...
}

其他

C++动态链接库

bazel build -c opt --config=android_arm64 //tensorflow/lite:libtensorflowlite.so --config=monolithic

GPU动态链接库

bazel build -c opt --config android_arm64 tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so

问题合集

1. bazel代理失效

错误提示含以下字眼

Proxy address 127.0.0.1:1087 is not a valid URL

可能导致的原因

  • 代理参数(如:HTTP_PROXY)写成了小写
  • 重新打开了terminal,zsh配置失效,需要重新source一次.zshrc文件
  • 代理没连通

2. 找不到mvn指令

brew install maven
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex-Leung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值