-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathModel.py
More file actions
463 lines (400 loc) · 23.1 KB
/
Copy pathModel.py
File metadata and controls
463 lines (400 loc) · 23.1 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""HTGS/Model.py: Implementation of the model for the HTGS method."""
import torch
import numpy as np
import Framework
from Cameras.Perspective import PerspectiveCamera
from Datasets.Base import BaseDataset
from Datasets.utils import BasicPointCloud
from Cameras.utils import quaternion_to_rotation_matrix
from Logging import Logger
from Methods.Base.Model import BaseModel
from Optim.adam_utils import replace_param_group_data, prune_param_groups, extend_param_groups
from Optim.lr_utils import LRDecayPolicy
from Optim.knn_utils import compute_root_mean_squared_knn_distances
from CudaUtils.MortonEncoding import morton_encode
from Methods.HTGS.HTGSCudaBackend import update_3d_filter
class Gaussians(torch.nn.Module):
"""Stores a set of points with 3D Gaussian extent."""
GOF_DENSIFICATION_GRAD = True
GOF_DENSIFICATION_CLONE = True
def __init__(self, sh_degree: int, pretrained: bool) -> None:
super().__init__()
self.active_sh_degree = sh_degree if pretrained else 0
self.active_sh_bases = (self.active_sh_degree + 1) ** 2
self.max_sh_degree = sh_degree
self.register_parameter('_positions', None)
self.register_parameter('_sh_0', None)
self.register_parameter('_sh_rest', None)
self.register_parameter('_scales', None)
self.register_parameter('_rotations', None)
self.register_parameter('_opacities', None)
self.densification_info = torch.empty(0)
self.optimizer = None
self.percent_dense = 0
self.training_cameras_extent = 1.0
self.filter_3D = torch.empty(0)
self.use_3d_filter = False
self.distance2filter = 0
# activation functions
self.scale_activation = torch.nn.Identity() if pretrained else torch.exp
self.inverse_scale_activation = torch.nn.Identity() if pretrained else torch.log
self.opacity_activation = torch.nn.Identity() if pretrained else torch.special.expit
self.inverse_opacity_activation = torch.nn.Identity() if pretrained else torch.special.logit
self.rotation_activation = torch.nn.Identity() if pretrained else torch.nn.functional.normalize
@property
def get_scales(self) -> torch.Tensor:
"""Returns the Gaussians' scales."""
return self.scale_activation(self._scales)
@property
def get_rotations(self) -> torch.Tensor:
"""Returns the Gaussians' rotations as quaternions."""
return self.rotation_activation(self._rotations)
@property
def get_positions(self) -> torch.Tensor:
"""Returns the Gaussians' means."""
return self._positions
@property
def get_sh_0(self) -> torch.Tensor:
"""Returns the Gaussians' 0-th degree SH features."""
return self._sh_0
@property
def get_sh_rest(self) -> torch.Tensor:
"""Returns the Gaussians' SH features beyond the 0-th degree."""
return self._sh_rest
@property
def get_opacities(self) -> torch.Tensor:
"""Returns the Gaussians' opacities."""
return self.opacity_activation(self._opacities)
@property
def get_opacities_with_3D_filter(self) -> torch.Tensor:
"""Returns the Gaussians' opacities with the 3D filter applied."""
# apply 3D filter
scales = self.get_scales
scales_square = torch.square(scales)
det1 = scales_square.prod(dim=1)
scales_after_square = scales_square + torch.square(self.filter_3D)
det2 = scales_after_square.prod(dim=1)
coef = torch.sqrt(det1 / det2)
return self.get_opacities * coef[..., None]
@property
def get_scales_with_3D_filter(self) -> torch.Tensor:
"""Returns the Gaussians' scales with the 3D filter applied."""
scales = self.get_scales
# apply 3D filter
scales = torch.square(scales) + torch.square(self.filter_3D)
scales = torch.sqrt(scales)
return scales
@property
def get_densification_info(self) -> torch.Tensor:
"""Returns the current densification info buffers."""
return self.densification_info
def setup_3d_filter(self, dataset: 'BaseDataset', dilation: float = 0.2) -> None:
"""Sets up a 3D filter (see https://arxiv.org/abs/2311.16493)."""
self.use_3d_filter = True
max_focal = 1.0e-12
for view in dataset:
if not isinstance(view.camera, PerspectiveCamera):
raise Framework.ModelError('3d filter only supports perspective cameras')
if view.camera.distortion is not None:
Logger.log_warning('3d filter ignores all distortion parameters')
max_focal = max(max_focal, max(view.camera.focal_x, view.camera.focal_y))
# assume max_focal is focal length of the highest resolution camera
self.distance2filter = dilation ** 0.5 / max_focal
self.compute_3d_filter(dataset)
def compute_3d_filter(self, dataset: 'BaseDataset', clipping_tolerance: float = 0.15) -> None:
"""Computes the 3D filter."""
positions = self.get_positions.contiguous()
filter_3d = torch.full((positions.shape[0], 1), fill_value=torch.finfo(torch.float32).max, device=positions.device, dtype=torch.float32)
visibility_mask = torch.zeros((positions.shape[0], 1), device=positions.device, dtype=torch.bool)
for view in dataset:
if not isinstance(view.camera, PerspectiveCamera):
raise Framework.ModelError('3d filter only supports perspective cameras')
if view.camera.distortion is not None:
Logger.log_warning('3d filter ignores all distortion parameters')
update_3d_filter(
positions,
view.w2c,
filter_3d,
visibility_mask,
view.camera.width,
view.camera.height,
view.camera.focal_x,
view.camera.focal_y,
view.camera.center_x,
view.camera.center_y,
view.camera.near_plane,
clipping_tolerance,
self.distance2filter,
)
filter_3d_max = filter_3d[visibility_mask].max()
filter_3d = torch.where(visibility_mask, filter_3d, filter_3d_max, out=filter_3d)
self.filter_3D = filter_3d
def increase_used_sh_degree(self) -> None:
"""Increases the used SH degree."""
if self.active_sh_degree < self.max_sh_degree:
self.active_sh_degree += 1
self.active_sh_bases = (self.active_sh_degree + 1) ** 2
def initialize_from_point_cloud(self, point_cloud: BasicPointCloud, training_cameras_extent: float) -> None:
"""Initializes the model from a point cloud."""
self.training_cameras_extent = training_cameras_extent
positions = point_cloud.positions.cuda()
rgbs = torch.full_like(positions, fill_value=0.5) if point_cloud.colors is None else point_cloud.colors.cuda()
n_initial_points = positions.shape[0]
sh_all = torch.zeros((n_initial_points, (self.max_sh_degree + 1) ** 2, 3), dtype=torch.float32, device='cuda')
sh_all[:, 0] = (rgbs - 0.5) / 0.28209479177387814
Logger.log_info(f'Number of points at initialization: {n_initial_points:,}')
distances = compute_root_mean_squared_knn_distances(positions)
scales = self.inverse_scale_activation(distances)[..., None].repeat(1, 3)
rotations = torch.zeros((n_initial_points, 4), dtype=torch.float32, device='cuda')
rotations[:, 0] = 1.0
opacities = self.inverse_opacity_activation(torch.full((n_initial_points, 1), fill_value=0.1, dtype=torch.float32, device='cuda'))
self._positions = torch.nn.Parameter(positions.contiguous())
self._sh_0 = torch.nn.Parameter(sh_all[:, 0:1].contiguous())
self._sh_rest = torch.nn.Parameter(sh_all[:, 1:].contiguous())
self._scales = torch.nn.Parameter(scales.contiguous())
self._rotations = torch.nn.Parameter(rotations.contiguous())
self._opacities = torch.nn.Parameter(opacities.contiguous())
self.reset_densification_info()
def training_setup(self, training_wrapper) -> None:
"""Sets up the optimizer."""
self.percent_dense = training_wrapper.PERCENT_DENSE
param_groups = [
{'params': [self._positions], 'lr': training_wrapper.LEARNING_RATE_POSITION_INIT * self.training_cameras_extent, 'name': 'positions'},
{'params': [self._sh_0], 'lr': training_wrapper.LEARNING_RATE_FEATURE, 'name': 'sh_0'},
{'params': [self._sh_rest], 'lr': training_wrapper.LEARNING_RATE_FEATURE / 20.0, 'name': 'sh_rest'},
{'params': [self._opacities], 'lr': training_wrapper.LEARNING_RATE_OPACITY, 'name': 'opacities'},
{'params': [self._scales], 'lr': training_wrapper.LEARNING_RATE_SCALING, 'name': 'scales'},
{'params': [self._rotations], 'lr': training_wrapper.LEARNING_RATE_ROTATION, 'name': 'rotations'}
]
try:
from Thirdparty.Apex import FusedAdam
# slightly faster than the PyTorch implementation
self.optimizer = FusedAdam(param_groups, lr=0.0, eps=1e-15, adam_w_mode=False)
Logger.log_info('using apex FusedAdam')
except Framework.ExtensionError:
Logger.log_warning('apex is not installed -> using the slightly slower PyTorch Adam instead')
Logger.log_warning('apex can be installed using ./scripts/install.py -e src/Thirdparty/Apex.py')
self.optimizer = torch.optim.Adam(param_groups, lr=0.0, eps=1e-15, fused=True)
self.position_lr_scheduler = LRDecayPolicy(
lr_init=training_wrapper.LEARNING_RATE_POSITION_INIT * self.training_cameras_extent,
lr_final=training_wrapper.LEARNING_RATE_POSITION_FINAL * self.training_cameras_extent,
max_steps=training_wrapper.LEARNING_RATE_POSITION_MAX_STEPS)
def update_learning_rate(self, iteration: int) -> None:
""" Learning rate scheduling per step """
for param_group in self.optimizer.param_groups:
if param_group['name'] == 'positions':
lr = self.position_lr_scheduler(iteration)
param_group['lr'] = lr
def reset_opacities(self, max_opacity: float) -> None:
"""Resets the opacities to a fixed value."""
current_opacities = self.get_opacities_with_3D_filter if self.use_3d_filter else self.get_opacities
opacities_new = current_opacities.clamp_max(max_opacity)
if self.use_3d_filter:
# make sure that the current 3d filter has the same effect on the new opacities
scales_square = torch.square(self.get_scales)
det1 = scales_square.prod(dim=1)
scales_after_square = scales_square + torch.square(self.filter_3D)
det2 = scales_after_square.prod(dim=1)
coef = torch.sqrt(det1 / det2)
opacities_new = opacities_new / coef[..., None]
opacities_new = self.inverse_opacity_activation(opacities_new)
replace_param_group_data(self.optimizer, opacities_new, 'opacities')
def decay_opacities(self, decay_factor: float):
"""Decays the opacities by a factor."""
opacities_new = self.inverse_opacity_activation(self.get_opacities * decay_factor)
replace_param_group_data(self.optimizer, opacities_new, 'opacities')
# these lines match the behavior of reset_opacities, but aren't necessary as the decay is only a small change
# current_opacities = self.get_opacities_with_3D_filter if self.use_3d_filter else self.get_opacities
# opacities_new = current_opacities * decay_factor
# if self.use_3d_filter:
# # make sure that the current 3d filter has the same effect on the new opacities
# scales = self.get_scales
# scales_square = torch.square(scales)
# det1 = scales_square.prod(dim=1)
# scales_after_square = scales_square + torch.square(self.filter_3D)
# det2 = scales_after_square.prod(dim=1)
# coef = torch.sqrt(det1 / det2)
# opacities_new = opacities_new / coef[..., None]
# opacities_new = self.inverse_opacity_activation(opacities_new)
# replace_param_group_data(self.optimizer, opacities_new, 'opacities')
def prune_points(self, prune_mask: torch.Tensor) -> None:
"""Prunes points that are not visible or too large."""
valid_mask = ~prune_mask
optimizable_tensors = prune_param_groups(self.optimizer, valid_mask)
self._positions = optimizable_tensors['positions']
self._sh_0 = optimizable_tensors['sh_0']
self._sh_rest = optimizable_tensors['sh_rest']
self._opacities = optimizable_tensors['opacities']
self._scales = optimizable_tensors['scales']
self._rotations = optimizable_tensors['rotations']
def densification_postfix(
self,
new_positions: torch.Tensor,
new_sh_0: torch.Tensor,
new_sh_rest: torch.Tensor,
new_opacities: torch.Tensor,
new_scales: torch.Tensor,
new_rotations: torch.Tensor
) -> None:
"""Incorporate the changes from the densification step into the parameter groups."""
optimizable_tensors = extend_param_groups(self.optimizer, {
'positions': new_positions,
'sh_0': new_sh_0,
'sh_rest': new_sh_rest,
'opacities': new_opacities,
'scales': new_scales,
'rotations': new_rotations
})
self._positions = optimizable_tensors['positions']
self._sh_0 = optimizable_tensors['sh_0']
self._sh_rest = optimizable_tensors['sh_rest']
self._opacities = optimizable_tensors['opacities']
self._scales = optimizable_tensors['scales']
self._rotations = optimizable_tensors['rotations']
def reset_densification_info(self):
n_points = self._positions.shape[0]
n_floats = 3 if Gaussians.GOF_DENSIFICATION_GRAD else 2
self.densification_info = torch.zeros((n_floats, n_points, 1), dtype=torch.float32, device='cuda')
def split(self, grads: torch.Tensor, grad_threshold: float, grads_abs: torch.Tensor | None, grad_abs_threshold: float | None) -> torch.Tensor:
"""Densify by splitting Gaussians that satisfy the gradient condition."""
n_init_points = self.get_positions.shape[0]
# Extract points that satisfy the gradient condition
padded_grad = torch.zeros(n_init_points, dtype=torch.float32, device='cuda')
padded_grad[:grads.shape[0]] = grads.squeeze()
selected_pts_mask = padded_grad >= grad_threshold
if grads_abs is not None:
padded_grad_abs = torch.zeros(n_init_points, dtype=torch.float32, device='cuda')
padded_grad_abs[:grads_abs.shape[0]] = grads_abs.squeeze()
selected_pts_mask |= padded_grad_abs >= grad_abs_threshold
selected_pts_mask &= torch.max(self.get_scales, dim=1).values > self.percent_dense * self.training_cameras_extent
stds = self.get_scales[selected_pts_mask].repeat(2, 1)
samples = torch.normal(mean=0.0, std=stds)
rots = quaternion_to_rotation_matrix(self._rotations[selected_pts_mask]).repeat(2, 1, 1)
new_positions = torch.bmm(rots, samples.unsqueeze(-1)).squeeze(-1) + self.get_positions[selected_pts_mask].repeat(2, 1)
new_scales = self.inverse_scale_activation(self.get_scales[selected_pts_mask].repeat(2, 1) / 1.6)
new_rotations = self._rotations[selected_pts_mask].repeat(2, 1)
new_sh_0 = self._sh_0[selected_pts_mask].repeat(2, 1, 1)
new_sh_rest = self._sh_rest[selected_pts_mask].repeat(2, 1, 1)
new_opacities = self._opacities[selected_pts_mask].repeat(2, 1)
self.densification_postfix(new_positions, new_sh_0, new_sh_rest, new_opacities, new_scales, new_rotations)
prune_filter = torch.cat((selected_pts_mask, torch.zeros(2 * selected_pts_mask.sum().item(), device='cuda', dtype=torch.bool)))
return prune_filter
def duplicate(self, grads: torch.Tensor, grad_threshold: float, grads_abs: torch.Tensor | None, grad_abs_threshold: float | None) -> None:
"""Densify by duplicating Gaussians that satisfy the gradient condition."""
# Extract points that satisfy the gradient condition
selected_pts_mask = grads.flatten() >= grad_threshold
if grads_abs is not None:
selected_pts_mask |= grads_abs.flatten() >= grad_abs_threshold
selected_pts_mask &= torch.max(self.get_scales, dim=1).values <= self.percent_dense * self.training_cameras_extent
if Gaussians.GOF_DENSIFICATION_CLONE:
# sample a new gaussian instead of fixing position (from gof)
stds = self.get_scales[selected_pts_mask]
samples = torch.normal(mean=0.0, std=stds)
rots = quaternion_to_rotation_matrix(self._rotations[selected_pts_mask])
new_positions = torch.bmm(rots, samples.unsqueeze(-1)).squeeze(-1) + self.get_positions[selected_pts_mask]
else:
new_positions = self._positions[selected_pts_mask] # 3dgs
new_sh_0 = self._sh_0[selected_pts_mask]
new_sh_rest = self._sh_rest[selected_pts_mask]
new_opacities = self._opacities[selected_pts_mask]
new_scales = self._scales[selected_pts_mask]
new_rotations = self._rotations[selected_pts_mask]
self.densification_postfix(new_positions, new_sh_0, new_sh_rest, new_opacities, new_scales, new_rotations)
def densify_and_prune(self, grad_threshold: float, min_opacity: float, prune_large_gaussians: bool) -> None:
"""Densifies the point cloud and prunes points that are not visible or too large."""
denominator = self.densification_info[0].clamp_min(1.0)
grads = self.densification_info[1] / denominator
grads_abs, grad_abs_threshold = None, None
if Gaussians.GOF_DENSIFICATION_GRAD:
grads_abs = self.densification_info[2] / denominator
ratio = (grads.flatten() >= grad_threshold).float().mean()
grad_abs_threshold = torch.quantile(grads_abs.flatten(), 1.0 - ratio).item()
self.duplicate(grads, grad_threshold, grads_abs, grad_abs_threshold)
prune_mask = self.split(grads, grad_threshold, grads_abs, grad_abs_threshold)
prune_mask |= self.get_opacities.flatten() < min_opacity
if prune_large_gaussians:
prune_mask |= self.get_scales.max(dim=1).values > 0.1 * self.training_cameras_extent
self.prune_points(prune_mask)
self.reset_densification_info()
torch.cuda.empty_cache()
def importance_pruning(self, max_blending_weights: torch.Tensor, threshold: float) -> None:
"""Prunes points based on the maximum blending weights."""
mask = max_blending_weights < threshold
self.prune_points(mask)
if self.use_3d_filter:
self.filter_3D = self.filter_3D[~mask].contiguous()
self.densification_info = self.densification_info[:, ~mask].contiguous()
def bake_activations(self):
"""Bakes relevant activation functions into the final parameters."""
# bake activation functions into final parameters
self._rotations.data = self.get_rotations
self.rotation_activation = torch.nn.Identity()
# Important: opacities must be baked before scales due to implementation of get_opacities_with_3D_filter
self._opacities.data = self.get_opacities_with_3D_filter if self.use_3d_filter else self.get_opacities
self.opacity_activation = torch.nn.Identity()
self.inverse_opacity_activation = torch.nn.Identity()
self._scales.data = self.get_scales_with_3D_filter if self.use_3d_filter else self.get_scales
self.scale_activation = torch.nn.Identity()
self.inverse_scale_activation = torch.nn.Identity()
# 3d filter is baked into relevant parameters now
self.use_3d_filter = False
# prune points that would never be visible anyway
self.prune_points((self._opacities < 0.00392156862).squeeze()) # 1/255
# morton sort
morton_encoding = morton_encode(self._positions)
order = torch.argsort(morton_encoding)
self._positions.data = self._positions[order].contiguous()
self._rotations.data = self._rotations[order].contiguous()
self._sh_0.data = self._sh_0[order].contiguous()
self._sh_rest.data = self._sh_rest[order].contiguous()
self._scales.data = self._scales[order].contiguous()
self._opacities.data = self._opacities[order].contiguous()
@torch.no_grad()
def as_ply_dict(self) -> dict[str, np.ndarray]:
"""Returns the model as a ply-compatible dictionary using structured numpy arrays."""
if self.get_positions.shape[0] == 0:
return {}
# construct attributes
positions = self.get_positions.detach().contiguous().cpu().numpy()
sh_0 = self.get_sh_0.detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
sh_rest = self.get_sh_rest.detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
opacities = self.get_opacities.logit().detach().contiguous().cpu().numpy() # most viewers expect unactivated opacities
scales = self.get_scales.log().detach().contiguous().cpu().numpy() # most viewers expect unactivated scales
rotations = self.get_rotations.detach().contiguous().cpu().numpy()
attributes = np.concatenate((positions, sh_0, sh_rest, opacities, scales, rotations), axis=1)
# construct structured array
attribute_names = (
['x', 'y', 'z'] # 3d mean
+ ['f_dc_0', 'f_dc_1', 'f_dc_2'] # 0-th SH degree coefficients
+ [f'f_rest_{i}' for i in range(sh_rest.shape[-1])] # remaining SH degree coefficients
+ ['opacity'] # opacity (pre-activation)
+ ['scale_0', 'scale_1', 'scale_2'] # 3d scale (pre-activation)
+ ['rot_0', 'rot_1', 'rot_2', 'rot_3'] # rotation quaternion
)
dtype = 'f4' # store all attributes as float32 for compatibility
full_dtype = [(attribute_name, dtype) for attribute_name in attribute_names]
vertices = np.empty(positions.shape[0], dtype=full_dtype)
# insert attributes into structured array
vertices[:] = list(map(tuple, attributes))
return {'vertex': vertices}
@Framework.Configurable.configure(
SH_DEGREE=3,
)
class HTGSModel(BaseModel):
"""Defines the HTGS model."""
def __init__(self, name: str = None) -> None:
super().__init__(name)
self.gaussians: Gaussians | None = None
def build(self) -> 'HTGSModel':
"""Builds the model."""
pretrained = self.num_iterations_trained > 0
self.gaussians = Gaussians(self.SH_DEGREE, pretrained)
return self
def get_ply_dict(self) -> dict[str, np.ndarray | list[str]]:
"""Returns the model as a ply-compatible dictionary using structured numpy arrays."""
data: dict[str, np.ndarray | list[str]] = {}
if self.gaussians is None or not (data := self.gaussians.as_ply_dict()):
return data
# add method-specific comments
data['comments'] = ['SplatRenderMode: default', 'Generated with NeRFICG/HTGS']
return data