-
Notifications
You must be signed in to change notification settings - Fork 4
/
mld_temos.py
283 lines (219 loc) · 10.9 KB
/
mld_temos.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import logging
import hydra
import os
from pathlib import Path
from omegaconf import DictConfig, OmegaConf
from sinc.data.tools.collate import collate_length_and_text
import sinc.launch.prepare
# from sinc.render.mesh_viz import visualize_meshes
# from sinc.render.video import save_video_samples, stack_vids
import torch
from sinc.transforms.base import Datastruct
from sinc.utils.inference import cfg_mean_nsamples_resolution, get_path
from sinc.utils.file_io import read_json, write_json
labels = read_json('deps/inference/labels.json')
logger = logging.getLogger(__name__)
@hydra.main(config_path="configs", config_name="sample_eval_latent")
def _calc_temos_score(cfg: DictConfig):
return calc_temos_score(cfg)
def fix_config_if_needed(cfg):
if 'gpt_path' not in cfg.data:
cfg.data['gpt_path'] = '${path.deps}/gpt/gpt3-labels.json'
def load_temos(classifier_path, ckpt_name="last"):
from hydra.utils import instantiate
temos_path = Path(classifier_path)
temoscfg = OmegaConf.load(temos_path / ".hydra/config.yaml")
# Overload it
logger.info("Loading Evaluation Classifier model")
# Instantiate all modules specified in the configs
temos_model = instantiate(temoscfg.model,
nfeats=135,
logger_name="none",
nvids_to_save=None,
_recursive_=False)
if ckpt_name == 'last':
last_ckpt_path = temos_path / "checkpoints/last.ckpt"
else:
last_ckpt_path = temos_path / f"checkpoints/latest-epoch={ckpt_name}.ckpt"
from collections import OrderedDict
state_dict = torch.load('/is/cluster/fast/nathanasiou/logs/sinc/sinc-arxiv/temos-bs64x1-scheduler/babel-amass/checkpoints/latest-epoch=599.ckpt', map_location='cpu')['state_dict']
model_dict = OrderedDict()
for k,v in state_dict.items():
if k.split('.')[0] not in ['eval_model', 'metrics']:
model_dict[k] = v
temos_model.load_state_dict(model_dict, strict=True)
# Load the last checkpoint
# temos_model = temos_model.load_from_checkpoint(last_ckpt_path)
temos_model.eval()
return temos_model, temoscfg
def get_metric_paths(sample_path: Path, set: str, split: str, onesample: bool, mean: bool, fact: float):
extra_str = ("_mean" if mean else "") if onesample else "_multi"
fact_str = "" if fact == 1 else f"{fact}_"
if set == 'pairs':
metric_str = "babel_metrics_sp"
else:
metric_str = "babel_metrics_all"
if onesample:
file_path = f"{fact_str}{metric_str}_{split}{extra_str}"
save_path = sample_path / file_path
return save_path
else:
file_path = f"{fact_str}{metric_str}_{split}_multi"
avg_path = sample_path / (file_path + "_avg")
best_path = sample_path / (file_path + "_best")
return avg_path, best_path
def calc_temos_score(newcfg: DictConfig) -> None:
# Load last config
output_dir = Path(hydra.utils.to_absolute_path(newcfg.folder))
last_ckpt_path = newcfg.last_ckpt_path
# Load previous config
prevcfg = OmegaConf.load(output_dir / ".hydra/config.yaml")
fix_config_if_needed(prevcfg)
# Overload it
cfg = OmegaConf.merge(prevcfg, newcfg)
onesample = cfg_mean_nsamples_resolution(cfg)
if cfg.mean and cfg.number_of_samples > 1:
logger.error("All the samples will be the mean.. cfg.number_of_samples=1 will be forced.")
cfg.number_of_samples = 1
logger.info("Sample script. The outputs will be stored in:")
path = output_dir / 'metrics'
path.mkdir(exist_ok=True, parents=True)
file_path = f"TemosScore_{cfg.set}_{cfg.naive}_{cfg.ckpt_name}"
metrics_path = path / file_path
import pytorch_lightning as pl
import numpy as np
from hydra.utils import instantiate
seed_logger = logging.getLogger("pytorch_lightning.utilities.seed")
seed_logger.setLevel(logging.WARNING)
pl.seed_everything(cfg.seed)
logger.info("Loading data module")
# only pair evaluation to be fair
# keep same order
cfg.data.dtype = 'spatial_pairs+seg+seq'
data_module = instantiate(cfg.data)
logger.info(f"Data module '{cfg.data.dataname}' loaded")
dataset = getattr(data_module, f"{cfg.split}_dataset")
from tqdm import tqdm
logger.info("Loading model")
# Instantiate all modules specified in the configs
from mld_specifics import parse_args
cfg_for_mld = parse_args() # parse config file
# MLD specific changes
from sinc.model.mld import MLD
model = MLD(cfg_for_mld, cfg.transforms, cfg.path)
state_dict = torch.load('/is/cluster/fast/nathanasiou/logs/sinc/sinc-arxiv/temos-bs64x1-scheduler/babel-amass/checkpoints/latest-epoch=599.ckpt', map_location='cpu')
# extract encoder/decoder
from collections import OrderedDict
decoder_dict = OrderedDict()
encoder_dict = OrderedDict()
for k, v in state_dict['state_dict'].items():
if k.split(".")[0] == "motionencoder":
name = k.replace("motionencoder.", "")
encoder_dict[name] = v
if k.split(".")[0] == "motiondecoder":
name = k.replace("motiondecoder.", "")
decoder_dict[name] = v
model.vae_encoder.load_state_dict(encoder_dict, strict=True)
model.vae_decoder.load_state_dict(decoder_dict, strict=True)
logger.info(f"Model '{cfg.model.modelname}' loaded")
# Load the last checkpoint
model = model.load_from_checkpoint(last_ckpt_path)
model.eval()
logger.info("Model weights restored")
model.sample_mean = cfg.mean
model.fact = cfg.fact
# trainer = pl.Trainer(**OmegaConf.to_container(cfg.trainer, resolve=True))
logger.info("Trainer initialized")
model.transforms.rots2joints.jointstype = cfg.jointstype
# ds = model.transforms.Datastruct
if cfg.set == 'submission':
from sinc.utils.inference import sinc_eval_set
keyids = sinc_eval_set
elif cfg.set == 'small':
from sinc.utils.inference import validation_nostand_notrain
keyids = validation_nostand_notrain
elif cfg.set == 'supmat':
from sinc.utils.inference import sinc_supmat
keyids = sinc_supmat
else:
if cfg.set == 'pairs':
keyids = [k for k in dataset.keyids if k.split('-')[0] == 'spatial_pairs']
elif cfg.set == 'single':
keyids = [k for k in dataset.keyids if k.split('-')[0] in ['seq', 'seg']]
else:
keyids = dataset.keyids
motion_type = "rotsd"
# full_dict = {}
temos_model, _ = load_temos(cfg.classifier_path, cfg.classifier_ckpt)
metrics = []
with torch.no_grad():
with tqdm(total=len(keyids), position=0, leave=True) as pbar:
for keyid in (pbar := tqdm(keyids, position=0, leave=True)):
pbar.set_description(f"Processing {keyid}")
one_data = dataset.load_keyid(keyid, mode='inference')
from sinc.data.tools import collate_text_and_length
batch = collate_text_and_length([one_data])
cur_lens = batch['length']
cur_texts = [list(batch['text'][0])]
# batch_size = 1 for reproductability
# fix the seed
pl.seed_everything(0)
try:
if model.hparams.gpt_proxy:
gpt_parts = batch['bp-gpt']
else:
gpt_parts = None
except AttributeError:
gpt_parts = None
dtype_sample = keyid.split('-')[0]
is_sp = dtype_sample == 'spatial_pairs'
if is_sp and cfg.naive == "gpt":
from sinc.tools.frank import combine_motions
gpt_parts = batch['bp-gpt'][0]
motion1 = model.text_to_motion_forward([[cur_texts[0][0]]],
cur_lens,
gpt_parts=None,
return_motion="rotsd")
motion2 = model.text_to_motion_forward([[cur_texts[0][1]]],
cur_lens,
gpt_parts=None,
return_motion="rotsd")
# rots and transl
frank_motion = combine_motions(motion1, motion2, gpt_parts[0], gpt_parts[1], squeeze=True)
frank_datastruct = model.Datastruct(rots_=frank_motion)
motion = model.motion_from_datastruct(frank_datastruct, return_type=motion_type)
# just in case
motion1 = model.motion_from_datastruct(model.Datastruct(rots_=motion1), return_type=motion_type)
motion2 = model.motion_from_datastruct(model.Datastruct(rots_=motion2), return_type=motion_type)
elif is_sp and cfg.naive == "concat":
# concat_text = [[" while ".join(cur_texts[0])]]
motion = model.text_to_motion_forward(cur_texts,
cur_lens,
gpt_parts=gpt_parts,
return_motion=motion_type)
else:
motion = model(cur_texts,cur_lens)
# motion = datastruct.rots
# rots, transl = motion.rots, motion.trans
# from sinc.transforms.smpl import RotTransDatastruct
# final_datastruct = self.Datastruct(
# rots_=RotTransDatastruct(rots=rots, trans=transl))
distribution_ref = temos_model.motionencoder(torch.squeeze(temos_model.transforms.rots2rfeats(one_data["datastruct"]))[None])
distribution_motion = temos_model.motionencoder(torch.squeeze(temos_model.transforms.rots2rfeats(motion))[None])
mu_ref = distribution_ref.mean[0,0]
mu_motion = distribution_motion.mean[0,0]
# dist = torch.linalg.norm(mu_motion-mu_ref)
metric = 2*(1-torch.nn.CosineSimilarity()(mu_motion[None], mu_ref[None]))
metrics.append(metric.detach().cpu().numpy())
metrics_dict = dict(zip(keyids, metrics))
metrics_dict = {key: round(1-val.item()/4, 5) for key, val in metrics_dict.items()}
mu_cos_metric = float(np.mean(metrics))
metrics_dict['TOTAL'] = 1-mu_cos_metric/4
metrics_dict['info'] = f'naive={cfg.naive}__set={cfg.set}__ckpt={cfg.ckpt_name}'
#write_json(metrics_dict, metrics_path)
logger.info(f"This metric is for the model which is under: {str(last_ckpt_path)}")
logger.info(f"The cosine similarity loss is {metrics_dict['TOTAL']}.")
logger.info(f"This saved file is: {metrics_path}")
return metrics_dict
if __name__ == '__main__':
_calc_temos_score()