-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_mask.py
184 lines (152 loc) · 8.05 KB
/
eval_mask.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
import os
from os.path import join as pjoin
import numpy as np
import torch
from options.eval_option import EvalT2MOptions
from utils.get_opt import get_opt
from utils.fixseed import fixseed
import utils.eval_t2m_ddp as eval_t2m
from motion_loaders.dataset_motion_loader import get_dataset_motion_loader
from models.t2m_eval_wrapper import EvaluatorModelWrapper
from models.transformer.transformer_aux import MaskTransformer
from models.transformer.transformer_ts import MaskTransformer2D
from models.vq.model import RVQVAE
def load_vq_model(vq_opt):
# opt_path = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.vq_name, 'opt.txt')
vq_model = RVQVAE(vq_opt,
dim_pose,
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', 'net_best_fid.tar'),
map_location=opt.device)
model_key = 'vq_model' if 'vq_model' in ckpt else 'net'
vq_model.load_state_dict(ckpt[model_key])
print(f'Loading VQ Model {vq_opt.name} Completed!', ckpt['ep'], ckpt['value'])
return vq_model, vq_opt
def load_trans_aux(model_opt, which_model):
transformer = MaskTransformer(code_dim=vq_model.code_dim1d,
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=512,
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)
missing_keys, unexpected_keys = transformer.load_state_dict(ckpt['t2m_transformer_aux'], strict=False)
assert len(unexpected_keys) == 0
assert all([k.startswith('clip_model.') for k in missing_keys])
print(f'Loading Mask Transformer {model_opt.name} from epoch {ckpt["ep"]}!', 'Value: ', ckpt['best_value'] if 'best_value' in ckpt.keys() else '')
return transformer
def load_trans_ts(model_opt, which_model):
transformer = MaskTransformer2D(code_dim=vq_model.code_dim2d,
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=512,
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)
missing_keys, unexpected_keys = transformer.load_state_dict(ckpt['t2m_transformer_ts'], strict=False)
assert len(unexpected_keys) == 0
assert all([k.startswith('clip_model.') for k in missing_keys])
print(f'Loading Mask Transformer {model_opt.name} from epoch {ckpt["ep"]}!', 'Value: ', ckpt['best_value'] if 'best_value' in ckpt.keys() else '')
return transformer
if __name__ == '__main__':
parser = EvalT2MOptions()
opt = parser.parse()
fixseed(opt.seed)
opt.device = torch.device("cpu" if opt.gpu_id == -1 else "cuda:" + str(opt.gpu_id))
torch.autograd.set_detect_anomaly(True)
dim_pose = 251 if opt.dataset_name == 'kit' else 263
root_dir = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.mtrans_name)
if not os.path.exists(root_dir): exit()
model_dir = pjoin(root_dir, 'model')
out_dir = pjoin(root_dir, 'eval')
os.makedirs(out_dir, exist_ok=True)
out_path = pjoin(out_dir, "%s.log"%opt.ext)
f = open(pjoin(out_path), 'w')
model_opt_path = pjoin(root_dir, 'opt.txt')
model_opt = get_opt(model_opt_path, device=opt.device)
clip_version = 'ViT-B/32'
vq_opt_path = pjoin(opt.checkpoints_dir, opt.dataset_name, model_opt.vq_name, 'opt.txt')
vq_opt = get_opt(vq_opt_path, device=opt.device)
vq_model, vq_opt = load_vq_model(vq_opt)
model_opt.num_tokens1d = vq_model.num_code1d
model_opt.num_tokens2d = vq_model.num_code2d
res_opt_path = None
res_opt = None
res_model = None
dataset_opt_path = 'checkpoints/kit/Comp_v6_KLD005/opt.txt' if opt.dataset_name == 'kit' \
else 'checkpoints/humanml3d/Comp_v6_KLD005/opt.txt'
wrapper_opt = get_opt(dataset_opt_path, torch.device('cuda'))
eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
##### ---- Dataloader ---- #####
opt.nb_joints = 21 if opt.dataset_name == 'kit' else 22
eval_val_loader, _ = get_dataset_motion_loader(dataset_opt_path, 32, 'test', device=opt.device)
for file in os.listdir(model_dir):
if opt.which_epoch != "all" and opt.which_epoch not in file:
continue
print('loading checkpoint {}'.format(file))
transformer_aux = load_trans_aux(model_opt, file)
transformer_ts = load_trans_ts(model_opt, file)
transformer_aux.eval()
transformer_ts.eval()
vq_model.eval()
transformer_aux.to(opt.device)
transformer_ts.to(opt.device)
vq_model.to(opt.device)
fid = []
div = []
top1 = []
top2 = []
top3 = []
matching = []
mm = []
repeat_time = 20
for i in range(repeat_time):
with torch.no_grad():
best_fid, best_div, Rprecision, best_matching, best_mm = \
eval_t2m.evaluation_mask_transformer_test(eval_val_loader, vq_model, transformer_aux, transformer_ts,
i, eval_wrapper=eval_wrapper,
time_steps=opt.time_steps, cond_scale=opt.cond_scale,
temperature=opt.temperature, topkr=opt.topkr,
force_mask=opt.force_mask, cal_mm=True)
fid.append(best_fid)
div.append(best_div)
top1.append(Rprecision[0])
top2.append(Rprecision[1])
top3.append(Rprecision[2])
matching.append(best_matching)
mm.append(best_mm)
fid = np.array(fid)
div = np.array(div)
top1 = np.array(top1)
top2 = np.array(top2)
top3 = np.array(top3)
matching = np.array(matching)
mm = np.array(mm)
print(f'{file} final result:')
print(f'{file} final result:', file=f, flush=True)
msg_final = f"\tFID: {np.mean(fid):.3f}, conf. {np.std(fid) * 1.96 / np.sqrt(repeat_time):.3f}\n" \
f"\tDiversity: {np.mean(div):.3f}, conf. {np.std(div) * 1.96 / np.sqrt(repeat_time):.3f}\n" \
f"\tTOP1: {np.mean(top1):.3f}, conf. {np.std(top1) * 1.96 / np.sqrt(repeat_time):.3f}, TOP2. {np.mean(top2):.3f}, conf. {np.std(top2) * 1.96 / np.sqrt(repeat_time):.3f}, TOP3. {np.mean(top3):.3f}, conf. {np.std(top3) * 1.96 / np.sqrt(repeat_time):.3f}\n" \
f"\tMatching: {np.mean(matching):.3f}, conf. {np.std(matching) * 1.96 / np.sqrt(repeat_time):.3f}\n" \
f"\tMultimodality:{np.mean(mm):.3f}, conf.{np.std(mm) * 1.96 / np.sqrt(repeat_time):.3f}\n\n"
print(msg_final)
print(msg_final, file=f, flush=True)
f.close()