本文来自社区投稿与征集,作者 郑亚斌,Arm 中国生态技术市场经理。
概述:
TensorFlow Lite Micro 是 TensorFlow Lite 针对 AIOT 的轻量级 AI 引擎,用于在微控制器和其他资源受限的设备上运行机器学习模型。端云链接从 TencentOS Tiny 开始。
1. 建立与转换模型
由于嵌入式设备存储空间有限,因此限制了深度学习的应用,同时考虑到平台算力以及算子支持等因素,因此在模型设计以及部署阶段需充分了解硬件平台资源以及预期性能。
本部分主要介绍如何将 TensorFlow 模型部署在资源受限的嵌入式设备的过程。
1.1模型转换
将一个已训练好的 TensorFlow 模型转换为可以在嵌入式设备中运行的 TensorFlow Lite 模型可以使用 TensorFlow Lite 转换器 Python API。它能够将模型转换成 FlatBuffer 格式,减小模型规模,并修改模型及算子以支持 TensorFlow Lite 运算。
-
TensorFlow Lite 转换器 Python API
https://tensorflow.google.cn/lite/microcontrollers/build_convert
-
FlatBuffer
https://google.github.io/flatbuffers/
1.1.1 量化
为了获得尽可能小的模型,某些情况下可以考虑使用训练后量化。它会降低模型中数字的精度,从而减小模型规模,比如将 FP32 转化为 Int8。不过,这种操作可能会导致模型推理准确性的下降,对于小规模模型尤为如此,所以我们需要在量化前后分析模型的准确性,以确保这种损失在可接受范围内。
-
训练后量化
https://tensorflow.google.cn/lite/performance/post_training_quantization
训练后量化需要 representive dataset 作为参考,以下这段 Python 代码片段展示了如何使用训练后量化进行模型转换:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
def representative_dataset_generator():
for value in reference_data:
yeild [np.array(value, dtype=np.float32, ndmin=2)]
converter.representative_dataset = representative_dataset_generator
tflite_quant_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_quant_model)
1.1.2 将模型文件转换为一个 C 数组
许多微控制器平台没有本地文件系统,从程序中使用一个模型最简单的方式是将其转换为 C 数组并将其编译进应用程序。
以下的 unix 命令会生成一个包含 TensorFlow Lite 模型的 C 源文件,其中模型数据以 char 数组形式表现:
xxd -i converted_model.tflite > model_data.cc
其输出类似如下:
unsigned char converted_model_tflite[] = {
0x18, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x0e, 0x00,
};
unsigned int converted_model_tflite_len = 18200;
生成此文件后,你可以将其包含到程序中。在嵌入式平台上,我们需要将该数组声明为 const 类型以获得更好的内存效率。
1.2 模型结构与训练
在设计一个面向微控制器的模型时,考虑模型的规模、工作负载以及模型所使用到的算子是非常重要的。
1.2.1 模型规模
一个模型必须在二进制和运行时方面都足够小,以使其编译进应用程序后满足目标设备的内存限制。
为了创建一个更小的模型,你可以在模型设计中采用少而小的层。然而,小规模的模型更易导致欠拟合问题。这意味着对于许多应用,尝试并使用符合内存限制的尽可能大的模型是有意义的。但是,使用更大规模的模型也会增加处理器工作负载。
注:在一个 Cortex M3 上,TensorFlow Lite Micro 的 core runtime 仅占约16 KB。
1.2.2 工作负载
工作负载受到模型规模,结构与复杂度的影响,大规模、复杂的模型可能会导致更高的功耗。在实际的应用场景中,功耗与热量的增加可能会带来其他问题。
1.2.3 算子支持
TensorFlow Lite Micro 目前仅支持有限的 TensorFlow 算子,因此可运行的模型也有所限制。Google 正致力于在参考实现和针对特定结构的优化方面扩展算子支持。Arm 的 CMSIS-NN 开源加速库也为算子的支持和优化提供了另一种可能。
已支持的算子在文件 all_ops_resolver.cc 中列出。
-
all_ops_resolver.cc
1.3 运行推断
以下部分将介绍软件包自带语音例程中的 main_functions.cc 文件,并阐述了如何使用 TensorFlow Lite Micro 来进行 AI 推理。
-
main_functions.cc
1.3.1 包含项
#include "tensorflow/lite/micro/kernels/micro_ops.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"
-
micro_ops.h 提供给解释器(interpreter)用于运行模型的操作(链接见文末)。
-
micro_error_reporter.h 输出调试信息。
-
micro_interpreter.h TensorFlow Lite Micro 解释器,用来运行模型。
-
schema_generated.h定义 TensorFlow Lite FlatBuffer 数据结构。
-
version.h 提供 TensorFlow Lite 架构的版本信息。
示例中还包括其他一些文件,比如:
#include "tensorflow/lite/micro/examples/micro_speech/micro_features/micro_model_settings.h"
#include "tensorflow/lite/micro/examples/micro_speech/micro_features/model.h"
model.h 将模型存储为 char 类型数组。阅读 "构建与转换模型" 了解如何将 TensorFlow Lite 模型转换为该格式。
-
构建与转换模型
https://tensorflow.google.cn/lite/microcontrollers/build_convert
micro_model_settings.h 定义与模型相关的各种常量。
1.3.2 设置日志记录
要记录日志,需要实例化 tflite::MicroErrorReporter 类:
tflite::MicroErrorReporter micro_error_reporter;
tflite::ErrorReporter* error_reporter = µ_error_reporte