-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
214 lines (188 loc) · 7.28 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# This code is based on https://github.com/ChenFengYe/motion-latent-diffusion under the MIT license.
import os
from pprint import pformat
import pytorch_lightning as pl
import torch
from omegaconf import OmegaConf
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks import ModelCheckpoint
# from pytorch_lightning.strategies.ddp import DDPStrategy
from mola.callback import ProgressLogger
from mola.config import parse_args
from mola.data.get_data import get_datasets
from mola.models.get_model import get_model
from mola.utils.logger import create_logger
def main():
# parse options
cfg = parse_args() # parse config file
# create logger
logger = create_logger(cfg, phase="train")
# resume
if cfg.TRAIN.RESUME:
resume = cfg.TRAIN.RESUME
backcfg = cfg.TRAIN.copy()
if os.path.exists(resume):
file_list = sorted(os.listdir(resume), reverse=True)
for item in file_list:
if item.endswith(".yaml"):
cfg = OmegaConf.load(os.path.join(resume, item))
cfg.TRAIN = backcfg
break
checkpoints = sorted(os.listdir(os.path.join(
resume, "checkpoints")),
key=lambda x: int(x[6:-5]),
reverse=True)
for checkpoint in checkpoints:
if "epoch=" in checkpoint:
cfg.TRAIN.PRETRAINED = os.path.join(
resume, "checkpoints", checkpoint)
break
if os.path.exists(os.path.join(resume, "wandb")):
wandb_list = sorted(os.listdir(os.path.join(resume, "wandb")),
reverse=True)
for item in wandb_list:
if "run-" in item:
cfg.LOGGER.WANDB.RESUME_ID = item.split("-")[-1]
else:
raise ValueError("Resume path is not right.")
# set seed
pl.seed_everything(cfg.SEED_VALUE)
# gpu setting
if cfg.ACCELERATOR == "gpu":
os.environ["PYTHONWARNINGS"] = "ignore"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# tensorboard logger and wandb logger
loggers = []
if cfg.LOGGER.WANDB.PROJECT:
wandb_logger = pl_loggers.WandbLogger(
project=cfg.LOGGER.WANDB.PROJECT,
offline=cfg.LOGGER.WANDB.OFFLINE,
id=cfg.LOGGER.WANDB.RESUME_ID,
save_dir=cfg.FOLDER_EXP,
version="",
name=cfg.NAME,
anonymous=False,
log_model=False,
)
loggers.append(wandb_logger)
if cfg.LOGGER.TENSORBOARD:
tb_logger = pl_loggers.TensorBoardLogger(save_dir=cfg.FOLDER_EXP,
sub_dir="tensorboard",
version="",
name="")
loggers.append(tb_logger)
logger.info(OmegaConf.to_yaml(cfg))
# create dataset
datasets = get_datasets(cfg, logger=logger)
logger.info("datasets module {} initialized".format("".join(
cfg.TRAIN.DATASETS)))
# create model
model = get_model(cfg, datasets[0])
logger.info("model {} loaded".format(cfg.model.model_type))
# optimizer
metric_monitor = {
"Train_jf": "recons/text2jfeats/train",
"Val_jf": "recons/text2jfeats/val",
"Train_rf": "recons/text2rfeats/train",
"Val_rf": "recons/text2rfeats/val",
"APE root": "Metrics/APE_root",
"APE mean pose": "Metrics/APE_mean_pose",
"AVE root": "Metrics/AVE_root",
"AVE mean pose": "Metrics/AVE_mean_pose",
"R_TOP_1": "Metrics/R_precision_top_1",
"R_TOP_2": "Metrics/R_precision_top_2",
"R_TOP_3": "Metrics/R_precision_top_3",
"gt_R_TOP_1": "Metrics/gt_R_precision_top_1",
"gt_R_TOP_2": "Metrics/gt_R_precision_top_2",
"gt_R_TOP_3": "Metrics/gt_R_precision_top_3",
"FID": "Metrics/FID",
"gt_FID": "Metrics/gt_FID",
"Diversity": "Metrics/Diversity",
"gt_Diversity": "Metrics/gt_Diversity",
"MM dist": "Metrics/Matching_score",
"Accuracy": "Metrics/accuracy",
"gt_Accuracy": "Metrics/gt_accuracy",
}
# callbacks
callbacks = [
pl.callbacks.RichProgressBar(),
ProgressLogger(metric_monitor=metric_monitor),
ModelCheckpoint(
dirpath=os.path.join(cfg.FOLDER_EXP, "checkpoints"),
filename="{epoch}",
monitor="step",
mode="max",
every_n_epochs=cfg.LOGGER.SACE_CHECKPOINT_EPOCH,
save_top_k=-1,
save_last=False,
save_on_train_epoch_end=True,
),
]
logger.info("Callbacks initialized")
if len(cfg.DEVICE) > 1:
ddp_strategy = "ddp"
else:
ddp_strategy = None
# trainer
trainer = pl.Trainer(
benchmark=False,
max_epochs=cfg.TRAIN.END_EPOCH,
accelerator=cfg.ACCELERATOR,
devices=cfg.DEVICE,
strategy=ddp_strategy,
# move_metrics_to_cpu=True,
default_root_dir=cfg.FOLDER_EXP,
log_every_n_steps=cfg.LOGGER.VAL_EVERY_STEPS,
deterministic=False,
detect_anomaly=False,
enable_progress_bar=True,
logger=loggers,
callbacks=callbacks,
check_val_every_n_epoch=cfg.LOGGER.VAL_EVERY_STEPS,
)
logger.info("Trainer initialized")
vae_type = cfg.model.motion_vae.target.split(".")[-1].lower().replace(
"vae", "")
# strict load vae model
if cfg.TRAIN.PRETRAINED_VAE:
logger.info("Loading pretrain vae from {}".format(
cfg.TRAIN.PRETRAINED_VAE))
state_dict = torch.load(cfg.TRAIN.PRETRAINED_VAE,
map_location="cpu")["state_dict"]
# extract encoder/decoder
from collections import OrderedDict
vae_dict = OrderedDict()
for k, v in state_dict.items():
if k.split(".")[0] == "vae_model":
name = k.replace("vae_model.", "")
vae_dict[name] = v
model.vae_model.load_state_dict(vae_dict, strict=True)
if cfg.TRAIN.PRETRAINED:
logger.info("Loading pretrain mode from {}".format(
cfg.TRAIN.PRETRAINED))
logger.info("Attention! VAE will be recovered")
state_dict = torch.load(cfg.TRAIN.PRETRAINED,
map_location="cpu")["state_dict"]
# remove mismatched and unused params
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
if k not in ["denoiser.sequence_pos_encoding.pe"]:
new_state_dict[k] = v
model.load_state_dict(new_state_dict, strict=False)
# fitting
if cfg.TRAIN.RESUME:
trainer.fit(model,
datamodule=datasets[0],
ckpt_path=cfg.TRAIN.PRETRAINED)
else:
trainer.fit(model, datamodule=datasets[0])
# checkpoint
checkpoint_folder = trainer.checkpoint_callback.dirpath
logger.info(f"The checkpoints are stored in {checkpoint_folder}")
logger.info(
f"The outputs of this experiment are stored in {cfg.FOLDER_EXP}")
# end
logger.info("Training ends!")
if __name__ == "__main__":
main()