繁体   English   中英

缺少 class 模板“std::array”的参数列表

[英]argument list for class template "std::array" is missing

我试着按照 ONNX C++ interence 的教程进行操作:

https://github.com/ilpropheta/onnxruntime-demo/blob/master/OnnxRuntimeDemo/Linear.cpp

当我尝试构建 C++ 应用程序控制台时出现 17 个错误。 我注意到主要错误与数组有关。 代码 E0441 显示

缺少 class 模板“std::array”的参数列表。 需要支持

#include "Linear.h"
#include <onnxruntime_cxx_api.h>
#include <array>
#include <iostream>

using namespace std;


void Demo::RunLinearRegression()
{
    // gives access to the underlying API (you can optionally customize log)
    // you can create one environment per process (each environment manages an internal thread pool)
    Ort::Env env;

    // creates an inference session for a certain model
    Ort::Session session{ env, LR"(linear.onnx)", Ort::SessionOptions{} };

    // Ort::Session gives access to input and output information:
    // - count
    // - name
    // - shape and type
    std::cout << "Number of model inputs: " << session.GetInputCount() << "\n";
    std::cout << "Number of model outputs: " << session.GetOutputCount() << "\n";

    // you can customize how allocation works. Let's just use a default allocator provided by the library
    Ort::AllocatorWithDefaultOptions allocator;
    // get input and output names
    auto* inputName = session.GetInputName(0, allocator);
    std::cout << "Input name: " << inputName << "\n";

    auto* outputName = session.GetOutputName(0, allocator);
    std::cout << "Output name: " << outputName << "\n";

    // get input shape
    auto inputShape = session.GetInputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
    // set some input values
    std::vector<float> inputValues = { 4, 5, 6 };

    // where to allocate the tensors
    auto memoryInfo = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);

    // create the input tensor (this is not a deep copy!)
    auto inputOnnxTensor = Ort::Value::CreateTensor<float>(memoryInfo,
        inputValues.data(), inputValues.size(),
        inputShape.data(), inputShape.size());

    // the API needs the array of inputs you set and the array of outputs you get
    array inputNames = { inputName };
    array outputNames = { outputName };

    // finally run the inference!
    auto outputValues = session.Run(
        Ort::RunOptions{ nullptr }, // e.g. set a verbosity level only for this run
        inputNames.data(), &inputOnnxTensor, 1, // input to set
        outputNames.data(), 1);                 // output to take 

    // extract first (and only) output
    auto& output1 = outputValues[0];
    const auto* floats = output1.GetTensorMutableData<float>();
    const auto floatsCount = output1.GetTensorTypeAndShapeInfo().GetElementCount();

    // just print the output values
    std::copy_n(floats, floatsCount, ostream_iterator<float>(cout, " "));


    // closing boilerplate
    allocator.Free(inputName);
    allocator.Free(outputName);
}

只需将inputNamesoutputNamesarray更改为std::vector<const char*>即可。

std::array 不起作用,因为它在声明中需要类型和数组大小,请检查此文档

所以要使 std::array 有效,您需要将其声明如下:

std::array<std::string, 1> inputNames = {inputName}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM