我正在创建一个类,该类将使用Tensorflow的tflite C++ API在C++中的嵌入式设备(不是树莓派)上运行推理。 Tensorflow似乎没有关于如何对n个图像数据样本进行推理的文档。我在python中的数据形状为(n,5、40、1)[n个样本,5个高度,40个宽度,1个通道]。我无法弄清楚的是如何输入数据并在输出中接收每个样本的推论。我有两个类(class),因此我应该接收n个2维数组输出。有谁知道您是否可以传递诸如Eigen之类的任何数据类型?我正在测试形状为(1、5、2、1)的输入,以简化测试。
#include "classifier.h"
#include <iostream>
using namespace tflite;
Classifier::Classifier(std::string modelPath) {
tflite::StderrReporter error_reporter;
model = tflite::FlatBufferModel::BuildFromFile(modelPath.c_str(), &error_reporter);
tflite::ops::builtin::BuiltinOpResolver resolver;
tflite::InterpreterBuilder(*model, resolver)(&interpreter); // private class variable interpreter
std::vector<int> sizes = {1, 5, 2, 1};
interpreter->ResizeInputTensor(0, sizes);
interpreter->AllocateTensors();
}
std::vector<std::vector<float> Classifier::getDataSamples() {
std::vector<std::vector<float> test = {{0.02, 0.02}, {0.02, 0.02}, {0.02, 0.02},{0.02, 0.02},{0.02, 0.02},};
return test;
}
float Classifier::predict() {
std::vector<float> signatures = getDataSamples();
for (int i = 0; i < signatures.size(); ++i) {
interpreter->typed_input_tensor<float>(0)[i];
}
// float* input = interpreter->typed_input_tensor<float>(0);
// *input = 1.0;
interpreter->Invoke();
float* output = interpreter->typed_output_tensor<float>(0);
return *output;
}
最佳答案
在Tensorflow文档中,我们可以找到以下详细信息,
您可以在C++ here中找到有关Load和运行模型的更多信息。