-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_calibration.py
138 lines (109 loc) · 3.47 KB
/
eval_calibration.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
import time
import config as c
import data.data_h36m
import models.model as model
import numpy as np
import torch
import wandb
from sklearn.metrics import auc
from tqdm import tqdm
from utils.data_utils import reinsert_root_joint_torch, root_center_poses
from utils.eval_functions import (
compute_3DPCK,
compute_CP_list,
err_3dpe_parallel,
pa_hypo_batch,
)
wandb.init(
project="propose_human36m",
entity="ppierzc",
job_type="evaluation",
name=f"wehrbein_calibration_{time.strftime('%d/%m/%Y::%H:%M:%S')}",
config={
"seed": 2,
},
)
# Set seed
torch.manual_seed(wandb.config.seed)
np.random.seed(wandb.config.seed)
print("Program is running on: ", c.device)
print("EVALUATING EXPERIMENT: ", c.experiment_name, "\n")
inn = model.poseINN()
inn.to(c.device)
inn.load(c.load_model_name, c.device)
inn.eval()
print(f"{sum(p.numel() for p in inn.parameters()):,}")
c.batch_size = 512
n_hypo = 200
std_dev = 1.0
cps_min_th = 1
cps_max_th = 300
cps_step = 1
cps_length = int((cps_max_th + 1 - cps_min_th) / cps_step)
quick_eval_stride = 16
test_dataset = data.data_h36m.H36MDataset(
c.test_file,
quick_eval=True,
quick_eval_stride=quick_eval_stride,
train_set=False,
hardsubset=False,
)
loader = torch.utils.data.DataLoader(
test_dataset, batch_size=1, shuffle=True, drop_last=False
)
n_poses = len(test_dataset)
total_err_z0_p1 = 0
total_err_mean_p1 = 0
total_err_worst_p1 = 0
total_err_best_p1 = 0
total_err_occ_best_p1 = 0
total_err_median_p1 = 0
total_err_z0_p2 = 0
total_err_mean_p2 = 0
total_err_worst_p2 = 0
total_err_best_p2 = 0
total_err_median_p2 = 0
total_best_pck_oracle_p1 = 0
total_auc_cps_p2_best = torch.zeros((cps_length,))
hypo_stddev = torch.zeros((3, 17))
quantiles = np.arange(0, 1.05, 0.05)
quantile_counts = np.zeros((len(quantiles), 16))
q_val = []
total = 0
for batch_idx, sample in tqdm(enumerate(loader)):
# if batch_idx == 10:
# break
x = sample["poses_3d"]
y_gt = sample["p2d_hrnet"]
cond = sample["gauss_fits"]
occl = sample["occlusions"]
bs = x.shape[0]
x_gt = sample["p3d_gt"]
# sample multiple z
z_all = std_dev * torch.randn(n_hypo, bs, c.ndim_z, device=c.device)
y_gt = y_gt[None, :].repeat(n_hypo, 1, 1)
y_rand = torch.cat((z_all, y_gt), dim=2)
y_rand = y_rand.view(-1, c.ndim_y + c.ndim_z)
cond = cond[None].repeat(n_hypo, 1, 1).view(-1, inn.cond_in_size)
with torch.no_grad():
poses_3d_pred = inn.reverse(y_rand, cond)
poses_3d_pred = reinsert_root_joint_torch(poses_3d_pred)
poses_3d_pred = root_center_poses(poses_3d_pred) * 1000
poses_3d_pred = poses_3d_pred.view(n_hypo, 3, 17).swapaxes(1, 2)[:, 1:]
x_gt = x_gt.view(1, 3, 17).swapaxes(1, 2)[:, 1:]
sample_median = torch.Tensor(poses_3d_pred).median(0).values
errors = ((sample_median - poses_3d_pred) ** 2).sum(-1) ** 0.5
true_error = ((sample_median - x_gt) ** 2).sum(-1) ** 0.5
q_vals = np.quantile(errors.data.numpy(), quantiles, 0)
q_val.append(q_vals)
v = (q_vals > true_error.data.numpy().squeeze()).astype(int)
if not np.isnan(v).any():
total += 1
quantile_counts += v
quantile_freqs = quantile_counts / total
calibration_score = np.abs(np.median(quantile_freqs, axis=1) - quantiles).mean()
wandb.log({"calibration_score": calibration_score})
print("Calibration score: ", calibration_score)
np.save("wehrbein_quantile_freqs.npy", quantile_freqs)
np.save("wehrbein_quantiles.npy", quantiles)
np.save("wehrbein_q_val.npy", q_val)