Belle II Software light-2406-ragdoll
import_existing_keras_model.py
1#!/usr/bin/env python3
2
3
10
11# This example shows how to convert a model trained with keras inside a basf2 weightfile.
12
14
15from tensorflow.keras.models import load_model
16
17
18def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
19 """
20 Just load keras model into state
21 """
22 return State(load_model(parameters['file_path']))
23
24
25def partial_fit(state, X, S, y, w, epoch, batch):
26 """
27 This should be empty, because our model is already fitted.
28 """
29 return False
30
31
32if __name__ == "__main__":
33 import os
34 import pandas
35 import uproot
36 import tempfile
37 import json
38 import numpy as np
39
40 import basf2_mva
41 import basf2_mva_util
42
43 from tensorflow.keras.layers import Input, Dense
44 from tensorflow.keras.models import Model
45 from tensorflow.keras.optimizers import Adam
46 from tensorflow.keras.losses import binary_crossentropy
47 from tensorflow.keras.activations import sigmoid, tanh
48 from basf2 import conditions
49 # NOTE: do not use testing payloads in production! Any results obtained like this WILL NOT BE PUBLISHED
50 conditions.testing_payloads = [
51 'localdb/database.txt'
52 ]
53
54 # ##############Building Data samples ###########################
55 # The training needs the same variables in the same orders as keras received them during training.
56 # For this example we just create some random variables.
57
58 variables = ['x' + str(i) for i in range(10)]
59
60 data = np.random.normal(size=[1000, 11])
61 data[:, -1] = np.int32(data[:, -1] > 0.5)
62
63 # Build some simple model as an example to convert into weightfile
64 input = Input(shape=(10,))
65
66 net = Dense(units=100, activation=tanh)(input)
67 output = Dense(units=1, activation=sigmoid)(net)
68
69 model = Model(input, output)
70 model.compile(optimizer=Adam(lr=0.01), loss=binary_crossentropy, metrics=['accuracy'])
71
72 model.fit(data[:, :-1], data[:, -1], batch_size=10, epochs=10)
73
74 # This path will delete itself with all data in it after end of program.
75 with tempfile.TemporaryDirectory() as path:
76 # Saving all variables in root files
77 dic = {}
78 for i, name in enumerate(variables):
79 dic.update({name: data[:, i]})
80 dic.update({'isSignal': data[:, -1]})
81
82 df = pandas.DataFrame(dic, dtype=np.float64)
83 with uproot.recreate(os.path.join(path, 'data.root')) as outfile:
84 outfile['tree'] = df
85
86 # Saving keras training model
87 model.save(os.path.join(path, 'example_existing_model'))
88
89 # ##########################Do Conversion#################################
90
91 general_options = basf2_mva.GeneralOptions()
92 general_options.m_datafiles = basf2_mva.vector(os.path.join(path, 'data.root'))
93 general_options.m_treename = "tree"
94 general_options.m_variables = basf2_mva.vector(*variables)
95 general_options.m_target_variable = "isSignal"
96
97 specific_options = basf2_mva.PythonOptions()
98 specific_options.m_framework = "keras"
99 specific_options.m_steering_file = 'mva/examples/keras/import_existing_keras_model.py'
100
101 general_options.m_identifier = 'converted_keras'
102 specific_options.m_config = json.dumps({'file_path': os.path.join(path, 'example_existing_model')})
103 basf2_mva.teacher(general_options, specific_options)
104
105 # ########################Apply weightfile####################################
106 method = basf2_mva_util.Method(general_options.m_identifier)
107 p, t = method.apply_expert(general_options.m_datafiles, general_options.m_treename)
def calculate_auc_efficiency_vs_background_retention(p, t, w=None)