1. 简介
TensorFlow Lite是一个用于端侧推理的工具集,它可以让我们将TensorFlow模型部署到手机、嵌入式设备甚至物联网设备上。它主要分为两部分:模型转换工具以及模型推理引擎。
顾名思义,模型转换工具是用于对模型进行转换的,目前也只是将TensorFlow导出的.pb模型转换为.tflite模型。对于高通SNPE这种面向多种框架导出的不同格式的模型的推理引擎,有必要将不同格式的模型统一到一个标准,因此需要一个转换工具,但是TensorFlow Lite是用于TensorFlow Lite模型推理的,那为什还要进行转换呢?原因很简单,TensorFlow Lite使用运行于移动端,有些设备甚至是资源非常有限,因此在内存和解析方面必须尽可能减少开销。TensorFlow导出的模型使用的是Protocol Buffer协议,因此有必要将它转换到性能更优的FlatBuffer格式。
作为端侧推理引擎,TensorFlow Lite有以下几个有点:
- 低延迟,省去了与服务器沟通的来回时间;
- 隐私性能好,所有数据都只在本地处理;
- 随时随地,没网也行;
- 低功耗,相比于网络通信,推理所需要的电能会更少。
接下来的日子,就让我们一起骑上心爱的小摩托,徜徉在TensorFlow Lite的代码里吧。
2. 使用流程
下面展示的是一个官方给的C++中使用TFLite的例子:
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "minimal <tflite model>\n");
return 1;
}
const char* filename = argv[1];
// Load model
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(filename);
TFLITE_MINIMAL_CHECK(model != nullptr);
// Build the interpreter with the InterpreterBuilder.
// Note: all Interpreters should be built with the InterpreterBuilder,
// which allocates memory for the Intrepter and does various set up
// tasks so that the Interpreter can read the provided model.
tflite::ops::builtin::BuiltinOpResolver resolver;
tflite::InterpreterBuilder builder(*model, resolver);
std::unique_ptr<tflite::Interpreter> interpreter;
builder(&interpreter);
TFLITE_MINIMAL_CHECK(interpreter != nullptr);
// Allocate tensor buffers.
TFLITE_MINIMAL_CHECK(interpreter->AllocateTensors() == kTfLiteOk);
printf("=== Pre-invoke

本文介绍了TensorFlow Lite作为端侧推理工具的用途,包括模型转换成.tflite格式的原因和优势,如低延迟、隐私保护和低功耗。详细解析了TFLite的使用流程,包括加载模型、创建推理引擎、准备输入数据和执行推理。通过C++代码示例展示了如何使用TFLite进行推理,解释了建造者模式在实例化过程中的重要性,并提供了一个简单的推理过程实例。
最低0.47元/天 解锁文章
765

被折叠的 条评论
为什么被折叠?



