17 Ort::SessionOptions sessionOptions;
19 sessionOptions.SetGraphOptimizationLevel(
20 GraphOptimizationLevel::ORT_ENABLE_BASIC);
22 m_session = std::make_unique<Ort::Session>(
env, modelPath, sessionOptions);
24 Ort::AllocatorWithDefaultOptions allocator;
27 size_t numInputNodes =
m_session->GetInputCount();
29 for (
size_t i = 0;
i < numInputNodes;
i++) {
31 m_session->GetInputNameAllocated(
i, allocator));
36 Ort::TypeInfo inputTypeInfo =
m_session->GetInputTypeInfo(
i);
37 auto tensorInfo = inputTypeInfo.GetTensorTypeAndShapeInfo();
42 size_t numOutputNodes =
m_session->GetOutputCount();
44 for (
size_t i = 0;
i < numOutputNodes;
i++) {
46 m_session->GetOutputNameAllocated(
i, allocator));
50 Ort::TypeInfo outputTypeInfo =
m_session->GetOutputTypeInfo(
i);
51 auto tensorInfo = outputTypeInfo.GetTensorTypeAndShapeInfo();
58 std::vector<float>& inputTensorValues)
const {
60 for (
size_t i = 0;
i < inputTensorValues.size();
i++) {
61 vectorInput(0,
i) = inputTensorValues[
i];
63 auto vectorOutput = runONNXInference(vectorInput);
64 return vectorOutput[0];
71 return runONNXInferenceMultiOutput(inputTensorValues).front();
75 std::vector<std::vector<std::vector<float>>>
78 int batchSize = inputTensorValues.rows();
79 std::vector<int64_t> inputNodeDims = m_inputNodeDims;
80 std::vector<std::vector<int64_t>> outputNodeDims = m_outputNodeDims;
84 if (inputNodeDims[0] == -1) {
85 inputNodeDims[0] = batchSize;
88 bool outputDimsMatch =
true;
89 for (std::vector<int64_t>& nodeDim : outputNodeDims) {
90 if (nodeDim[0] == -1) {
91 nodeDim[0] = batchSize;
93 outputDimsMatch &= batchSize == 1 || nodeDim[0] == batchSize;
96 if (batchSize != 1 && (inputNodeDims[0] != batchSize || !outputDimsMatch)) {
97 throw std::runtime_error(
98 "runONNXInference: batch size doesn't match the input or output node "
103 Ort::MemoryInfo memoryInfo =
104 Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
105 Ort::Value inputTensor = Ort::Value::CreateTensor<float>(
106 memoryInfo, inputTensorValues.data(), inputTensorValues.size(),
107 inputNodeDims.data(), inputNodeDims.size());
109 if (!inputTensor.IsTensor()) {
110 throw std::runtime_error(
111 "runONNXInference: conversion of input to Tensor failed. ");
114 Ort::RunOptions run_options;
115 std::vector<Ort::Value> outputTensors =
116 m_session->Run(run_options, m_inputNodeNames.data(), &inputTensor,
117 m_inputNodeNames.size(), m_outputNodeNames.data(),
118 m_outputNodeNames.size());
122 if (!outputTensors[0].IsTensor() ||
123 (outputTensors.size() != m_outputNodeNames.size())) {
124 throw std::runtime_error(
125 "runONNXInference: calculation of output failed. ");
128 std::vector<std::vector<std::vector<float>>> multiOutput;
130 for (
size_t i_out = 0; i_out < outputTensors.size(); i_out++) {
132 float* outputTensor = outputTensors.at(i_out).GetTensorMutableData<
float>();
134 std::vector<std::vector<float>> outputTensorValues(
135 batchSize, std::vector<float>(outputNodeDims.at(i_out)[1], -1));
136 for (
int i = 0;
i < outputNodeDims.at(i_out)[0];
i++) {
137 for (
int j = 0;
j < ((outputNodeDims.at(i_out).size() > 1)
138 ? outputNodeDims.at(i_out)[1]
141 outputTensorValues[
i][
j] =
142 outputTensor[
i * outputNodeDims.at(i_out)[1] +
j];
145 multiOutput.push_back(
std::move(outputTensorValues));