63 if (! itsSession) itsSession.reset();
66 LINFO(
"Loading " << m <<
" ...");
69 itsSession.reset(
new Ort::Session(itsEnv, m.c_str(), itsSessionOptions));
72 itsInNamePtrs.clear();
74 itsOutNamePtrs.clear();
78 size_t const num_input_nodes = itsSession->GetInputCount();
79 Ort::AllocatorWithDefaultOptions allocator;
80 LINFO(
"Network has " << num_input_nodes <<
" inputs:");
81 for (
size_t i = 0; i < num_input_nodes; ++i)
83 Ort::AllocatedStringPtr input_name = itsSession->GetInputNameAllocated(i, allocator);
84 Ort::TypeInfo
const type_info = itsSession->GetInputTypeInfo(i);
85 Ort::ConstTensorTypeAndShapeInfo
const tensor_info = type_info.GetTensorTypeAndShapeInfo();
88 itsInNames.emplace_back(input_name.get());
89 itsInNamePtrs.emplace_back(std::move(input_name));
93 size_t const num_output_nodes = itsSession->GetOutputCount();
94 LINFO(
"Network has " << num_output_nodes <<
" outputs:");
95 for (
size_t i = 0; i < num_output_nodes; ++i)
97 Ort::AllocatedStringPtr output_name = itsSession->GetOutputNameAllocated(i, allocator);
98 Ort::TypeInfo
const type_info = itsSession->GetOutputTypeInfo(i);
99 Ort::ConstTensorTypeAndShapeInfo
const tensor_info = type_info.GetTensorTypeAndShapeInfo();
102 itsOutNames.emplace_back(output_name.get());
103 itsOutNamePtrs.emplace_back(std::move(output_name));
105 LINFO(
"Network " << m <<
" ready.");
110 std::vector<std::string> & info)
112 if (! itsSession)
LFATAL(
"Internal inconsistency");
114 if (blobs.size() != itsInAttrs.size())
115 LFATAL(
"Received " << blobs.size() <<
" inputs but network wants " << itsInAttrs.size());
118 std::vector<Ort::Value> inputs;
119 std::vector<char const *> input_node_names;
120 for (
size_t i = 0; i < itsInAttrs.size(); ++i)
122 vsi_nn_tensor_attr_t
const & attr = itsInAttrs[i];
123 cv::Mat
const & m = blobs[i];
130 for (
size_t k = 0; k < attr.dim_num; ++k)
132 dims.emplace_back(attr.size[attr.dim_num - 1 - k]);
133 sz *= attr.size[attr.dim_num - 1 - k];
136 if (sz != m.total() * m.elemSize())
140 Ort::MemoryInfo meminfo = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
142 switch (attr.dtype.vx_type)
144 case VSI_NN_TYPE_FLOAT32:
145 inputs.emplace_back(Ort::Value::CreateTensor<float>(meminfo,
reinterpret_cast<float *
>(m.data),
146 sz, dims.data(), dims.size()));
149 case VSI_NN_TYPE_UINT8:
150 inputs.emplace_back(Ort::Value::CreateTensor<uint8_t>(meminfo,
reinterpret_cast<uint8_t *
>(m.data),
151 sz, dims.data(), dims.size()));
154 case VSI_NN_TYPE_INT8:
155 inputs.emplace_back(Ort::Value::CreateTensor<int8_t>(meminfo,
reinterpret_cast<int8_t *
>(m.data),
156 sz, dims.data(), dims.size()));
159 case VSI_NN_TYPE_UINT32:
160 inputs.emplace_back(Ort::Value::CreateTensor<uint32_t>(meminfo,
reinterpret_cast<uint32_t *
>(m.data),
161 sz, dims.data(), dims.size()));
164 case VSI_NN_TYPE_INT32:
165 inputs.emplace_back(Ort::Value::CreateTensor<int32_t>(meminfo,
reinterpret_cast<int32_t *
>(m.data),
166 sz, dims.data(), dims.size()));
172 if (inputs.back().IsTensor() ==
false)
LFATAL(
"Failed to create tensor for input " << i);
176 itsOutputs = itsSession->Run(Ort::RunOptions{
nullptr}, itsInNames.data(), inputs.data(), inputs.size(),
177 itsOutNames.data(), itsOutNames.size());
178 if (itsOutputs.size() != itsOutNames.size())
179 LFATAL(
"Received " << itsOutputs.size() <<
" outputs but network should produce " << itsOutNames.size());
182 std::vector<cv::Mat> outs;
183 for (
size_t i = 0; i < itsOutputs.size(); ++i)
185 Ort::Value & out = itsOutputs[i];
186 vsi_nn_tensor_attr_t
const & attr = itsOutAttrs[i];
187 if (out.IsTensor() ==
false)
LFATAL(
"Network produced a non-tensor output " << i);
189 switch (attr.dtype.vx_type)
191 case VSI_NN_TYPE_FLOAT32:
192 outs.emplace_back(cv::Mat(
jevois::dnn::attrmat(itsOutAttrs[i], out.GetTensorMutableData<
float>())));
195 case VSI_NN_TYPE_UINT8:
196 outs.emplace_back(cv::Mat(
jevois::dnn::attrmat(itsOutAttrs[i], out.GetTensorMutableData<uint8_t>())));
199 case VSI_NN_TYPE_INT8:
200 outs.emplace_back(cv::Mat(
jevois::dnn::attrmat(itsOutAttrs[i], out.GetTensorMutableData<int8_t>())));
203 case VSI_NN_TYPE_UINT32:
204 outs.emplace_back(cv::Mat(
jevois::dnn::attrmat(itsOutAttrs[i], out.GetTensorMutableData<uint32_t>())));
207 case VSI_NN_TYPE_INT32:
208 outs.emplace_back(cv::Mat(
jevois::dnn::attrmat(itsOutAttrs[i], out.GetTensorMutableData<int32_t>())));
216 info.emplace_back(
"Forward Network OK");