-
Notifications
You must be signed in to change notification settings - Fork 19
/
train.py
57 lines (45 loc) · 1.53 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
import logging
import hydra
from omegaconf import DictConfig
from hydra.utils import instantiate
from src.config import read_config, save_config
logger = logging.getLogger(__name__)
@hydra.main(config_path="configs", config_name="train", version_base="1.3")
def train(cfg: DictConfig):
# Resuming if needed
ckpt = None
if cfg.resume_dir is not None:
assert cfg.ckpt is not None
ckpt = cfg.ckpt
cfg = read_config(cfg.resume_dir)
logger.info("Resuming training")
logger.info(f"The config is loaded from: \n{cfg.resume_dir}")
else:
config_path = save_config(cfg)
logger.info("Training script")
logger.info(f"The config can be found here: \n{config_path}")
import src.prepare # noqa
import pytorch_lightning as pl
pl.seed_everything(cfg.seed)
logger.info("Loading the dataloaders")
train_dataset = instantiate(cfg.data, split="train")
val_dataset = instantiate(cfg.data, split="val")
train_dataloader = instantiate(
cfg.dataloader,
dataset=train_dataset,
collate_fn=train_dataset.collate_fn,
shuffle=True,
)
val_dataloader = instantiate(
cfg.dataloader,
dataset=val_dataset,
collate_fn=val_dataset.collate_fn,
shuffle=False,
)
logger.info("Loading the model")
model = instantiate(cfg.model)
logger.info("Training")
trainer = instantiate(cfg.trainer)
trainer.fit(model, train_dataloader, val_dataloader, ckpt_path=ckpt)
if __name__ == "__main__":
train()