-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_run.py
130 lines (109 loc) · 4 KB
/
main_run.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import logging
import os
from hashlib import md5
from uuid import uuid4
import hydra
from dotenv import load_dotenv
from omegaconf import DictConfig, OmegaConf
from trainer.trainer import ModelingGrounded3DLLM
from pytorch_lightning.callbacks import ModelCheckpoint
from utils.utils import (
flatten_dict,
load_checkpoint_with_missing_or_exsessive_keys,
)
import pytorch_lightning as pl
from pytorch_lightning import Trainer, seed_everything
import MinkowskiEngine as ME
class RegularCheckpointing(pl.Callback):
def on_train_epoch_end(
self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
):
general = pl_module.config.general
trainer.save_checkpoint(f"{general.save_dir}/last-epoch.ckpt")
if trainer.global_rank == 0:
print("Checkpoint created")
def get_parameters(cfg: DictConfig):
logger = logging.getLogger(__name__)
load_dotenv(".env")
# parsing input parameters
seed_everything(cfg.general.seed)
# getting basic configuration
if cfg.general.get("gpus", None) is None:
cfg.general.gpus = os.environ.get("CUDA_VISIBLE_DEVICES", None)
loggers = []
cfg.general.experiment_id = "0" # str(Repo("./").commit())[:8]
params = flatten_dict(OmegaConf.to_container(cfg, resolve=True))
# create unique id for experiments that are run locally
unique_id = "_" + str(uuid4())[:4]
cfg.general.version = md5(str(params).encode("utf-8")).hexdigest()[:8] + unique_id
if not os.path.exists(cfg.general.save_dir):
os.makedirs(cfg.general.save_dir)
else:
if os.path.exists(f"{cfg.general.save_dir}/last-epoch.ckpt"):
cfg["trainer"][
"resume_from_checkpoint"
] = f"{cfg.general.save_dir}/last-epoch.ckpt"
# if cfg.general.train_mode is False:
print(f'Load weights from: {f"{cfg.general.save_dir}/last-epoch.ckpt"}')
cfg.general.checkpoint = f"{cfg.general.save_dir}/last-epoch.ckpt"
else:
print(f'Note that *No* checkpoint is found.')
for log in cfg.logging:
print(log)
loggers.append(hydra.utils.instantiate(log))
loggers[-1].log_hyperparams(
flatten_dict(OmegaConf.to_container(cfg, resolve=True))
)
model = ModelingGrounded3DLLM(cfg)
if cfg.general.gpus > 1:
model = ME.MinkowskiSyncBatchNorm.convert_sync_batchnorm(model)
if cfg.general.checkpoint is not None:
cfg, model = load_checkpoint_with_missing_or_exsessive_keys(cfg, model)
logger.info(flatten_dict(OmegaConf.to_container(cfg, resolve=True)))
return cfg, model, loggers
@hydra.main(
config_path="conf", config_name="config_base.yaml"
)
def train(cfg: DictConfig):
os.chdir(hydra.utils.get_original_cwd())
cfg, model, loggers = get_parameters(cfg)
callbacks = []
for cb in cfg.callbacks:
callbacks.append(hydra.utils.instantiate(cb))
callbacks.append(RegularCheckpointing())
runner = Trainer(
logger=loggers,
gpus=cfg.general.gpus,
accelerator='gpu' if cfg.general.gpus > 1 else None,
strategy="ddp" if cfg.general.gpus > 1 else None,
callbacks=callbacks,
weights_save_path=str(cfg.general.save_dir),
**cfg.trainer,
)
runner.fit(model)
@hydra.main(
config_path="conf", config_name="config_base.yaml"
)
def test(cfg: DictConfig):
# because hydra wants to change dir for some reason
os.chdir(hydra.utils.get_original_cwd())
cfg, model, loggers = get_parameters(cfg)
runner = Trainer(
gpus=cfg.general.gpus,
logger=loggers,
accelerator='gpu' if cfg.general.gpus > 1 else None,
strategy="ddp" if cfg.general.gpus > 1 else None,
weights_save_path=str(cfg.general.save_dir),
**cfg.trainer,
)
runner.test(model)
@hydra.main(
config_path="conf", config_name="config_base.yaml"
)
def main(cfg: DictConfig):
if cfg["general"]["train_mode"]:
train(cfg)
else:
test(cfg)
if __name__ == "__main__":
main()