繁体   English   中英

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

[英]How to work with TF Lite library in a c++ project

在过去的 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);
}

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

提前致谢

试试下面的代码,它已经用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();

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM