机器学习深度学习程序员

人工智能 - TensorFlow Android 版 [7]

2017-09-20  本文已影响534人  SpikeKing

欢迎Follow我的GitHub,关注我的简书

在Android项目中,集成TensorFlow,目前(170920)TensorFlow 1.3版本的TF Detect模块无法使用,观察效果,需要使用1.2版本。

TF Android

项目位置:tensorflow/tree/master/tensorflow/examples/android

配置

下载Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Bazel用于构建项目,必须要安装。最好的方式是通过Brew下载,由于Bazel较大,下载较慢,经常失败,每次从断点处自动连接,需要不断重复下载,直至全部下载完成。

brew install bazel

在TensorFlow的根目录下,创建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的模型,执行

sh ./download_android_model.sh

配置文件,修改根目录的WORKSPACE,打开Android相关的脚本,填写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 = [
        "//tensorflow/examples/android/assets:asset_files",
        # ":external_assets",
    ],
)

参考:
bazelasset conflict


构建

在项目的根目录下,使用Bazel构建Android项目,执行项目中的BUILD文件,第一次构建时间较长,耐心等待。

bazel build -c opt //tensorflow/examples/android:tensorflow_demo

成功,没有任何错误。

Bazel

安装Android APK,生成三个入口:TF Classify、TF Detect、TF Stylize。

adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

AS

本地执行

gradle assembleDebug

全部编译通过,buildNativeBazel较慢,耐心等待。

点击Android Studio的Run按钮,即可。

如果是小米手机,like me,在调度程序时提示“Installation failed with message Failed to establish session”错误,需要在在开发者选项里关闭MIUI优化。

如果出现错误:

java.io.FileNotFoundException: .gradle/buildOutputCleanup/cache.properties (No such file or directory)

则删除.gradle文件夹,即可,参考


原理

tensorflow的Java包,来源于Bazel的构建,

compile 'org.tensorflow:tensorflow-android:+'

核心类是Classifier,调用接口类TensorFlowInferenceInterface,执行常见的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);

模型数据存储于asset文件夹中,文件大小约

tensorflow_inception_graph.pb  53.9MB
ssd_mobilenet_v1_android_export.pb  29.1MB
stylize_quantized.pb  564KB

效果

Demo

Thanks 我的同学 @高岩

OK, that's all!

上一篇 下一篇

猜你喜欢

热点阅读