-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
49 lines (33 loc) · 1.15 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 logging
from omegaconf import OmegaConf, DictConfig
import hydra
from hydra.utils import instantiate
os.environ["HYDRA_FULL_ERROR"] = "1"
import torch
import pytorch_lightning as pl
from src.config import *
from src.datasets import *
from src.core import *
from src import initialize_task
from src.utils.loggers import initialize_loggers
from src.utils.jit import check_jittable
log = logging.getLogger(__name__)
def train(cfg):
log.info(OmegaConf.to_yaml(OmegaConf.to_container(cfg, resolve=True)))
OmegaConf.set_struct(cfg, False)
model, data_module = initialize_task(cfg)
loggers: List[pl.callbacks.Callback] = initialize_loggers(
cfg
) # , **model.config, **data_module.config
gpus = list(range(torch.cuda.device_count())) if torch.cuda.is_available() else None
trainer: pl.Trainer = instantiate(cfg.trainer, gpus=gpus, logger=loggers)
if cfg.jit:
check_jittable(model, data_module)
trainer.fit(model, data_module)
log.info("Training complete.")
@hydra.main(config_path="conf", config_name="config")
def main(cfg: DictConfig) -> None:
train(cfg)
if __name__ == "__main__":
main()