1. ornew

    Posted

    ornew
Changes in title
+TensorFlow for Android 勉強会@浜松
Changes in tags
Changes in body
Source | HTML | Preview
@@ -0,0 +1,106 @@
+一時的な資料なのでイベント終わったらこの記事は消します(すべて整理してまた投稿し直します)
+
+UbuntuのDockerコンテナがおすすめ
+
+```bash
+docker pull ubuntu:latest
+docker run -it --rm ubuntu
+```
+
+セットアップ
+
+```sh
+# バージョンは適当に変えてください
+SDK_VERSION=25.2.3
+NDK_VERSION=r13b
+SDK_API_VERSION=25
+BUILD_TOOLS_VERSION=25.0.2
+TENSORFLOW_TAG=v0.12.0
+
+# ディレクトリは適当に変えてください
+WORK_DIR=/home
+LIB_DIR=/usr/local/lib
+
+SDK_URL=https://dl.google.com/android/repository/tools_r${SDK_VERSION}-linux.zip
+NDK_URL=https://dl.google.com/android/repository/android-ndk-${NDK_VERSION}-linux-x86_64.zip
+
+cat >> /etc/profile << EOS
+export ANDROID_HOME=${LIB_DIR}/android-sdk
+export ANDROID_NDK_HOME=${LIB_DIR}/android-ndk-${NDK_VERSION}
+EOS
+cat >> /etc/profile << 'EOS'
+export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
+export PATH=${PATH}:${ANDROID_NDK_HOME}
+EOS
+source /etc/profile
+
+# 好きなリポジトリにしてください
+sed -i.bak -e "s%http://archive.ubuntu.com/ubuntu/%http://ftp.jaist.ac.jp/pub/Linux/ubuntu/%g" /etc/apt/sources.list
+
+apt-get update
+apt-get upgrade -y
+apt-get install -y git curl unzip
+
+echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list
+curl https://bazel.build/bazel-release.pub.gpg | apt-key add -
+
+apt-get update && apt-get install -y bazel &
+
+# 適当にハッシュチェックとかしてください
+curl ${SDK_URL} -sSL -o /tmp/sdk.zip && unzip -q /tmp/sdk.zip -d ${LIB_DIR}/android-sdk &
+curl ${NDK_URL} -sSL -o /tmp/ndk.zip && unzip -q /tmp/ndk.zip -d ${LIB_DIR} &
+
+wait
+
+apt-get clean
+
+cd $WORK_DIR
+git clone https://github.com/ornew/MNIST-for-Android.git
+pushd MNIST-for-Android
+pushd lib
+cat > WORKSPACE << EOS
+workspace(name = "mnist_android")
+
+android_sdk_repository(
+ name = "androidsdk",
+ api_level = ${SDK_VERSION},
+ build_tools_version = "${BUILD_TOOLS_VERSION}",
+ path = "${ANDROID_HOME}",
+)
+
+android_ndk_repository(
+ name = "androidndk",
+ path = "${ANDROID_NDK_HOME}",
+ api_level = 21)
+
+git_repository(
+ name = "org_tensorflow",
+ remote = "https://github.com/tensorflow/tensorflow.git",
+ init_submodules = 1,
+ tag = "${TENSORFLOW_TAG}"
+)
+
+load("@org_tensorflow//tensorflow:workspace.bzl", "tf_workspace")
+tf_workspace()
+EOS
+```
+
+soのビルド
+
+```sh
+cd $WORK_DIR/MNIST-for-Android/lib
+bazel build libmnist_android.so --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
+```
+
+gradlewでコンソールからビルドする場合
+
+```sh
+cd $WORK_DIR/MNIST-for-Android
+cp lib/bazel-bin/libmnist_android.so android/app/src/main/jniLibs/armeabi-v7a/
+android list sdk -a | grep -E "SDK Platform Android .* API ${SDK_VERSION}" | awk '{sub(/.$/,"",$1);print $1}' | xargs -I{} sh -c "echo y | android update sdk -a -u -t {}"
+android list sdk -a | grep "Android SDK Build-tools, revision ${BUILD_TOOLS_VERSION}" | awk '{sub(/.$/,"",$1);print $1}' | xargs -I{} sh -c "echo y | android update sdk -a -u -t {}"
+android list sdk -a | grep "Android Support Repository" | awk '{sub(/.$/,"",$1);print $1}' | xargs -I{} sh -c "echo y | android update sdk -a -u -t {}"
+cd android
+bash ./gradlew build
+# build/outputs/apk/app-debug.apk
+```