IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> MacOS编译Tensorflow-Lite详细教程(生成Android项目用ARR) -> 正文阅读

[移动开发]MacOS编译Tensorflow-Lite详细教程(生成Android项目用ARR)

本教程不适用于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
  1. 等待编译成功,中途会从google云盘下载很多依赖,需要确保网络通畅
  2. 最后在源码根目录下bazel-bin/tensorflow/lite/java/文件夹里面找到.arr文件
    arr文件

问题合集

1. bazel代理失效

错误提示含以下字眼

Proxy address 127.0.0.1:1087 is not a valid URL

可能导致的原因

  • 代理参数(如:HTTP_PROXY)写成了小写
  • 重新打开了terminal,zsh配置失效,需要重新source一次.zshrc文件
  • 代理没连通
  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:19:40  更:2021-08-04 11:20:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 21:51:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码