編譯Tensorflow Lite 靜態(tài)庫(kù)

簡(jiǎn)介

最近需要編譯Tensorflow Lite的靜態(tài)庫(kù),但是網(wǎng)上資料不是很多,要么就是編譯出問(wèn)題

本來(lái)是根據(jù)官網(wǎng)來(lái)進(jìn)行編譯的

Build TensorFlow Lite with CMake

但是編譯的時(shí)候會(huì)出現(xiàn)以下提示:

CMake Warning:
 Manually-specified variables were not used by the project:
  ANDROID_ABI

ANDROID_ABI似乎并沒(méi)有起到作用

不過(guò)我又找到另一種方式,并且實(shí)測(cè)是可行的

防止以后踩坑,在此記錄一下,希望能幫助需要的人

PS:以下內(nèi)容我寫在了我的Github上面,所以內(nèi)容基本是一樣的

構(gòu)建Tensorflow Lite 靜態(tài)庫(kù)

準(zhǔn)備

Tensorflow版本:2.4.1

NDK版本:r18b

git clone https://github.com/rianlu/build_tflite_static_library.git

git clone https://github.com/tensorflow/tensorflow.git

倉(cāng)庫(kù)里已經(jīng)有編譯好的Tensorflow Lite 2.4.1的靜態(tài)庫(kù)了,可以直接使用

生成工具鏈

生成對(duì)應(yīng)架構(gòu)的工具鏈

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch arm --api 21 --install-dir arm-android-toolchain

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch arm64 --api 21 --install-dir arm64-android-toolchain

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch x86 --api 21 --install-dir x86-android-toolchain

$ANDROID_NDK/build/tools/make_standalone_toolchain.py --arch x86_64 --api 21 --install-dir x86_64-android-toolchain

修改腳本

下載對(duì)應(yīng)的Tensorflow源碼,把倉(cāng)庫(kù)里的tensorflow/lite/tools/make/.sh放到Tensorflow源碼的對(duì)應(yīng)位置,這里額外添加了x86以及x86_64的腳本

然后把倉(cāng)庫(kù)里的tensorflow/lite/tools/make/targets/android_makefile.inc放到Tensorflow源碼的對(duì)應(yīng)位置,這里已經(jīng)對(duì)android_makefile.inc作了修改,可以支持arm64-v8a、armeabi-v7ax86、x86_64四種架構(gòu)

然后修改tensorflow/lite/tools/make/.sh里的ndk_standalone_root,使其對(duì)應(yīng)各個(gè)架構(gòu)的工具鏈

修改Makefile.android

如果使用的是Tensorflow 2.4.1可以直接使用倉(cāng)庫(kù)里的Makefile.android,原倉(cāng)庫(kù)里的Makefile.android對(duì)應(yīng)的版本是Tensorflow 2.3.0,如果是更高版本,作如下修改:

進(jìn)入到${tensorflow_root}/tensorflow/lite/tools/make目錄,將Makefile改為Makefile.android(可以先備份一下)

# 注釋下面兩行

# TARGET_TOOLCHAIN_PREFIX :=
# CC_PREFIX :=

# 原
PROFILER_SRCS := \
    tensorflow/lite/profiling/memory_info.cc \
    tensorflow/lite/profiling/platform_profiler.cc \
    tensorflow/lite/profiling/time.cc
    
# 修改后
PROFILER_SRCS := \
    tensorflow/lite/profiling/memory_info.cc \
    tensorflow/lite/profiling/platform_profiler.cc \
    tensorflow/lite/profiling/atrace_profiler.cc \
    tensorflow/lite/profiling/time.cc
    
# 原
ifeq ($(TARGET_ARCH),aarch64)

# 修改后
ifeq ($(findstring $(TARGET_ARCH),  android_armv8a, android_armv7a),)

執(zhí)行腳本

執(zhí)行./download_dependencies.sh,下載Tensorflow Lite依賴

執(zhí)行chmod +x build_android_xx.sh以及./build_android_xx.sh

可能會(huì)出現(xiàn)以下錯(cuò)誤:

fatal error: -f/--auxiliary may not be used without -shared
clang70++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/enjoymusic/Development/tensorflow-repository/tensorflow-2.4.1/tensorflow/lite/tools/make/gen/android_armv7a/bin/benchmark_model] Error 1

這個(gè)對(duì)生成的benchmark-lib.a可能有影響,但是對(duì)libtensorflow-lite.a無(wú)影響

到此就完成了Tensorflow Lite靜態(tài)庫(kù)的編譯,如果有什么疑問(wèn)或建議,一起討論~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容