1. 程式人生 > >Ubuntu16.04編譯tensorflow官方Android demo

Ubuntu16.04編譯tensorflow官方Android demo

1.下載tensorflow官方程式碼

git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git

2.下載Android SDK和Android NDK

Android SDK下載:在安裝Android Studio時進行選擇,

https://developer.android.com/studio/index.html?hl=zh-cn
Android NDK

下載地址:https://developer.android.com/ndk/downloads/older_releases.html

選擇NDK r12b進行下載,高版本(r14b)目前編譯可能報如下錯誤

tensorflow/core/kernels/BUILD:4542:1: C++ compilation of rule '//tensorflow/core/kernels:android_tensorflow_kernels'
 failed: clang failed: error executing command external/androidndk/ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
 -gcc-toolchain external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -fpic -ffunction-sections
 ... (remaining 73 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.

3.編輯tensorflow/WORKSPACE檔案,取消部分註釋後並填入sdk和ndk路徑後如下

# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
    name = "androidsdk",
    api_level = 26,
    # Ensure that you have the build_tools_version below installed in the
    # SDK manager as it updates periodically.
    build_tools_version = "25.0.2",
    # Replace with path to Android SDK on your system
    path = "/home/sc/Android/Sdk",
)
#
# Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
    name="androidndk",
    path="/home/sc/android-ndk-r12b",
#    # 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=24)
build_tools_version參考Sdk下面build-tools資料夾名稱,api_level為SDK對應的Android版本對應數字

好幾分鐘後,就會有編譯成功的提示

Target //tensorflow/examples/android:tensorflow_demo up-to-date:
  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: 394.902s, Critical Path: 56.04s
生成的apk檔案位於bazel-bin/tensorflow/examples/android/資料夾下