forked from bastianwandt/RepNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_functions.py
More file actions
100 lines (67 loc) · 2.02 KB
/
eval_functions.py
File metadata and controls
100 lines (67 loc) · 2.02 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
import numpy as np
def procrustes(X, Y, scaling=True, reflection='best'):
"""
Reimplementation of MATLAB's `procrustes` function to Numpy.
"""
n, m = X.shape
ny, my = Y.shape
muX = X.mean(0)
muY = Y.mean(0)
X0 = X - muX
Y0 = Y - muY
ssX = (X0**2.).sum()
ssY = (Y0**2.).sum()
# centred Frobenius norm
normX = np.sqrt(ssX)
normY = np.sqrt(ssY)
# scale to equal (unit) norm
X0 /= normX
Y0 /= normY
if my < m:
Y0 = np.concatenate((Y0, np.zeros(n, m-my)),0)
# optimum rotation matrix of Y
A = np.dot(X0.T, Y0)
U,s,Vt = np.linalg.svd(A,full_matrices=False)
V = Vt.T
T = np.dot(V, U.T)
if reflection is not 'best':
# does the current solution use a reflection?
have_reflection = np.linalg.det(T) < 0
# if that's not what was specified, force another reflection
if reflection != have_reflection:
V[:,-1] *= -1
s[-1] *= -1
T = np.dot(V, U.T)
traceTA = s.sum()
if scaling:
# optimum scaling of Y
b = traceTA * normX / normY
# standarised distance between X and b*Y*T + c
d = 1 - traceTA**2
# transformed coords
Z = normX*traceTA*np.dot(Y0, T) + muX
else:
b = 1
d = 1 + ssY/ssX - 2 * traceTA * normY / normX
Z = normY*np.dot(Y0, T) + muX
# transformation matrix
if my < m:
T = T[:my,:]
c = muX - b*np.dot(muY, T)
#transformation values
tform = {'rotation':T, 'scale':b, 'translation':c}
return d, Z, tform
def err_3dpe(p_ref, p):
import numpy as np
# reshape pose if necessary
if p.shape[0] == 1:
p = p.reshape(3, int(p.shape[1]/3))
if p_ref.shape[0] == 1:
p_ref = p_ref.reshape(3, int(p_ref.shape[1]/3))
d, Z, tform = procrustes(p_ref.T, p.T)
Z = Z.T
sum_dist = 0
for i in range(p.shape[1]):
sum_dist += np.linalg.norm(Z[:, i]-p_ref[:, i], 2)
err = np.sum(sum_dist) / Z.shape[1]
return err