Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_SOFIE_Inference.py
Go to the documentation of this file.
1### \file
2### \ingroup tutorial_ml
3### \notebook -nodraw
4### This macro provides an example of using a trained model with PyTorch
5### and make inference using SOFIE directly from Numpy
6### This macro uses as input an ONNX model generated with the
7### TMVA_SOFIE_PyTorch_HiggsModel.py tutorial
8### You need to run that macro before this one.
9### In this case we are parsing the input file and then run the inference in the same
10### macro making use of the ROOT JITing capability
11###
12###
13### \macro_code
14### \macro_output
15### \author Lorenzo Moneta
16
17from os.path import exists
18
19import numpy as np
20import ROOT
21
22# check if the input file exists
23modelFile = "HiggsModel.onnx"
24
25if not exists(modelFile):
26 raise FileNotFoundError("You need to run TMVA_SOFIE_PyTorch_HiggsModel.py to generate the ONNX trained model")
27
28
29# parse the input ONNX model into RModel object
31model = parser.Parse(modelFile)
32
33generatedHeaderFile = modelFile.replace(".onnx", ".hxx")
34print("Generating inference code for the ONNX model from ", modelFile, "in the header ", generatedHeaderFile)
35#Generating inference code
37model.OutputGenerated(generatedHeaderFile)
39
40# now compile using ROOT JIT trained model
41modelName = modelFile.replace(".onnx", "")
42print("compiling SOFIE model ", modelName)
43ROOT.gInterpreter.Declare('#include "' + generatedHeaderFile + '"')
44
45inputFileName = "Higgs_data.root"
46inputFile = str(ROOT.gROOT.GetTutorialDir()) + "/machine_learning/data/" + inputFileName
47
48
49
50
51
52# make SOFIE inference on signal data
53
54df1 = ROOT.RDataFrame("sig_tree", inputFile)
55sigData = df1.AsNumpy(columns=['m_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb'])
56#print(sigData)
57
58# stack all the 7 numpy array in a single array (nevents x nvars)
59xsig = np.column_stack(list(sigData.values()))
60dataset_size = xsig.shape[0]
61print("size of signal data", dataset_size)
62
63#instantiate SOFIE session class
64#session = ROOT.TMVA_SOFIE_HiggsModel.Session()
65#get the sofie session namespace
66sofie = getattr(ROOT, 'TMVA_SOFIE_' + modelName)
67session = sofie.Session()
68
69print("Evaluating SOFIE models on signal data")
70hs = ROOT.TH1D("hs","Signal result",100,0,1)
71for i in range(0,dataset_size):
72 result = session.infer(xsig[i,:])
73 if (i % dataset_size/10 == 0) :
74 print("result for signal event ",i,result[0])
75 hs.Fill(result[0])
76
77print("using RDsataFrame to extract input data in a numpy array")
78# make SOFIE inference on background data
79df2 = ROOT.RDataFrame("bkg_tree", inputFile)
80bkgData = df2.AsNumpy(columns=['m_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb'])
81
82xbkg = np.column_stack(list(bkgData.values()))
83dataset_size = xbkg.shape[0]
84print("size of background data", dataset_size)
85
86hb = ROOT.TH1D("hb","Background result",100,0,1)
87for i in range(0,dataset_size):
88 result = session.infer(xbkg[i,:])
89 if (i % dataset_size/10 == 0) :
90 print("result for background event ",i,result[0])
91
92 hb.Fill(result[0])
93
94
95c1 = ROOT.TCanvas()
97hs.SetLineColor("kRed")
98hs.Draw()
99hb.SetLineColor("kBlue")
100hb.Draw("SAME")
102c1.Draw()
103
104
105print("Number of signal entries",hs.GetEntries())
106print("Number of background entries",hb.GetEntries())
107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...