Tensorflow Lite 编译
Google最近發布了Tensorflow Lite,并且提供了demo,雖然該demo可以使用bazel build –cxxopt=’–std=c++11’ //tensorflow/contrib/lite/java/demo/app/src/main:TfLiteCameraDemo命令成功編譯出來,但是文檔中并沒有提及如何純粹的編譯出動態庫,參考之前的一篇文章《當 Android 開發者遇見 TensorFlow》,這篇文章就簡單介紹一下如何編譯動態庫
clone 代碼
| 1 | git clone https://github.com/tensorflow/tensorflow.git |
修改TensorFlow項目根下的WROKSPACE文件
將以下代碼反注釋
| 1234567891011121314151617181920 | # 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 systempath = "/Users/lizhangqu/AndroidSDK",)#android_ndk_repository( name="androidndk", path="/Users/lizhangqu/AndroidNDK/android-ndk-r14b",# 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) |
然后修改android_sdk_repository中的path為自己電腦中的android sdk目錄,修改android_ndk_repository中的path為自己電腦的android ndk目錄。
值得注意的是,ndk的版本,官方建議使用大于r14的版本,下載地址android-ndk-r14b-darwin-x86_64.zip
編譯
確保必要的工具已經安裝,如bazel
| 1234 | bazel build --cxxopt='--std=c++11' //tensorflow/contrib/lite/java:tensorflowlite \--crosstool_top=//external:android/crosstool \--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \--cpu=armeabi |
編譯其他ABI請修改cpu參數,分別為
| 1234567 | --cpu=armeabi--cpu=armeabi-v7a--cpu=arm64-v8a--cpu=mips--cpu=mips64--cpu=x86--cpu=x86_64 |
產物位于
| 12 | bazel-bincontribjava/libtensorflowlite_jni.sobazel-bincontribjava/libtensorflowlitelib.jar |
注意編譯mips和mips64的時候,需要將構建腳本稍微修改一下,刪除一部分代碼,否則會報錯
找到/tensorflow/contrib/lite/build_def.bzl文件,找到如下代碼
| 12345678910111213141516171819202122232425262728293031323334353637383940414243 | def tflite_linkopts_unstripped(): """Defines linker flags to reduce size of TFLite binary. These are useful when trying to investigate the relative size of the symbols in TFLite. Returns: a select object with proper linkopts """ return select({ "//tensorflow:android": [ "-Wl,--no-export-dynamic", # Only inc syms referenced by dynamic obj. "-Wl,--exclude-libs,ALL", # Exclude syms in all libs from auto export. "-Wl,--gc-sections", # Eliminate unused code and data. "-Wl,--as-needed", # Don't link unused libs.], "//tensorflow/contrib/lite:mips": [], "//tensorflow/contrib/lite:mips64": [], "//conditions:default": [ "-Wl,--icf=all", # Identical code folding.],})def tflite_jni_linkopts_unstripped(): """Defines linker flags to reduce size of TFLite binary with JNI. These are useful when trying to investigate the relative size of the symbols in TFLite. Returns: a select object with proper linkopts """ return select({ "//tensorflow:android": [ "-Wl,--gc-sections", # Eliminate unused code and data. "-Wl,--as-needed", # Don't link unused libs.], "//tensorflow/contrib/lite:mips": [], "//tensorflow/contrib/lite:mips64": [], "//conditions:default": [ "-Wl,--icf=all", # Identical code folding.],}) |
將上面代碼中的下面這段刪除
| 123456 | "//tensorflow:android": [ "-Wl,--no-export-dynamic", # Only inc syms referenced by dynamic obj. "-Wl,--exclude-libs,ALL", # Exclude syms in all libs from auto export. "-Wl,--gc-sections", # Eliminate unused code and data. "-Wl,--as-needed", # Don't link unused libs.], |
以及這一段也刪除
| 1234 | "//tensorflow:android": [ "-Wl,--gc-sections", # Eliminate unused code and data. "-Wl,--as-needed", # Don't link unused libs.], |
臨時刪除后,編譯完mips和mips64還原即可,不然會報如下錯誤
| 12345678910 | ERROR: /Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so://tensorflow/contrib/lite:mips//tensorflow:androidMultiple matches are not allowed unless one is unambiguously more specialized.ERROR: Analysis of target '//tensorflow/contrib/lite/java:tensorflowlite' failed; build aborted:/Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so://tensorflow/contrib/lite:mips//tensorflow:androidMultiple matches are not allowed unless one is unambiguously more specialized. |
| 12345678910 | ERROR: /Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so://tensorflow/contrib/lite:mips64//tensorflow:androidMultiple matches are not allowed unless one is unambiguously more specialized.ERROR: Analysis of target '//tensorflow/contrib/lite/java:tensorflowlite' failed; build aborted:/Users/lizhangqu/Desktop/tensorflow/tensorflow/contrib/lite/java/BUILD:133:1: Illegal ambiguous match on configurable attribute "linkopts" in //tensorflow/contrib/lite/java:libtensorflowlite_jni.so://tensorflow/contrib/lite:mips64//tensorflow:androidMultiple matches are not allowed unless one is unambiguously more specialized. |
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的Tensorflow Lite 编译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 在 NDK 层使用 Op
- 下一篇: aapt2 适配之资源 id 固定