-
Notifications
You must be signed in to change notification settings - Fork 160
/
slam_helpers.py
251 lines (206 loc) · 8.9 KB
/
slam_helpers.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import torch
import torch.nn.functional as F
from utils.slam_external import build_rotation
def l1_loss_v1(x, y):
return torch.abs((x - y)).mean()
def l1_loss_v2(x, y):
return (torch.abs(x - y).sum(-1)).mean()
def weighted_l2_loss_v1(x, y, w):
return torch.sqrt(((x - y) ** 2) * w + 1e-20).mean()
def weighted_l2_loss_v2(x, y, w):
return torch.sqrt(((x - y) ** 2).sum(-1) * w + 1e-20).mean()
def quat_mult(q1, q2):
w1, x1, y1, z1 = q1.T
w2, x2, y2, z2 = q2.T
w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2
x = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2
y = w1 * y2 - x1 * z2 + y1 * w2 + z1 * x2
z = w1 * z2 + x1 * y2 - y1 * x2 + z1 * w2
return torch.stack([w, x, y, z]).T
def _sqrt_positive_part(x: torch.Tensor) -> torch.Tensor:
"""
Returns torch.sqrt(torch.max(0, x))
but with a zero subgradient where x is 0.
Source: https://pytorch3d.readthedocs.io/en/latest/_modules/pytorch3d/transforms/rotation_conversions.html#matrix_to_quaternion
"""
ret = torch.zeros_like(x)
positive_mask = x > 0
ret[positive_mask] = torch.sqrt(x[positive_mask])
return ret
def matrix_to_quaternion(matrix: torch.Tensor) -> torch.Tensor:
"""
Convert rotations given as rotation matrices to quaternions.
Args:
matrix: Rotation matrices as tensor of shape (..., 3, 3).
Returns:
quaternions with real part first, as tensor of shape (..., 4).
Source: https://pytorch3d.readthedocs.io/en/latest/_modules/pytorch3d/transforms/rotation_conversions.html#matrix_to_quaternion
"""
if matrix.size(-1) != 3 or matrix.size(-2) != 3:
raise ValueError(f"Invalid rotation matrix shape {matrix.shape}.")
batch_dim = matrix.shape[:-2]
m00, m01, m02, m10, m11, m12, m20, m21, m22 = torch.unbind(
matrix.reshape(batch_dim + (9,)), dim=-1
)
q_abs = _sqrt_positive_part(
torch.stack(
[
1.0 + m00 + m11 + m22,
1.0 + m00 - m11 - m22,
1.0 - m00 + m11 - m22,
1.0 - m00 - m11 + m22,
],
dim=-1,
)
)
# we produce the desired quaternion multiplied by each of r, i, j, k
quat_by_rijk = torch.stack(
[
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and
# `int`.
torch.stack([q_abs[..., 0] ** 2, m21 - m12, m02 - m20, m10 - m01], dim=-1),
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and
# `int`.
torch.stack([m21 - m12, q_abs[..., 1] ** 2, m10 + m01, m02 + m20], dim=-1),
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and
# `int`.
torch.stack([m02 - m20, m10 + m01, q_abs[..., 2] ** 2, m12 + m21], dim=-1),
# pyre-fixme[58]: `**` is not supported for operand types `Tensor` and
# `int`.
torch.stack([m10 - m01, m20 + m02, m21 + m12, q_abs[..., 3] ** 2], dim=-1),
],
dim=-2,
)
# We floor here at 0.1 but the exact level is not important; if q_abs is small,
# the candidate won't be picked.
flr = torch.tensor(0.1).to(dtype=q_abs.dtype, device=q_abs.device)
quat_candidates = quat_by_rijk / (2.0 * q_abs[..., None].max(flr))
# if not for numerical problems, quat_candidates[i] should be same (up to a sign),
# forall i; we pick the best-conditioned one (with the largest denominator)
return quat_candidates[
F.one_hot(q_abs.argmax(dim=-1), num_classes=4) > 0.5, :
].reshape(batch_dim + (4,))
def params2rendervar(params):
rendervar = {
'means3D': params['means3D'],
'colors_precomp': params['rgb_colors'],
'rotations': F.normalize(params['unnorm_rotations']),
'opacities': torch.sigmoid(params['logit_opacities']),
'scales': torch.exp(torch.tile(params['log_scales'], (1, 3))),
'means2D': torch.zeros_like(params['means3D'], requires_grad=True, device="cuda") + 0
}
return rendervar
def transformed_params2rendervar(params, transformed_pts):
rendervar = {
'means3D': transformed_pts,
'colors_precomp': params['rgb_colors'],
'rotations': F.normalize(params['unnorm_rotations']),
'opacities': torch.sigmoid(params['logit_opacities']),
'scales': torch.exp(torch.tile(params['log_scales'], (1, 3))),
'means2D': torch.zeros_like(params['means3D'], requires_grad=True, device="cuda") + 0
}
return rendervar
def project_points(points_3d, intrinsics):
"""
Function to project 3D points to image plane.
params:
points_3d: [num_gaussians, 3]
intrinsics: [3, 3]
out: [num_gaussians, 2]
"""
points_2d = torch.matmul(intrinsics, points_3d.transpose(0, 1))
points_2d = points_2d.transpose(0, 1)
points_2d = points_2d / points_2d[:, 2:]
points_2d = points_2d[:, :2]
return points_2d
def params2silhouette(params):
sil_color = torch.zeros_like(params['rgb_colors'])
sil_color[:, 0] = 1.0
rendervar = {
'means3D': params['means3D'],
'colors_precomp': sil_color,
'rotations': F.normalize(params['unnorm_rotations']),
'opacities': torch.sigmoid(params['logit_opacities']),
'scales': torch.exp(torch.tile(params['log_scales'], (1, 3))),
'means2D': torch.zeros_like(params['means3D'], requires_grad=True, device="cuda") + 0
}
return rendervar
def transformed_params2silhouette(params, transformed_pts):
sil_color = torch.zeros_like(params['rgb_colors'])
sil_color[:, 0] = 1.0
rendervar = {
'means3D': transformed_pts,
'colors_precomp': sil_color,
'rotations': F.normalize(params['unnorm_rotations']),
'opacities': torch.sigmoid(params['logit_opacities']),
'scales': torch.exp(torch.tile(params['log_scales'], (1, 3))),
'means2D': torch.zeros_like(params['means3D'], requires_grad=True, device="cuda") + 0
}
return rendervar
def get_depth_and_silhouette(pts_3D, w2c):
"""
Function to compute depth and silhouette for each gaussian.
These are evaluated at gaussian center.
"""
# Depth of each gaussian center in camera frame
pts4 = torch.cat((pts_3D, torch.ones_like(pts_3D[:, :1])), dim=-1)
pts_in_cam = (w2c @ pts4.transpose(0, 1)).transpose(0, 1)
depth_z = pts_in_cam[:, 2].unsqueeze(-1) # [num_gaussians, 1]
depth_z_sq = torch.square(depth_z) # [num_gaussians, 1]
# Depth and Silhouette
depth_silhouette = torch.zeros((pts_3D.shape[0], 3)).cuda().float()
depth_silhouette[:, 0] = depth_z.squeeze(-1)
depth_silhouette[:, 1] = 1.0
depth_silhouette[:, 2] = depth_z_sq.squeeze(-1)
return depth_silhouette
def params2depthplussilhouette(params, w2c):
rendervar = {
'means3D': params['means3D'],
'colors_precomp': get_depth_and_silhouette(params['means3D'], w2c),
'rotations': F.normalize(params['unnorm_rotations']),
'opacities': torch.sigmoid(params['logit_opacities']),
'scales': torch.exp(torch.tile(params['log_scales'], (1, 3))),
'means2D': torch.zeros_like(params['means3D'], requires_grad=True, device="cuda") + 0
}
return rendervar
def transformed_params2depthplussilhouette(params, w2c, transformed_pts):
rendervar = {
'means3D': transformed_pts,
'colors_precomp': get_depth_and_silhouette(transformed_pts, w2c),
'rotations': F.normalize(params['unnorm_rotations']),
'opacities': torch.sigmoid(params['logit_opacities']),
'scales': torch.exp(torch.tile(params['log_scales'], (1, 3))),
'means2D': torch.zeros_like(params['means3D'], requires_grad=True, device="cuda") + 0
}
return rendervar
def transform_to_frame(params, time_idx, gaussians_grad, camera_grad):
"""
Function to transform Isotropic Gaussians from world frame to camera frame.
Args:
params: dict of parameters
time_idx: time index to transform to
gaussians_grad: enable gradients for Gaussians
camera_grad: enable gradients for camera pose
Returns:
transformed_pts: Transformed Centers of Gaussians
"""
# Get Frame Camera Pose
if camera_grad:
cam_rot = F.normalize(params['cam_unnorm_rots'][..., time_idx])
cam_tran = params['cam_trans'][..., time_idx]
else:
cam_rot = F.normalize(params['cam_unnorm_rots'][..., time_idx].detach())
cam_tran = params['cam_trans'][..., time_idx].detach()
rel_w2c = torch.eye(4).cuda().float()
rel_w2c[:3, :3] = build_rotation(cam_rot)
rel_w2c[:3, 3] = cam_tran
# Get Centers and norm Rots of Gaussians in World Frame
if gaussians_grad:
pts = params['means3D']
else:
pts = params['means3D'].detach()
# Transform Centers and Unnorm Rots of Gaussians to Camera Frame
pts_ones = torch.ones(pts.shape[0], 1).cuda().float()
pts4 = torch.cat((pts, pts_ones), dim=1)
transformed_pts = (rel_w2c @ pts4.T).T[:, :3]
return transformed_pts