-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
55 lines (40 loc) · 1.36 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
import os
import hydra
from hydra.utils import instantiate
from omegaconf import OmegaConf
os.environ["HYDRA_FULL_ERROR"] = "1"
from omegaconf import DictConfig
import torch
import pytorch_lightning as pl
from examples.config import *
from examples.datasets import *
from examples.core import *
from examples.utils.loggers import initialize_loggers
from examples.utils import (
instantiate_model,
instantiate_data_module,
run_explainer,
check_jittable,
)
import logging
log = logging.getLogger(__name__)
def train(cfg):
log.info(OmegaConf.to_yaml(cfg))
data_module: pl.LightningDataModule = instantiate_data_module(cfg)
model: pl.LightningModule = instantiate_model(cfg, data_module)
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)
trainer.fit(model, data_module)
log.info("Training complete.")
if cfg.explainer.activate:
run_explainer(cfg.explainer.params, trainer, model, data_module)
if cfg.jit:
check_jittable(model, data_module)
@hydra.main(config_path="conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
train(cfg)
if __name__ == "__main__":
my_app()