c++ - 矩阵数据数组的Tensorflow tflite c ++ api推理
问题描述
我正在创建一个类,该类将用于使用 tensorflow 的 tflite c++ api 在 c++ 中的嵌入式设备(不是树莓派)上运行推理。Tensorflow 似乎没有关于如何对 n 个图像数据样本进行推理的良好文档。我在 python 中的数据形状是 (n, 5, 40, 1) [n 个样本,5 个高度,40 个宽度,1 个通道]。我无法弄清楚的是如何输入数据并在输出中接收每个样本的推断。我有两个班级,所以我应该收到 n 二维数组输出。有谁知道您是否可以传入任何数据类型,例如 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;
}
解决方案
From the Tensorflow documentation we can find below details,
It should be noted that:
- Tensors are represented by integers, in order to avoid string comparisons (and any fixed dependency on string libraries).
- An interpreter must not be accessed from concurrent threads.
- Memory allocation for input and output tensors must be triggered by calling AllocateTensors() right after resizing tensors.
You can find more about the Load and run a model in C++ here.
推荐阅读
- html - 如何让你的 flexbox 在移动设备上悬停和灵活
- scala - 猫中 foldLeft 和 foldRight 的区别
- python - 在反转数组的情况下,如何修复“标量变量的索引无效”错误
- node.js - 使用 nginx 为在 node.js+mongodb+basic Html 和 Docker 中的 CSS 上运行的 Web 应用程序设置反向代理=> 不工作
- c# - 为什么某些行在 c# 中显示不同列的数据?
- mysql - MySQL在值更改时减少用户变量?
- java - 在具有自定义操作系统的 android 设备中启动永无止境的后台服务的最佳实践
- javascript - 为什么我的 javascript 测验的所有答案都是错误的?
- java - Java枚举在我的枚举类的静态方法中为空
- mysql - 使用 Cloudformation 创建 Rd