-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVQ_Limo_eval.py
202 lines (144 loc) · 8.11 KB
/
VQ_Limo_eval.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
import os
import json
import torch
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import models.vqvae as vqvae
import options.option_limo as option_limo
import utils.utils_model as utils_model
from dataset import dataset_TM_eval
import utils.eval_trans as eval_trans
from options.get_eval_option import get_opt
from models.evaluator_wrapper import EvaluatorModelWrapper
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from utils.motion_process import recover_from_ric
##### ---- Exp dirs ---- #####
args = option_limo.get_args_parser()
torch.manual_seed(args.seed)
args.out_dir = os.path.join(args.out_dir, f'{args.exp_name}')
os.makedirs(args.out_dir, exist_ok = True)
##### ---- Logger ---- #####
logger = utils_model.get_logger(args.out_dir)
writer = SummaryWriter(args.out_dir)
logger.info(json.dumps(vars(args), indent=4, sort_keys=True))
from utils.eval_trans import *
from utils.word_vectorizer import WordVectorizer
w_vectorizer = WordVectorizer('./glove', 'our_vab')
dataset_opt_path = 'checkpoints/kit/Comp_v6_KLD005/opt.txt' if args.dataname == 'kit' else 'checkpoints/t2m/Comp_v6_KLD005/opt.txt'
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
wrapper_opt = get_opt(dataset_opt_path, device)
eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
##### ---- Dataloader ---- #####
args.nb_joints = 21 if args.dataname == 'kit' else 22
val_loader = dataset_TM_eval.DATALoader(args.dataname, True, 32, w_vectorizer, unit_length=2**args.down_t,data_root=args.data_root)
##### ---- Network ---- #####
@torch.no_grad()
def evaluation_limo(out_dir, val_loader, logger, writer, nb_iter, best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, eval_wrapper, draw = True) :
nb_sample = 0
motion_annotation_list = []
motion_pred_list = []
R_precision_real = 0
R_precision = 0
nb_sample = 0
matching_score_real = 0
matching_score_pred = 0
for batch in val_loader:
word_embeddings, pos_one_hots, caption, sent_len, motion, m_length, token, name, mcs_score = batch
# print("motion length:", motion.shape)
motion = motion.cuda()
et, em = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, motion, m_length)
bs, seq = motion.shape[0], motion.shape[1]
# print(bs,seq,et.shape,em.shape)
num_joints = 21 if motion.shape[-1] == 251 else 22
pred_pose_eval = torch.zeros((bs, seq, motion.shape[-1])).cuda()
for i in range(bs):
sample_folder = "category_" + "_".join(caption[i].split())
sample_path = os.path.join(args.out_dir, sample_folder, f'entry_{np.random.choice(100)}.npy')
pred_pose = np.load(sample_path)
pred_pose = (pred_pose - val_loader.dataset.mean)/val_loader.dataset.std
m_length[i] = min(m_length[i], pred_pose.shape[0])
pred_pose_eval[i:i+1,:m_length[i],:] = torch.from_numpy(pred_pose[:m_length[i],:]).to(device)
et_pred, em_pred = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, pred_pose_eval, m_length)
motion_pred_list.append(em_pred)
motion_annotation_list.append(em)
temp_R, temp_match = calculate_R_precision(et.cpu().numpy(), em.cpu().numpy(), top_k=3, sum_all=True)
R_precision_real += temp_R
matching_score_real += temp_match
temp_R, temp_match = calculate_R_precision(et_pred.cpu().numpy(), em_pred.cpu().numpy(), top_k=3, sum_all=True)
R_precision += temp_R
matching_score_pred += temp_match
nb_sample += bs
motion_annotation_np = torch.cat(motion_annotation_list, dim=0).cpu().numpy()
motion_pred_np = torch.cat(motion_pred_list, dim=0).cpu().numpy()
gt_mu, gt_cov = calculate_activation_statistics(motion_annotation_np)
mu, cov= calculate_activation_statistics(motion_pred_np)
# print(motion_pred_np.shape,motion_annotation_np.shape)
diversity_real = calculate_diversity(motion_annotation_np, 300 if nb_sample > 300 else 10)
diversity = calculate_diversity(motion_pred_np, 300 if nb_sample > 300 else 10)
R_precision_real = R_precision_real / nb_sample
R_precision = R_precision / nb_sample
matching_score_real = matching_score_real / nb_sample
matching_score_pred = matching_score_pred / nb_sample
fid = calculate_frechet_distance(gt_mu, gt_cov, mu, cov)
msg = f"--> \t Eva. Iter {nb_iter} :, FID. {fid:.4f}, Diversity Real. {diversity_real:.4f}, Diversity. {diversity:.4f}, R_precision_real. {R_precision_real}, R_precision. {R_precision}, matching_score_real. {matching_score_real}, matching_score_pred. {matching_score_pred}"
# msg = f"--> \t Eva. Iter {nb_iter} :, FID. {fid:.4f}, R_precision_real. {R_precision_real}, R_precision. {R_precision}, matching_score_real. {matching_score_real}, matching_score_pred. {matching_score_pred}"
logger.info(msg)
if fid < best_fid :
msg = f"--> --> \t FID Improved from {best_fid:.5f} to {fid:.5f} !!!"
logger.info(msg)
best_fid, best_iter = fid, nb_iter
if abs(diversity_real - diversity) < abs(diversity_real - best_div) :
msg = f"--> --> \t Diversity Improved from {best_div:.5f} to {diversity:.5f} !!!"
logger.info(msg)
best_div = diversity
if R_precision[0] > best_top1 :
msg = f"--> --> \t Top1 Improved from {best_top1:.4f} to {R_precision[0]:.4f} !!!"
logger.info(msg)
best_top1 = R_precision[0]
if R_precision[1] > best_top2 :
msg = f"--> --> \t Top2 Improved from {best_top2:.4f} to {R_precision[1]:.4f} !!!"
logger.info(msg)
best_top2 = R_precision[1]
if R_precision[2] > best_top3 :
msg = f"--> --> \t Top3 Improved from {best_top3:.4f} to {R_precision[2]:.4f} !!!"
logger.info(msg)
best_top3 = R_precision[2]
if matching_score_pred < best_matching :
msg = f"--> --> \t matching_score Improved from {best_matching:.5f} to {matching_score_pred:.5f} !!!"
logger.info(msg)
best_matching = matching_score_pred
return best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger
fid = []
div = []
top1 = []
top2 = []
top3 = []
matching = []
repeat_time = 20
for i in range(repeat_time):
best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = evaluation_limo(args.out_dir, val_loader, logger, writer, 0, best_fid=1000, best_iter=0, best_div=100, best_top1=0, best_top2=0, best_top3=0, best_matching=100, eval_wrapper=eval_wrapper, draw=True)
fid.append(best_fid)
div.append(best_div)
top1.append(best_top1)
top2.append(best_top2)
top3.append(best_top3)
matching.append(best_matching)
print('final result:')
print('fid: ', sum(fid)/repeat_time)
print('div: ', sum(div)/repeat_time)
print('top1: ', sum(top1)/repeat_time)
print('top2: ', sum(top2)/repeat_time)
print('top3: ', sum(top3)/repeat_time)
print('matching: ', sum(matching)/repeat_time)
fid = np.array(fid)
div = np.array(div)
top1 = np.array(top1)
top2 = np.array(top2)
top3 = np.array(top3)
matching = np.array(matching)
msg_final = f"FID. {np.mean(fid):.3f}, conf. {np.std(fid)*1.96/np.sqrt(repeat_time):.3f}, Diversity. {np.mean(div):.3f}, conf. {np.std(div)*1.96/np.sqrt(repeat_time):.3f}, TOP1. {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}, Matching. {np.mean(matching):.3f}, conf. {np.std(matching)*1.96/np.sqrt(repeat_time):.3f}"
logger.info(msg_final)
msg_final = f"$ {np.mean(div):.3f}^{{\pm {np.std(div)*1.96/np.sqrt(repeat_time):.3f}}} $ & ${np.mean(fid):.3f}^{{\pm {np.std(fid)*1.96/np.sqrt(repeat_time):.3f}}}$ & ${np.mean(top1):.3f}^{{\pm {np.std(top1)*1.96/np.sqrt(repeat_time):.3f}}}, {np.mean(top2):.3f}^{{\pm {np.std(top2)*1.96/np.sqrt(repeat_time):.3f}}}, {np.mean(top3):.3f}^{{\pm {np.std(top3)*1.96/np.sqrt(repeat_time):.3f}}} $ & ${np.mean(matching):.3f}^{{{np.std(matching)*1.96/np.sqrt(repeat_time):.3f}}}$"
logger.info(msg_final)