[DL] Tensorflow android demo

    
     最近在學習如何將tensorflow架在android上面,利用平板去跑深度學習。基本上是照的tensorflow官網的步驟,在修改自己環境問題,經過千辛萬苦終於跑出可執行的apk檔,現在就開始一步一步進行教學,希望之後使用的大家,也別踩到雷呀!!

執行的範例主要有以下三種:
  1. TF Classify: 物體分類
  2. TF Stylize:   對相機預覽的影像進行圖像風格的變換
  3. TF Detect:   偵測影像有人的位置,並劃出偵測框
引用:https://goo.gl/Wr2lKg
先前環境準備:
  • 安裝android studio,並完成環境設置
    https://developer.android.com/sx.html
  • 下載NDK
https://developer.android.com/ndk/downloads/older_releases.html#ndk-12b-downloads
  • 下載tensprflow的官方文件
       並執行
$ cd tensorflow
$ ./configure 
  • 下載bazel,mac 可以直接使用指令進行下載  
$ brew install bazel
  • 完成前置作業,開始修改要執行此專案需修改的程式部分修改<tensorflow_root>/WORKSPACE檔案,將android_sdk_repositoryandroid_ndk_repository修改成自己電腦的環境
原始程式:
# Uncomment and update the paths in these entries to build the Android demo.
#android_sdk_repository(
#    name = "androidsdk",
#    api_level = 23,
#    build_tools_version = "25.0.1",
#    # Replace with path to Android SDK on your system
#    path = "<PATH_TO_SDK>",
#)
#
#android_ndk_repository(
#    name="androidndk",
#    path="<PATH_TO_NDK>",
#    api_level=14)
修改完:
android_sdk_repository(
    name = "androidsdk",
    api_level = 25,
    # Ensure that you have the build_tools_version below installed in the
    # SDK manager as it updates periodically.
    build_tools_version = "25.0.0",
    # Replace with path to Android SDK on your system
    path = "/Users/bella/Library/Android/sdk", )

# Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
    name="androidndk",
    path="/Users/bella/Documents/android/android-ndk-r12b",
    # This needs to be 14 or higher to compile TensorFlow.
    # Note that the NDK version is not the API level.
    api_level=14)
  • 編譯Android Demo App
$ cd ~/tensorflow
$ bazel build //tensorflow/examples/android:tensorflow_demo
完成之後會生成3個檔案,如沒產生就代表編譯有錯誤
bazel-bin/tensorflow/examples/android/tensorflow_demo_deploy.jar
bazel-bin/tensorflow/examples/android/tensorflow_demo_unsigned.apk
bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
INFO: Elapsed time: 36.799s, Critical Path: 21.96s
  • 安装APK
adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk

完成安裝就可以看到有三個app可進行測試囉!!


參考網址: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android

留言

這個網誌中的熱門文章

[Android] TextView 換行

[Android]android Global variable 寫法

[Android] build the JAR file in Android Studio