1. 先 git clone
tensorflow 的倉庫,可以在 github 也可以在 gitee, gitee 會比 github 慢一天左右。但是下載速度快多了。
git clone https://github.com/tensorflow/tensorflow.git
git clone https://gitee.com/mirrors/tensorflow.git
2. 確定環境里面有 g++, gcc, ar
3. 下載需要的依賴
./tensorflow/lite/tools/make/download_dependencies.sh
下載的依賴文件在 ./tensorflow/lite/tools/make/downloads
文件夾下面
4. 修改 Makefile
vim ./tensorflow/lite/tools/make/Makefile
把里面的
CXX := $(CC_PREFIX)${TARGET_TOOLCHAIN_PREFIX}g++
CC := $(CC_PREFIX)${TARGET_TOOLCHAIN_PREFIX}gcc
AR := $(CC_PREFIX)${TARGET_TOOLCHAIN_PREFIX}ar
這三行,修改為對應的 g++, gcc, ar
4. 編譯為庫
./tensorflow/lite/tools/make/build_aarch64_lib.sh
生成的靜態庫文件在 tensorflow/lite/tools/make/gen/aarch64_armv8-a/lib/libtensorflow-lite.a
參考: https://tensorflow.google.cn/lite/guide/build_arm64?hl=zh-cn
5. 抽取 tflite 的頭文件,並打包
cd tensorflow/tensorflow
find ./lite -name "*.h" | tar -cf headers.tar -T -
這個 headers.tar 里面包含了頭文件和一些不需要的東西。
注意:有些頭文件在 tensorflow/tensorflow/lite/tools/make/downloads 這個下載的包里面。比如說 flatbuffers,要把他們專門復制到相應的頭文件夾下面。
include 頭文件夾下面的組織形式
[xxx@localhost include]$ tree -L 3
.
├── downloads
│ └── include
│ └── flatbuffers
└── tensorflow
└── lite
├── allocation.h
├── arena_planner.h
├── builtin_op_data.h
├── builtin_ops.h
├── c
├── context.h
├── context_util.h
├── core
├── delegates
├── error_reporter.h
├── examples
├── experimental
├── external_cpu_backend_context.h
├── graph_info.h
├── interpreter_builder.h
├── interpreter.h
├── java
├── kernels
├── memory_planner.h
├── micro
├── minimal_logging.h
├── model_builder.h
├── model.h
├── mutable_op_resolver.h
├── nnapi
├── op_resolver.h
├── optional_debug_tools.h
├── profiling
├── python
├── schema
├── simple_memory_arena.h
├── stderr_reporter.h
├── string_type.h
├── string_util.h
├── testing
├── tflite_with_xnnpack_optional.h
├── toco
├── tools
├── type_to_tflitetype.h
├── util.h
└── version.h
20 directories, 26 files
參考: https://blog.csdn.net/shui123546yi/article/details/105410781
6. 把前面編譯好的靜態庫也復制到 lib 文件夾下面。
7. 編寫 cmake 文件
#注意:如果工程有依賴庫的話,ADD_EXECUTABLE指令要放在LINK_DIRECTORIES指令之后,
# 不然會報錯:Linking C executable main
# /usr/bin/ld: cannot find -lhello
# collect2: ld 返回 1
#1) 設置 cmake 的最低版本
cmake_minimum_required(VERSION 3.10)
#2) 設置 project 名稱
project(tflite_test)
#3) 設置代碼源文件列表
set(SRC_LIST main.cpp model.cpp)
#4) 增加頭文件搜索路徑,解決編譯期間找不到頭文件的問題
#COMMAND: INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dire1 dire2 ...)
#定義:向工程添加多個特定的頭文件搜索路徑,路徑之間用空格分開,
# 如果路徑中包含空格,可以使用雙引號括起來
# 默認是追加到當前的頭文件搜索路徑之后,你可以用2種方式控制搜索路徑的添加>方式
# 1)CMAKE_INCLUDE_DIRECTORIES_BEFORE 通過SET設置其為on,使用前置模式
# 2)通過AFTER或BEFORE參數,控制追加還是置前
include_directories("/usr/include/x86_64-linux-gnu")
include_directories("./include")
include_directories(".")
include_directories("./include/downloads/include")
#5) 增加庫文件: 解決鏈接期間找不到調用外部接口的問題
#main.cpp:(.text+0x5): undefined reference to `HelloFunc()'
#collect2: error: ld returned 1 exit status
#6) 增加庫文件搜索路徑:解決鏈接期間找不到庫文件的問題
#COMMAND: LINK_DIRECTORIES(dir1 dir2 ...)
#定義:添加非標准的共享庫搜索路徑
#/usr/bin/ld: cannot find -lhello
#collect2: error: ld returned 1 exit status
#好像相對路徑會找不到庫文件
link_directories("/usr/lib/x86_64-linux-gnu")
link_directories("./lib")
#7) 生成二進制文件
add_executable(${PROJECT_NAME} ${SRC_LIST})
#8) 鏈接庫
#COMMAND: TARGET_LINK_LIBRARIES(target library1
# <debug | optimized> library2
# ...)
#定義:用來為target添加需要鏈接的共享庫
#TARGET_LINK_LIBRARIES(${PROJECT_NAME} hello) #鏈接動態庫指令
#TARGET_LINK_LIBRARIES(${PROJECT_NAME} libhello.a) #鏈接靜態庫指令
target_link_libraries(${PROJECT_NAME} PRIVATE tensorflow-lite pthread ${CMAKE_DL_LIBS})
注意:如果提示沒有 undefined reference to dlopen
,undefined reference to pthread_create
別忘了在鏈接庫里面添加 pthread
${CMAKE_DL_LIBS}
參考:
https://github.com/jiangxinyang227/nlp_tflite/blob/master/cpp_tflite/src/inference.cpp
https://www.cnblogs.com/jiangxinyang/p/13215724.html
https://github.com/tensorflow/tensorflow/tree/master/tensorflow
https://tensorflow.google.cn/lite/microcontrollers/get_started
https://www.cnblogs.com/vitoyeah/p/10273299.html
https://github.com/gdyshi/model_deployment/blob/master/tflite/C%2B%2B/model.cc
https://github.com/gdyshi/model_deployment/blob/master/tflite/C%2B%2B/example.cc
https://github.com/gdyshi/model_deployment
https://blog.csdn.net/chongtong/article/details/90379347
https://blog.csdn.net/chongtong/column/info/39386
https://blog.csdn.net/chongtong/article/details/95355814