-
Notifications
You must be signed in to change notification settings - Fork 2
/
loss_func.py
164 lines (116 loc) · 5.12 KB
/
loss_func.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
import torch.nn as nn
import torch
import numpy as np
from utils.geometry import batch_rodrigues, rot6d_to_rotmat
class L1(nn.Module):
def __init__(self, device):
super(L1, self).__init__()
self.device = device
self.L1Loss = nn.L1Loss(size_average=False)
def forward(self, x, y):
b = x.shape[0]
diff = self.L1Loss(x, y)
diff = diff / b
return diff
class MPJPE(nn.Module):
def __init__(self, device):
super(MPJPE, self).__init__()
self.device = device
self.regressor = torch.from_numpy(np.load('data/J_regressor_lsp.npy')).to(torch.float32).to(device)
self.halpe2lsp = [16,14,12,11,13,15,10,8,6,5,7,9,18,17]
def forward_instance(self, pred_verts, gt_verts):
loss_dict = {}
pred_joints = torch.matmul(self.regressor, pred_verts)
gt_joints = torch.matmul(self.regressor, gt_verts)
pred_joints = self.align_by_pelvis(pred_joints, format='lsp')
gt_joints = self.align_by_pelvis(gt_joints, format='lsp')
diff = torch.sqrt(torch.sum((pred_joints - gt_joints)**2, dim=[2]))
diff = torch.mean(diff, dim=[1])
diff = diff * 1000
return diff.detach().cpu().numpy()
def forward(self, pred_verts, gt_verts):
loss_dict = {}
bs, f , _, _ = pred_verts.shape
pred_joints = torch.matmul(self.regressor, pred_verts).reshape(bs*f, -1, 3)
gt_joints = torch.matmul(self.regressor, gt_verts).reshape(bs*f, -1, 3)
pred_joints = self.align_by_pelvis(pred_joints, format='lsp')
gt_joints = self.align_by_pelvis(gt_joints, format='lsp')
diff = torch.sqrt(torch.sum((pred_joints - gt_joints)**2, dim=[2]))
diff = torch.mean(diff, dim=[1])
diff = torch.mean(diff) * 1000
return diff
def pa_mpjpe(self, pred_verts, gt_verts):
loss_dict = {}
bs, f , _, _ = pred_verts.shape
pred_joints = torch.matmul(self.regressor, pred_verts).reshape(bs*f, -1, 3)
gt_joints = torch.matmul(self.regressor, gt_verts).reshape(bs*f, -1, 3)
pred_joints = pred_joints.detach().cpu()
gt_joints = gt_joints.detach().cpu()
pred_joints = self.align_by_pelvis(pred_joints, format='lsp')
gt_joints = self.align_by_pelvis(gt_joints, format='lsp')
pred_joints = self.batch_compute_similarity_transform(pred_joints, gt_joints)
# diff = torch.sqrt(torch.sum((pred_joints - gt_joints)**2, dim=[2]) * conf)
diff = torch.sqrt(torch.sum((pred_joints - gt_joints)**2, dim=[2]))
diff = torch.mean(diff, dim=[1])
diff = torch.mean(diff) * 1000
return diff
def batch_compute_similarity_transform(self, S1, S2):
'''
Computes a similarity transform (sR, t) that takes
a set of 3D points S1 (3 x N) closest to a set of 3D points S2,
where R is an 3x3 rotation matrix, t 3x1 translation, s scale.
i.e. solves the orthogonal Procrutes problem.
'''
transposed = False
if S1.shape[0] != 3 and S1.shape[0] != 2:
S1 = S1.permute(0,2,1)
S2 = S2.permute(0,2,1)
transposed = True
assert(S2.shape[1] == S1.shape[1])
# 1. Remove mean.
mu1 = S1.mean(axis=-1, keepdims=True)
mu2 = S2.mean(axis=-1, keepdims=True)
X1 = S1 - mu1
X2 = S2 - mu2
# 2. Compute variance of X1 used for scale.
var1 = torch.sum(X1**2, dim=1).sum(dim=1)
# 3. The outer product of X1 and X2.
K = X1.bmm(X2.permute(0,2,1))
# 4. Solution that Maximizes trace(R'K) is R=U*V', where U, V are
# singular vectors of K.
U, s, V = torch.svd(K)
# Construct Z that fixes the orientation of R to get det(R)=1.
Z = torch.eye(U.shape[1], device=S1.device, dtype=S1.dtype).unsqueeze(0)
Z = Z.repeat(U.shape[0],1,1)
t1 = U.bmm(V.permute(0,2,1))
t2 = torch.det(t1)
Z[:,-1, -1] = Z[:,-1, -1] * torch.sign(t2)
# Z[:,-1, -1] *= torch.sign(torch.det(U.bmm(V.permute(0,2,1))))
# Construct R.
R = V.bmm(Z.bmm(U.permute(0,2,1)))
# 5. Recover scale.
scale = torch.cat([torch.trace(x).unsqueeze(0) for x in R.bmm(K)]) / var1
# 6. Recover translation.
t = mu2 - (scale.unsqueeze(-1).unsqueeze(-1) * (R.bmm(mu1)))
# 7. Error:
S1_hat = scale.unsqueeze(-1).unsqueeze(-1) * R.bmm(S1) + t
if transposed:
S1_hat = S1_hat.permute(0,2,1)
return S1_hat
def align_by_pelvis(self, joints, format='lsp'):
"""
Assumes joints is 14 x 3 in LSP order.
Then hips are: [3, 2]
Takes mid point of these points, then subtracts it.
"""
if format == 'lsp':
left_id = 3
right_id = 2
pelvis = (joints[:,left_id, :] + joints[:,right_id, :]) / 2.
elif format in ['smpl', 'h36m']:
pelvis_id = 0
pelvis = joints[pelvis_id, :]
elif format in ['mpi']:
pelvis_id = 14
pelvis = joints[pelvis_id, :]
return joints - pelvis[:,None,:].repeat(1, 14, 1)