-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (47 loc) · 1.63 KB
/
Copy pathmain.py
File metadata and controls
59 lines (47 loc) · 1.63 KB
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
import logging
import hydra
import torch
import random
import numpy as np
from omegaconf import DictConfig, OmegaConf
from hydra.utils import instantiate
from lightning.pytorch import Trainer
from lightning.pytorch.callbacks import ModelCheckpoint, EarlyStopping, LearningRateMonitor
logger = logging.getLogger(__name__)
@hydra.main(version_base='1.3.2', config_path='conf', config_name='config')
def main(cfg: DictConfig) -> float:
logger.info(f"Training with the following config:\n{OmegaConf.to_yaml(cfg)}")
torch.manual_seed(cfg.seed)
random.seed(cfg.seed)
# Instantiation
data = instantiate(cfg.data)
model = instantiate(cfg.model)
# Wandb logger
if cfg.wandb_use:
wandb_logger = instantiate(cfg.wandb)
else:
wandb_logger = False
# Training
checkpoint_callback = ModelCheckpoint(
monitor='val_loss',
dirpath=cfg.model_dir,
filename=cfg.model_save_name + '-{epoch:02d}-{val_loss:.2f}',
mode ='min'
)
lr_monitor = LearningRateMonitor(logging_interval='step')
if wandb_logger:
callbacks = [checkpoint_callback, lr_monitor]
else:
callbacks = [checkpoint_callback]
trainer = Trainer(**cfg.pl_trainer,
logger=wandb_logger,
devices="auto",
callbacks=callbacks)
if not cfg.inference_only:
trainer.fit(model, data, ckpt_path=cfg.checkpoint)
# Testing
checkpoint_path = 'best' if cfg.checkpoint is None else cfg.checkpoint
trainer.test(model, ckpt_path=checkpoint_path, dataloaders=data)
return 0
if __name__ == '__main__':
main()