【问题标题】:Tensorflow tflite c++ api inference for matrix data array矩阵数据数组的Tensorflow tflite c ++ api推理
【发布时间】:2020-04-20 20:54:48
【问题描述】:

我正在创建一个类,该类将用于使用 tensorflow 的 tflite c++ api 在 C++ 中的嵌入式设备(不是树莓派)上运行推理。 Tensorflow 似乎没有关于如何对 n 个图像数据样本进行推理的体面文档。我在 python 中的数据形状是 (n, 5, 40, 1) [n 个样本,5 个高度,40 个宽度,1 个通道]。我无法弄清楚的是如何输入数据并在输出中接收每个样本的推断。我有两个班级,所以我应该收到 n 二维数组输出。有谁知道您是否可以传入任何数据类型,例如 Eigen?我正在使用形状 (1, 5, 2, 1) 的输入进行测试,以简化我的测试。

#include "classifier.h"
#include <iostream>

using namespace tflite;

Classifier::Classifier(std::string modelPath) {
    tflite::StderrReporter error_reporter;
    model = tflite::FlatBufferModel::BuildFromFile(modelPath.c_str(), &error_reporter);

    tflite::ops::builtin::BuiltinOpResolver resolver;
    tflite::InterpreterBuilder(*model, resolver)(&interpreter); // private class variable interpreter
    std::vector<int> sizes = {1, 5, 2, 1};
    interpreter->ResizeInputTensor(0, sizes);
    interpreter->AllocateTensors();


}

std::vector<std::vector<float> Classifier::getDataSamples() {
    std::vector<std::vector<float> test = {{0.02, 0.02}, {0.02, 0.02}, {0.02, 0.02},{0.02, 0.02},{0.02, 0.02},};
    return test;
}

float Classifier::predict() {


    std::vector<float> signatures = getDataSamples();
    for (int i = 0; i < signatures.size(); ++i) {
        interpreter->typed_input_tensor<float>(0)[i];
    }

    // float* input = interpreter->typed_input_tensor<float>(0);
    // *input = 1.0;

    interpreter->Invoke();

    float* output = interpreter->typed_output_tensor<float>(0);

    return *output;
}

【问题讨论】:

    标签: c++ tensorflow tensorflow-lite


    【解决方案1】:

    我们可以从 Tensorflow 文档中找到以下详细信息,

    需要注意的是:

    • 张量由整数表示,以避免字符串比较(以及对字符串库的任何固定依赖)。
    • 不得从并发线程访问解释器。
    • 必须在调整张量大小后立即调用 AllocateTensors() 来触发输入和输出张量的内存分配。

    您可以在 C++ here 中找到有关加载和运行模型的更多信息。

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多