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