-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
64 lines (53 loc) · 1.85 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
58
59
60
61
62
63
64
import argparse
import os
import lightning # noqa: F401
import mlflow # noqa: F401
import torch
from hydra.utils import instantiate
from lightning.pytorch.callbacks import LearningRateMonitor
from lightning.pytorch.loggers import MLFlowLogger, TensorBoardLogger
from omegaconf import OmegaConf
import src # noqa: F401
lightning.pytorch.seed_everything(0)
torch.set_float32_matmul_precision("medium")
def main(args):
config = OmegaConf.load(args.config)
module = instantiate(config.module)
if args.root is None:
datamodule = instantiate(config.datamodule)
else:
datamodule = instantiate(config.datamodule, root=args.root)
if args.logger == "mlflow":
logger = MLFlowLogger(
experiment_name=os.environ.get(
"MLFLOW_EXPERIMENT_NAME", config.experiment_name
),
run_id=os.environ.get("MLFLOW_RUN_ID", None),
)
logger.log_hyperparams(dict(config))
else:
logger = TensorBoardLogger(
save_dir="lightning_logs", name=config.experiment_name
)
callbacks = [LearningRateMonitor(logging_interval="step")]
devices = [args.device] if args.device is not None else config.trainer.devices
trainer = instantiate(
config.trainer, logger=logger, callbacks=callbacks, devices=devices
)
trainer.fit(module, datamodule)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, required=True)
parser.add_argument(
"--root",
type=str,
default=None,
required=False,
help="Optionally override root from config",
)
parser.add_argument(
"--logger", type=str, choices=["tensorboard", "mlflow"], default="mlflow"
)
parser.add_argument("--device", type=int, default=None)
args = parser.parse_args()
main(args)