forked from gohar-malik/InterMask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
475 lines (399 loc) · 20.2 KB
/
Copy patheval.py
File metadata and controls
475 lines (399 loc) · 20.2 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import numpy as np
import torch
import os
from os.path import join as pjoin
from tqdm import tqdm
from datetime import datetime
from options.eval_option import arg_parse
from models.vq.model import RVQVAE
from models.mask_transformer.transformer import MaskTransformer
from utils.metrics import *
from utils.get_opt import get_opt
from utils.utils import fixseed
from collections import OrderedDict
os.environ['WORLD_SIZE'] = '1'
os.environ['RANK'] = '0'
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12345'
torch.multiprocessing.set_sharing_strategy('file_system')
def build_models(cfg):
if cfg.NAME == "InterGen":
model = InterGen(cfg)
return model
def evaluate_matching_score(motion_loaders, file):
match_score_dict = OrderedDict({})
R_precision_dict = OrderedDict({})
activation_dict = OrderedDict({})
# print(motion_loaders.keys())
print('========== Evaluating MM Distance ==========')
for motion_loader_name, motion_loader in motion_loaders.items():
all_motion_embeddings = []
score_list = []
all_size = 0
mm_dist_sum = 0
top_k_count = 0
# print(motion_loader_name)
with torch.no_grad():
for idx, batch in enumerate(motion_loader):
text_embeddings, motion_embeddings = eval_wrapper.get_co_embeddings(batch)
# print(text_embeddings.shape)
# print(motion_embeddings.shape)
dist_mat = euclidean_distance_matrix(text_embeddings.cpu().numpy(),
motion_embeddings.cpu().numpy())
# print(dist_mat.shape)
mm_dist_sum += dist_mat.trace()
argsmax = np.argsort(dist_mat, axis=1)
# print(argsmax.shape)
top_k_mat = calculate_top_k(argsmax, top_k=3)
top_k_count += top_k_mat.sum(axis=0)
all_size += text_embeddings.shape[0]
all_motion_embeddings.append(motion_embeddings.cpu().numpy())
all_motion_embeddings = np.concatenate(all_motion_embeddings, axis=0)
mm_dist = mm_dist_sum / all_size
R_precision = top_k_count / all_size
match_score_dict[motion_loader_name] = mm_dist
R_precision_dict[motion_loader_name] = R_precision
activation_dict[motion_loader_name] = all_motion_embeddings
print(f'---> [{motion_loader_name}] MM Distance: {mm_dist:.4f}')
print(f'---> [{motion_loader_name}] MM Distance: {mm_dist:.4f}', file=file, flush=True)
line = f'---> [{motion_loader_name}] R_precision: '
for i in range(len(R_precision)):
line += '(top %d): %.4f ' % (i+1, R_precision[i])
print(line)
print(line, file=file, flush=True)
return match_score_dict, R_precision_dict, activation_dict
def evaluate_fid(groundtruth_loader, activation_dict, file):
eval_dict = OrderedDict({})
gt_motion_embeddings = []
print('========== Evaluating FID ==========')
with torch.no_grad():
for idx, batch in enumerate(groundtruth_loader):
motion_embeddings = eval_wrapper.get_motion_embeddings(batch)
gt_motion_embeddings.append(motion_embeddings.cpu().numpy())
gt_motion_embeddings = np.concatenate(gt_motion_embeddings, axis=0)
gt_mu, gt_cov = calculate_activation_statistics(gt_motion_embeddings, emb_scale)
# print(gt_mu)
for model_name, motion_embeddings in activation_dict.items():
mu, cov = calculate_activation_statistics(motion_embeddings, emb_scale)
# print(mu)
fid = calculate_frechet_distance(gt_mu, gt_cov, mu, cov)
print(f'---> [{model_name}] FID: {fid:.4f}')
print(f'---> [{model_name}] FID: {fid:.4f}', file=file, flush=True)
eval_dict[model_name] = fid
return eval_dict
def evaluate_diversity(activation_dict, file):
eval_dict = OrderedDict({})
print('========== Evaluating Diversity ==========')
for model_name, motion_embeddings in activation_dict.items():
diversity = calculate_diversity(motion_embeddings, diversity_times, emb_scale, divide_by)
eval_dict[model_name] = diversity
print(f'---> [{model_name}] Diversity: {diversity:.4f}')
print(f'---> [{model_name}] Diversity: {diversity:.4f}', file=file, flush=True)
return eval_dict
def evaluate_multimodality(mm_motion_loaders, file):
eval_dict = OrderedDict({})
print('========== Evaluating MultiModality ==========')
for model_name, mm_motion_loader in mm_motion_loaders.items():
mm_motion_embeddings = []
with torch.no_grad():
for idx, batch in enumerate(mm_motion_loader):
# (1, mm_replications, dim_pos)
if len(batch) == 5:
batch[2] = batch[2][0]
batch[3] = batch[3][0]
batch[4] = batch[4][0]
motion_embedings = eval_wrapper.get_motion_embeddings(batch)
mm_motion_embeddings.append(motion_embedings.unsqueeze(0))
if len(mm_motion_embeddings) == 0:
multimodality = 0
else:
mm_motion_embeddings = torch.cat(mm_motion_embeddings, dim=0).cpu().numpy()
multimodality = calculate_multimodality(mm_motion_embeddings, mm_num_times, emb_scale, divide_by)
print(f'---> [{model_name}] Multimodality: {multimodality:.4f}')
print(f'---> [{model_name}] Multimodality: {multimodality:.4f}', file=file, flush=True)
eval_dict[model_name] = multimodality
return eval_dict
def get_metric_statistics(values):
mean = np.mean(values, axis=0)
std = np.std(values, axis=0)
conf_interval = 1.96 * std / np.sqrt(replication_times)
return mean, conf_interval
def evaluation(log_file):
with open(log_file, 'w') as f:
all_metrics = OrderedDict({'MM Distance': OrderedDict({}),
'R_precision': OrderedDict({}),
'FID': OrderedDict({}),
'Diversity': OrderedDict({}),
'MultiModality': OrderedDict({})})
for replication in range(replication_times):
motion_loaders = {}
mm_motion_loaders = {}
motion_loaders['ground truth'] = gt_loader
if replication > 0:
opt.save_vis = False
motion_loaders['ground truth'].dataset.normalize = True
for motion_loader_name, motion_loader_getter in eval_motion_loaders.items():
print(f'Generating motions from {motion_loader_name}')
motion_loader, mm_motion_loader = motion_loader_getter()
motion_loaders[motion_loader_name] = motion_loader
if mm_motion_loader is not None:
mm_motion_loaders[motion_loader_name] = mm_motion_loader
motion_loaders['ground truth'].dataset.normalize = False
print(f'\n==================== Replication {replication} ====================')
print(f'\n==================== Replication {replication} ====================', file=f, flush=True)
print(f'Time: {datetime.now()}')
print(f'Time: {datetime.now()}', file=f, flush=True)
mat_score_dict, R_precision_dict, acti_dict = evaluate_matching_score(motion_loaders, f)
print(f'Time: {datetime.now()}')
print(f'Time: {datetime.now()}', file=f, flush=True)
fid_score_dict = evaluate_fid(gt_loader, acti_dict, f)
print(f'Time: {datetime.now()}')
print(f'Time: {datetime.now()}', file=f, flush=True)
div_score_dict = evaluate_diversity(acti_dict, f)
if mm_motion_loaders:
print(f'Time: {datetime.now()}')
print(f'Time: {datetime.now()}', file=f, flush=True)
mm_score_dict = evaluate_multimodality(mm_motion_loaders, f)
print(f'!!! DONE !!!\n')
print(f'!!! DONE !!!\n', file=f, flush=True)
for key, item in mat_score_dict.items():
if key not in all_metrics['MM Distance']:
all_metrics['MM Distance'][key] = [item]
else:
all_metrics['MM Distance'][key] += [item]
for key, item in R_precision_dict.items():
if key not in all_metrics['R_precision']:
all_metrics['R_precision'][key] = [item]
else:
all_metrics['R_precision'][key] += [item]
for key, item in fid_score_dict.items():
if key not in all_metrics['FID']:
all_metrics['FID'][key] = [item]
else:
all_metrics['FID'][key] += [item]
for key, item in div_score_dict.items():
if key not in all_metrics['Diversity']:
all_metrics['Diversity'][key] = [item]
else:
all_metrics['Diversity'][key] += [item]
if mm_motion_loaders:
for key, item in mm_score_dict.items():
if key not in all_metrics['MultiModality']:
all_metrics['MultiModality'][key] = [item]
else:
all_metrics['MultiModality'][key] += [item]
# print(all_metrics['Diversity'])
for metric_name, metric_dict in all_metrics.items():
print('========== %s Summary ==========' % metric_name)
print('========== %s Summary ==========' % metric_name, file=f, flush=True)
for model_name, values in metric_dict.items():
# print(metric_name, model_name)
mean, conf_interval = get_metric_statistics(np.array(values))
# print(mean, mean.dtype)
if isinstance(mean, np.float64) or isinstance(mean, np.float32):
print(f'---> [{model_name}] Mean: {mean:.4f} CInterval: {conf_interval:.4f}')
print(f'---> [{model_name}] Mean: {mean:.4f} CInterval: {conf_interval:.4f}', file=f, flush=True)
elif isinstance(mean, np.ndarray):
line = f'---> [{model_name}]'
for i in range(len(mean)):
line += '(top %d) Mean: %.4f CInt: %.4f;' % (i+1, mean[i], conf_interval[i])
print(line)
print(line, file=f, flush=True)
def evaluation_during_training(opt, net, test_loader, eval_wrapper_passed, epoch, file, trans=None):
mm_num_samples = 0 #100
mm_num_repeats = 30
time_steps = 20
cond_scale = 2
topkr = 0.9
global eval_wrapper, emb_scale, divide_by
eval_wrapper = eval_wrapper_passed
test_loader.dataset.normalize = True
if opt.dataset_name == "interhuman":
from models.evaluator.evaluator import get_motion_loader
emb_scale = 6
divide_by = 2
elif opt.dataset_name == "interx":
from models.evaluator.evaluator_interx import get_motion_loader
emb_scale = 1
divide_by = 1
opt.gen_react = False
gen_motion_loader, _ = get_motion_loader(
opt.test_batch_size,
net,
trans,
test_loader.dataset,
opt.device,
mm_num_samples,
mm_num_repeats,
None,
opt,
time_steps,
cond_scale,
topkr
)
test_loader.dataset.normalize = False
eval_motion_loaders = {'gt': test_loader,
'gen': gen_motion_loader}
with open(file, 'a') as f:
print(f'==================== Epoch {epoch} ====================')
print(f'\n==================== Epoch {epoch} ====================', file=f, flush=True)
mat_score_dict, R_precision_dict, acti_dict = evaluate_matching_score(eval_motion_loaders, f)
fid_score_dict = evaluate_fid(test_loader, acti_dict, f)
return fid_score_dict['gen'], mat_score_dict['gen'], R_precision_dict['gen'][0]
def load_vq_model(vq_opt, which_epoch):
# opt_path = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.vq_name, 'opt.txt')
vq_model = RVQVAE(vq_opt,
dim_pose,
vq_opt.nb_code,
vq_opt.code_dim,
vq_opt.code_dim,
vq_opt.down_t,
vq_opt.stride_t,
vq_opt.width,
vq_opt.depth,
vq_opt.dilation_growth_rate,
vq_opt.vq_act,
vq_opt.vq_norm)
ckpt = torch.load(pjoin(vq_opt.checkpoints_dir, vq_opt.dataset_name, vq_opt.name, 'model', which_epoch), map_location='cpu')
model_key = 'vq_model' if 'vq_model' in ckpt else 'net'
missing_keys, unexpected_keys = vq_model.load_state_dict(ckpt[model_key], strict=False)
assert len(unexpected_keys) == 0, f"Unexpected keys: {unexpected_keys}"
assert all([k.startswith('decoder.conv') or k.startswith('decoder.resnets')for k in missing_keys])
vq_epoch = ckpt['ep'] if 'ep' in ckpt else -1
print(f'Loading VQ Model {vq_opt.name} Completed!, Epoch {vq_epoch}')
return vq_model, vq_epoch
def load_trans_model(model_opt, which_model):
# clip_version = 'ViT-B/32'
clip_version = 'ViT-L/14@336px'
t2m_transformer = MaskTransformer(code_dim=model_opt.code_dim,
cond_mode='text',
latent_dim=model_opt.latent_dim,
ff_size=model_opt.ff_size,
num_layers=model_opt.n_layers,
num_heads=model_opt.n_heads,
dropout=model_opt.dropout,
clip_dim=768,
cond_drop_prob=model_opt.cond_drop_prob,
clip_version=clip_version,
opt=model_opt)
ckpt = torch.load(pjoin(model_opt.checkpoints_dir, model_opt.dataset_name, model_opt.name, 'model', which_model),
map_location=opt.device)
model_key = 't2m_transformer' if 't2m_transformer' in ckpt else 'trans'
# print(ckpt.keys())
missing_keys, unexpected_keys = t2m_transformer.load_state_dict(ckpt[model_key], strict=False)
assert len(unexpected_keys) == 0
assert all([k.startswith('clip_') for k in missing_keys])
print(f'Loading Mask Transformer {opt.name} from epoch {ckpt["ep"]}!')
return t2m_transformer
if __name__ == '__main__':
opt = arg_parse()
opt.device = torch.device("cpu" if opt.gpu_id == -1 else "cuda:" + str(opt.gpu_id))
print(f"Using Device: {opt.device}")
if opt.use_trans:
trans_opt_path = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.name, 'opt.txt')
main_opt = get_opt(trans_opt_path, opt.device)
fixseed(main_opt.seed)
vq_opt_path = pjoin(opt.checkpoints_dir, opt.dataset_name, main_opt.vq_name, 'opt.txt')
vq_opt = get_opt(vq_opt_path, opt.device)
main_opt.num_tokens = vq_opt.nb_code
main_opt.code_dim = vq_opt.code_dim
else:
opt.mm_num_samples = 0
vq_opt_path = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.name, 'opt.txt')
main_opt = get_opt(vq_opt_path, opt.device)
mm_num_samples = opt.mm_num_samples
mm_num_repeats = opt.mm_num_repeats
mm_num_times = opt.mm_num_times
diversity_times = opt.diversity_times
replication_times = opt.replication_times
opt.save_root = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.name)
opt.model_dir = pjoin(opt.save_root, 'model')
opt.eval_dir = pjoin(opt.save_root, 'eval')
opt.vis_dir = pjoin(opt.save_root, 'animation')
if opt.dataset_name == "interhuman":
opt.npy_dir = pjoin(opt.vis_dir, 'keypoint_npy')
opt.vis_dir = pjoin(opt.vis_dir, 'keypoint_mp4')
os.makedirs(opt.npy_dir, exist_ok=True)
elif opt.dataset_name == "interx":
opt.vis_dir = pjoin(opt.vis_dir, 'smpl_npy')
os.makedirs(opt.eval_dir, exist_ok=True)
os.makedirs(opt.vis_dir, exist_ok=True)
react_name = "react_" if opt.gen_react else ""
if main_opt.dataset_name == "interhuman":
main_opt.data_root = 'data/InterHuman'
main_opt.joints_num = 22
dim_pose = 12
fps = 30
opt.batch_size = 96
main_opt.mode = "test"
emb_scale = 6
divide_by = 2
from models.evaluator.evaluator import EvaluatorModelWrapper, get_dataset_motion_loader, get_motion_loader
evalmodel_cfg = get_opt("checkpoints/eval_model/eval_model.yaml", opt.device, complete=False)
eval_wrapper = EvaluatorModelWrapper(evalmodel_cfg, opt.device)
elif main_opt.dataset_name == "interx":
main_opt.data_root = 'data/InterX'
opt.data_root = main_opt.data_root
main_opt.motion_dir = pjoin(main_opt.data_root, 'motions')
main_opt.text_dir = pjoin(main_opt.data_root, 'texts_processed')
main_opt.motion_rep = "smpl"
main_opt.joints_num = 55 if main_opt.motion_rep == "global" else 56
dim_pose = 12 if main_opt.motion_rep == "global" else 6
fps = 30
opt.batch_size = 32
main_opt.max_motion_length = 150
main_opt.max_text_len = 35
main_opt.unit_length = 4
emb_scale = 1
divide_by = 1
from models.evaluator.evaluator_interx import EvaluatorModelWrapper, get_dataset_motion_loader, get_motion_loader
wrapper_opt = get_opt("checkpoints/hhi/Comp_v6_KLD01/opt.txt", opt.device, complete=False)
eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
else:
raise KeyError('Dataset Does not Exists')
data_cfg = main_opt
gt_loader, gt_dataset = get_dataset_motion_loader(data_cfg, opt.batch_size)
def make_callable(net, file, trans=None):
return lambda: get_motion_loader(
opt.batch_size,
net,
trans,
gt_dataset,
opt.device,
mm_num_samples,
mm_num_repeats,
file,
opt,
time_step,
cond_scale,
topkr
)
if opt.use_trans:
for cond_scale in opt.cond_scales:
for time_step in opt.time_steps:
for topkr in opt.topkr:
eval_motion_loaders = {}
for file in os.listdir(opt.model_dir):
if opt.which_epoch != "all" and opt.which_epoch not in file:
continue
print(f"\n\nLoading model epoch: {file}")
trans = load_trans_model(main_opt, file)
net, ep = load_vq_model(vq_opt, "best_fid.tar")
file = react_name + file
eval_motion_loaders[file] = make_callable(net, file, trans)
which_epoch = opt.which_epoch
log_file_name = f'evaluation_{which_epoch}_ts{time_step}_cs{cond_scale}_topkr{topkr}.log'
log_file_name = react_name + log_file_name
log_file = pjoin(opt.eval_dir, log_file_name)
evaluation(log_file)
else:
eval_motion_loaders = {}
for file in os.listdir(opt.model_dir):
if opt.which_epoch != "all" and opt.which_epoch not in file:
continue
cond_scale, time_step, topkr = None, None, None
print(f"\n\nLoading model epoch: {file}")
net, ep = load_vq_model(main_opt, file)
eval_motion_loaders[file] = make_callable(net, file)
log_file = pjoin(opt.eval_dir, f'evaluation_{opt.which_epoch}.log')
evaluation(log_file)