2#include "Tools/ONNXRuntime.h"
15#if ORT_API_VERSION == 2
19std::string get_input_name(std::unique_ptr<Session>& s,
size_t i,
20 AllocatorWithDefaultOptions a) {
21 return s->GetInputName(i, a);
23std::string get_output_name(std::unique_ptr<Session>& s,
size_t i,
24 AllocatorWithDefaultOptions a) {
25 return s->GetOutputName(i, a);
30std::string get_input_name(std::unique_ptr<Session>& s,
size_t i,
31 AllocatorWithDefaultOptions a) {
32 return s->GetInputNameAllocated(i, a).get();
34std::string get_output_name(std::unique_ptr<Session>& s,
size_t i,
35 AllocatorWithDefaultOptions a) {
36 return s->GetOutputNameAllocated(i, a).get();
38#if ORT_API_VERSION != 15
40 "Untested ONNX version, not certain of API, assuming API version 15.")
44Env ONNXRuntime::env_(ORT_LOGGING_LEVEL_WARNING,
"");
47 const SessionOptions* session_options) {
49 if (session_options) {
50 session_.reset(
new Session(env_, model_path.c_str(), *session_options));
52 SessionOptions sess_opts;
53 sess_opts.SetIntraOpNumThreads(1);
54 session_.reset(
new Session(env_, model_path.c_str(), sess_opts));
56 AllocatorWithDefaultOptions allocator;
59 size_t num_input_nodes = session_->GetInputCount();
60 input_node_strings_.resize(num_input_nodes);
61 input_node_names_.resize(num_input_nodes);
62 input_node_dims_.clear();
64 for (
size_t i = 0; i < num_input_nodes; i++) {
66 std::string input_name(get_input_name(session_, i, allocator));
67 input_node_strings_[i] = input_name;
68 input_node_names_[i] = input_node_strings_[i].c_str();
71 auto type_info = session_->GetInputTypeInfo(i);
72 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
73 size_t num_dims = tensor_info.GetDimensionsCount();
74 input_node_dims_[input_name].resize(num_dims);
75 const auto input_shape = tensor_info.GetShape();
76 std::copy(input_shape.begin(), input_shape.end(),
77 input_node_dims_[input_name].begin());
80 input_node_dims_[input_name].at(0) = 1;
83 size_t num_output_nodes = session_->GetOutputCount();
84 output_node_strings_.resize(num_output_nodes);
85 output_node_names_.resize(num_output_nodes);
86 output_node_dims_.clear();
88 for (
size_t i = 0; i < num_output_nodes; i++) {
90 std::string output_name(get_output_name(session_, i, allocator));
91 output_node_strings_[i] = output_name;
92 output_node_names_[i] = output_node_strings_[i].c_str();
95 auto type_info = session_->GetOutputTypeInfo(i);
96 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
97 size_t num_dims = tensor_info.GetDimensionsCount();
98 output_node_dims_[output_name].resize(num_dims);
99 const auto output_shape = tensor_info.GetShape();
100 std::copy(output_shape.begin(), output_shape.end(),
101 output_node_dims_[output_name].begin());
104 output_node_dims_[output_name].at(0) = -1;
108ONNXRuntime::~ONNXRuntime() {}
111 FloatArrays& input_values,
112 const std::vector<std::string>& output_names,
113 int64_t batch_size)
const {
114 assert(input_names.size() == input_values.size());
115 assert(batch_size > 0);
118 std::vector<Value> input_tensors;
120 MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
121 for (
const auto& name : input_node_strings_) {
122 auto iter = std::find(input_names.begin(), input_names.end(), name);
123 if (iter == input_names.end()) {
124 throw std::runtime_error(
"Input " + name +
" is not provided!");
126 auto value = input_values.begin() + (iter - input_names.begin());
127 auto input_dims = input_node_dims_.at(name);
128 input_dims[0] = batch_size;
129 auto expected_len = std::accumulate(input_dims.begin(), input_dims.end(), 1,
130 std::multiplies<int64_t>());
131 if (expected_len != (int64_t)value->size()) {
132 throw std::runtime_error(
"Input array " + name +
" has a wrong size of " +
133 std::to_string(value->size()) +
", expected " +
134 std::to_string(expected_len));
137 Value::CreateTensor<float>(memory_info, value->data(), value->size(),
138 input_dims.data(), input_dims.size());
139 assert(input_tensor.IsTensor());
140 input_tensors.emplace_back(std::move(input_tensor));
145 std::vector<const char*> run_output_node_names;
146 if (output_names.empty()) {
147 run_output_node_names = output_node_names_;
149 for (
const auto& name : output_names) {
150 run_output_node_names.push_back(name.c_str());
155 auto output_tensors =
156 session_->Run(RunOptions{
nullptr}, input_node_names_.data(),
157 input_tensors.data(), input_tensors.size(),
158 run_output_node_names.data(), run_output_node_names.size());
162 for (
auto& output_tensor : output_tensors) {
163 assert(output_tensor.IsTensor());
166 auto tensor_info = output_tensor.GetTensorTypeAndShapeInfo();
167 auto length = tensor_info.GetElementCount();
169 auto floatarr = output_tensor.GetTensorMutableData<
float>();
170 outputs.emplace_back(floatarr, floatarr + length);
172 assert(outputs.size() == run_output_node_names.size());
179 return output_node_strings_;
181 throw std::runtime_error(
"ONNXRuntime session is not initialized!");
186 const std::string& output_name)
const {
187 auto iter = output_node_dims_.find(output_name);
188 if (iter == output_node_dims_.end()) {
189 throw std::runtime_error(
"Output name " + output_name +
" is invalid!");
FloatArrays run(const std::vector< std::string > &input_names, FloatArrays &input_values, const std::vector< std::string > &output_names={}, int64_t batch_size=1) const
Run model inference and get outputs.
ONNXRuntime(const std::string &model_path, const ::Ort::SessionOptions *session_options=nullptr)
Class constructor.
const std::vector< int64_t > & getOutputShape(const std::string &output_name) const
Get the shape of a output node.
const std::vector< std::string > & getOutputNames() const
Get the names of all the output nodes.