在过去的 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/