如何通过 C++ 向 tflite 提供多维输入 API
How to give multi-dimensional inputs to tflite via C++ API
我正在为 运行 我构建的模型试用 tflite C++ API。我通过以下代码段将模型转换为 tflite 格式:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5')
tfmodel = converter.convert()
open("model.tflite", "wb").write(tfmodel)
我正在按照 tflite official guide 中提供的步骤进行操作,到目前为止我的代码如下所示
// Load the model
std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("model.tflite");
// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder builder(*model, resolver);
builder(&interpreter);
interpreter->AllocateTensors();
// Check interpreter state
tflite::PrintInterpreterState(_interpreter.get());
这表明我的输入层的形状为 (1, 2050, 6)。为了从 C++ 提供输入,我遵循 this thread,我的输入代码如下所示:
std::vector<std::vector<double>> tensor; // I filled this vector, (dims are 2050, 6)
int input = interpreter->inputs()[0];
float* input_data_ptr = interpreter->typed_input_tensor<float>(input);
for (int i = 0; i < 2050; ++i) {
for (int j = 0; j < 6; j++) {
*(input_data_ptr) = (float)tensor[i][j];
input_data_ptr++;
}
}
该模型的最后一层 returns 单个浮点(概率)。我从以下代码得到输出。
interpreter->Invoke();
int output_idx = interpreter->outputs()[0];
float* output = interpreter->typed_output_tensor<float>(output_idx);
std::cout << "OUTPUT: " << *output << std::endl;
我的问题是不同的输入得到相同的输出。此外,输出与 tensorflow-python 输出不匹配。
我不明白为什么会这样。另外,任何人都可以确认这是否是向模型提供输入的正确方法?
一些额外信息:
我从源代码 v1.14.0 构建了 tflite,使用命令:bazel build -c opt //tensorflow/contrib/lite:libtensorflowLite.so --cxxopt="-std=c++11" --verbose_failures
我训练了我的模型并在另一台机器上使用 tensorflow v2.0 将其转换为 tflite
这是错误的API用法。
将 typed_input_tensor
更改为 typed_tensor
并将 typed_output_tensor
更改为 typed_tensor
解决了我的问题。
对于遇到同样问题的其他人,
int input_tensor_idx = 0;
int input = interpreter->inputs()[input_tensor_idx];
float* input_data_ptr = interpreter->typed_input_tensor<float>(input_tensor_idx);
和
int input_tensor_idx = 0;
int input = interpreter->inputs()[input_tensor_idx];
float* input_data_ptr = interpreter->typed_tensor<float>(input);
完全相同。
这可以通过查看 typed_input_tensor 的实现来验证。
template <class T>
T* typed_input_tensor(int index) {
return typed_tensor<T>(inputs()[index]);
}
我正在为 运行 我构建的模型试用 tflite C++ API。我通过以下代码段将模型转换为 tflite 格式:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5')
tfmodel = converter.convert()
open("model.tflite", "wb").write(tfmodel)
我正在按照 tflite official guide 中提供的步骤进行操作,到目前为止我的代码如下所示
// Load the model
std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("model.tflite");
// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder builder(*model, resolver);
builder(&interpreter);
interpreter->AllocateTensors();
// Check interpreter state
tflite::PrintInterpreterState(_interpreter.get());
这表明我的输入层的形状为 (1, 2050, 6)。为了从 C++ 提供输入,我遵循 this thread,我的输入代码如下所示:
std::vector<std::vector<double>> tensor; // I filled this vector, (dims are 2050, 6)
int input = interpreter->inputs()[0];
float* input_data_ptr = interpreter->typed_input_tensor<float>(input);
for (int i = 0; i < 2050; ++i) {
for (int j = 0; j < 6; j++) {
*(input_data_ptr) = (float)tensor[i][j];
input_data_ptr++;
}
}
该模型的最后一层 returns 单个浮点(概率)。我从以下代码得到输出。
interpreter->Invoke();
int output_idx = interpreter->outputs()[0];
float* output = interpreter->typed_output_tensor<float>(output_idx);
std::cout << "OUTPUT: " << *output << std::endl;
我的问题是不同的输入得到相同的输出。此外,输出与 tensorflow-python 输出不匹配。
我不明白为什么会这样。另外,任何人都可以确认这是否是向模型提供输入的正确方法?
一些额外信息:
我从源代码 v1.14.0 构建了 tflite,使用命令:
bazel build -c opt //tensorflow/contrib/lite:libtensorflowLite.so --cxxopt="-std=c++11" --verbose_failures
我训练了我的模型并在另一台机器上使用 tensorflow v2.0 将其转换为 tflite
这是错误的API用法。
将 typed_input_tensor
更改为 typed_tensor
并将 typed_output_tensor
更改为 typed_tensor
解决了我的问题。
对于遇到同样问题的其他人,
int input_tensor_idx = 0;
int input = interpreter->inputs()[input_tensor_idx];
float* input_data_ptr = interpreter->typed_input_tensor<float>(input_tensor_idx);
和
int input_tensor_idx = 0;
int input = interpreter->inputs()[input_tensor_idx];
float* input_data_ptr = interpreter->typed_tensor<float>(input);
完全相同。
这可以通过查看 typed_input_tensor 的实现来验证。
template <class T>
T* typed_input_tensor(int index) {
return typed_tensor<T>(inputs()[index]);
}