TensorFlow Lite C ++ API示例进行推理

DocDriven

我正在尝试获取一个TensorFlow Lite示例以在具有ARM Cortex-A72处理器的计算机上运行。不幸的是,由于缺少有关如何使用C ++ API的示例,因此我无法部署测试模型。我将尝试解释到目前为止我所取得的成就。

创建tflite模型

我创建了一个简单的线性回归模型并将其转换为近似函数f(x) = 2x - 1我从一些教程中获得了此代码段,但现在找不到了。

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.contrib import lite

model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')

xs = np.array([ -1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([ -3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)

model.fit(xs, ys, epochs=500)

print(model.predict([10.0]))

keras_file = 'linear.h5'
keras.models.save_model(model, keras_file)

converter = lite.TocoConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open('linear.tflite', 'wb').write(tflite_model)

这将创建一个名为的二进制文件linear.tflite,我应该可以加载它。

为我的机器编译TensorFlow Lite

TensorFlow Lite随附一个脚本,用于在具有aarch64架构的计算机上进行编译。即使必须稍加修改Makefile,我仍按照此处的指南进行操作。请注意,我是在目标系统上本地编译的。这创建了一个名为的静态库libtensorflow-lite.a

问题:推论

我尝试按照此处站点上的教程进行操作,只是将加载和运行模型中的代码段粘贴在一起,例如

class FlatBufferModel {
  // Build a model based on a file. Return a nullptr in case of failure.
  static std::unique_ptr<FlatBufferModel> BuildFromFile(
      const char* filename,
      ErrorReporter* error_reporter);

  // Build a model based on a pre-loaded flatbuffer. The caller retains
  // ownership of the buffer and should keep it alive until the returned object
  // is destroyed. Return a nullptr in case of failure.
  static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
      const char* buffer,
      size_t buffer_size,
      ErrorReporter* error_reporter);
};

tflite::FlatBufferModel model("./linear.tflite");

tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);

// Resize input tensors, if desired.
interpreter->AllocateTensors();

float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.

interpreter->Invoke();

float* output = interpreter->typed_output_tensor<float>(0);

当试图通过编译

g++ demo.cpp libtensorflow-lite.a

我遇到了很多错误。日志:

root@localhost:/inference# g++ demo.cpp libtensorflow-lite.a 
demo.cpp:3:15: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type
   static std::unique_ptr<FlatBufferModel> BuildFromFile(
               ^~~~~~~~~~
demo.cpp:10:15: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type
   static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
               ^~~~~~~~~~
demo.cpp:16:1: error: ‘tflite’ does not name a type
 tflite::FlatBufferModel model("./linear.tflite");
 ^~~~~~
demo.cpp:18:1: error: ‘tflite’ does not name a type
 tflite::ops::builtin::BuiltinOpResolver resolver;
 ^~~~~~
demo.cpp:19:6: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type
 std::unique_ptr<tflite::Interpreter> interpreter;
      ^~~~~~~~~~
demo.cpp:20:1: error: ‘tflite’ does not name a type
 tflite::InterpreterBuilder(*model, resolver)(&interpreter);
 ^~~~~~
demo.cpp:23:1: error: ‘interpreter’ does not name a type
 interpreter->AllocateTensors();
 ^~~~~~~~~~~
demo.cpp:25:16: error: ‘interpreter’ was not declared in this scope
 float* input = interpreter->typed_input_tensor<float>(0);
                ^~~~~~~~~~~
demo.cpp:25:48: error: expected primary-expression before ‘float’
 float* input = interpreter->typed_input_tensor<float>(0);
                                                ^~~~~
demo.cpp:28:1: error: ‘interpreter’ does not name a type
 interpreter->Invoke();
 ^~~~~~~~~~~
demo.cpp:30:17: error: ‘interpreter’ was not declared in this scope
 float* output = interpreter->typed_output_tensor<float>(0);
                 ^~~~~~~~~~~
demo.cpp:30:50: error: expected primary-expression before ‘float’
 float* output = interpreter->typed_output_tensor<float>(0);

我是C ++的新手,所以这里可能缺少明显的东西。但是,似乎其他人也对C ++ API感到麻烦(请参见GitHub问题)。是否有人也偶然发现并运行了它?

我要介绍的最重要方面是:

1.)我在哪里以及如何定义签名,以便模型知道将什么视为输入和输出?

2.)我必须包括哪些标题?

谢谢!

编辑

感谢@Alex Cohn,链接器能够找到正确的标头。我还意识到,我可能不需要重新定义flatbuffers类,因此我得到了以下代码(标记了小更改):

#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"

auto model = tflite::FlatBufferModel::BuildFromFile("linear.tflite");   //CHANGED

tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);

// Resize input tensors, if desired.
interpreter->AllocateTensors();

float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.

interpreter->Invoke();

float* output = interpreter->typed_output_tensor<float>(0);

这样可以大大减少错误的数量,但是我不确定如何解决其余的错误:

root@localhost:/inference# g++ demo.cpp -I/tensorflow
demo.cpp:10:34: error: expected ‘)’ before ‘,’ token
 tflite::InterpreterBuilder(*model, resolver)(&interpreter);
                                  ^
demo.cpp:10:44: error: expected initializer before ‘)’ token
 tflite::InterpreterBuilder(*model, resolver)(&interpreter);
                                            ^
demo.cpp:13:1: error: ‘interpreter’ does not name a type
 interpreter->AllocateTensors();
 ^~~~~~~~~~~
demo.cpp:18:1: error: ‘interpreter’ does not name a type
 interpreter->Invoke();
 ^~~~~~~~~~~

我该如何解决这些问题?看来我必须定义自己的解析器,但是我不知道该怎么做。

亚历克斯·科恩

这是最小的包括:

#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"

这些将包括其他标头,例如<memory>define std::unique_ptr

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在(Android Studio)NDK(C / C ++ API)中运行Tensorflow-Lite推理?

如何设置Tensorflow Lite C ++的输入

如何使用TensorFlow Lite进行批处理?

如何基于模型 Tensorflow lite 进行预测?

在Tensorflow Lite C API中注册自定义运算符

从C ++中的Tensorflow的.meta文件加载图形进行推理

Tensorflow js VS Tensorflow Lite

使用 Keras Functional API 为 Tensorflow LITE 构建模型

Tensorflow Lite 模型可以用于 Windows 10 上的推理吗?

尝试在Android上使用专为ARM64构建的Tensorflow-Lite C API时使用未定义的引用-NDK ARM独立工具链

带有 NativeScript 的 Tensorflow lite

将Tensorflow转换为Tensorflow-lite

我可以在PC(x86,x64)平台上使用Tensorflow lite或Tensorflow mobile吗?如果可以的话,是否已针对此平台进行了推理优化?

为TensorFlow Lite C ++编写read_jpeg和Decode_jpeg函数

如何解释tensorflow lite c ++中的输出张量数据打包?

指定输入/输出节点以在TensorFlow 1.0+中在加载了C ++ API的模型上运行推理

使用保存的模型进行TensorFlow推理

修改并组合使用tensorflow对象检测API生成的两个不同的冻结图以进行推理

将ONNX模型转换为TensorFlow Lite

为Edge TPU构建Tensorflow Lite失败

Tensorflow Lite模型输出错误

tensorflow lite 添加自定义操作

Tensorflow Lite现在支持设备培训吗

TensorFlow Lite是否支持keras函数Flatten()?

TensorFlow Lite无法识别op VarHandleOp

在 python 中加载 Tensorflow Lite 模型

带有自定义模型的 Tensorflow lite 示例 - “input_product_scale < output_scale was not true”

为什么TensorFlow Lite比台式机上的TensorFlow慢?

Tensorflow Lite 模型输出比 Tensorflow 模型更大的值