-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtrain.py
49 lines (36 loc) · 1.28 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
import os
import hydra
from omegaconf import DictConfig
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning import Trainer
from model.lightning import LightningDLReg
from utils.misc import MyModelCheckpoint
import random
random.seed(7)
@hydra.main(config_path="conf", config_name="config")
def main(cfg: DictConfig) -> None:
# set via CLI hydra.run.dir
model_dir = os.getcwd()
# use only one GPU
gpus = None if cfg.gpu is None else 1
if isinstance(cfg.gpu, int):
os.environ['CUDA_VISIBLE_DEVICES'] = str(cfg.gpu)
# lightning model
model = LightningDLReg(hparams=cfg)
# configure logger
logger = TensorBoardLogger(model_dir, name='log')
# model checkpoint callback with ckpt metric logging
ckpt_callback = MyModelCheckpoint(save_last=True,
dirpath=f'{model_dir}/checkpoints/',
verbose=True
)
trainer = Trainer(default_root_dir=model_dir,
logger=logger,
callbacks=[ckpt_callback],
gpus=gpus,
**cfg.training.trainer
)
# run training
trainer.fit(model)
if __name__ == "__main__":
main()