Belle II Software  release-06-02-00
keras_to_weightfile.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 
15 
16 from keras.models import load_model
17 
18 
19 def get_model(number_of_features, number_of_spectators, number_of_events, training_fraction, parameters):
20  """
21  Just load keras model into state
22  """
23  return State(load_model(parameters['file_path']))
24 
25 
26 def partial_fit(state, X, S, y, w, epoch):
27  """
28  This should be empty, because our model is already fitted.
29  """
30  return False
31 
32 
33 if __name__ == "__main__":
34  import os
35  import pandas
36  from root_pandas import to_root
37  import tempfile
38  import json
39  import numpy as np
40 
41  import basf2_mva
42  import basf2_mva_util
43 
44  from keras.layers import Input, Dense
45  from keras.models import Model
46  from keras.optimizers import Adam
47  from keras.losses import binary_crossentropy
48  from keras.activations import sigmoid, tanh
49  from basf2 import conditions
50  # NOTE: do not use testing payloads in production! Any results obtained like this WILL NOT BE PUBLISHED
51  conditions.testing_payloads = [
52  'localdb/database.txt'
53  ]
54 
55  # ##############Building Data samples ###########################
56  # The training needs the same variables in the same orders as keras received them during training.
57  # For this example we just create some random variables.
58 
59  variables = ['x' + str(i) for i in range(10)]
60 
61  data = np.random.normal(size=[1000, 11])
62  data[:, -1] = np.int32(data[:, -1] > 0.5)
63 
64  # Build some simple model to convert into weightfile
65  input = Input(shape=(10,))
66 
67  net = Dense(units=100, activation=tanh)(input)
68  output = Dense(units=1, activation=sigmoid)(net)
69 
70  model = Model(input, output)
71  model.compile(optimizer=Adam(lr=0.01), loss=binary_crossentropy, metrics=['accuracy'])
72 
73  model.fit(data[:, :-1], data[:, -1], batch_size=10, epochs=10)
74 
75  # This path will delete itself with all data in it after end of program.
76  with tempfile.TemporaryDirectory() as path:
77  # Saving all variables in root files
78  dic = {}
79  for i, name in enumerate(variables):
80  dic.update({name: data[:, i]})
81  dic.update({'isSignal': data[:, -1]})
82 
83  df = pandas.DataFrame(dic, dtype=np.float32)
84  to_root(df, os.path.join(path, 'data.root'), key='tree')
85 
86  # Saving keras training model
87  model.save(os.path.join(path, 'weights.h5'))
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 = "contrib_keras"
99  specific_options.m_steering_file = 'mva/examples/keras/keras_to_weightfile.py'
100 
101  general_options.m_identifier = 'converted_keras'
102  specific_options.m_config = json.dumps({'file_path': os.path.join(path, 'weights.h5')})
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)
108  print('Overtraining AUC: ', basf2_mva_util.calculate_roc_auc(p, t))
def calculate_roc_auc(p, t)