提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
在android端通过C++调用tflite比使用java接口效率高,需要通过编译tensorflow的源码编译生成tflite的so库, 网上教程大多数是linux或者ubantu环境下的。官方教程教程大部分也是基于此https://tensorflow.google.cn/lite/android/lite_build,我不太熟悉linux环境,所以摸索了下windows下的安装方法
cmd运行winver, 确认版本号1709
目前谷歌主推的高效编译工具,逐渐替代Cmake.
https://github.com/bazelbuild/bazel/tags 下载 bazel-4.2.1-windows-x86_64.exe
Msys2是一个工具和库的集合,为您提供了一个易于使用的环境,用于构建、安装和运行本地 windows 软件。其实就是给windows下的bazel创建一个类linux环境。
https://www.msys2.org/ 下载 msys2-x86_64-20220503.exe 双击运行 目录随意,我放置在e盘
设置环境变量 BAZEL_SH =E:\Program Files\msys64\usr\bin\bash.exe
将之前下载的bazel-4.2.1-windows-x86_64.exe重命名为bazel.exe放入E:\Program Files\msys64
管理员运行cmd验证
C:\Users\Administrator>bazel version
WARNING: Ignoring JAVA_HOME, because it must point to a JDK, not a JRE.
Build label: 4.2.1
Build target: bazel-out/x64_windows-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Mon Aug 30 15:19:20 2021 (1630336760)
Build timestamp: 1630336760
Build timestamp as int: 1630336760
搜索并打开 MSYS2 终端并运行以下命令:$ pacman -S zip unzip patch diffutils git 更新系统
安装clang编译工具 : $ pacman -S mingw-w64-clang-x86-64-toolchain
管理员运行cmd验证
C:\Users\Administrator>gcc --version
clang version 14.0.0
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: E:/Program Files/msys64/clang64/bin
Msys2是一个工具和库的集合,为您提供了一个易于使用的环境,用于构建、安装和运行本地 windows 软件。其实就是给windows下的bazel创建一个类linux环境。
https://my.visualstudio.com/Downloads?q=visual%20studio%202019&wt.mc_id=omsftvscom~older-downloads
下载 Visual C++ Redistributable for Visual Studio 2019 (version 16.11)
安装完毕后,勾选:使用C++ 的桌面开发,我选择的企业版,设置环境变量
BAZEL_VC =C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC
BAZEL_WINSDK_FULL_VERSION = 10.0.19041.0
BAZEL_VC_FULL_VERSION =14.29.30133
https://github.com/tensorflow/tensorflow/releases/tag/v2.8.0 下载源码
https://www.python.org/downloads/windows/ 下载Windows x86-64 executable installer, 双击后安装,安装时勾选 Add Python to PATH 可自动设置好环境变量
管理员运行cmd验证
C:\Users\Administrator>python
C:\Users\Administrator>python
Python 3.8.13 (default, Mar 28 2022, 06:59:08) [MSC v.1916 64 bit (AMD64)], Inc
Type “help”, “copyright”, “credits” or “license” for more information.
https://github.com/android/ndk/wiki/Unsupported-Downloads
下载 android-ndk-r19c-windows-x86_64.zip
目前只有这个版本同时包含platforms和toolchainsl两个关键包,其他的版本不知道为什么不齐全
解压后重新命名为androidndk放在tensorflow源码目录中
由于我以前安装过AndroidStudio,所以我使用了这个最简单的方法下载SDK
勾选后下载完毕后,将文件包重新命名为androidsdk放在tensorflow源码目录中
管理员运行cmd, cd 到tensorflow源码根目录
E:\Develop\tflite\tensorflow-2.8.0>python ./configure.py
You have bazel 4.2.1 installed.
Please specify the location of python. [Default is C:\Users\Administrator\python38\python.exe]: >Enter
Found possible Python library paths: C:\Users\Administrator\python38\lib\site-packages
Please input the desired Python library path to use. Default is [C:\Users\Administrator\python38\lib\site-packages]:>Enter
Do you wish to build TensorFlow with ROCm support? [y/N]: >N
Do you wish to build TensorFlow with CUDA support? [y/N]: >N
Please specify optimization flags to use during compilation when bazel option “–config=opt” is specified [Default is /arch:AVX]: > -march=native(大坑,由于windows默认选项支持AVX,但是android均不支持,默认会出错)
Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]: >Y
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: >Y
Please specify the home path of the Android NDK to use. [Default is C:/Users/Administrator/AppData/Roaming/Android/Sdk/ndk-bundle]: >androidndk
Please specify the (min) Android NDK API level to use. [Available levels: [‘16’, ‘17’, ‘18’, ‘19’, ‘21’, ‘22’, ‘23’, ‘24’, ‘26’, ‘27’, ‘28’]] [Default is 21]: > 21
Please specify the home path of the Android SDK to use. [Default is C:/Users/Administrator/AppData/Roaming/Android/Sdk]: > androidsdk
Please specify the Android SDK API level to use. [Available levels: [‘28’, ‘29’, ‘30’, ‘31’]] [Default is 31]: > 30
Please specify an Android build tools version to use. [Available versions: [‘28.0.3’, ‘29.0.2’, ‘30.0.2’, ‘31.0.0’]] [Default is 31.0.0]: > 30.0.0.2
配置完毕后输入如下编译指令
bazel build -c opt //tensorflow/lite:libtensorflowlite.so --config=android_arm64 --cxxopt=“-std=c++11” --verbose_failure
由于需要非常下载多外网资源,大概率会出现fetch错误时,重新输入编译指令即可