[英]TensorFlow Lite model testing: right code, probabilities of classes
I'm trying to test TensorFlow lite c++ code with TensorflowLite model.我正在尝试使用 TensorflowLite model 测试 TensorFlow lite c++ 代码。 Model gets 256*256 array of floats (spectrogram or image) and do some inference on this data.
Model 获取 256*256 浮点数组(频谱图或图像)并对这些数据进行一些推断。 The TF Lite model is designed to solve the problem of classification into 5 classes.
TF Lite model 旨在解决分类为 5 类的问题。 It was derived from a conventional TF model by conversion.
它是通过转换从传统的 TF model 衍生而来的。 I use TF Lite 2.0.
我使用 TF Lite 2.0。
This is my code:这是我的代码:
#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");
}
I have some questions:我有一些问题:
how to organize the input data (how to apply a two-dimensional spectrogram to the input of the model)?如何组织输入数据(如何将二维频谱图应用于模型的输入)?
how to get the output probability of classes in right way?如何以正确的方式获得类的 output 概率?
did I write the right code to test the model?我是否编写了正确的代码来测试 model?
Your code looks about right.您的代码看起来正确。 And since Tensorflow Lite looks at tensors in row-major format, your way of assigning inputs seems reasonable.
由于 Tensorflow Lite 以行优先格式查看张量,因此您分配输入的方式似乎是合理的。
You probably don't need this:你可能不需要这个:
int output = interpreter->outputs()[0];
printf("%d ", output);
Otherwise, things look okay.否则,事情看起来还不错。 If you pre-process the input image/spectogram the same way you did during training, you should obtain the outputs you expect.
如果您以与训练期间相同的方式预处理输入图像/频谱图,您应该获得您期望的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.