-
Notifications
You must be signed in to change notification settings - Fork 6
/
train.py
94 lines (80 loc) · 3.04 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
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
import copy
import shutil
from infiniate_nature_data.utils.utils import *
import os
from pytorch_lightning.loggers import WandbLogger
import signal
import datetime
import sys
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--config_path', help="config path",
default="configs/google_earth.yaml")
parser.add_argument('--experiment_name_suffix', help="",
default="InfiniteNature-CLEVR-NeurIPS2022")
args = parser.parse_args()
now = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
sys.path.append(os.getcwd())
config = OmegaConf.load(args.config_path)
# add callback which sets up log directory
default_callbacks_cfg = {
"image_logger": {
"target": "infiniate_nature_data.utils.utils.ImageLogger",
"params": {
"batch_frequency": 500,
"max_images": 4,
"clamp": True
}
},
}
callbacks_cfg = OmegaConf.create()
callbacks_cfg = OmegaConf.merge(default_callbacks_cfg, callbacks_cfg)
# prepare log name
log_name = args.experiment_name_suffix
if config.log_keywords is not None:
for keyword in config.log_keywords.split(','):
keyword = keyword.strip()
value = None
curr_config = copy.deepcopy(config)
for k in keyword.split('.'):
curr_config = curr_config[k]
value = curr_config
log_name += f"_{k}_{value}"
log_name += f"_{str(now)}"
logdir = os.path.join("logs", log_name)
os.makedirs(logdir, exist_ok=True)
cfgdir = os.path.join(logdir, "configs")
os.makedirs(cfgdir, exist_ok=True)
shutil.copy(args.config_path, str(cfgdir) + "/config.yaml")
data = instantiate_from_config(config.data)
# config.model.params.data_config = config.infiniate_nature_data.params
model = instantiate_from_config(config.model)
model.logdir = logdir
wandb_logger = WandbLogger(
entity="generating_sfm",
project='InfiniteNature',
save_dir=logdir,
name=logdir.split('/')[-1],
)
gpu_ids = []
for gpu_id in config.model.gpu_ids.split(','):
if gpu_id != '':
gpu_ids.append(int(gpu_id))
trainer_kwargs = dict()
trainer_opt = argparse.Namespace(**{
"gpus": 0 if -1 in gpu_ids else len(gpu_ids),
"strategy": "ddp",
"logger": wandb_logger,
"devices": gpu_ids,
"accelerator": 'gpu' if len(gpu_ids) > 0 else 'cpu'
})
trainer_kwargs["callbacks"] = [instantiate_from_config(callbacks_cfg[k]) for k in callbacks_cfg]
trainer_kwargs["callbacks"].append(CheckpointEveryNSteps(10000, os.path.join(logdir, "checkpoints", "last.ckpt")))
trainer = Trainer.from_argparse_args(trainer_opt, **trainer_kwargs)
trainer.num_sanity_val_steps = 2
def divein(*args, **kwargs):
if trainer.global_rank == 0:
import pudb
pudb.set_trace()
signal.signal(signal.SIGUSR2, divein)
trainer.fit(model, data)