-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
35 lines (27 loc) · 1.26 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
from argparse import ArgumentParser
from pytorch_lightning.strategies import DDPStrategy
import pytorch_lightning as pl
from omegaconf import OmegaConf
import torch
from utils.common import instantiate_from_config, load_state_dict
def main() -> None:
parser = ArgumentParser()
parser.add_argument("--config", type=str, required=True)
args = parser.parse_args()
config = OmegaConf.load(args.config)
pl.seed_everything(config.lightning.seed, workers=True)
data_module = instantiate_from_config(config.data)
model = instantiate_from_config(OmegaConf.load(config.model.config))
# TODO: resume states saved in checkpoint.
if config.model.get("resume"):
load_state_dict(model, torch.load(config.model.resume, map_location="cpu"), strict=False)
callbacks = []
for callback_config in config.lightning.callbacks:
callbacks.append(instantiate_from_config(callback_config))
# trainer = pl.Trainer(callbacks=callbacks, **config.lightning.trainer)
strategy = DDPStrategy(find_unused_parameters=True)
trainer = pl.Trainer(callbacks=callbacks, **config.lightning.trainer, strategy=strategy)
data_module.setup(stage='fit')
trainer.fit(model, datamodule=data_module)
if __name__ == "__main__":
main()