38def MakeImagesTree(n, nh, nw):
41 fileOutName =
"images_data_16x16.root"
48 sX2 = sX1 + delta_sigma
49 sY2 = sY1 - delta_sigma
50 h1 = ROOT.TH2D(
"h1",
"h1", nh, 0, 10, nw, 0, 10)
51 h2 = ROOT.TH2D(
"h2",
"h2", nh, 0, 10, nw, 0, 10)
52 f1 = ROOT.TF2(
"f1",
"xygaus")
53 f2 = ROOT.TF2(
"f2",
"xygaus")
54 sgn = ROOT.TTree(
"sig_tree",
"signal_tree")
55 bkg = ROOT.TTree(
"bkg_tree",
"background_tree")
57 f =
TFile(fileOutName,
"RECREATE")
58 x1 = ROOT.std.vector[
"float"](ntot)
59 x2 = ROOT.std.vector[
"float"](ntot)
63 bkg.Branch(
"vars",
"std::vector<float>", x1)
64 sgn.Branch(
"vars",
"std::vector<float>", x2)
69 f1.SetParameters(1, 5, sX1, 5, sY1)
70 f2.SetParameters(1, 5, sX2, 5, sY2)
71 ROOT.gRandom.SetSeed(0)
72 ROOT.Info(
"TMVA_CNN_Classification",
"Filling ROOT tree \n")
75 print(
"Generating image event ...", i)
80 f1.SetParameter(1, ROOT.gRandom.Uniform(3, 7))
81 f1.SetParameter(3, ROOT.gRandom.Uniform(3, 7))
82 f2.SetParameter(1, ROOT.gRandom.Uniform(3, 7))
83 f2.SetParameter(3, ROOT.gRandom.Uniform(3, 7))
85 h1.FillRandom(
"f1", nRndmEvts)
86 h2.FillRandom(
"f2", nRndmEvts)
92 x1[m] = h1.GetBinContent(k + 1, l + 1) + ROOT.gRandom.Gaus(0, pixelNoise)
93 x2[m] = h2.GetBinContent(k + 1, l + 1) + ROOT.gRandom.Gaus(0, pixelNoise)
101 print(
"Signal and background tree with images data written to the file %s", f.GetName())
106hasGPU =
"tmva-gpu" in ROOT.gROOT.GetConfigFeatures()
107hasCPU =
"tmva-cpu" in ROOT.gROOT.GetConfigFeatures()
111useTMVACNN = opt[0]
if len(opt) > 0
else False
112useKerasCNN = opt[1]
if len(opt) > 1
else False
113useTMVADNN = opt[2]
if len(opt) > 2
else False
114useTMVABDT = opt[3]
if len(opt) > 3
else False
115usePyTorchCNN = opt[4]
if len(opt) > 4
else False
117if (
not hasCPU
and not hasGPU) :
118 ROOT.Warning(
"TMVA_CNN_Classificaton",
"ROOT is not supporting tmva-cpu and tmva-gpu skip using TMVA-DNN and TMVA-CNN")
122if not "tmva-pymva" in ROOT.gROOT.GetConfigFeatures():
124 usePyTorchCNN =
False
128tf_spec = importlib.util.find_spec(
"tensorflow")
131 ROOT.Warning(
"TMVA_CNN_Classificaton",
"Skip using Keras since tensorflow is not installed")
133torch_spec = importlib.util.find_spec(
"torch")
134if torch_spec
is None:
135 usePyTorchCNN =
False
136 ROOT.Warning(
"TMVA_CNN_Classificaton",
"Skip using PyTorch since torch is not installed")
140 "TMVA_CNN_Classificaton",
141 "TMVA is not build with GPU or CPU multi-thread support. Cannot use TMVA Deep Learning for CNN",
144writeOutputFile =
True
151if "imt" in ROOT.gROOT.GetConfigFeatures():
153 ROOT.gSystem.Setenv(
"OMP_NUM_THREADS",
"1")
156 print(
"Running in serial mode since ROOT does not support MT")
163 outputFile =
TFile.Open(
"TMVA_CNN_ClassificationOutput.root",
"RECREATE")
188 "TMVA_CNN_Classification",
194 AnalysisType=
"Classification",
195 Transformations=
None,
218inputFileName =
"images_data_16x16.root"
221if ROOT.gSystem.AccessPathName(inputFileName):
222 MakeImagesTree(nevt, 16, 16)
226 ROOT.Warning(
"TMVA_CNN_Classification",
"Error opening input file %s - exit", inputFileName.Data())
234signalTree = inputFile.Get(
"sig_tree")
235backgroundTree = inputFile.Get(
"bkg_tree")
237nEventsSig = signalTree.GetEntries()
238nEventsBkg = backgroundTree.GetEntries()
242backgroundWeight = 1.0
245loader.AddSignalTree(signalTree, signalWeight)
246loader.AddBackgroundTree(backgroundTree, backgroundWeight)
250loader.AddVariablesArray(
"vars", imgSize)
268nTrainSig = 0.8 * nEventsSig
269nTrainBkg = 0.8 * nEventsBkg
273loader.PrepareTrainingAndTestTree(
276 nTrain_Signal=nTrainSig,
277 nTrain_Background=nTrainBkg,
280 NormMode=
"NumEvents",
282 CalcCorrelations=
False,
308 BoostType=
"AdaBoost",
311 BaggedSampleFraction=0.5,
312 SeparationType=
"GiniIndex",
323 layoutString = ROOT.TString(
324 "DENSE|100|RELU,BNORM,DENSE|100|RELU,BNORM,DENSE|100|RELU,BNORM,DENSE|100|RELU,DENSE|1|LINEAR"
330 trainingString1 = ROOT.TString(
331 "LearningRate=1e-3,Momentum=0.9,Repetitions=1,"
332 "ConvergenceSteps=5,BatchSize=100,TestRepetitions=1,"
333 "WeightDecay=1e-4,Regularization=None,"
334 "Optimizer=ADAM,DropConfig=0.0+0.0+0.0+0."
336 trainingString1 +=
",MaxEpochs=" + str(max_epochs)
339 dnnMethodName =
"TMVA_DNN_CPU"
345 dnnMethodName =
"TMVA_DNN_GPU"
353 ErrorStrategy=
"CROSSENTROPY",
355 WeightInitialization=
"XAVIER",
357 TrainingStrategy=trainingString1,
358 Architecture=dnnOptions
391 trainingString1 = ROOT.TString(
392 "LearningRate=1e-3,Momentum=0.9,Repetitions=1,"
393 "ConvergenceSteps=5,BatchSize=100,TestRepetitions=1,"
394 "WeightDecay=1e-4,Regularization=None,"
395 "Optimizer=ADAM,DropConfig=0.0+0.0+0.0+0.0"
397 trainingString1 +=
",MaxEpochs=" + str(max_epochs)
400 cnnMethodName =
"TMVA_CNN_CPU"
405 cnnMethodName =
"TMVA_CNN_GPU"
413 ErrorStrategy=
"CROSSENTROPY",
415 WeightInitialization=
"XAVIER",
416 InputLayout=
"1|16|16",
417 Layout=
"CONV|10|3|3|1|1|1|1|RELU,BNORM,CONV|10|3|3|1|1|1|1|RELU,MAXPOOL|2|2|1|1,RESHAPE|FLAT,DENSE|100|RELU,DENSE|1|LINEAR",
418 TrainingStrategy=trainingString1,
419 Architecture=cnnOptions,
427 ROOT.Info(
"TMVA_CNN_Classification",
"Using Convolutional PyTorch Model")
428 pyTorchFileName = str(ROOT.gROOT.GetTutorialDir())
429 pyTorchFileName +=
"/tmva/PyTorch_Generate_CNN_Model.py"
431 torch_spec = importlib.util.find_spec(
"torch")
432 if torch_spec
is not None and os.path.exists(pyTorchFileName):
436 ROOT.Info(
"TMVA_CNN_Classification",
"Booking PyTorch CNN model")
444 FilenameModel=
"PyTorchModelCNN.pt",
445 FilenameTrainedModel=
"PyTorchTrainedModelCNN.pt",
446 NumEpochs=max_epochs,
448 UserCode=str(pyTorchFileName)
452 "TMVA_CNN_Classification",
453 "PyTorch is not installed or model building file is not existing - skip using PyTorch",
457 ROOT.Info(
"TMVA_CNN_Classification",
"Building convolutional keras model")
461 from tensorflow.keras.models
import Sequential
462 from tensorflow.keras.optimizers
import Adam
466 from tensorflow.keras.layers
import Input, Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Reshape
470 model.add(Reshape((16, 16, 1), input_shape=(256,)))
471 model.add(Conv2D(10, kernel_size=(3, 3), kernel_initializer=
"TruncatedNormal", activation=
"relu", padding=
"same"))
472 model.add(Conv2D(10, kernel_size=(3, 3), kernel_initializer=
"TruncatedNormal", activation=
"relu", padding=
"same"))
474 model.add(MaxPooling2D(pool_size=(2, 2)))
476 model.add(Dense(64, activation=
"tanh"))
478 model.add(Dense(2, activation=
"sigmoid"))
479 model.compile(loss=
"binary_crossentropy", optimizer=Adam(learning_rate=0.001), weighted_metrics=[
"accuracy"])
480 model.save(
"model_cnn.h5")
483 if not os.path.exists(
"model_cnn.h5"):
484 raise FileNotFoundError(
"Error creating Keras model file - skip using Keras")
487 ROOT.Info(
"TMVA_CNN_Classification",
"Booking convolutional keras model")
495 FilenameModel=
"model_cnn.h5",
496 FilenameTrainedModel=
"trained_model_cnn.h5",
497 NumEpochs=max_epochs,
499 GpuOptions=
"allow_growth=True",
506factory.TrainAllMethods()
510factory.TestAllMethods()
512factory.EvaluateAllMethods()
516c1 = factory.GetROCCurve(loader)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
This is the main MVA steering class.
static void PyInitialize()
Initialize Python interpreter.
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT's implicit multi-threading for all objects and methods that provide an internal paralleli...
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.