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