This macro provides an example of using a trained model with PyTorch and make inference using SOFIE directly from Numpy This macro uses as input an ONNX model generated with the TMVA_SOFIE_PyTorch_HiggsModel.py tutorial You need to run that macro before this one.
In this case we are parsing the input file and then run the inference in the same macro making use of the ROOT JITing capability
import numpy as np
import ROOT
modelFile = "HiggsModel.onnx"
if not exists(modelFile):
raise FileNotFoundError(
"You need to run TMVA_SOFIE_PyTorch_HiggsModel.py to generate the ONNX trained model")
print("Generating inference code for the ONNX model from ", modelFile, "in the header ", generatedHeaderFile)
print("compiling SOFIE model ", modelName)
inputFileName = "Higgs_data.root"
sigData =
df1.AsNumpy(columns=[
'm_jj',
'm_jjj',
'm_lv',
'm_jlv',
'm_bb',
'm_wbb',
'm_wwbb'])
print("size of signal data", dataset_size)
sofie =
getattr(ROOT,
'TMVA_SOFIE_' + modelName)
print("Evaluating SOFIE models on signal data")
for i
in range(0,dataset_size):
if (i % dataset_size/10 == 0) :
print("result for signal event ",i,result[0])
print("using RDsataFrame to extract input data in a numpy array")
bkgData =
df2.AsNumpy(columns=[
'm_jj',
'm_jjj',
'm_lv',
'm_jlv',
'm_bb',
'm_wbb',
'm_wwbb'])
print("size of background data", dataset_size)
hb =
ROOT.TH1D(
"hb",
"Background result",100,0,1)
for i
in range(0,dataset_size):
if (i % dataset_size/10 == 0) :
print("result for background event ",i,result[0])
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 ,...
- Author
- Lorenzo Moneta
Definition in file TMVA_SOFIE_Inference.py.