c++ - TensorFlow Lite 模型测试:正确的代码、类的概率
问题描述
我正在尝试使用 TensorflowLite 模型测试 TensorFlow lite c++ 代码。模型获取 256*256 的浮点数组(频谱图或图像)并对这些数据进行一些推断。TF Lite 模型旨在解决分类为 5 类的问题。它是通过转换从传统的 TF 模型导出的。我使用 TF Lite 2.0。
这是我的代码:
#include <iostream>
#include <cstdio>
#include "../tensorflow/tensorflow/lite/interpreter.h"
#include "../tensorflow/tensorflow/lite/model.h"
#include "../tensorflow/tensorflow/lite/kernels/register.h"
#include "../tensorflow/tensorflow/lite/op_resolver.h"
#include <cstdlib>
int main(int argc, char** argv)
{
const char* filename = argv[1];
std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(filename);
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
interpreter->SetNumThreads(4);
interpreter->AllocateTensors();
for(int i = 0; i < 256*256; i++){
float input = rand() % 10 + rand() % 10;
interpreter->typed_input_tensor<float>(0)[i] = input;
//printf("%f ", input);
}
//printf("\n");
interpreter->Invoke();
int output = interpreter->outputs()[0];
printf("%d ", output);
for(int i = 0; i < 5; i++)
{
float output = interpreter->typed_output_tensor<float>(0)[i];
printf("%f ", (output));
}
printf("\n");
}
我有一些疑问:
如何组织输入数据(如何将二维频谱图应用于模型的输入)?
如何以正确的方式获得类的输出概率?
我是否编写了正确的代码来测试模型?
解决方案
您的代码看起来正确。由于 Tensorflow Lite 以行优先格式查看张量,因此您分配输入的方式似乎是合理的。
你可能不需要这个:
int output = interpreter->outputs()[0];
printf("%d ", output);
否则,事情看起来还不错。如果您以与训练期间相同的方式预处理输入图像/频谱图,您应该获得您期望的输出。
推荐阅读
- git - Git - 按分支/标签名称递归列出文件
- ruby-on-rails - 在 Rails 中使用选择或循环记录数组更好吗?
- javascript - 我需要 Javascript 代码来捕获 Ipad 设备的本地系统的 IP 地址
- c# - .net Core 中的自定义属性设置
- module - 模糊过程 calcChecksum
- python - 如何在使用 numpy 数组保持内存的同时提高速度?
- java - 如何使用 Sentry 优雅地处理错误峰值?
- jsf - JSF 参数错误:参数 responseWriter 为空
- javascript - 如何在 ViewController 函数中获取设置值组合框/ Extjs
- c# - 发出属性以设置字典值