【问题标题】:How to work with TF Lite library in a c++ project如何在 c++ 项目中使用 TF Lite 库
【发布时间】:2019-10-27 15:07:55
【问题描述】:

在过去的 1-2 天里,我一直在苦苦研究如何构建 TensorFlow Lite,以便我可以在自己的 C\C++ 项目中将其用作头文件或库。

例如,我有一个带有 main.cpp 的 C++ 项目,代码如下:

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

int main()
{
    std::unique_ptr<tflite::FlatBufferModel> model;
    model = tflite::FlatBufferModel::BuildFromBuffer(h5_converted_tflite, h5_converted_tflite_len);

    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);
}

我应该下载什么\build,从哪里下载,这样我才能成功编译这段代码?目前它显然说找不到 h 文件,当我克隆 TF 存储库并将其添加到包含文件夹时,它没有找到“flatbuffers.h”文件,当我手动添加它时,它给出了我有很多链接错误。 任何帮助将不胜感激......

提前致谢

【问题讨论】:

  • 根据您的架构和目标操作系统,您可以在此处找到构建 tf lite 所需的所有文件:github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/…。首先,您必须运行脚本来安装依赖项,然后您应该能够运行 makefile。
  • @pierrom 非常感谢。我的操作系统是 64 位 Windows。我应该运行 download_dependencies.sh 然后 build_aarch64_lib.sh 吗?这些脚本的输出是什么?
  • 没有 aarch64 不适合你。这适用于安卓智能手机中使用的 ARM 处理器。在 Github 上有一个拉取请求,试图在 Windows 上实现 tf lite 的构建配置,但尚未合并。
  • @pierrom 我可以提供该 PR 的链接以查看其更改\添加吗?

标签: c++ tensorflow tensorflow-lite


【解决方案1】:

最近,TFLite 支持 CMake,它似乎解决了您的依赖问题。

https://www.tensorflow.org/lite/guide/build_cmake#create_a_cmake_project_which_uses_tensorflow_lite

【讨论】:

    【解决方案2】:

    试试下面的代码,已经用TensorFlow lite 1.14.0 测试过:

    std::string str = "model.tflite";
    ifstream file(str, std::ifstream::binary);
    file.seekg(0, file.end);
    int length = file.tellg();
    file.seekg(0, file.beg);
    char * model_data = new char [length];
    file.read (model_data, length);
    file.close();
    std::unique_ptr<tflite::Interpreter> interpreter;
    std::unique_ptr<tflite::FlatBufferModel> model;
    tflite::ops::builtin::BuiltinOpResolver resolver;
    model = tflite::FlatBufferModel::BuildFromBuffer(model_data, length);
    tflite::InterpreterBuilder(*model, resolver)(&interpreter);
    interpreter->AllocateTensors();
    

    【讨论】:

    • 好像缺少了一些重要的东西:resolver 是什么?
    • 对不起tflite::ops::builtin::BuiltinOpResolver resolver;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多