使用C+解析_service _input _receiver _fn推断Tensorflow lite模型的示例+;美国石油学会

使用C+解析_service _input _receiver _fn推断Tensorflow lite模型的示例+;美国石油学会,tensorflow,Tensorflow,我已经遵循Tensorflow2文档,将经过训练的tf.estimator模型转换为tflite模型;为了转换我的模型,首先我必须使用输入\接收器\ fn以保存的\模型格式保存我的模型,然后使用选择\操作标志进行转换: classifier = tf.estimator.LinearClassifier(n_classes=2, model_dir = classifier_dir, feature_columns=features) classifier.train(input_fn = la

我已经遵循Tensorflow2文档,将经过训练的tf.estimator模型转换为tflite模型;为了转换我的模型,首先我必须使用输入\接收器\ fn以保存的\模型格式保存我的模型,然后使用选择\操作标志进行转换:

classifier = tf.estimator.LinearClassifier(n_classes=2, model_dir = classifier_dir, feature_columns=features)
classifier.train(input_fn = lambda: trian_fn(features = train_datas, labels = trian_labels))

serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(tf.feature_column.make_parse_example_spec(features))

classifier.export_saved_model(classifier_dir+"\saved_model", serving_input_fn)

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir = saved_model_dir , signature_keys=['serving_default']) 
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)
<>我想在没有Python支持的ARM设备上运行我的TFLITE模型,所以我用Bazel创建了C++解释程序共享LIBS,如文档中所解释的:

我的模型有3个输入特征,但当我尝试使用以下特征进行推断时,我得到了一个分段错误。 我使用以下代码提取模型详细信息:

interpreter = tf.lite.Interpreter(model_path="./model.tflite")
interpreter.allocate_tensors()
print("all ok")
# Print input shape and type
inputs = interpreter.get_input_details()
print('{} input(s):'.format(len(inputs)))
for i in range(0, len(inputs)):
    print('{} {}'.format(inputs[i]['shape'], inputs[i]['dtype']))

# Print output shape and type
outputs = interpreter.get_output_details()
print('\n{} output(s):'.format(len(outputs)))
for i in range(0, len(outputs)):
    print('{} {}'.format(outputs[i]['shape'], outputs[i]['dtype']))
我得到了以下输出:

all ok
1 input(s):
[1] <class 'numpy.bytes_'>

2 output(s):
[1 2] <class 'numpy.bytes_'>
[1 2] <class 'numpy.float32'>
输出说明输入形状与原始模型不同,输入类型也不同,但Tensorflow 2模型输入为[numpy.float32,numpy.float32,numpy.float32]。 TF2模型中预测的输入字典类似于:{'feature0':data0,'feature1':data1,'feature2':data2}

这是谷歌对Tensorflow模型的支持 我以前没有推断TysFoSLite模型的经验,所以我先搜索并找出了下面的相关问题,这些问题帮助我在C++代码下面写:

我试图用零向量填充输入缓冲区,但没有成功。这是我的C++代码,加载TFLITE模型并输入预测的输入。有人能给我指出正确的方向吗?因为我找不到任何示例或相关文档,用于将输入输入输入到带有服务输入的转换tf.estimator

#include <cstdio>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/optional_debug_tools.h"

int main()
{
  // Load model
      std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("model.tflite");
      
  // Build the interpreter with the InterpreterBuilder.
  tflite::ops::builtin::BuiltinOpResolver resolver;
  tflite::InterpreterBuilder builder(*model, resolver);
  std::unique_ptr<tflite::Interpreter> interpreter;
  builder(&interpreter);
  tflite::PrintInterpreterState(interpreter.get());
  
  // Allocate tensor buffers.
  interpreter->AllocateTensors();
  printf("=== Pre-invoke Interpreter State ===\n");
  tflite::PrintInterpreterState(interpreter.get());

  // Fill input buffers
  std::vector<float> tensor(3, 0);  //Vector of zeros
  int input = interpreter->inputs()[0];
  float* input_data_ptr = interpreter->typed_input_tensor<float>(input);
  for(int i = 0; i < 3; ++i)
  {
    *(input_data_ptr) = (float)tensor[i];
    input_data_ptr++;
  }
  // Run inference
  interpreter->Invoke();
  printf("\n\n=== Post-invoke Interpreter State ===\n");
  
  return 0;
}
#包括
#包括“tensorflow/lite/解释器.h”
#包括“tensorflow/lite/kernels/register.h”
#包括“tensorflow/lite/model.h”
#包括“tensorflow/lite/optional_debug_tools.h”
int main()
{
//负荷模型
std::unique_ptr model=tflite::FlatBufferModel::BuildFromFile(“model.tflite”);
//使用解释器构建器构建解释器。
tflite::ops::builtin::BuiltinOpResolver解析器;
tflite::解释器生成器(*模型,解析器);
std::唯一的ptr解释器;
建设者和口译员;
tflite::PrintExplorersState(解释器.get());
//分配张量缓冲区。
解释器->分配传感器();
printf(“===调用前解释器状态===\n”);
tflite::PrintExplorersState(解释器.get());
//填充输入缓冲区
向量张量(3,0);//零向量
int input=解释器->输入()[0];
浮点*输入\数据\ ptr=解释器->类型化\输入\张量(输入);
对于(int i=0;i<3;++i)
{
*(输入数据)=(浮点)张量[i];
输入_数据_ptr++;
}
//运行推理
解释器->调用();
printf(“\n\n===调用后解释器状态===\n”);
返回0;
}
编辑1: 我在Tensorflow的GitHub中也提出了这个问题,并得到了一条评论,提到我必须以“示例原型”的形式提供输入,现在问题归结为什么是“示例原型”,如何从示例原型中向tflite模型提供输入


如何在编译C++时生成TFLITH报头?任何线索都会很有用。在编译C++时如何生成TFLITH头文件?任何线索都会非常有用。
#include <cstdio>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/optional_debug_tools.h"

int main()
{
  // Load model
      std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("model.tflite");
      
  // Build the interpreter with the InterpreterBuilder.
  tflite::ops::builtin::BuiltinOpResolver resolver;
  tflite::InterpreterBuilder builder(*model, resolver);
  std::unique_ptr<tflite::Interpreter> interpreter;
  builder(&interpreter);
  tflite::PrintInterpreterState(interpreter.get());
  
  // Allocate tensor buffers.
  interpreter->AllocateTensors();
  printf("=== Pre-invoke Interpreter State ===\n");
  tflite::PrintInterpreterState(interpreter.get());

  // Fill input buffers
  std::vector<float> tensor(3, 0);  //Vector of zeros
  int input = interpreter->inputs()[0];
  float* input_data_ptr = interpreter->typed_input_tensor<float>(input);
  for(int i = 0; i < 3; ++i)
  {
    *(input_data_ptr) = (float)tensor[i];
    input_data_ptr++;
  }
  // Run inference
  interpreter->Invoke();
  printf("\n\n=== Post-invoke Interpreter State ===\n");
  
  return 0;
}