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

标签: C++

添加新评论