This tutorial shows how to store several models in a single header file and the weights in a ROOT binary file. The models are then evaluated using the RDataFrame
import inspect
import os
import numpy as np
import ROOT
import torch
import torch.nn as nn
def CreateModel(nlayers=4, nunits=64):
layers = []
ninputs = 7
for i in range(nlayers):
layers += [nn.Linear(ninputs, nunits), nn.ReLU()]
ninputs = nunits
layers += [nn.Linear(ninputs, 1), nn.Sigmoid()]
model = nn.Sequential(*layers)
print(model)
return model
def TrainModel(model, x, y, epochs=5, batch_size=50):
x = torch.from_numpy(x)
y = torch.from_numpy(y)
criterion = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters())
nbatches = x.shape[0] // batch_size
for epoch in range(epochs):
perm = torch.randperm(x.shape[0])
running_loss = 0.0
for i in range(nbatches):
idx = perm[i * batch_size : (i + 1) * batch_size]
optimizer.zero_grad()
loss = criterion(model(x[idx]), y[idx])
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch {epoch + 1}/{epochs} - average loss: {running_loss / nbatches:.4f}")
def ExportModel(model, modelName):
model.eval()
modelFile = modelName + ".onnx"
dummy_x = torch.randn(1, 7)
model(dummy_x)
def filtered_kwargs(func, **candidate_kwargs):
sig = inspect.signature(func)
return {k: v for k, v in candidate_kwargs.items() if k in sig.parameters}
kwargs = filtered_kwargs(
torch.onnx.export,
input_names=["input"],
output_names=["output"],
external_data=False,
dynamo=True,
)
print("calling torch.onnx.export with parameters", kwargs)
torch.onnx.export(model, dummy_x, modelFile, **kwargs)
print("model exported to ONNX as", modelFile)
return modelFile
def PrepareData():
inputFile = str(ROOT.gROOT.GetTutorialDir()) + "/machine_learning/data/Higgs_data.root"
sigData = df1.AsNumpy(columns=["m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"])
xsig = np.column_stack(list(sigData.values()))
data_sig_size = xsig.shape[0]
print("size of data", data_sig_size)
bkgData = df2.AsNumpy(columns=["m_jj", "m_jjj", "m_lv", "m_jlv", "m_bb", "m_wbb", "m_wwbb"])
xbkg = np.column_stack(list(bkgData.values()))
data_bkg_size = xbkg.shape[0]
ysig = np.ones(data_sig_size)
ybkg = np.zeros(data_bkg_size)
inputs_data = np.concatenate((xsig, xbkg), axis=0).astype(np.float32)
inputs_targets = np.concatenate((ysig, ybkg), axis=0).astype(np.float32)
rng = np.random.default_rng(1234)
idx = rng.permutation(inputs_data.shape[0])
ntrain = inputs_data.shape[0] // 2
x_train = inputs_data[idx[:ntrain]]
y_train = inputs_targets[idx[:ntrain]].reshape(-1, 1)
x_test = inputs_data[idx[ntrain:]]
y_test = inputs_targets[idx[ntrain:]].reshape(-1, 1)
return x_train, y_train, x_test, y_test
def TrainModels(x_train, y_train, modelNames):
modelFiles = []
for modelName in modelNames:
model = CreateModel(4, 64)
TrainModel(model, x_train, y_train)
modelFiles.append(ExportModel(model, modelName))
return modelFiles
x_train, y_train, x_test, y_test = PrepareData()
modelNames = ["Higgs_Model_4L_50", "Higgs_Model_4L_200", "Higgs_Model_2L_500"]
model1, model2, model3 = TrainModels(x_train, y_train, modelNames)
def GenerateModelCode(modelFile, generatedHeaderFile, weightFile):
parser = ROOT.TMVA.Experimental.SOFIE.RModelParser_ONNX()
model = parser.Parse(modelFile)
print("Generating inference code for the ONNX model from ", modelFile, "in the header ", generatedHeaderFile)
model.Generate(ROOT.TMVA.Experimental.SOFIE.Options.kSafetensorsWeightFile)
model.OutputGenerated(generatedHeaderFile, True)
os.replace(generatedHeaderFile.replace(".hxx", ".safetensors"), weightFile)
return generatedHeaderFile
generatedHeaderFile = "Higgs_Model.hxx"
if os.path.exists(generatedHeaderFile):
print("removing existing file", generatedHeaderFile)
os.remove(generatedHeaderFile)
weightFiles = [name + ".safetensors" for name in modelNames]
for weightFile in weightFiles:
if os.path.exists(weightFile):
print("removing existing file", weightFile)
os.remove(weightFile)
GenerateModelCode(model1, generatedHeaderFile, weightFiles[0])
GenerateModelCode(model2, generatedHeaderFile, weightFiles[1])
GenerateModelCode(model3, generatedHeaderFile, weightFiles[2])
ROOT.gInterpreter.Declare('#include "' + generatedHeaderFile + '"')
session1 = ROOT.TMVA_SOFIE_Higgs_Model_4L_50.Session(weightFiles[0])
session2 = ROOT.TMVA_SOFIE_Higgs_Model_4L_200.Session(weightFiles[1])
session3 = ROOT.TMVA_SOFIE_Higgs_Model_2L_500.Session(weightFiles[2])
hs1 = ROOT.TH1D("hs1", "Signal result 4L 50", 100, 0, 1)
hs2 = ROOT.TH1D("hs2", "Signal result 4L 200", 100, 0, 1)
hs3 = ROOT.TH1D("hs3", "Signal result 2L 500", 100, 0, 1)
hb1 = ROOT.TH1D("hb1", "Background result 4L 50", 100, 0, 1)
hb2 = ROOT.TH1D("hb2", "Background result 4L 200", 100, 0, 1)
hb3 = ROOT.TH1D("hb3", "Background result 2L 500", 100, 0, 1)
def EvalModel(session, x):
result = session.infer(x)
return result[0]
for i in range(0, x_test.shape[0]):
result1 = EvalModel(session1, x_test[i, :])
result2 = EvalModel(session2, x_test[i, :])
result3 = EvalModel(session3, x_test[i, :])
if y_test[i] == 1:
hs1.Fill(result1)
hs2.Fill(result2)
hs3.Fill(result3)
else:
hb1.Fill(result1)
hb2.Fill(result2)
hb3.Fill(result3)
def PlotHistos(hs, hb):
hs.SetLineColor("kRed")
hb.SetLineColor("kBlue")
hs.Draw()
hb.Draw("same")
c1 = ROOT.TCanvas()
c1.Divide(1, 3)
c1.cd(1)
PlotHistos(hs1, hb1)
c1.cd(2)
PlotHistos(hs2, hb2)
c1.cd(3)
PlotHistos(hs3, hb3)
c1.Draw()
def GetContent(h):
n = h.GetNbinsX()
x = ROOT.std.vector["float"](n)
w = ROOT.std.vector["float"](n)
for i in range(0, n):
x[i] = h.GetBinCenter(i + 1)
w[i] = h.GetBinContent(i + 1)
return x, w
def MakeROCCurve(hs, hb):
xs, ws = GetContent(hs)
xb, wb = GetContent(hb)
roc = ROOT.TMVA.ROCCurve(xs, xb, ws, wb)
print("ROC integral for ", hs.GetName(), roc.GetROCIntegral())
curve = roc.GetROCCurve()
curve.SetName(hs.GetName())
return roc, curve
c2 = ROOT.TCanvas()
r1, curve1 = MakeROCCurve(hs1, hb1)
curve1.SetLineColor("kRed")
curve1.Draw("AC")
r2, curve2 = MakeROCCurve(hs2, hb2)
curve2.SetLineColor("kBlue")
curve2.Draw("C")
r3, curve3 = MakeROCCurve(hs3, hb3)
curve3.SetLineColor("kGreen")
curve3.Draw("C")
c2.Draw()
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
size of data 10000
Sequential(
(0): Linear(in_features=7, out_features=64, bias=True)
(1): ReLU()
(2): Linear(in_features=64, out_features=64, bias=True)
(3): ReLU()
(4): Linear(in_features=64, out_features=64, bias=True)
(5): ReLU()
(6): Linear(in_features=64, out_features=64, bias=True)
(7): ReLU()
(8): Linear(in_features=64, out_features=1, bias=True)
(9): Sigmoid()
)
Epoch 1/5 - average loss: 0.6698
Epoch 2/5 - average loss: 0.6454
Epoch 3/5 - average loss: 0.6323
Epoch 4/5 - average loss: 0.6213
Epoch 5/5 - average loss: 0.6155
calling torch.onnx.export with parameters {'input_names': ['input'], 'output_names': ['output'], 'external_data': False, 'dynamo': True}
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`...
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`... ✅
[torch.onnx] Run decompositions...
[torch.onnx] Run decompositions... ✅
[torch.onnx] Translate the graph into ONNX...
[torch.onnx] Translate the graph into ONNX... ✅
[torch.onnx] Optimize the ONNX graph...
[torch.onnx] Optimize the ONNX graph... ✅
model exported to ONNX as Higgs_Model_4L_50.onnx
Sequential(
(0): Linear(in_features=7, out_features=64, bias=True)
(1): ReLU()
(2): Linear(in_features=64, out_features=64, bias=True)
(3): ReLU()
(4): Linear(in_features=64, out_features=64, bias=True)
(5): ReLU()
(6): Linear(in_features=64, out_features=64, bias=True)
(7): ReLU()
(8): Linear(in_features=64, out_features=1, bias=True)
(9): Sigmoid()
)
Epoch 1/5 - average loss: 0.6742
Epoch 2/5 - average loss: 0.6463
Epoch 3/5 - average loss: 0.6324
Epoch 4/5 - average loss: 0.6275
Epoch 5/5 - average loss: 0.6185
calling torch.onnx.export with parameters {'input_names': ['input'], 'output_names': ['output'], 'external_data': False, 'dynamo': True}
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`...
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`... ✅
[torch.onnx] Run decompositions...
[torch.onnx] Run decompositions... ✅
[torch.onnx] Translate the graph into ONNX...
[torch.onnx] Translate the graph into ONNX... ✅
[torch.onnx] Optimize the ONNX graph...
[torch.onnx] Optimize the ONNX graph... ✅
model exported to ONNX as Higgs_Model_4L_200.onnx
Sequential(
(0): Linear(in_features=7, out_features=64, bias=True)
(1): ReLU()
(2): Linear(in_features=64, out_features=64, bias=True)
(3): ReLU()
(4): Linear(in_features=64, out_features=64, bias=True)
(5): ReLU()
(6): Linear(in_features=64, out_features=64, bias=True)
(7): ReLU()
(8): Linear(in_features=64, out_features=1, bias=True)
(9): Sigmoid()
)
Epoch 1/5 - average loss: 0.6759
Epoch 2/5 - average loss: 0.6518
Epoch 3/5 - average loss: 0.6357
Epoch 4/5 - average loss: 0.6264
Epoch 5/5 - average loss: 0.6212
calling torch.onnx.export with parameters {'input_names': ['input'], 'output_names': ['output'], 'external_data': False, 'dynamo': True}
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`...
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`... ✅
[torch.onnx] Run decompositions...
[torch.onnx] Run decompositions... ✅
[torch.onnx] Translate the graph into ONNX...
[torch.onnx] Translate the graph into ONNX... ✅
[torch.onnx] Optimize the ONNX graph...
[torch.onnx] Optimize the ONNX graph... ✅
model exported to ONNX as Higgs_Model_2L_500.onnx
Generating inference code for the ONNX model from Higgs_Model_4L_50.onnx in the header Higgs_Model.hxx
Generating inference code for the ONNX model from Higgs_Model_4L_200.onnx in the header Higgs_Model.hxx
Generating inference code for the ONNX model from Higgs_Model_2L_500.onnx in the header Higgs_Model.hxx
ROC integral for hs1 0.7333611304018524
ROC integral for hs2 0.7281743310212844
ROC integral for hs3 0.7239186074312697