-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
67 lines (48 loc) · 2.1 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
import hydra
from hydra.utils import instantiate
from omegaconf import DictConfig
from pytorch_lightning.loggers import WandbLogger
from framework.callbacks import EvaluationCallback
from framework.utils.wandb_utils import add_config
from pytorch_lightning import seed_everything
@hydra.main(config_path='config', config_name='config.yaml')
def parse(conf: DictConfig):
if 'seed' in conf:
seed_everything(conf.seed, workers=True)
# Instantiate the Pytorch Lightning Trainer
trainer = instantiate(conf.trainer)
# On the first thread
if trainer.global_rank == 0:
# If using the Weights and Bias Logger
if isinstance(trainer.logger, WandbLogger):
# Add the hydra configuration to the experiment using the Wandb API
add_config(trainer.logger.experiment, conf)
# Add all the callbacks to the trainer (for logging, checkpointing, ...)
if not trainer.fast_dev_run:
# Evaluation Callbacks
if 'evaluation' in conf:
for name, eval_params in conf.evaluation.items():
# Instantiate the evaluator
evaluator = instantiate(eval_params['evaluator'])
# Create a corresponding evaluation callback
callback = EvaluationCallback(
name=name,
evaluator=evaluator,
evaluate_every=eval_params['evaluate_every']
)
# And add it to the trainer
trainer.callbacks.append(
callback
)
# Add the rest of the callbacks
for callback_conf in conf.callbacks:
callback = instantiate(callback_conf)
trainer.callbacks.append(callback)
# Instantiate the optimization procedure, which is a Pytorch Lightning module
optimization = instantiate(conf.optimization)
# print the model structure
print(optimization)
# Call the Pytorch Lightning training loop
trainer.fit(optimization)
if __name__ == '__main__':
parse()