使用docker生成Tensorflow Lite aar文件
使用docker生成Tensorflow Lite aar文件
1. 下载docker镜像
下载dockerfile,重命名为tflite.Dockerfile
docker pull tensorflow/build:latest-python3.11 # 根据dockerfile里面的内容进行修改
docker build . -t tflite-builder -f tflite.Dockerfile # dockerfile中拉取某些文件,要翻墙开vpn
2. 运行docker镜像
挂载到本地目录,用于保存android库 等文件
镜像名字通过docker images查看
docker run -it --network=host -v pwd:/host_dir tflite-builder bash # linux改成 $PWD ; --network=host 是为了后面可以使用宿主机vpn
3. download additional Android tools and libraries
sdkmanager \
"build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
"platform-tools" \
"platforms;android-${ANDROID_API_LEVEL}"
4. 下载tensorflow源码
cd /host_dir/
git clone https://github.com/tensorflow/tensorflow.git
git checkout 某个版本 # 如果是最新的docker镜像和代码,可以跳过
5. Configure WORKSPACE and .bazelrc
cd /host_dir/tensorflow/
./configure
配置如下,一路回车默认,interactively configure选择y
tf-docker /host_dir/tensorflow > ./configure
WARNING: current bazel installation is not a release version.
Please specify the location of python. [Default is /usr/bin/python3]:
Found possible Python library paths:
/usr/lib/python3/dist-packages
/usr/local/lib/python3.11/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]:
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]:
No CUDA support will be enabled for TensorFlow.
Do you want to use Clang to build TensorFlow? [Y/n]:
Clang will be used to compile TensorFlow.
Please specify the path to clang executable. [Default is /usr/lib/llvm-17/bin/clang]:
You have Clang 17.0.6 installed.
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.
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=v1 # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
--config=nogcp # Disable GCP support.
--config=nonccl # Disable NVIDIA NCCL support.
Configuration finished
6. Build and install
需要翻墙,设置代理
curl google.com # 测试能否翻墙
export https_proxy=http://host.docker.internal:7890;http_proxy=http://host.docker.internal:7890; all_proxy=socks5://host.docker.internal:7890
参考:
https://github.com/kubernetes-sigs/kind/issues/3416
bazel build -c opt --cxxopt=--std=c++17 --config=android_arm64 \
--fat_apk_cpu=x86,x86_64,arm64-v8a,armeabi-v7a \
--define=android_dexmerger_tool=d8_dexmerger \
--define=android_incremental_dexing_tool=d8_dexbuilder \
//tensorflow/lite/java:tensorflow-lite
ls bazel-bin/tensorflow/lite/java/*.aar
就可以看到生成的aar文件了
7. 参考
https://www.tensorflow.org/lite/android/lite_build
https://github.com/tensorflow/tensorflow/issues/60831