在Android項目中,集成TensorFlow,目前(170920)TensorFlow 1.3版本的TF Detect模塊無法使用,觀察效果,需要使用1.2版本。

項目位置:tensorflow/tree/master/tensorflow/examples/android
配置
下載Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Bazel用于構(gòu)建項目,必須要安裝。最好的方式是通過Brew下載,由于Bazel較大,下載較慢,經(jīng)常失敗,每次從斷點處自動連接,需要不斷重復(fù)下載,直至全部下載完成。
brew install bazel
在TensorFlow的根目錄下,創(chuàng)建download_android_model.sh,添加
BASE_URL=https://storage.googleapis.com/download.tensorflow.org/models
for MODEL_ZIP in inception5h.zip ssd_mobilenet_v1_android_export.zip stylize_v1.zip
do
curl -L ${BASE_URL}/${MODEL_ZIP} -o /tmp/${MODEL_ZIP}
unzip /tmp/${MODEL_ZIP} -d tensorflow/examples/android/assets/
done
用于下載Android的模型,執(zhí)行
sh ./download_android_model.sh
配置文件,修改根目錄的WORKSPACE,打開Android相關(guān)的腳本,填寫androidsdk路徑和androidndk路徑,并且修改build_tools_version為26.0.1,支持Bazel編譯。
# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
name = "androidsdk",
api_level = 23,
# Ensure that you have the build_tools_version below installed in the
# SDK manager as it updates periodically.
build_tools_version = "26.0.1",
# Replace with path to Android SDK on your system
path = "/Users/wangchenlong/Installations/android-sdk",
)
# Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
name="androidndk",
path="/Users/wangchenlong/Installations/android-ndk-r10e",
# This needs to be 14 or higher to compile TensorFlow.
# Please specify API level to >= 21 to build for 64-bit
# archtectures or the Android NDK will automatically select biggest
# API level that it supports without notice.
# Note that the NDK version is not the API level.
api_level=14)
根目錄的BUILD文件注釋assets的external_assets選項,使用本地asset的模型文件。
android_binary(
assets = [
"http://tensorflow/examples/android/assets:asset_files",
# ":external_assets",
],
)
參考:
bazel、asset conflict
構(gòu)建
在項目的根目錄下,使用Bazel構(gòu)建Android項目,執(zhí)行項目中的BUILD文件,第一次構(gòu)建時間較長,耐心等待。
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
成功,沒有任何錯誤。

安裝Android APK,生成三個入口:TF Classify、TF Detect、TF Stylize。
adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
AS
本地執(zhí)行
gradle assembleDebug
全部編譯通過,buildNativeBazel較慢,耐心等待。
點擊Android Studio的Run按鈕,即可。
如果是小米手機,like me,在調(diào)度程序時提示“Installation failed with message Failed to establish session”錯誤,需要在在開發(fā)者選項里關(guān)閉MIUI優(yōu)化。
如果出現(xiàn)錯誤:
java.io.FileNotFoundException: .gradle/buildOutputCleanup/cache.properties (No such file or directory)
則刪除.gradle文件夾,即可,參考
原理
tensorflow的Java包,來源于Bazel的構(gòu)建,
compile 'org.tensorflow:tensorflow-android:+'
核心類是Classifier,調(diào)用接口類TensorFlowInferenceInterface,執(zhí)行常見的TF操作,如feed、run、fetch等操作。
// Copy the input data into TensorFlow.
inferenceInterface.feed(inputName, floatValues, 1, inputSize, inputSize, 3);
// Run the inference call.
inferenceInterface.run(outputNames, logStats);
// Copy the output Tensor back into the output array.
inferenceInterface.fetch(outputName, outputs);
模型數(shù)據(jù)存儲于asset文件夾中,文件大小約
tensorflow_inception_graph.pb 53.9MB
ssd_mobilenet_v1_android_export.pb 29.1MB
stylize_quantized.pb 564KB
效果

Thanks 我的同學(xué) @高巖
OK, that's all!