Open6
android 用 TFLite ランタイムビルド用環境の整備試行錯誤 (Docker環境上でGUI使用)
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
RUN dpkg --add-architecture i386 \
&& apt-get update \
&& apt-get install -y \
nano wget curl cmake sudo \
build-essential unzip git \
libc6:i386 libncurses5:i386 \
libstdc++6:i386 lib32z1 \
libbz2-1.0:i386 \
libxext-dev libxrender-dev libxtst-dev \
python-is-python3 git-lfs \
python3-all-dev python3-pip \
default-jdk mlocate gnupg \
lsb-release software-properties-common \
&& pip install pip -U
RUN wget https://apt.llvm.org/llvm.sh \
&& chmod u+x llvm.sh \
&& yes | ./llvm.sh 17 \
&& rm llvm.sh
ENV USERNAME=user
RUN echo "root:root" | chpasswd \
&& adduser --disabled-password --gecos "" "${USERNAME}" \
&& echo "${USERNAME}:${USERNAME}" | chpasswd \
&& echo "%${USERNAME} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/${USERNAME} \
&& chmod 0440 /etc/sudoers.d/${USERNAME}
USER ${USERNAME}
ARG WKDIR=/workdir
WORKDIR ${WKDIR}
RUN sudo chown ${USERNAME}:${USERNAME} ${WKDIR}
docker build -t androidbuild .
xhost +local: && \
docker run --rm -it \
-e XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR \
-e DISPLAY=$DISPLAY \
-v /tmp/.X11-unix/:/tmp/.X11-unix:rw \
androidbuild
-
ここから
Android NDK
をダウンロードして解凍する
https://developer.android.com/ndk/downloads?hl=ja -
ここから
Android Studio
をダウンロードして解凍する
https://developer.android.com/studio/install?hl=ja#64bit-libs
wget https://dl.google.com/android/repository/android-ndk-r26b-linux.zip
wget https://redirector.gvt1.com/edgedl/android/studio/ide-zips/2022.3.1.21/android-studio-2022.3.1.21-linux.tar.gz
unzip android-ndk-r26b-linux.zip \
&& rm android-ndk-r26b-linux.zip
tar -zxvf android-studio-2022.3.1.21-linux.tar.gz \
&& rm android-studio-2022.3.1.21-linux.tar.gz
-
Android Studio
をインストールする
cd android-studio/bin/
./studio.sh
- Bazel をダウンロードしてインストールする
cd /workdir
wget https://github.com/bazelbuild/bazel/releases/download/6.1.0/bazel-6.1.0-linux-x86_64 -O bazel
sudo chmod +x bazel
sudo mv bazel /usr/local/bin
- 上記を終えた状態
# NDKのインストール先 `/workdir/android-ndk-r26b`
# bazelのインストール先 `/workdir/bazel`
ls -l
total 53480
drwxr-xr-x 11 user user 4096 Oct 5 20:51 android-ndk-r26b
drwxr-xr-x 7 user user 4096 Jan 1 2010 android-studio
-rwxr-xr-x 1 user user 54749747 Oct 19 17:44 bazel
# SDKのインストール先 `/home/user/Android/Sdk`
ls -l ~/Android/Sdk/
total 32
drwxr-xr-x 3 user user 4096 Dec 10 15:39 build-tools
drwxr-xr-x 9 user user 4096 Dec 10 15:38 emulator
drwxr-xr-x 2 user user 4096 Dec 10 15:38 licenses
drwxr-xr-x 3 user user 4096 Dec 10 15:39 platform-tools
drwxr-xr-x 3 user user 4096 Dec 10 15:39 platforms
drwxr-xr-x 3 user user 4096 Dec 10 15:39 skins
drwxr-xr-x 3 user user 4096 Dec 10 15:39 sources
drwxr-xr-x 3 user user 4096 Dec 10 15:38 system-images
- TensorFlow v2.15.0 の clone と configure
cd /workdir
git clone -b v2.15.0 https://github.com/tensorflow/tensorflow && cd tensorflow
./configure
You have bazel 6.4.0 installed.
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.10/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 14.0.0-1ubuntu1.1 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.
Please specify the home path of the Android NDK to use. [Default is /home/user/Android/Sdk/ndk-bundle]: /workdir/android-ndk-r26b
WARNING: The NDK version in /workdir/android-ndk-r26b is 26, which is not supported by Bazel (officially supported versions: [19, 20, 21, 25]). 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, 30, 31, 32, 33, 34]] [Default is 26]:
Please specify the home path of the Android SDK to use. [Default is /home/user/Android/Sdk]:
Please specify the Android SDK API level to use. [Available levels: ['34']] [Default is 34]:
Please specify an Android build tools version to use. [Available versions: ['34.0.0']] [Default is 34.0.0]:
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
cd /workdir
-
tf-lite-unity-sample
の clone
git clone -b v2.15.0 https://github.com/asus4/tf-lite-unity-sample.git \
&& cd tf-lite-unity-sample
git lfs install && git lfs pull
# git lfs が成功していることの確認
# ファイルが大量に GitLFS からプルされてローカルに存在すること
ls -l Assets/StreamingAssets
total 187132
-rw-r--r-- 1 user user 4183312 Dec 10 16:06 coco_ssd_mobilenet_quant.tflite
-rw-r--r-- 1 user user 155 Dec 10 16:04 coco_ssd_mobilenet_quant.tflite.meta
-rw-r--r-- 1 user user 2779264 Dec 10 16:06 deeplabv3_257_mv_gpu.tflite
-rw-r--r-- 1 user user 155 Dec 10 16:04 deeplabv3_257_mv_gpu.tflite.meta
-rw-r--r-- 1 user user 4993712 Dec 10 16:06 esrgan.tflite
-rw-r--r-- 1 user user 155 Dec 10 16:04 esrgan.tflite.meta
-rw-r--r-- 1 user user 7992000 Dec 10 16:06 lite-model_movinet_a0_stream_kinetics-600_classification_tflite_float16_2.tflite
-rw-r--r-- 1 user user 155 Dec 10 16:04 lite-model_movinet_a0_stream_kinetics-600_classification_tflite_float16_2.tflite.meta
drwxr-xr-x 3 user user 4096 Dec 10 16:04 mediapipe
:
:
-
build_tflite.py
のカスタム - before
# Main
run_cmd(f'bazel build -c opt --fat_apk_cpu=arm64-v8a,armeabi-v7a,x86_64 //tensorflow/lite/java:tensorflow-lite')
copy('bazel-bin/tensorflow/lite/java/tensorflow-lite.aar', 'Android')
- after
# Main
run_cmd(f'bazel build -c opt --fat_apk_cpu=arm64-v8a,armeabi-v7a,x86_64 --define xnnpack_force_float_precision=fp16 //tensorflow/lite/java:tensorflow-lite')
copy('bazel-bin/tensorflow/lite/java/tensorflow-lite.aar', 'Android')
-
tf-lite-unity-sample
のビルド
./build_tflite.py --tfpath ../tensorflow -android
- エラー1
INFO: Analyzed target //tensorflow/lite/java:tensorflow-lite (139 packages loaded, 15320 targets configured).
INFO: Found 1 target...
ERROR: /workdir/tensorflow/tensorflow/lite/core/api/BUILD:64:11: Compiling tensorflow/lite/core/api/error_reporter.cc failed: undeclared inclusion(s) in rule '//tensorflow/lite/core/api:error_reporter':
this rule is missing dependency declarations for the following files included by 'tensorflow/lite/core/api/error_reporter.cc':
'external/androidndk/toolchains/llvm/prebuilt/linux-x86_64/lib/clang/17/include/stdarg.h'
Target //tensorflow/lite/java:tensorflow-lite failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 22.599s, Critical Path: 2.06s
INFO: 497 processes: 490 internal, 7 local.
FAILED: Build did NOT complete successfully