145 const int batchSize = inputTensorValues.rows();
151 if (inputNodeDims[0] == -1)
153 inputNodeDims[0] = batchSize;
155 if (outputNodeDims[0] == -1)
157 outputNodeDims[0] = batchSize;
160 if(inputNodeDims[1] != inputTensorValues.cols())
162 throw std::runtime_error(
"runONNXInference: feature size doesn't match the input size: inputSize required: " +
std::to_string(inputNodeDims[1]) +
" inputSize provided: " +
std::to_string(inputTensorValues.cols()));
165 if (batchSize != 1 &&(inputNodeDims[0] != batchSize || outputNodeDims[0] != batchSize))
167 throw std::runtime_error(
"runONNXInference: batch size doesn't match the input or output node size");
171 Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
172 Ort::Value inputTensor = Ort::Value::CreateTensor<float>(memoryInfo, inputTensorValues.data(), inputTensorValues.size(), inputNodeDims.data(), inputNodeDims.size());
174 if (!inputTensor.IsTensor())
176 throw std::runtime_error(
"runONNXInference: conversion of input to Tensor failed. ");
179 Ort::RunOptions run_options;
180 std::vector<Ort::Value> outputTensors =
186 if (!outputTensors[0].IsTensor() || (outputTensors.size() !=
m_outputNodeNames.size())) {
187 throw std::runtime_error(
"runONNXInference: calculation of output failed. ");
191 std::map<int, Eigen::MatrixXf> outputTensorMap;
192 size_t numOutputNodes =
m_session->GetOutputCount();
193 for (
size_t i=0;
i<numOutputNodes;
i++){
196 float*
output = outputTensors.at(
i).GetTensorMutableData<
float>();
197 Ort::TypeInfo outputTypeInfo =
m_session->GetOutputTypeInfo(
i);
198 auto outputTensorInfo = outputTypeInfo.GetTensorTypeAndShapeInfo();
201 outputNodeDims = outputTensorInfo.GetShape();
203 int nNodes = outputNodeDims.size() > 1 ? outputNodeDims[1] : 1;
204 Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> batchMatrix(batchSize, nNodes);
205 for (
int j = 0; j < batchSize; j++)
207 Eigen::VectorXf
vec(nNodes);
208 for (
int k = 0;
k<nNodes;
k++)
210 float val =
output[j * outputNodeDims[1] +
k];
213 batchMatrix.row(j) =
vec;
215 outputTensorMap[
i] = batchMatrix;
217 return outputTensorMap;