標籤:資料線 led sdn local 複製 install 連結 tools user
首先是把tensorflow
複製到本地一份。
git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git
既然是Google官方要求的,最好把--recurse-submodules
加上,文檔說可以避免一些資料結構序列化時的編譯問題。
這是android demo的github首頁。
準備編譯1.安裝bazel
bazel
是Google自己的構建工具。tensorflow
只能部分支援cmake
或者gradle
,而bazel
是tensorflow
工程的主要構建工具。
點這裡下載Bazel。
Mac和Linux使用者根據文檔進行安裝。Windows使用者,按照官方建議到下面的連結下載demo的二apk檔案,目前bazel在windows平台還處於實驗階段。
Windows使用者點這裡直接下載apk
bazel
安裝成功與否,用bazel version
檢查版本即可。
2.下載NDK
點這裡下載最新版本NDK。
最好下載r12b
版本的,最新的r13b
可能與bazel
有相容問題。
下載完成後解壓到自訂目錄,然後在~/.bash_profile
(linux
在~/.bashrc
)下添加環境變數。環境變數的添加過程大家百度一下吧,不是這裡的重點。
3.下載
>=23 Android SDK
Tensorflow Android Demo
必須在大於等於23
的API
環境中編譯。可以開啟Android Studio
中的SDK Manager
來安裝最新的SDK
。
4. 編輯Tensorflow根目錄下的WORKSPACE檔案
回到tensorflow
根目錄,(當前在android
目錄就往上兩級)。開啟WORKSPACE
檔案。
在檔案開頭部分找到
# 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 = "25.0.2",# # Replace with path to Android SDK on your system# path = "/path/to/your/sdk",#)# Android NDK r12b is recommended (higher may cause issues with Bazel)#android_ndk_repository(# name="androidndk",# path="/path/to/your/ndk",# # This needs to be 14 or higher to compile TensorFlow.# # Note that the NDK version is not the API level.# api_level=14)
這兩部分定義了SDK
和NDK
的路徑,把/path/to/your
的部分改成系統相應的路徑。然後將每一行前的注釋去掉。如下:
# 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 = "25.0.2", # Replace with path to Android SDK on your system path = "/Users/XXX/Library/Android/sdk",)## Android NDK r12b is recommended (higher may cause issues with Bazel)android_ndk_repository( name="androidndk", path="/usr/local/lib/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)
開始編譯
在tansorflow
根目錄執行,
bazel build -c opt //tensorflow/examples/android:tensorflow_demo
進行編譯,變異過程如下:
一切順利的話,編譯成功,如:
安裝APK DEMO
變異成功之後bazel
會在bazel-bin
目錄下面產生apk
檔案。用資料線連上手機,執行
adb install -r bazel-bin/tensorflow/examples/android/tensorflow_demo.apk
即可安裝到手機。
TensorFlow編譯androiddemo