forked from KIT-CMS/KingMaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunTraining.py
620 lines (583 loc) · 22.3 KB
/
RunTraining.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# coding: utf-8
"""
Task to run NMSSM NN trainings
And Task to run all trainings for each mass,batch,channel combination
"""
import os
import luigi
import law
from shutil import rmtree
from rich.console import Console
from framework import Task, HTCondorWorkflow, PuppetMaster
from ast import literal_eval
from law.target.collection import flatten_collections
import re
from CreateTrainingDatasets import (
CreateTrainingDataShardConfig,
CreateTrainingDataShard,
)
law.contrib.load("tasks") # to have the RunOnceTask class
try:
current_width = os.get_terminal_size().columns
except OSError:
current_width = 140
console = Console(width=current_width)
# Task to create config files for the NN trainings
# One config file is created for each valid combination of:
# era, channel, mass and batch
# For this the shards are combined and reused as necessary
class CreateTrainingConfig(Task):
# Define luigi parameters
era = luigi.Parameter(description="Run era")
channel = luigi.Parameter(description="Decay Channel")
masses = luigi.ListParameter(
description="List of mass hypotheses of heavy NMSSM Higgs boson."
)
batch_nums = luigi.ListParameter(
description="List of groups of mass hypotheses of light NMSSM Higgs boson."
)
processes_and_classes = luigi.ListParameter(
description="List of processes and their classes used in training"
)
all_eras = ["2016", "2017", "2018"]
# Set other variables and templates used by this class.
dir_template = "{era}_{channel}_{mass}_{batch}"
dir_template_in = "{era}_{channel}"
file_template = "training_config.yaml"
files_template_shard = "{process}_datashard_fold{fold}.root"
files_template_shard_config = "{process}_datashard_config.yaml"
# Function to check for invalid mass-batch combinations
def valid_batches(self, mass):
mass = str(mass)
if mass in ["240", "280"]:
max_batch = 3
elif mass in ["320", "360", "400", "450"]:
max_batch = 4
elif mass in ["500", "550", "600"]:
max_batch = 5
elif mass in ["700", "800", "heavier"]:
max_batch = 6
elif mass in ["900", "1000"]:
max_batch = 7
else:
raise Exception("Provided mass {} is not valid.".format(mass))
valid_batches = [batch for batch in self.batch_nums if int(batch) <= max_batch]
return valid_batches
# Set prerequisites of this task:
# All configs for the dataset shards have to be completed
# All dataset shards have to be completed
def requires(self):
if self.era == "all_eras":
use_eras = self.all_eras
else:
use_eras = [self.era]
requirements = {}
for era in use_eras:
requirements_args = {
"era": era,
"channel": self.channel,
"processes_and_classes": self.processes_and_classes,
"identifier": [self.channel],
}
requirements["CreateTrainingDataShard_{}".format(era)] = PuppetMaster(
puppet_task=CreateTrainingDataShard(**requirements_args),
identifier=[era, self.channel],
)
requirements["CreateTrainingDataShardConfig_{}".format(era)] = PuppetMaster(
puppet_task=CreateTrainingDataShardConfig(**requirements_args),
identifier=[era, self.channel],
)
return requirements
# Define output targets. Task is considerd complete if all targets are present.
def output(self):
files = [
"/".join(
[
self.dir_template.format(
era=self.era,
channel=self.channel,
mass=mass,
batch=batch_num,
),
self.file_template,
]
)
for mass in self.masses
for batch_num in self.valid_batches(mass)
]
targets = self.remote_targets(files)
for target in targets:
target.parent.touch()
return targets
def run(self):
if self.era == "all_eras":
use_eras = self.all_eras
else:
use_eras = [self.era]
processes, process_classes = zip(*self.processes_and_classes)
prefix = self.temporary_local_path("")
run_loc = "sm-htt-analysis"
# Create data directory for each valid branch and mass/batch combination
for era in use_eras + [self.era]:
for mass in self.masses:
for batch_num in self.valid_batches(mass):
os.makedirs(
"/".join(
[
prefix,
self.dir_template.format(
era=era,
channel=self.channel,
mass=mass,
batch=batch_num,
),
]
),
exist_ok=True,
)
files = [
"/".join(
[
prefix,
self.dir_template.format(
era=self.era,
channel=self.channel,
mass=mass,
batch=batch_num,
),
self.file_template,
]
)
for mass in self.masses
for batch_num in self.valid_batches(mass)
]
# Get list of all shard targets
allbranch_shards = {
era: flatten_collections(
self.requires()[
"CreateTrainingDataShard_{}".format(era)
].give_puppet_outputs()["collection"]
)
for era in use_eras
}
# Get prefix of remote storage for root shards
remote_shard_base = {
era: self.wlcg_path + os.path.dirname(allbranch_shards[era][0].path)
for era in use_eras
}
# Get shard config targets
branch_shardconfigs = {
era: self.requires()["CreateTrainingDataShardConfig_{}".format(era)]
.give_puppet_outputs()
.targets
for era in use_eras
}
for era in use_eras:
assert len(branch_shardconfigs[era]) == len(
processes
), "There should be {} targets, but there are {}".format(
len(processes),
len(branch_shardconfigs[era]),
)
# Copy shard config files into data directory
console.log("File copy in start.")
for era in use_eras:
for copy_file in branch_shardconfigs[era]:
copy_file_name = os.path.basename(copy_file.path)
copy_file_name_path = os.path.basename(os.path.dirname(copy_file.path))
copy_file.copy_to_local(
"/".join([prefix, copy_file_name_path, copy_file_name])
)
console.log("File copy in end.")
# Write training config file
for era in use_eras:
self.run_command(
[
"python",
"ml_trainings/write_training_config.py",
"--era {}".format(era),
"--channel {}".format(self.channel),
"--masses {}".format(" ".join(self.masses)),
"--batches {}".format(" ".join(self.batch_nums)),
"--config-dir {}/{}_{}".format(prefix, era, self.channel),
"--dataset-dir {}".format(remote_shard_base[era]),
"--processes {}".format(" ".join(processes)),
"--training-template datasets/templates/{}_{}_training.yaml".format(
era, self.channel
),
"--write-weights True",
"--overwrite-configs {}".format(""),
],
run_location=run_loc,
sourcescripts="{}/utils/setup_python.sh".format(run_loc),
)
if self.era == "all_eras":
# Create data directory
for mass in self.masses:
for batch_num in self.valid_batches(mass):
conf_files = "/".join(
[
prefix,
self.dir_template.format(
era="{era}",
channel=self.channel,
mass=mass,
batch=batch_num,
),
]
)
# Combine the configs of each era
self.run_command(
[
"python",
"ml_trainings/create_combined_config.py",
"--input-path {}".format(conf_files),
"--output-dir {}".format(conf_files.format(era="all_eras")),
],
run_location=run_loc,
)
# Copy locally created files to remote storage
console.log("File copy out start.")
for file_remote, file_local in zip(self.output(), files):
file_remote.parent.touch()
file_remote.copy_from_local(file_local)
console.log("File copy out end.")
# Remove data directories
rmtree(prefix)
# Task to run NN training (2 folds)
# One training is performed for each valid combination of:
# channel, mass, batch and fold
# The datashards are combined based on the training parameters
class RunTraining(HTCondorWorkflow, law.LocalWorkflow):
# Define luigi parameters
era = luigi.Parameter(description="Run era")
channels = luigi.ListParameter(description="List of decay Channels")
masses = luigi.ListParameter(
description="List of mass hypotheses of heavy NMSSM Higgs boson."
)
batch_nums = luigi.ListParameter(
description="List of groups of mass hypotheses of light NMSSM Higgs boson."
)
# Set other variables and templates used by this class.
all_eras = ["2016", "2017", "2018"]
dir_template = "{era}_{channel}_{mass}_{batch}"
dir_template_in = "{era}_{channel}"
file_template_shard = "{process}_datashard_fold{fold}.root"
file_template_config = "training_config.yaml"
file_templates = [
"fold{fold}_keras_model.h5",
"fold{fold}_keras_preprocessing.pickle",
"fold{fold}_loss.pdf",
"fold{fold}_loss.png",
]
# Function to check for invalid mass-batch combinations
def valid_batches(self, mass):
mass = str(mass)
if mass in ["240", "280"]:
max_batch = 3
elif mass in ["320", "360", "400", "450"]:
max_batch = 4
elif mass in ["500", "550", "600"]:
max_batch = 5
elif mass in ["700", "800", "heavier"]:
max_batch = 6
elif mass in ["900", "1000"]:
max_batch = 7
else:
raise Exception("Provided mass {} is not valid.".format(mass))
valid_batches = [batch for batch in self.batch_nums if int(batch) <= max_batch]
return valid_batches
# Function to get relevant processes and their classes
def get_process_tuple(self, channel, mass, batch_num):
run_loc = "sm-htt-analysis"
# Get processes and classes as list of tuples
process_tuples = literal_eval(
self.run_command(
[
"python",
"utils/get_processes.py",
"--channel {}".format(channel),
"--mass {}".format(mass),
"--batch {}".format(batch_num),
"--training-z-estimation-method emb",
"--training-jetfakes-estimation-method ff",
],
run_location=run_loc,
sourcescripts="{}/utils/setup_python.sh".format(run_loc),
collect_out=True,
silent=True,
)
)
return process_tuples
# Create map for the branches of this task
def create_branch_map(self):
branches = [
{"channel": channel, "mass": mass, "batch_num": batch_num, "fold": fold}
for channel in self.channels
for mass in self.masses
for batch_num in self.valid_batches(mass)
for fold in ["0", "1"]
]
assert (
branches
), "There are no valid branches for this set of parameters: \
\n{}".format(
self
)
return branches
# Set prerequisites of this task:
# All dataset shards have to be completed
# All training config files have to be completed
# The prerequisites are also dependant on whether all_eras is used
def requires(self):
channel = self.branch_data["channel"]
mass = self.branch_data["mass"]
batch_num = self.branch_data["batch_num"]
requirements = {}
processes_and_classes = self.get_process_tuple(
channel,
mass,
batch_num,
)
requirements_conf = {
"era": self.era,
"channel": channel,
"processes_and_classes": processes_and_classes,
"masses": [mass],
"batch_nums": [batch_num],
"identifier": [channel],
}
requirements["CreateTrainingConfig_{}".format(channel)] = CreateTrainingConfig(
**requirements_conf
)
if self.era == "all_eras":
use_eras = self.all_eras
else:
use_eras = [self.era]
# use_eras is set to be either given era, or a list of all eras
for era in use_eras:
requirements_shard = {
"era": era,
"channel": channel,
"processes_and_classes": processes_and_classes,
"identifier": [era, channel],
}
requirements[
"CreateTrainingDataShard_{}_{}".format(
channel,
era,
)
] = CreateTrainingDataShard(**requirements_shard)
return requirements
def workflow_requires(self):
# Shortcut is necessary for Remote workflows without access to local filesystem
if self.is_branch():
return None
requirements = super(RunTraining, self).workflow_requires()
process_tuple = {
channel: [
self.get_process_tuple(channel, mass, batch_num)
for mass in self.masses
for batch_num in self.valid_batches(mass)
]
for channel in self.channels
}
# Tasks of diffrent required channels are set up as seperate tasks, not branches
# This is, because the process classes are dependant on the decay channel
for channel in self.channels:
# Get list of unique processes and their classes for each decay channel
processes_and_classes = set(
[
element
for element_list in process_tuple[channel]
for element in element_list
]
)
processes_and_classes = [list(elem) for elem in processes_and_classes]
requirements_conf = {
"era": self.era,
"channel": channel,
"processes_and_classes": processes_and_classes,
"masses": self.masses,
"batch_nums": self.batch_nums,
"identifier": [channel],
}
# requires CreateTrainingConfig task if all_eras is not used
requirements["CreateTrainingConfig_{}".format(channel)] = PuppetMaster(
puppet_task=CreateTrainingConfig(**requirements_conf),
identifier=[channel],
)
if self.era == "all_eras":
use_eras = self.all_eras
else:
use_eras = [self.era]
# use_eras is set to be either given era, or a list of all eras
for era in use_eras:
requirements_shard = {
"era": era,
"channel": channel,
"processes_and_classes": processes_and_classes,
"identifier": [era, channel],
}
requirements[
"CreateTrainingDataShard_{}_{}".format(channel, era)
] = PuppetMaster(
puppet_task=CreateTrainingDataShard(**requirements_shard),
identifier=[era, channel],
)
return requirements
# Define output targets. Task is considerd complete if all targets are present.
def output(self):
files = [
"/".join(
[
self.dir_template.format(
era=self.era,
channel=self.branch_data["channel"],
mass=self.branch_data["mass"],
batch=self.branch_data["batch_num"],
),
file_template.format(fold=self.branch_data["fold"]),
]
)
for file_template in self.file_templates
]
targets = self.remote_targets(files)
for target in targets:
target.parent.touch()
return targets
def run(self):
fold = self.branch_data["fold"]
prefix = self.temporary_local_path("")
run_loc = "sm-htt-analysis"
# Create data directory for each branch
local_dir = "/".join(
[
prefix,
self.dir_template.format(
era=self.era,
channel=self.branch_data["channel"],
mass=self.branch_data["mass"],
batch=self.branch_data["batch_num"],
),
]
)
os.makedirs("/".join([local_dir, self.era]), exist_ok=True)
if self.era == "all_eras":
use_eras = self.all_eras
else:
use_eras = [self.era]
files = [
"/".join(
[
local_dir,
self.era,
file_template.format(fold=self.branch_data["fold"]),
]
)
for file_template in self.file_templates
]
# Get training config target
branch_trainingconfigs = self.input()[
"CreateTrainingConfig_{}".format(self.branch_data["channel"])
]
assert (
len(branch_trainingconfigs) == 1
), "There should be 1 target, but there are {}".format(
len(branch_trainingconfigs)
)
trainingconfig = branch_trainingconfigs[0]
# Get prefix of remote storage for root shards
allbranch_shards = {
era: flatten_collections(
self.input()[
"CreateTrainingDataShard_{}_{}".format(
self.branch_data["channel"],
era,
)
]["collection"]
)
for era in use_eras
}
remote_shard_base = [
self.wlcg_path
+ os.path.dirname(os.path.dirname(allbranch_shards[era][0].path))
for era in use_eras
]
assert (
len(set(remote_shard_base)) == 1
), "Basepaths of different eras do not match"
remote_shard_base = remote_shard_base[0]
# Copy config to local
trainingconfig_name = os.path.basename(trainingconfig.path)
console.log("File copy in start.")
trainingconfig.copy_to_local("/".join([local_dir, trainingconfig_name]))
console.log("File copy in end.")
# self.run_command(
# command=["ls", "-R"],
# run_location=local_dir
# )
# Set maximum number of threads (this number is somewhat inaccurate
# as TensorFlow only abides by it for some aspects of the training)
if "OMP_NUM_THREADS" in os.environ:
max_threads = os.getenv("OMP_NUM_THREADS")
else:
max_threads = 12
# Run NN training and save the model,
# the preprocessing object and some images of the trasining process
self.run_command(
command=[
"python",
"ml_trainings/keras_training.py",
"--data-dir {}".format(remote_shard_base),
"--config-dir {}".format(local_dir),
"--era-training {}".format(self.era),
"--channel-training {}".format(self.branch_data["channel"]),
"--fold {}".format(fold),
"--balance-batches 1",
"--max-threads {}".format(max_threads),
],
run_location=run_loc,
)
# self.run_command(
# command=["ls", "-R"],
# run_location=prefix_w
# )
# Copy locally created files to remote storage
console.log("File copy out start.")
for file_remote, file_local in zip(self.output(), files):
file_remote.parent.touch()
file_remote.copy_from_local(file_local)
console.log("File copy out end.")
# Remove data directories
rmtree(prefix)
# Task to run all NN trainings for all decay channels,
# heavy higgs masses and groups of light higgs masses
class RunAllTrainings(Task, law.tasks.RunOnceTask):
# Requires ALL trainings
def requires(self):
requirements = {
"era": "all_eras",
"channels": ["tt", "et", "mt"],
"masses": [
"240",
"280",
"320",
"360",
"400",
"450",
"500",
"550",
"600",
"700",
"800",
"900",
"1000",
"heavier",
],
"batch_nums": ["1", "2", "3", "4", "5", "6", "7"],
}
return PuppetMaster(puppet_task=RunTraining(**requirements))
def run(self):
console.log("All trainings are done!")
self.mark_complete()