ONNX 运行时在传递模式时不进行计算

问题描述 投票:0回答:3

我对 C++ 和 ONNX 相当陌生,我需要为 ONNX C++ 推理建立随机森林模型。 我按照 youtube 上的教程进行操作:https://www.youtube.com/watch?v=exsgNLf-MyY 并重现代码如下。到目前为止,构建得很好。没有返回错误。 我的随机森林有 5 个输入和 4 个输出。 当我打开我的应用程序时,它不会进行计算,而只会留下“模型加载成功”消息。需要支持。

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

    Ort::Session session{ env, L"C:\\data\\RF.onnx", Ort::SessionOptions{}};
    std::cout << "Model Loaded Successfully!\n";
    system("PAUSE");

    // Ort::Session gives access to input and output information:
    // - counts
    // - 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 = { 2, 3, 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
    std::vector<const char*> inputNames = { inputName };
    std::vector<const char*> 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, 5, // input to set
        outputNames.data(), 4);                 // 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);
}
c++ inference onnx
3个回答
0
投票

Run() 调用期间会发生什么?应用程序会崩溃吗?


0
投票

由于系统(“PAUSE”),推理被卡住了

std::cout << "Model Loaded Successfully!\n";
system("PAUSE");

系统停留在该声明上并且没有前进

请使用断点或调试器, 给定的线程会告诉你为什么不应该 系统(“暂停”); - 为什么错了?


-1
投票
© www.soinside.com 2019 - 2024. All rights reserved.