forked from BoyuanChen/neural-state-variables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (114 loc) · 4.5 KB
/
main.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
131
132
133
import os
import sys
import yaml
import torch
import pprint
from munch import munchify
from models import VisDynamicsModel
from models_latentpred import VisLatentDynamicsModel
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.callbacks import ModelCheckpoint
def load_config(filepath):
with open(filepath, 'r') as stream:
try:
trainer_params = yaml.safe_load(stream)
return trainer_params
except yaml.YAMLError as exc:
print(exc)
def seed(cfg):
torch.manual_seed(cfg.seed)
if cfg.if_cuda:
torch.cuda.manual_seed(cfg.seed)
def main():
config_filepath = str(sys.argv[1])
cfg = load_config(filepath=config_filepath)
pprint.pprint(cfg)
cfg = munchify(cfg)
seed(cfg)
seed_everything(cfg.seed)
log_dir = '_'.join([cfg.log_dir,
cfg.dataset,
cfg.model_name,
str(cfg.seed)])
model = VisDynamicsModel(lr=cfg.lr,
seed=cfg.seed,
if_cuda=cfg.if_cuda,
if_test=False,
gamma=cfg.gamma,
log_dir=log_dir,
train_batch=cfg.train_batch,
val_batch=cfg.val_batch,
test_batch=cfg.test_batch,
num_workers=cfg.num_workers,
model_name=cfg.model_name,
data_filepath=cfg.data_filepath,
dataset=cfg.dataset,
lr_schedule=cfg.lr_schedule)
# define callback for selecting checkpoints during training
checkpoint_callback = ModelCheckpoint(
filepath=log_dir + "/lightning_logs/checkpoints/{epoch}_{val_loss}",
verbose=True,
monitor='val_loss',
mode='min',
prefix='')
# define trainer
trainer = Trainer(gpus=cfg.num_gpus,
max_epochs=cfg.epochs,
deterministic=True,
accelerator='ddp',
amp_backend='native',
default_root_dir=log_dir,
val_check_interval=1.0,
checkpoint_callback=checkpoint_callback)
trainer.fit(model)
def main_latentpred():
config_filepath = str(sys.argv[2])
high_dim_checkpoint_filepath = str(sys.argv[3])
refine_checkpoint_filepath = str(sys.argv[4])
cfg = load_config(filepath=config_filepath)
pprint.pprint(cfg)
cfg = munchify(cfg)
seed(cfg)
seed_everything(cfg.seed)
log_dir = '_'.join([cfg.log_dir,
cfg.dataset,
cfg.model_name,
str(cfg.seed)])
model = VisLatentDynamicsModel(lr=cfg.lr,
seed=cfg.seed,
if_cuda=cfg.if_cuda,
if_test=False,
gamma=cfg.gamma,
log_dir=log_dir,
train_batch=cfg.train_batch,
val_batch=cfg.val_batch,
test_batch=cfg.test_batch,
num_workers=cfg.num_workers,
model_name=cfg.model_name,
data_filepath=cfg.data_filepath,
dataset=cfg.dataset,
lr_schedule=cfg.lr_schedule)
model.load_high_dim_refine_model(high_dim_checkpoint_filepath, refine_checkpoint_filepath)
# define callback for selecting checkpoints during training
checkpoint_callback = ModelCheckpoint(
filepath=log_dir + "/lightning_logs/checkpoints/{epoch}_{val_loss}",
verbose=True,
monitor='val_loss',
mode='min',
prefix='')
# define trainer
trainer = Trainer(gpus=cfg.num_gpus,
max_epochs=cfg.epochs,
deterministic=True,
accelerator='ddp',
amp_backend='native',
default_root_dir=log_dir,
val_check_interval=1.0,
checkpoint_callback=checkpoint_callback)
trainer.fit(model)
if __name__ == '__main__':
if sys.argv[1] == 'latentpred':
main_latentpred()
else:
main()