使用cpp推理tflite模型

发布于 2025-01-15 22:35:34 字数 1097 浏览 3 评论 0原文

我正在尝试使用 cpp 推断我的 tflite 模型。我已经训练了模型并保存了 h5 文件。现在我正在尝试使用 cpp 加载它并获得预测。我尝试了以下一组代码。

#include <iostream>
#include <cstdio>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"

int main(int argc, char** argv) {
    auto model = tflite::FlatBufferModel::BuildFromFile("model.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);
    
    return 0;
}

但我收到一个错误:

3   41  E:\kaino\TF\tf\app\main.cpp [Error] tensorflow/lite/interpreter.h: No such file or directory

我必须安装软件包还是必须添加另一个文件?我对此很陌生,请有人帮助我解决这个问题。

I am trying to infer my tflite model using cpp. I have trained the model and save h5 file. Now I'm trying to load it with cpp and get predictions. I tried the following set of codes.

#include <iostream>
#include <cstdio>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"

int main(int argc, char** argv) {
    auto model = tflite::FlatBufferModel::BuildFromFile("model.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);
    
    return 0;
}

But I'm getting an error as:

3   41  E:\kaino\TF\tf\app\main.cpp [Error] tensorflow/lite/interpreter.h: No such file or directory

Do I have to install a package or do I have to add another file? I'm new to this and please can someone help me to resolve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2025-01-22 22:35:34

您的编译器可能缺少包含目录。
基本上,您希望包含“tensorflow/lite/...”等所有文件所在的路径,以便编译器可以搜索该目录并找到所需的头文件。

GCC 示例,

g++ main.cc -I /tmp/my_local_path

了解更多信息 https://gcc.gnu .org/onlinedocs/gcc/Directory-Options.html#Directory-Options

You are probably missing includes directory to your compiler.
Basically you want to include the path where all files like "tensorflow/lite/..." are there, so the compiler can search this directory and find the required header files.

Example for GCC,

g++ main.cc -I /tmp/my_local_path

For more information https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Directory-Options

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文