如何通过 C++ API 为 tflite 提供多维输入 [英] How to give multi-dimensional inputs to tflite via C++ API

查看:70
本文介绍了如何通过 C++ API 为 tflite 提供多维输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tflite C++ API 来运行我构建的模型.我通过以下代码段将模型转换为 tflite 格式:

I am trying out tflite C++ API for running a model that I built. I converted the model to tflite format by following snippet:

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model_file('model.h5') 
tfmodel = converter.convert() 
open("model.tflite", "wb").write(tfmodel)

我正在按照 tflite 官方指南 中提供的步骤进行操作,我的代码高达这个点看起来像这样

I am following the steps provided at tflite official guide, and my code upto this point looks like this

// 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 shows my input layer has a shape of (1, 2050, 6). For giving input from C++, I followed this thread, and my input code looks like this:

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++;
    }
}

该模型的最后一层返回单个浮点数(概率).我从以下代码获得输出.

Last layer of this model returns a single floating point(a probability). I get output from following code.

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 输出不匹配.

My problem is that I am getting same output for different inputs. Moreover, the outputs are not matching with tensorflow-python outputs.

我不明白为什么它会这样.另外,谁能确认这是否是向模型提供输入的正确方法?

I don't understand why it's behaving this way. Also, can anyone confirm if this is the right way to give inputs to the model?

一些额外的信息:

  1. 我从源代码 v1.14.0 构建了 tflite,使用命令:bazel build -c opt//tensorflow/contrib/lite:libtensorflowLite.so --cxxopt="-std=c++11" --verbose_failures

我训练了我的模型并将其转换为 tflite 在另一台机器上使用 tensorflow v2.0

I trained my model and converted it to tflite on a different machine, with tensorflow v2.0

推荐答案

这是错误的 API 用法.

This is wrong API usage.

typed_input_tensor 更改为 typed_tensor 并将 typed_output_tensor 更改为 typed_tensor 为我解决了问题.

Changing typed_input_tensor to typed_tensor and typed_output_tensor to typed_tensor resolved the issue for me.

对于有同样问题的其他人,

For anyone else having the same issue,

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);

是相同的.

这可以通过查看

This can be verified by looking at implementation of typed_input_tensor.

  template <class T>
  T* typed_input_tensor(int index) {
    return typed_tensor<T>(inputs()[index]);
  }

这篇关于如何通过 C++ API 为 tflite 提供多维输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆