-
Notifications
You must be signed in to change notification settings - Fork 5
/
calc_metrics.py
164 lines (132 loc) · 6.14 KB
/
calc_metrics.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
import glob
import json
import os.path
import numpy as np
import time
import argparse
from utils.func import R_from_2poses, merge_from_2hands, matrix_from_quaternion, quaternion_from_matrix
from consistency import mpjpe_per_hand
JNUM = 21
def R_from_2hands(hands1, hands2, valids1, valids2, R12=None, R21=None):
for i in range(hands1.shape[0]):
pred1, pred2 = hands1[i], hands2[i]
p1, p2 = [], []
for j in range(pred1.shape[0]):
if valids1[i, j] and valids2[i, j]:
p1.append(pred1[j])
p2.append(pred2[j])
if R12 is None:
R12 = R_from_2poses(p1, p2, is_torch=False)
if R21 is None:
R21 = R_from_2poses(p2, p1, is_torch=False)
return R12, R21
def matrix2angle(m):
"""
Calculates Rotation Matrix given euler angles.
:param theta: 1-by-3 list [rx, ry, rz] angle in degree
:return:
RPY, the object will be rotated with the order of [rx, ry, rz]
"""
x = np.arctan2(m[2, 1], m[2, 2])
y = np.arcsin(-m[2, 0])
z = np.arctan2(m[1, 0], m[0, 0])
return np.array([x, y, z])
def calc_R(log_path):
lines = open(log_path).readlines()
lines.pop(0)
mono = float(lines.pop(-1).split(':')[-1])
line_num = len(lines)
R_ls, R_gt = [], None
for i in range(min(512, line_num // 2)):
view0 = lines[2 * i].split()
view1 = lines[2 * i + 1].split()
pred0 = np.array(view0[4].split(',')).astype(np.float64).reshape(JNUM, 3)
valid0 = np.array(view0[6].split(',')).astype(np.float64).reshape(JNUM, )
pred1 = np.array(view1[4].split(',')).astype(np.float64).reshape(JNUM, 3)
valid1 = np.array(view1[6].split(',')).astype(np.float64).reshape(JNUM, )
R_pred, _ = R_from_2hands(pred0[np.newaxis], pred1[np.newaxis],
valid0[np.newaxis, :], valid1[np.newaxis, :])
R_ls.append(R_pred)
quan_ls = [quaternion_from_matrix(R) for R in R_ls]
quan_avg = np.mean(quan_ls, axis=0)
quan_avg /= np.linalg.norm(quan_avg)
R_est = matrix_from_quaternion(np.mean(quan_ls, axis=0))
return R_est
def calc_merged_metric(log_path, use_gt=False, R12=None, R21=None, dynamic_R=False):
if dynamic_R:
print('calculating new R...')
R12 = calc_R(log_path)
R21 = R12.T
print('done')
name = os.path.basename(log_path).split('.')[0]
lines = open(log_path).readlines()
lines.pop(0)
mono = float(lines.pop(-1).split(':')[-1])
line_num = len(lines)
sumf = []
mpjpe_ls, q_diff_ls = [], []
for i in range(line_num // 2):
view0 = lines[2 * i].split()
view1 = lines[2 * i + 1].split()
pred0 = np.array(view0[4].split(',')).astype(np.float64).reshape(JNUM, 3)
gt0 = np.array(view0[5].split(',')).astype(np.float64).reshape(JNUM, 3)
R0 = np.array(view0[7].split(',')).astype(np.float64).reshape(3, 3)
valid0 = np.array(view0[6].split(',')).astype(np.float64).reshape(JNUM, )
pred1 = np.array(view1[4].split(',')).astype(np.float64).reshape(JNUM, 3)
gt1 = np.array(view1[5].split(',')).astype(np.float64).reshape(JNUM, 3)
R1 = np.array(view1[7].split(',')).astype(np.float64).reshape(3, 3)
valid1 = np.array(view1[6].split(',')).astype(np.float64).reshape(JNUM, )
R = R_from_2poses(pred0, pred1)
R_gt = R0 @ R1.T
q_diff = matrix2angle(R) - matrix2angle(R_gt)
q_diff_ls.append(q_diff)
if not use_gt:
merge0, merge1 = merge_from_2hands(pred0[np.newaxis, :, :], pred1[np.newaxis, :, :],
valid0[np.newaxis, :], valid1[np.newaxis, :])
else:
if R12 is None:
R12 = R0 @ R1.T
if R21 is None:
R21 = R1 @ R0.T
merge0, merge1 = merge_from_2hands(pred0[np.newaxis, :, :], pred1[np.newaxis, :, :],
valid0[np.newaxis, :], valid1[np.newaxis, :],
R12=R12, R21=R21)
merge0, merge1 = np.squeeze(merge0), np.squeeze(merge1)
# f0 = np.array(mpjpe_per_hand(merge0, gt0, valid0)) < np.array(mpjpe_per_hand(pred0, gt0, valid0))
# f1 = np.array(mpjpe_per_hand(merge1, gt1, valid1)) < np.array(mpjpe_per_hand(pred1, gt1, valid1))
# sumf.append(sum(f0 == f1))
mpjpe_ls.append(mpjpe_per_hand(merge0, gt0, valid0))
mpjpe_ls.append(mpjpe_per_hand(merge1, gt1, valid1))
q_mean = np.mean(np.abs(q_diff_ls), axis=0)
q_mean_str = ','.join([f'{u:.2f}' for u in q_mean])
print(f'{name} num:{len(mpjpe_ls)}, Mono-M:{mono:.2f}, Dual-M:{np.nanmean(mpjpe_ls):.2f}, '
f'Rotation_quat_err:({q_mean_str})')
# print(np.mean(sumf))
return len(mpjpe_ls), mono, np.nanmean(mpjpe_ls), q_diff_ls
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PyTorch Train: DetNet')
# Dataset setting
parser.add_argument('--checkpoint', type=str, default='in_dataset_adapt/evaluation/ah',
help='save dir of the test logs')
parser.add_argument('--setup', type=int, default=0, help='id of headset')
parser.add_argument('--pair', type=str, default='1,2', help='id of dual-camera pair')
parser.add_argument('-eid', '--evaluate_id', default=None, type=int, metavar='N',
help='number of data loading workers (default: 8)')
args = parser.parse_args()
eid = args.evaluate_id if args.evaluate_id is not None else '*'
logs = glob.glob(os.path.join(args.checkpoint, f'{eid}-set{args.setup}-{args.pair}.log'))
logs.sort()
print(logs)
R_config = json.load(open('./R_config.json'))
q_diffs = []
num, mono, merge = 0, 0, 0
for log in logs:
print(log)
setup, pair = os.path.basename(log).split('.')[0].split('-')[1:]
R12 = np.array(R_config[f'{setup}-{pair}']['R_pred'])
n, mo, mer, q_diff = calc_merged_metric(log, use_gt=True, R12=R12, R21=R12.T, dynamic_R=False)
num += n
mono += mo * n
merge += mer * n
q_diffs += q_diff
print(f'total:{num}, mean Mono-M:{mono / num:.2f}, mean Dual-M:{merge / num:.2f}')