-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
executable file
·147 lines (121 loc) · 3.59 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
#!/usr/bin/env python
import faulthandler
import logging
import warnings
import hydra
import torch
import wandb
from hydra.utils import instantiate
from omegaconf import DictConfig, OmegaConf
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import (
EarlyStopping,
TQDMProgressBar,
)
from pytorch_lightning.loggers import WandbLogger
from add_thin.config import (
instantiate_datamodule,
instantiate_model,
instantiate_task,
)
from add_thin.utils import (
WandbModelCheckpoint,
WandbSummaries,
filter_device_available,
get_logger,
log_hyperparameters,
print_config,
print_exceptions,
set_seed,
)
def get_callbacks(config):
monitor = {"monitor": config.task.metric, "mode": "min"}
callbacks = [
WandbSummaries(**monitor),
WandbModelCheckpoint(
save_last=True,
save_top_k=1,
every_n_epochs=1,
filename="best",
**monitor,
),
TQDMProgressBar(refresh_rate=1),
]
if config.early_stopping is not None:
stopper = EarlyStopping(
patience=int(config.early_stopping),
min_delta=0,
strict=False,
check_on_train_epoch_end=False,
**monitor,
)
callbacks.append(stopper)
return callbacks
# Log to traceback to stderr on segfault
faulthandler.enable(all_threads=False)
# Stop lightning from pestering us about things we already know
warnings.filterwarnings(
"ignore",
"There is a wandb run already in progress",
module="pytorch_lightning.loggers.wandb",
)
warnings.filterwarnings(
"ignore",
"The dataloader, [^,]+, does not have many workers",
module="pytorch_lightning",
)
logging.getLogger("pytorch_lightning.utilities.rank_zero").addFilter(
filter_device_available
)
log = get_logger()
@hydra.main(config_path="config", config_name="train", version_base=None)
@print_exceptions
def main(config: DictConfig):
rng = set_seed(config)
torch.use_deterministic_algorithms(True)
# Resolve interpolations to work around a bug:
# https://github.com/omry/omegaconf/issues/862
OmegaConf.resolve(config)
print_config(config)
wandb.init(
entity=config.entity,
project=config.project,
group=config.group,
name=config.name,
resume="allow",
id=config.id,
mode=config.mode,
dir=config.run_dir,
)
OmegaConf.save(config, wandb.run.dir + "/config_hydra.yaml")
log.info(wandb.run.dir)
log.info("Loading data")
datamodule = instantiate_datamodule(config.data, config.task.name)
datamodule.prepare_data()
log.info(config.data.name)
log.info("Instantiating model")
model = instantiate_model(config.model, datamodule)
task = instantiate_task(config.task, model)
logger = WandbLogger()
log_hyperparameters(logger, config, model)
log.info("Loading checkpoint")
callbacks = get_callbacks(config)
log.info("Instantiating trainer")
trainer: Trainer = instantiate(
config.trainer,
callbacks=callbacks,
logger=logger,
)
log.info("Starting training!")
trainer.fit(task, datamodule=datamodule)
if config.eval_testset:
log.info("Starting testing!")
trainer.test(ckpt_path="best", datamodule=datamodule)
wandb.finish()
log.info(
f"Best checkpoint path:\n{trainer.checkpoint_callback.best_model_path}"
)
best_score = trainer.checkpoint_callback.best_model_score
return float(best_score) if best_score is not None else None
if __name__ == "__main__":
main()