文章目录
0 关于tflite micro
关于tflite micro在这里接不做过多介绍了,可以查看相关论文:TensorFlow Lite Micro: Embedded Machine Learning on TinyML Systems
我非常喜欢tflite micro,占用资源很少,是一个用来学习推理框架的好工程。
1 克隆仓库
在tensorflow的git里面,看到git已经迁移至一个单独的仓库。
As of June 25, 2021, the TFLM codebase has moved to a stand-alone github repository.
于是执行命令:
git clone https://github.com/tensorflow/tflite-micro.git
2 编译静态库
求助了ChatGPT 4.0:
「在这个 https://github.com/tensorflow/tflite-micro , 怎么编译静态库?」
在TensorFlow Lite Micro的GitHub仓库中,你可以使用以下步骤来编译静态库:
首先,你需要克隆仓库:
git clone https://github.com/tensorflow/tflite-micro.git
进入克隆下来的仓库目录:
cd tflite-micro
使用make命令编译静态库:
make -f tensorflow/lite/micro/tools/make/Makefile TARGET=<TARGET> TARGET_ARCH=<ARCH> microlite
在上述命令中,<TARGET>应替换为你的目标平台(如linux),<ARCH>应替换为你的目标架构(如x86_64)。这将在tensorflow/lite/micro/tools/make/gen/<TARGET>_<ARCH>/lib/目录下生成一个名为libtensorflow-microlite.a的静态库。
请注意,你可能需要根据你的具体需求和环境来修改这些步骤。例如,你可能需要安装额外的依赖库,或者修改makefile来包含你需要的特性。
真是太棒了,于是我执行指令:
make -f tensorflow/lite/micro/tools/make/Makefile TARGET=linux TARGET_ARCH=x86_64 microlite
在 make/download/ 目录下,分别下载了flatbuffers、gemmlowp、kissfft、pigweed、ruy(如果你“网络不佳”,这里可能要花些精力)。
编译完成后,得到了静态库文件——
ar: creating gen/linux_x86_64_default/lib/libtensorflow-microlite.a
3 模型转换
以lenet5模型为例:
- 先把tflite模型转换为cpp文件:
xxd -i lenet5.tflite > lenet5.cpp
- 包一下模型接口
在lenet5.cpp的文件最后加入了这几行代码unsigned char * get_model_pointer() { return lenet5_tflite; } unsigned int get_model_size() { return lenet5_tflite_len; }
- 增加函数头文件
#ifndef __MODEL_INTERFACE_H__ #define __MODEL_INTERFACE_H__ unsigned char * get_model_pointer(); unsigned int get_model_size(); #