c++ - 如何在 C++ 项目中使用 TF Lite 库

标签 c++ tensorflow tensorflow-lite

在过去的 1-2 天里,我一直在为如何构建 TensorFlow Lite 而苦苦挣扎,以便我可以在我自己的 C\C++ 项目中将其用作 header 或库。

例如,我有一个带有 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);
}

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

提前致谢

最佳答案

试试下面的代码,已经用 TensorFlow 测试过了精简版 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();

关于c++ - 如何在 C++ 项目中使用 TF Lite 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573425/

相关文章:

tensorflow - 模块 'tensorflow' 没有属性 'contrib'

tensorflow 构建错误

java - 将自定义TensorFlowLite模型添加到就绪的Android应用程序时出现问题

android - 用最少的精力进行面部识别?

c++ - 树莓派上的 tensorflow lite 量化 ssd 对象检测

c++ - 如何使模板类型推导与引用一起工作?

c++ - C++ Singleton vs静态类vs extern vs命名空间

c++ - 将路径作为参数传递时出现 "error: cannot convert ' std::__cxx11::string* {aka std::__cxx11::basic_string<char>* }' to ' const char*' 错误

c++ - 将定义的宏字符串转换为 void*

arrays - Tensorflow 中的二进制掩码