ATLAS Offline Software
Loading...
Searching...
No Matches
JSSMLTool.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3// Local include(s).
5
6// Framework include(s).
8
9// ROOT includes
10#include "TSystem.h"
11#include "TH2D.h"
12
13namespace AthONNX {
14
15 //*******************************************************************
16 // for reading jet images
17 std::vector<float> JSSMLTool::ReadJetImagePixels( std::vector<TH2D> Images ) const //function to load test images
18 {
19
20 int n_rows = m_nPixelsX;
21 int n_cols = m_nPixelsY;
22 int n_colors = m_nPixelsZ;
23
24 std::vector<float> input_tensor_values(n_rows*n_cols*n_colors);
25
26 for(int iRow=0; iRow<n_rows; ++iRow){
27 for(int iColumn=0; iColumn<n_cols; ++iColumn){
28 for(int iColor=0; iColor<n_colors; ++iColor){
29 input_tensor_values[ (n_colors*n_cols*iRow) + iColumn*n_colors + iColor] = Images[iColor].GetBinContent(iRow+1, iColumn+1);
30 }
31 }
32 }
33
34 return input_tensor_values;
35 }
36
37 //********************************************************************************
38 // for reading DNN inputs
39 std::vector<float> JSSMLTool::ReadJSSInputs(std::map<std::string, double> JSSVars) const //function to load test images
40 {
41
42 std::vector<float> input_tensor_values(m_nvars);
43
44 // apply features scaling
45 for(const auto & var : JSSVars){
46 auto p = m_scaler.find(var.first);
47 if (p == m_scaler.end()) continue;
48 double mean = p->second[0];
49 double std = p->second[1];
50 JSSVars[var.first] = (var.second - mean) / std;
51 }
52
53 // then dump it to a vector
54 for(int v=0; v<m_nvars; ++v){
55 if (auto found = m_JSSInputMap.find(v);found != m_JSSInputMap.end())[[likely]]{
56 input_tensor_values[v] = JSSVars[found->second];
57 }
58 }
59
60 return input_tensor_values;
61 }
62
63 //********************************************************************************
64 // for reading jet labels for DNN
65 // this can be extended in case of multi-class models
66 std::vector<int> JSSMLTool::ReadOutputLabels() const
67 {
68 std::vector<int> output_tensor_values(1);
69
70 output_tensor_values[0] = 1;
71
72 return output_tensor_values;
73 }
74
75 // constructor ---
76 JSSMLTool::JSSMLTool(const std::string& name):
77 AsgTool(name)
78 {
79 declareProperty("ModelPath", m_modelFileName);
80 declareProperty("nPixelsX", m_nPixelsX);
81 declareProperty("nPixelsY", m_nPixelsY);
82 declareProperty("nPixelsZ", m_nPixelsZ);
83 }
84
85 // initialize ---
86 StatusCode JSSMLTool::initialize( ) {
87
88 // Access the service.
89 // Find the model file.
90 ATH_MSG_INFO( "Using model file: " << m_modelFileName );
91
92 // Set up the ONNX Runtime session.
93 Ort::SessionOptions sessionOptions;
94 sessionOptions.SetIntraOpNumThreads( 1 );
95 sessionOptions.SetGraphOptimizationLevel( ORT_ENABLE_BASIC );
96
97 // according to the discussion here https://its.cern.ch/jira/browse/ATLASG-2866
98 // this should reduce memory use while slowing things down slightly
99 sessionOptions.DisableCpuMemArena();
100
101 // declare an allocator
102 Ort::AllocatorWithDefaultOptions allocator;
103
104 // create session and load model into memory
105 m_env = std::make_unique< Ort::Env >(ORT_LOGGING_LEVEL_WARNING, "");
106 m_session = std::make_unique< Ort::Session >( *m_env,
107 m_modelFileName.c_str(),
108 sessionOptions );
109
110 ATH_MSG_INFO( "Created the ONNX Runtime session" );
111
112 m_num_input_nodes = m_session->GetInputCount();
114
115 for( std::size_t i = 0; i < m_num_input_nodes; i++ ) {
116 // print input node names
117 char* input_name = m_session->GetInputNameAllocated(i, allocator).release();
118 ATH_MSG_DEBUG("Input "<<i<<" : "<<" name = "<<input_name);
119 m_input_node_names[i] = input_name;
120 // print input node types
121 Ort::TypeInfo type_info = m_session->GetInputTypeInfo(i);
122 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
123 ONNXTensorElementDataType type = tensor_info.GetElementType();
124 ATH_MSG_DEBUG("Input "<<i<<" : "<<" type = "<<type);
125
126 // print input shapes/dims
127 m_input_node_dims = tensor_info.GetShape();
128 ATH_MSG_DEBUG("Input "<<i<<" : num_dims = "<<m_input_node_dims.size());
129 for (std::size_t j = 0; j < m_input_node_dims.size(); j++){
130 if(m_input_node_dims[j]<0)
131 m_input_node_dims[j] =1;
132 ATH_MSG_DEBUG("Input"<<i<<" : dim "<<j<<" = "<<m_input_node_dims[j]);
133 }
134 }
135
136 m_num_output_nodes = m_session->GetOutputCount();
138
139 for( std::size_t i = 0; i < m_num_output_nodes; i++ ) {
140 // print output node names
141 char* output_name = m_session->GetOutputNameAllocated(i, allocator).release();
142 ATH_MSG_DEBUG("Output "<<i<<" : "<<" name = "<<output_name);
143 m_output_node_names[i] = output_name;
144
145 Ort::TypeInfo type_info = m_session->GetOutputTypeInfo(i);
146 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
147 ONNXTensorElementDataType type = tensor_info.GetElementType();
148 ATH_MSG_DEBUG("Output "<<i<<" : "<<" type = "<<type);
149
150 // print output shapes/dims
151 m_output_node_dims = tensor_info.GetShape();
152 ATH_MSG_INFO("Output "<<i<<" : num_dims = "<<m_output_node_dims.size());
153 for (std::size_t j = 0; j < m_output_node_dims.size(); j++){
154 if(m_output_node_dims[j]<0)
155 m_output_node_dims[j] =1;
156 ATH_MSG_INFO("Output"<<i<<" : dim "<<j<<" = "<<m_output_node_dims[j]);
157 }
158 }
159
160 // Return gracefully.
161 return StatusCode::SUCCESS;
162 } // end initialize ---
163
164 // constituents image based
165 double JSSMLTool::retrieveConstituentsScore(std::vector<TH2D> Images) const {
166
167 //*************************************************************************
168 // Score the model using sample data, and inspect values
169
170 // preparing container to hold input data
171 size_t input_tensor_size = m_nPixelsX*m_nPixelsY*m_nPixelsZ;
172 std::vector<float> input_tensor_values(input_tensor_size);
173
174 // loading input data
175 input_tensor_values = ReadJetImagePixels(std::move(Images));
176
177 // preparing container to hold output data
178 int testSample = 0;
179 std::vector<int> output_tensor_values_ = ReadOutputLabels();
180 int output_tensor_values = output_tensor_values_[testSample];
181
182 // create input tensor object from data values
183 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
184 Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info, input_tensor_values.data(), input_tensor_size, m_input_node_dims.data(), m_input_node_dims.size());
185 assert(input_tensor.IsTensor());
186
187 auto output_tensors = m_session->Run(Ort::RunOptions{nullptr}, m_input_node_names.data(), &input_tensor, m_input_node_names.size(), m_output_node_names.data(), m_output_node_names.size());
188 assert(output_tensors.size() == 1 && output_tensors.front().IsTensor());
189
190 // Get pointer to output tensor float values
191 float* floatarr = output_tensors.front().GetTensorMutableData<float>();
192 int arrSize = sizeof(*floatarr)/sizeof(floatarr[0]);
193
194 // show true label for the test input
195 ATH_MSG_DEBUG("Label for the input test data = "<<output_tensor_values);
196 float ConstScore = -999;
197 int max_index = 0;
198 for (int i = 0; i < arrSize; i++){
199 ATH_MSG_VERBOSE("Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
200 if (ConstScore<floatarr[i]){
201 ConstScore = floatarr[i];
202 max_index = i;
203 }
204 }
205 ATH_MSG_DEBUG("Class: "<<max_index<<" has the highest score: "<<floatarr[max_index]);
206
207 return ConstScore;
208
209 } // end retrieve CNN score ----
210
211 // constituents transformer based
212 double JSSMLTool::retrieveConstituentsScore(std::vector<std::vector<float>> constituents) const {
213
214 // the format of the packed constituents is:
215 // constituents.size() ---> 4, for example, (m pT, eta, phi)
216 // constituents.at(0) ---> number of constituents
217 // the packing can be done for any kind of low level inputs
218 // i.e. PFO/UFO constituents, topo-towers, tracks, etc
219 // they can be concatened one after the other in case of multiple inputs
220
221 //*************************************************************************
222 // Score the model using sample data, and inspect values
223 // loading input data
224
225 std::vector<int> output_tensor_values_ = ReadOutputLabels();
226
227 int testSample = 0;
228
229 //preparing container to hold output data
230 int output_tensor_values = output_tensor_values_[testSample];
231
232 // prepare the inputs
233 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
234 std::vector<Ort::Value> input_tensors;
235 for (long unsigned int i=0; i<constituents.size(); i++) {
236
237 // test
238 std::vector<int64_t> const_dim = {1, static_cast<int64_t>(constituents.at(i).size())};
239
240 input_tensors.push_back(Ort::Value::CreateTensor<float>(
241 memory_info,
242 constituents.at(i).data(), constituents.at(i).size(), const_dim.data(), const_dim.size()
243 )
244 );
245 }
246
247 auto output_tensors = m_session->Run(Ort::RunOptions{nullptr}, m_input_node_names.data(), input_tensors.data(), m_input_node_names.size(), m_output_node_names.data(), m_output_node_names.size());
248 assert(output_tensors.size() == 1 && output_tensors.front().IsTensor());
249
250 // Get pointer to output tensor float values
251 float* floatarr = output_tensors.front().GetTensorMutableData<float>();
252 int arrSize = sizeof(*floatarr)/sizeof(floatarr[0]);
253
254 // show true label for the test input
255 ATH_MSG_DEBUG("Label for the input test data = "<<output_tensor_values);
256 float ConstScore = -999;
257 int max_index = 0;
258 for (int i = 0; i < arrSize; i++){
259 ATH_MSG_VERBOSE("Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
260 ATH_MSG_VERBOSE(" +++ Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
261 if (ConstScore<floatarr[i]){
262 ConstScore = floatarr[i];
263 max_index = i;
264 }
265 }
266 ATH_MSG_DEBUG("Class: "<<max_index<<" has the highest score: "<<floatarr[max_index]);
267
268 return ConstScore;
269
270 } // end retrieve constituents score ----
271
272 // constituents transformer based with const/inter variables
273 double JSSMLTool::retrieveConstituentsScore(std::vector<std::vector<float>> constituents, std::vector<std::vector<std::vector<float>>> interactions) const {
274
275 // the format of the constituents/interaction variables is:
276 // constituents ---> (nConstituents + nTowers, 7)
277 // interactions ---> (i, j, 4), with i, j in {nConstituents + nTowers}
278 // the packing can be done for any kind of low level inputs
279 // i.e. PFO/UFO constituents, topo-towers, tracks, etc
280 // they can be concatened one after the other in case of multiple inputs
281
282 //*************************************************************************
283 // Score the model using sample data, and inspect values
284 // loading input data
285
286 std::vector<int> output_tensor_values_ = ReadOutputLabels();
287
288 int testSample = 0;
289
290 //preparing container to hold output data
291 int output_tensor_values = output_tensor_values_[testSample];
292
293 // prepare the inputs
294 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
295 std::vector<Ort::Value> input_tensors;
296
297 // unroll the inputs
298 std::vector<float> constituents_values; //(constituents.size()*7);
299 for (long unsigned int i=0; i<constituents.size(); i++) {
300 for (long unsigned int j=0; j<7; j++) {
301 constituents_values.push_back(constituents.at(i).at(j));
302 }
303 }
304
305 std::vector<float> interactions_values; //(interactions.size()*interactions.size()*4);
306 for (long unsigned int i=0; i<interactions.size(); i++) {
307 for (long unsigned int k=0; k<interactions.size(); k++) {
308 for (long unsigned int j=0; j<4; j++) {
309 interactions_values.push_back(interactions.at(i).at(k).at(j));
310 }
311 }
312 }
313
314 std::vector<int64_t> const_dim = {1, static_cast<int64_t>(constituents.size()), 7};
315 input_tensors.push_back(Ort::Value::CreateTensor<float>(
316 memory_info,
317 constituents_values.data(), constituents_values.size(), const_dim.data(), const_dim.size()
318 )
319 );
320
321 std::vector<int64_t> inter_dim = {1, static_cast<int64_t>(constituents.size()), static_cast<int64_t>(constituents.size()), 4};
322 input_tensors.push_back(Ort::Value::CreateTensor<float>(
323 memory_info,
324 interactions_values.data(), interactions_values.size(), inter_dim.data(), inter_dim.size()
325 )
326 );
327
328 auto output_tensors = m_session->Run(Ort::RunOptions{nullptr}, m_input_node_names.data(), input_tensors.data(), m_input_node_names.size(), m_output_node_names.data(), m_output_node_names.size());
329 assert(output_tensors.size() == 1 && output_tensors.front().IsTensor());
330
331 // Get pointer to output tensor float values
332 float* floatarr = output_tensors.front().GetTensorMutableData<float>();
333 int arrSize = sizeof(*floatarr)/sizeof(floatarr[0]);
334
335 // show true label for the test input
336 ATH_MSG_DEBUG("Label for the input test data = "<<output_tensor_values);
337 float ConstScore = -999;
338 int max_index = 0;
339 for (int i = 0; i < arrSize; i++){
340 ATH_MSG_VERBOSE("Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
341 ATH_MSG_VERBOSE(" +++ Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
342 if (ConstScore<floatarr[i]){
343 ConstScore = floatarr[i];
344 max_index = i;
345 }
346 }
347 ATH_MSG_DEBUG("Class: "<<max_index<<" has the highest score: "<<floatarr[max_index]);
348
349 return ConstScore;
350
351 } // end retrieve constituents score ----
352
353 // constituents transformer based with const/mask/inter variables
354 double JSSMLTool::retrieveConstituentsScore(std::vector<std::vector<float>> constituents, std::vector<std::vector<std::vector<float>>> interactions, std::vector<std::vector<float>> mask) const {
355
356 // the format of the constituents/interaction variables is:
357 // constituents ---> (nConstituents, 7)
358 // interactions ---> (i, j, 4), with i, j in {nConstituents}
359 // masks ---> (nConstituents, 1)
360 // the packing can be done for any kind of low level inputs
361 // i.e. PFO/UFO constituents, topo-towers, tracks, etc
362 // they can be concatened one after the other in case of multiple inputs
363
364 //*************************************************************************
365 // Score the model using sample data, and inspect values
366 // loading input data
367
368 std::vector<int> output_tensor_values_ = ReadOutputLabels();
369
370 int testSample = 0;
371
372 //preparing container to hold output data
373 int output_tensor_values = output_tensor_values_[testSample];
374
375 // prepare the inputs
376 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
377 std::vector<Ort::Value> input_tensors;
378
379 // unroll the inputs
380 std::vector<float> constituents_values;
381 for (long unsigned int j=0; j<7; j++) {
382 for (long unsigned int i=0; i<constituents.size(); i++) {
383 constituents_values.push_back(constituents.at(i).at(j));
384 }
385 }
386
387 std::vector<float> interactions_values;
388 for (long unsigned int k=0; k<4; k++) {
389 for (long unsigned int i=0; i<interactions.size(); i++) {
390 for (long unsigned int j=0; j<interactions.size(); j++) {
391 interactions_values.push_back(interactions.at(i).at(j).at(k));
392 }
393 }
394 }
395
396 std::vector<float> mask_values;
397 for (long unsigned int j=0; j<1; j++) {
398 for (long unsigned int i=0; i<mask.size(); i++) {
399 mask_values.push_back(mask.at(i).at(j));
400 }
401 }
402
403 std::vector<int64_t> const_dim = {1, 7, static_cast<int64_t>(constituents.size())};
404 input_tensors.push_back(Ort::Value::CreateTensor<float>(
405 memory_info,
406 constituents_values.data(), constituents_values.size(), const_dim.data(), const_dim.size()
407 )
408 );
409
410 std::vector<int64_t> inter_dim = {1, 4, static_cast<int64_t>(interactions.size()), static_cast<int64_t>(interactions.size())};
411 input_tensors.push_back(Ort::Value::CreateTensor<float>(
412 memory_info,
413 interactions_values.data(), interactions_values.size(), inter_dim.data(), inter_dim.size()
414 )
415 );
416
417 std::vector<int64_t> mask_dim = {1, 1, static_cast<int64_t>(mask.size())};
418 input_tensors.push_back(Ort::Value::CreateTensor<float>(
419 memory_info,
420 mask_values.data(), mask_values.size(), mask_dim.data(), mask_dim.size()
421 )
422 );
423
424 auto output_tensors = m_session->Run(Ort::RunOptions{nullptr}, m_input_node_names.data(), input_tensors.data(), m_input_node_names.size(), m_output_node_names.data(), m_output_node_names.size());
425 assert(output_tensors.size() == 1 && output_tensors.front().IsTensor());
426
427 // Get pointer to output tensor float values
428 float* floatarr = output_tensors.front().GetTensorMutableData<float>();
429 int arrSize = sizeof(*floatarr)/sizeof(floatarr[0]);
430
431 // show true label for the test input
432 ATH_MSG_DEBUG("Label for the input test data = "<<output_tensor_values);
433 float ConstScore = -999;
434 int max_index = 0;
435 for (int i = 0; i < arrSize; i++){
436 ATH_MSG_VERBOSE("Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
437 ATH_MSG_VERBOSE(" +++ Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
438 if (ConstScore<floatarr[i]){
439 ConstScore = floatarr[i];
440 max_index = i;
441 }
442 }
443 ATH_MSG_DEBUG("Class: "<<max_index<<" has the highest score: "<<floatarr[max_index]);
444
445 return ConstScore;
446
447 } // end retrieve constituents score ----
448
449 // constituents transformer based with const/mask/inter variables
450 std::vector<float> JSSMLTool::retrieveConstituentsScoreMultiClass(const std::vector<std::vector<float>>& constituents, const std::vector<std::vector<std::vector<float>>>& interactions, const std::vector<std::vector<float>>& mask) const {
451
452 // the format of the constituents/interaction variables is:
453 // constituents ---> (nConstituents, 9)
454 // interactions ---> (i, j, 4), with i, j in {nConstituents}
455 // masks ---> (nConstituents, 1)
456 // the packing can be done for any kind of low level inputs
457 // i.e. PFO/UFO constituents, topo-towers, tracks, etc
458 // they can be concatened one after the other in case of multiple inputs
459
460 //*************************************************************************
461 // Score the model using sample data, and inspect values
462 // loading input data
463
464 // input info
465 const int nParticleVariables = 9;
466 const int nInteractionVariables = 4;
467
468 std::vector<int> output_tensor_values_ = ReadOutputLabels();
469
470 int testSample = 0;
471
472 //preparing container to hold output data
473 int output_tensor_values = output_tensor_values_[testSample];
474
475 // prepare the inputs
476 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
477 std::vector<Ort::Value> input_tensors;
478
479 // unroll the inputs
480 std::vector<float> constituents_values;
481 for (const auto& c : constituents)
482 constituents_values.insert(constituents_values.end(), c.begin(), c.end());
483
484 std::vector<float> interactions_values;
485 for (const auto& inter_i : interactions) {
486 for (const auto& inter_j : inter_i)
487 interactions_values.insert(interactions_values.end(), inter_j.begin(), inter_j.end());
488 }
489
490 std::vector<uint8_t> mask_values;
491 for (const auto& m : mask)
492 mask_values.push_back(m[0]);
493
494 std::vector<int64_t> const_dim = {1, static_cast<int64_t>(constituents.size()), nParticleVariables};
495 input_tensors.push_back(Ort::Value::CreateTensor<float>(
496 memory_info,
497 constituents_values.data(), constituents_values.size(), const_dim.data(), const_dim.size()
498 )
499 );
500
501 std::vector<int64_t> inter_dim = {1, static_cast<int64_t>(interactions.size()), static_cast<int64_t>(interactions.size()), nInteractionVariables};
502 input_tensors.push_back(Ort::Value::CreateTensor<float>(
503 memory_info,
504 interactions_values.data(), interactions_values.size(), inter_dim.data(), inter_dim.size()
505 )
506 );
507
508 std::vector<int64_t> mask_dim = {1, static_cast<int64_t>(mask.size())};
509 input_tensors.push_back(Ort::Value::CreateTensor<bool>(
510 memory_info,
511 reinterpret_cast<bool*>(mask_values.data()),
512 mask_values.size(), mask_dim.data(), mask_dim.size()
513 )
514 );
515
516 std::vector<Ort::Value> output_tensors = m_session->Run(Ort::RunOptions{nullptr}, m_input_node_names.data(), input_tensors.data(), m_input_node_names.size(), m_output_node_names.data(), m_output_node_names.size());
517 assert(output_tensors.front().IsTensor());
518
519 // Get pointer to output tensor float values
520 float* floatarr = output_tensors.front().GetTensorMutableData<float>();
521 auto info = output_tensors.front().GetTensorTypeAndShapeInfo();
522 size_t arrSize = info.GetElementCount();
523
524 // show true label for the test input
525 ATH_MSG_DEBUG("Label for the input test data = "<<output_tensor_values);
526 std::vector<float> ConstScores;
527 for (long unsigned int i = 0; i < arrSize; i++){
528 ATH_MSG_VERBOSE(" +++ Score for class " << i << " = " << floatarr[i]);
529 ConstScores.push_back(floatarr[i]);
530 }
531
532 return ConstScores;
533
534 } // end retrieve constituents score ----
535
536 // dedicated DisCo/DNN method ---
537 double JSSMLTool::retrieveHighLevelScore(std::map<std::string, double> JSSVars) const {
538
539 //*************************************************************************
540 // Score the model using sample data, and inspect values
541
542 //preparing container to hold input data
543 size_t input_tensor_size = m_nvars;
544 std::vector<float> input_tensor_values(m_nvars);
545
546 // loading input data
547 input_tensor_values = ReadJSSInputs(std::move(JSSVars));
548
549 // preparing container to hold output data
550 int testSample = 0;
551 std::vector<int> output_tensor_values_ = ReadOutputLabels();
552 int output_tensor_values = output_tensor_values_[testSample];
553
554 // create input tensor object from data values
555 auto memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
556
557 // we need a multiple tensor input structure for DisCo model
558 Ort::Value input1 = Ort::Value::CreateTensor<float>(memory_info, const_cast<float*>(input_tensor_values.data()), input_tensor_size, m_input_node_dims.data(), m_input_node_dims.size());
559 std::vector<float> empty = {1.};
560 Ort::Value input2 = Ort::Value::CreateTensor<float>(memory_info, empty.data(), 1, m_input_node_dims.data(), m_input_node_dims.size());
561 Ort::Value input3 = Ort::Value::CreateTensor<float>(memory_info, empty.data(), 1, m_input_node_dims.data(), m_input_node_dims.size());
562 Ort::Value input4 = Ort::Value::CreateTensor<float>(memory_info, empty.data(), 1, m_input_node_dims.data(), m_input_node_dims.size());
563 std::vector<Ort::Value> input_tensor;
564 std::vector<int64_t> aaa = {1, m_nvars};
565 input_tensor.emplace_back(
566 Ort::Value::CreateTensor<float>(memory_info, input_tensor_values.data(), input_tensor_size, aaa.data(), aaa.size())
567 );
568 input_tensor.emplace_back(
569 Ort::Value::CreateTensor<float>(memory_info, input_tensor_values.data(), input_tensor_size, m_input_node_dims.data(), m_input_node_dims.size())
570 );
571 input_tensor.emplace_back(
572 Ort::Value::CreateTensor<float>(memory_info, input_tensor_values.data(), input_tensor_size, m_input_node_dims.data(), m_input_node_dims.size())
573 );
574 input_tensor.emplace_back(
575 Ort::Value::CreateTensor<float>(memory_info, input_tensor_values.data(), input_tensor_size, m_input_node_dims.data(), m_input_node_dims.size())
576 );
577
578 auto output_tensors = m_session->Run(Ort::RunOptions{nullptr}, m_input_node_names.data(), input_tensor.data(), m_input_node_names.size(), m_output_node_names.data(), m_output_node_names.size());
579 assert(output_tensors.size() == 1 && output_tensors.front().IsTensor());
580
581 // Get pointer to output tensor float values
582 float* floatarr = output_tensors.front().GetTensorMutableData<float>();
583 int arrSize = sizeof(*floatarr)/sizeof(floatarr[0]);
584
585 // show true label for the test input
586 ATH_MSG_DEBUG("Label for the input test data = "<<output_tensor_values);
587 float HLScore = -999;
588 int max_index = 0;
589 for (int i = 0; i < arrSize; i++){
590 ATH_MSG_VERBOSE("Score for class "<<i<<" = "<<floatarr[i]<<std::endl);
591 if (HLScore<floatarr[i]){
592 HLScore = floatarr[i];
593 max_index = i;
594 }
595 }
596 ATH_MSG_DEBUG("Class: "<<max_index<<" has the highest score: "<<floatarr[max_index]);
597
598 return HLScore;
599
600 } // end retrieve HighLevel score ----
601
602 // extra methods
603 StatusCode JSSMLTool::SetScaler(const std::map<std::string, std::vector<double>> & scaler){
604 m_scaler = scaler;
605
606 // ToDo:
607 // this will have an overriding config as property
608 m_JSSInputMap = {
609 {0,"pT"}, {1,"CNN"}, {2,"D2"}, {3,"nTracks"}, {4,"ZCut12"},
610 {5,"Tau1_wta"}, {6,"Tau2_wta"}, {7,"Tau3_wta"},
611 {8,"KtDR"}, {9,"Split12"}, {10,"Split23"},
612 {11,"ECF1"}, {12,"ECF2"}, {13,"ECF3"},
613 {14,"Angularity"}, {15,"FoxWolfram0"}, {16,"FoxWolfram2"},
614 {17,"Aplanarity"}, {18,"PlanarFlow"}, {19,"Qw"},
615 };
616 m_nvars = m_JSSInputMap.size();
617
618 return StatusCode::SUCCESS;
619 }
620
621} // namespace AthONNX
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
size_t size() const
Number of registered mappings.
static const Attributes_t empty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
std::string m_modelFileName
Name of the model file to load.
Definition JSSMLTool.h:83
std::map< std::string, std::vector< double > > m_scaler
Definition JSSMLTool.h:77
virtual double retrieveHighLevelScore(std::map< std::string, double > JSSVars) const override
virtual StatusCode initialize() override
Function initialising the tool.
Definition JSSMLTool.cxx:86
std::vector< float > ReadJetImagePixels(std::vector< TH2D > Images) const
Definition JSSMLTool.cxx:17
JSSMLTool(const std::string &name)
Definition JSSMLTool.cxx:76
virtual std::vector< float > retrieveConstituentsScoreMultiClass(const std::vector< std::vector< float > > &constituents, const std::vector< std::vector< std::vector< float > > > &interactions, const std::vector< std::vector< float > > &mask) const override
std::vector< int > ReadOutputLabels() const
Definition JSSMLTool.cxx:66
std::unique_ptr< Ort::Env > m_env
Definition JSSMLTool.h:75
std::vector< int64_t > m_output_node_dims
Definition JSSMLTool.h:93
virtual double retrieveConstituentsScore(std::vector< TH2D > Images) const override
Function executing the tool for a single event.
size_t m_num_output_nodes
Definition JSSMLTool.h:94
size_t m_num_input_nodes
Definition JSSMLTool.h:89
std::vector< float > ReadJSSInputs(std::map< std::string, double > JSSVars) const
Definition JSSMLTool.cxx:39
std::vector< const char * > m_output_node_names
Definition JSSMLTool.h:95
std::vector< int64_t > m_input_node_dims
Definition JSSMLTool.h:88
std::map< int, std::string > m_JSSInputMap
Definition JSSMLTool.h:78
std::vector< const char * > m_input_node_names
Definition JSSMLTool.h:90
std::unique_ptr< Ort::Session > m_session
Definition JSSMLTool.h:74
StatusCode SetScaler(const std::map< std::string, std::vector< double > > &scaler) override
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
STL namespace.
#define likely(x)