-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtrain.py
488 lines (403 loc) · 20.4 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import tensorflow
import tensorflow.keras.backend as K
from tensorflow.keras import optimizers, initializers
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping, CSVLogger
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Model
from sklearn.model_selection import train_test_split
import numpy as np
import tables
import matplotlib.pyplot as plt
import argparse
import math
#import setGPU
import time
import os
import pathlib
import datetime
import tqdm
import h5py
from glob import glob
import itertools
# Import custom modules
from Write_MET_binned_histogram import *
from cyclical_learning_rate import CyclicLR
from models import *
from utils import *
from loss import custom_loss_wrapper
from DataGenerator import DataGenerator
import matplotlib.pyplot as plt
import mplhep as hep
def MakeEdgeHist(edge_feat, xname, outputname, nbins=1000, density=False, yname="# of edges"):
plt.style.use(hep.style.CMS)
plt.figure(figsize=(10, 8))
plt.hist(edge_feat, bins=nbins, density=density, histtype='step', facecolor='k', label='Truth')
plt.xlabel(xname)
plt.ylabel(yname)
plt.savefig(outputname)
plt.close()
def deltaR_calc(eta1, phi1, eta2, phi2):
""" calculate deltaR """
dphi = (phi1-phi2)
gt_pi_idx = (dphi > np.pi)
lt_pi_idx = (dphi < -np.pi)
dphi[gt_pi_idx] -= 2*np.pi
dphi[lt_pi_idx] += 2*np.pi
deta = eta1-eta2
return np.hypot(deta, dphi)
def kT_calc(pti, ptj, dR):
min_pt = np.minimum(pti, ptj)
kT = min_pt * dR
return kT
def z_calc(pti, ptj):
epsilon = 1.0e-12
min_pt = np.minimum(pti, ptj)
z = min_pt/(pti + ptj + epsilon)
return z
def mass2_calc(pi, pj):
pij = pi + pj
m2 = pij[:, :, 0]**2 - pij[:, :, 1]**2 - pij[:, :, 2]**2 - pij[:, :, 3]**2
return m2
def get_callbacks(path_out, sample_size, batch_size):
# early stopping callback
early_stopping = EarlyStopping(monitor='val_loss', patience=40, verbose=1, restore_best_weights=False)
csv_logger = CSVLogger(f'{path_out}loss_history.log')
# model checkpoint callback
# this saves our model architecture + parameters into model.h5
model_checkpoint = ModelCheckpoint(f'{path_out}model.h5', monitor='val_loss',
verbose=0, save_best_only=True,
save_weights_only=False, mode='auto',
period=1)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4, min_lr=0.000001, cooldown=3, verbose=1)
lr_scale = 1.
clr = CyclicLR(base_lr=0.0003*lr_scale, max_lr=0.001*lr_scale, step_size=sample_size/batch_size, mode='triangular2')
stop_on_nan = tensorflow.keras.callbacks.TerminateOnNaN()
callbacks = [early_stopping, clr, stop_on_nan, csv_logger, model_checkpoint]
return callbacks
def test(Yr_test, predict_test, PUPPI_pt, path_out):
MakePlots(Yr_test, predict_test, PUPPI_pt, path_out=path_out)
Yr_test = convertXY2PtPhi(Yr_test)
predict_test = convertXY2PtPhi(predict_test)
PUPPI_pt = convertXY2PtPhi(PUPPI_pt)
extract_result(predict_test, Yr_test, path_out, 'TTbar', 'ML')
extract_result(PUPPI_pt, Yr_test, path_out, 'TTbar', 'PU')
MET_rel_error_opaque(predict_test[:, 0], PUPPI_pt[:, 0], Yr_test[:, 0], name=''+path_out+'rel_error_opaque.png')
MET_binned_predict_mean_opaque(predict_test[:, 0], PUPPI_pt[:, 0], Yr_test[:, 0], 20, 0, 500, 0, '.', name=''+path_out+'PrVSGen.png')
Phi_abs_error_opaque(PUPPI_pt[:, 1], predict_test[:, 1], Yr_test[:, 1], name=path_out+'Phi_abs_err')
Pt_abs_error_opaque(PUPPI_pt[:, 0], predict_test[:, 0], Yr_test[:, 0], name=path_out+'Pt_abs_error')
def train_dataGenerator(args):
# general setup
maxNPF = args.maxNPF
n_features_pf = 6
n_features_pf_cat = 2
normFac = 1.
custom_loss = custom_loss_wrapper(normFac)
epochs = args.epochs
batch_size = args.batch_size
preprocessed = True
t_mode = args.mode
inputPath = args.input
path_out = args.output
quantized = args.quantized
model = args.model
units = list(map(int, args.units))
compute_ef = args.compute_edge_feat
edge_list = args.edge_features
# separate files into training, validation, and testing
filesList = glob(os.path.join(inputPath, '*.root'))
filesList.sort(reverse=True)
assert len(filesList) >= 3, "Need at least 3 files for DataGenerator: 1 valid, 1 test, 1 train"
valid_nfiles = max(1, int(.1*len(filesList)))
train_nfiles = len(filesList) - 2*valid_nfiles
test_nfiles = valid_nfiles
train_filesList = filesList[0:train_nfiles]
valid_filesList = filesList[train_nfiles: train_nfiles+valid_nfiles]
test_filesList = filesList[train_nfiles+valid_nfiles:test_nfiles+train_nfiles+valid_nfiles]
if compute_ef == 1:
# set up data generators; they perform h5 conversion if necessary and load in data batch by batch
trainGenerator = DataGenerator(list_files=train_filesList, batch_size=batch_size, maxNPF=maxNPF, compute_ef=1, edge_list=edge_list)
validGenerator = DataGenerator(list_files=valid_filesList, batch_size=batch_size, maxNPF=maxNPF, compute_ef=1, edge_list=edge_list)
testGenerator = DataGenerator(list_files=test_filesList, batch_size=batch_size, maxNPF=maxNPF, compute_ef=1, edge_list=edge_list)
Xr_train, Yr_train = trainGenerator[0] # this apparenly calls all the attributes, so that we can get the correct input dimensions (train_generator.emb_input_dim)
else:
trainGenerator = DataGenerator(list_files=train_filesList, batch_size=batch_size)
validGenerator = DataGenerator(list_files=valid_filesList, batch_size=batch_size)
testGenerator = DataGenerator(list_files=test_filesList, batch_size=batch_size)
Xr_train, Yr_train = trainGenerator[0] # this apparenly calls all the attributes, so that we can get the correct input dimensions (train_generator.emb_input_dim)
# Load training model
if quantized is None:
if model == 'dense_embedding':
keras_model = dense_embedding(n_features=n_features_pf,
emb_out_dim=2,
n_features_cat=n_features_pf_cat,
activation='tanh',
embedding_input_dim=trainGenerator.emb_input_dim,
number_of_pupcandis=maxNPF,
t_mode=t_mode,
with_bias=False,
units=units)
elif model == 'graph_embedding':
keras_model = graph_embedding(n_features=n_features_pf,
emb_out_dim=2,
n_features_cat=n_features_pf_cat,
activation='tanh',
embedding_input_dim=trainGenerator.emb_input_dim,
number_of_pupcandis=maxNPF,
units=units, compute_ef=compute_ef, edge_list=edge_list)
else:
logit_total_bits = int(quantized[0])
logit_int_bits = int(quantized[1])
activation_total_bits = int(quantized[0])
activation_int_bits = int(quantized[1])
keras_model = dense_embedding_quantized(n_features=n_features_pf,
emb_out_dim=2,
n_features_cat=n_features_pf_cat,
activation_quantizer='quantized_relu',
embedding_input_dim=trainGenerator.emb_input_dim,
number_of_pupcandis=maxNPF,
t_mode=t_mode,
with_bias=False,
logit_quantizer='quantized_bits',
logit_total_bits=logit_total_bits,
logit_int_bits=logit_int_bits,
activation_total_bits=activation_total_bits,
activation_int_bits=activation_int_bits,
alpha=1,
use_stochastic_rounding=False,
units=units)
# Check which model will be used (0 for L1MET Model, 1 for DeepMET Model)
if t_mode == 0:
keras_model.compile(optimizer='adam', loss=custom_loss, metrics=['mean_absolute_error', 'mean_squared_error'])
verbose = 1
elif t_mode == 1:
optimizer = optimizers.Adam(lr=1., clipnorm=1.)
keras_model.compile(loss=custom_loss, optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
verbose = 1
# Run training
print(keras_model.summary())
start_time = time.time() # check start time
history = keras_model.fit(trainGenerator,
epochs=epochs,
verbose=verbose, # switch to 1 for more verbosity
validation_data=validGenerator,
callbacks=get_callbacks(path_out, len(trainGenerator), batch_size))
end_time = time.time() # check end time
predict_test = keras_model.predict(testGenerator) * normFac
all_PUPPI_pt = []
Yr_test = []
for (Xr, Yr) in tqdm.tqdm(testGenerator):
puppi_pt = np.sum(Xr[1], axis=1)
all_PUPPI_pt.append(puppi_pt)
Yr_test.append(Yr)
PUPPI_pt = normFac * np.concatenate(all_PUPPI_pt)
Yr_test = normFac * np.concatenate(Yr_test)
test(Yr_test, predict_test, PUPPI_pt, path_out)
fi = open("{}time.txt".format(path_out), 'w')
fi.write("Working Time (s) : {}".format(end_time - start_time))
fi.write("Working Time (m) : {}".format((end_time - start_time)/60.))
fi.close()
def train_loadAllData(args):
# general setup
maxNPF = args.maxNPF
n_features_pf = 6
n_features_pf_cat = 2
normFac = 1.
custom_loss = custom_loss_wrapper(normFac)
epochs = args.epochs
batch_size = args.batch_size
preprocessed = True
t_mode = args.mode
inputPath = args.input
path_out = args.output
quantized = args.quantized
units = list(map(int, args.units))
compute_ef = args.compute_edge_feat
model = args.model
edge_list = args.edge_features
# Read inputs
# convert root files to h5 and store in same location
h5files = []
for ifile in glob(os.path.join(f'{inputPath}', '*.root')):
h5file_path = ifile.replace('.root', '.h5')
if not os.path.isfile(h5file_path):
os.system(f'python convertNanoToHDF5_L1triggerToDeepMET.py -i {ifile} -o {h5file_path}')
h5files.append(h5file_path)
# It may be desireable to set specific files as the train, test, valid data sets
# For now I keep train.py used: selection from a list of indicies
Xorg, Y = read_input(h5files)
if maxNPF < 100:
order = Xorg[:, :, 0].argsort(axis=1)[:, ::-1]
shape = np.shape(Xorg)
for x in range(shape[0]):
Xorg[x, :, :] = Xorg[x, order[x], :]
Xorg = Xorg[:, 0:maxNPF, :]
Y = Y / -normFac
N = maxNPF
Nr = N*(N-1)
receiver_sender_list = [i for i in itertools.product(range(N), range(N)) if i[0] != i[1]]
Xi, Xp, Xc1, Xc2 = preProcessing(Xorg, normFac)
if compute_ef == 1:
eta = Xi[:, :, 1]
phi = Xi[:, :, 2]
pt = Xi[:, :, 0]
if ('m2' in edge_list):
px = Xp[:, :, 0]
py = Xp[:, :, 1]
pz = pt*np.sinh(eta)
energy = np.sqrt(px**2 + py**2 + pz**2)
p4 = np.stack((energy, px, py, pz), axis=-1)
receiver_sender_list = [i for i in itertools.product(range(N), range(N)) if i[0] != i[1]]
edge_idx = np.array(receiver_sender_list)
edge_stack = []
if ('dR' in edge_list) or ('kT' in edge_list):
eta1 = eta[:, edge_idx[:, 0]]
phi1 = phi[:, edge_idx[:, 0]]
eta2 = eta[:, edge_idx[:, 1]]
phi2 = phi[:, edge_idx[:, 1]]
dR = deltaR_calc(eta1, phi1, eta2, phi2)
edge_stack.append(dR)
if ('kT' in edge_list) or ('z' in edge_list):
pt1 = pt[:, edge_idx[:, 0]]
pt2 = pt[:, edge_idx[:, 1]]
if ('kT' in edge_list):
kT = kT_calc(pt1, pt2, dR)
edge_stack.append(kT)
if ('z' in edge_list):
z = z_calc(pt1, pt2)
edge_stack.append(z)
if ('m2' in edge_list):
p1 = p4[:, edge_idx[:, 0], :]
p2 = p4[:, edge_idx[:, 1], :]
m2 = mass2_calc(p1, p2)
edge_stack.append(m2)
ef = np.stack(edge_stack, axis=-1)
Xc = [Xc1, Xc2]
# dimension parameter for keras model
emb_input_dim = {i: int(np.max(Xc[i][0:1000])) + 1 for i in range(n_features_pf_cat)}
# Prepare training/val data
Xc = [Xc1, Xc2]
Yr = Y
Xr = [Xi, Xp] + Xc + [ef]
else:
Xi, Xp, Xc1, Xc2 = preProcessing(Xorg, normFac)
Xc = [Xc1, Xc2]
emb_input_dim = {
i: int(np.max(Xc[i][0:1000])) + 1 for i in range(n_features_pf_cat)
}
# Prepare training/val data
Yr = Y
Xr = [Xi, Xp] + Xc
indices = np.array([i for i in range(len(Yr))])
indices_train, indices_test = train_test_split(indices, test_size=1./7., random_state=7)
indices_train, indices_valid = train_test_split(indices_train, test_size=1./6., random_state=7)
# roughly the same split as the data generator workflow (train:valid:test=5:1:1)
Xr_train = [x[indices_train] for x in Xr]
Xr_test = [x[indices_test] for x in Xr]
Xr_valid = [x[indices_valid] for x in Xr]
Yr_train = Yr[indices_train]
Yr_test = Yr[indices_test]
Yr_valid = Yr[indices_valid]
# Load training model
if quantized is None:
if model == 'dense_embedding':
keras_model = dense_embedding(n_features=n_features_pf,
emb_out_dim=2,
n_features_cat=n_features_pf_cat,
activation='tanh',
embedding_input_dim=emb_input_dim,
number_of_pupcandis=maxNPF,
t_mode=t_mode,
with_bias=False,
units=units)
elif model == 'graph_embedding':
keras_model = graph_embedding(n_features=n_features_pf,
emb_out_dim=2,
n_features_cat=n_features_pf_cat,
activation='tanh',
embedding_input_dim=emb_input_dim,
number_of_pupcandis=maxNPF,
units=units,
compute_ef=compute_ef,
edge_list=edge_list)
else:
logit_total_bits = int(quantized[0])
logit_int_bits = int(quantized[1])
activation_total_bits = int(quantized[0])
activation_int_bits = int(quantized[1])
keras_model = dense_embedding_quantized(n_features=n_features_pf,
emb_out_dim=2,
n_features_cat=n_features_pf_cat,
activation_quantizer='quantized_relu',
embedding_input_dim=emb_input_dim,
number_of_pupcandis=maxNPF,
t_mode=t_mode,
with_bias=False,
logit_quantizer='quantized_bits',
logit_total_bits=logit_total_bits,
logit_int_bits=logit_int_bits,
activation_total_bits=activation_total_bits,
activation_int_bits=activation_int_bits,
alpha=1,
use_stochastic_rounding=False,
units=units)
# Check which model will be used (0 for L1MET Model, 1 for DeepMET Model)
if t_mode == 0:
keras_model.compile(optimizer='adam', loss=custom_loss, metrics=['mean_absolute_error', 'mean_squared_error'])
verbose = 1
elif t_mode == 1:
optimizer = optimizers.Adam(lr=1., clipnorm=1.)
keras_model.compile(loss=custom_loss, optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
verbose = 1
# Run training
print(keras_model.summary())
start_time = time.time() # check start time
history = keras_model.fit(Xr_train,
Yr_train,
epochs=epochs,
batch_size=batch_size,
verbose=verbose, # switch to 1 for more verbosity
validation_data=(Xr_valid, Yr_valid),
callbacks=get_callbacks(path_out, len(Yr_train), batch_size))
end_time = time.time() # check end time
predict_test = keras_model.predict(Xr_test) * normFac
PUPPI_pt = normFac * np.sum(Xr_test[1], axis=1)
Yr_test = normFac * Yr_test
test(Yr_test, predict_test, PUPPI_pt, path_out)
fi = open("{}time.txt".format(path_out), 'w')
fi.write("Working Time (s) : {}".format(end_time - start_time))
fi.write("Working Time (m) : {}".format((end_time - start_time)/60.))
fi.close()
def main():
time_path = time.strftime('%Y-%m-%d', time.localtime(time.time()))
parser = argparse.ArgumentParser()
parser.add_argument(
'--workflowType',
action='store',
type=str,
required=True,
choices=[
'dataGenerator',
'loadAllData'],
help='designate wheather youre using the data generator or loading all data into memory ')
parser.add_argument('--input', action='store', type=str, required=True, help='designate input file path')
parser.add_argument('--output', action='store', type=str, required=True, help='designate output file path')
parser.add_argument('--mode', action='store', type=int, required=True, choices=[0, 1], help='0 for L1MET, 1 for DeepMET')
parser.add_argument('--epochs', action='store', type=int, required=False, default=100, help='number of epochs to train for')
parser.add_argument('--batch-size', action='store', type=int, required=False, default=1024, help='batch size')
parser.add_argument('--quantized', action='store', required=False, nargs='+', help='optional argument: flag for quantized model and specify [total bits] [int bits]; empty for normal model')
parser.add_argument('--units', action='store', required=False, nargs='+', help='optional argument: specify number of units in each layer (also sets the number of layers)')
parser.add_argument('--model', action='store', required=False, choices=['dense_embedding', 'graph_embedding', 'node_select'], default='dense_embedding', help='optional argument: model')
parser.add_argument('--compute-edge-feat', action='store', type=int, required=False, choices=[0, 1], default=0, help='0 for no edge features, 1 to include edge features')
parser.add_argument('--maxNPF', action='store', type=int, required=False, default=100, help='maximum number of PUPPI candidates')
parser.add_argument('--edge-features', action='store', required=False, nargs='+', help='which edge features to use (i.e. dR, kT, z, m2)')
args = parser.parse_args()
workflowType = args.workflowType
os.makedirs(args.output, exist_ok=True)
if workflowType == 'dataGenerator':
train_dataGenerator(args)
elif workflowType == 'loadAllData':
train_loadAllData(args)
if __name__ == "__main__":
main()