-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple_gaussian.py
More file actions
670 lines (588 loc) · 25.2 KB
/
simple_gaussian.py
File metadata and controls
670 lines (588 loc) · 25.2 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
import os
import pickle
import sys
from argparse import ArgumentParser
from errno import EEXIST
from os import makedirs, path
import numpy as np
import torch
from plyfile import PlyData, PlyElement
from torch import nn
sys.path.append("./")
from simple_knn._C import distCUDA2
from simple_gaussian_utils import (build_rotation, build_scaling_rotation,
get_expon_lr_func, inverse_sigmoid,
inverse_softplus, strip_symmetric)
EPS = 1e-5
def t2a(tensor):
if torch.is_tensor(tensor):
return tensor.detach().cpu().numpy()
else:
return tensor
def mkdir_p(folder_path):
# Creates a directory. equivalent to using mkdir -p on the command line
try:
makedirs(folder_path)
except OSError as exc: # Python >2.5
if exc.errno == EEXIST and path.isdir(folder_path):
pass
else:
raise
class GroupParams:
pass
class ParamGroup:
def __init__(self, parser: ArgumentParser, name: str, fill_none=False):
group = parser.add_argument_group(name)
for key, value in vars(self).items():
shorthand = False
if key.startswith("_"):
shorthand = True
key = key[1:]
t = type(value)
value = value if not fill_none else None
if shorthand:
if t == bool:
group.add_argument(
"--" + key, ("-" + key[0:1]), default=value, action="store_true"
)
else:
group.add_argument(
"--" + key, ("-" + key[0:1]), default=value, type=t
)
else:
if t == bool:
group.add_argument("--" + key, default=value, action="store_true")
else:
group.add_argument("--" + key, default=value, type=t)
def extract(self, args):
group = GroupParams()
for arg in vars(args).items():
if arg[0] in vars(self) or ("_" + arg[0]) in vars(self):
setattr(group, arg[0], arg[1])
return group
class OptimizationParams():
def __init__(self):
self.iterations = 30_000
self.position_lr_init = 0.0002
self.position_lr_final = 0.00002
self.position_lr_max_steps = 30_000
self.density_lr_init = 0.01
self.density_lr_final = 0.001
self.density_lr_max_steps = 30_000
self.scaling_lr_init = 0.005
self.scaling_lr_final = 0.0005
self.scaling_lr_max_steps = 30_000
self.rotation_lr_init = 0.001
self.rotation_lr_final = 0.0001
self.rotation_lr_max_steps = 30_000
self.lambda_dssim = 0.25
self.lambda_tv = 0.05
self.tv_vol_size = 32
self.density_min_threshold = 0.00001
self.densification_interval = 100
self.densify_from_iter = 500
self.densify_until_iter = 15000
self.densify_grad_threshold = 5.0e-5
self.densify_scale_threshold = 0.1 # percent of volume size
self.max_screen_size = None
self.max_scale = None # percent of volume size
self.max_num_gaussians = 5_000_000
class GaussianModel:
def setup_functions(self):
def build_covariance_from_scaling_rotation(scaling, scaling_modifier, rotation):
L = build_scaling_rotation(scaling_modifier * scaling, rotation)
actual_covariance = L @ L.transpose(1, 2)
symm = strip_symmetric(actual_covariance)
return symm
if self.scale_bound is not None:
scale_min_bound, scale_max_bound = self.scale_bound
assert (
scale_min_bound < scale_max_bound
), "scale_min must be smaller than scale_max."
self.scaling_activation = (
lambda x: torch.sigmoid(x) * (scale_max_bound - scale_min_bound)
+ scale_min_bound
)
self.scaling_inverse_activation = lambda x: inverse_sigmoid(
torch.relu((x - scale_min_bound) / (scale_max_bound - scale_min_bound))
)
else:
self.scaling_activation = torch.exp
self.scaling_inverse_activation = torch.log
self.covariance_activation = build_covariance_from_scaling_rotation
self.density_activation = torch.nn.Softplus() # use softplus for [0, +inf]
self.density_inverse_activation = inverse_softplus
self.rotation_activation = torch.nn.functional.normalize
def __init__(self, scale_bound=None, num_points=100000):
self._xyz = torch.empty(0) # world coordinate
self._scaling = torch.empty(0) # 3d scale
self._rotation = torch.empty(0) # rotation expressed in quaternions
self._density = torch.empty(0) # density
self.max_radii2D = torch.empty(0)
self.xyz_gradient_accum = torch.empty(0)
self.denom = torch.empty(0)
self.optimizer = None
self.spatial_lr_scale = 0
self.scale_bound = scale_bound
self.num_points = num_points
self.setup_functions()
self.create_from_pcd()
def capture(self):
return (
self._xyz,
self._scaling,
self._rotation,
self._density,
self.max_radii2D,
self.xyz_gradient_accum,
self.denom,
self.optimizer.state_dict(),
self.spatial_lr_scale,
self.scale_bound,
)
def restore(self, model_args, training_args):
(
self._xyz,
self._scaling,
self._rotation,
self._density,
self.max_radii2D,
xyz_gradient_accum,
denom,
opt_dict,
self.spatial_lr_scale,
self.scale_bound,
) = model_args
self.training_setup(training_args)
self.xyz_gradient_accum = xyz_gradient_accum
self.denom = denom
self.optimizer.load_state_dict(opt_dict)
self.setup_functions() # Reset activation functions
@property
def get_scaling(self): # used
return self.scaling_activation(self._scaling)
# activated_isotropic_scale = self.scaling_activation(self._scaling) # self._scaling is (N, 1), so result is (N, 1)
# return activated_isotropic_scale.repeat(1, 3) # Repeat to (N, 3)
@property
def get_rotation(self): # used
return self.rotation_activation(self._rotation)
@property
def get_xyz(self): # used
return self._xyz
@property
def get_density(self): # used
return self.density_activation(self._density)
def get_covariance(self, scaling_modifier=1):
return self.covariance_activation(
self.get_scaling, scaling_modifier, self._rotation
)
def create_from_pcd(
self,
xyz=None,
density=None,
spatial_lr_scale: float = 1.0
):
if xyz is None:
xyz = torch.rand(self.num_points, 3) * 2 - 1
# raw_points = torch.randn(self.num_points, 3)
# norms = torch.norm(raw_points, p=2, dim=1, keepdim=True) + EPS
# directions = raw_points / norms
# random_scales = torch.rand(self.num_points, 1) ** (1/3) * 0.25
# xyz = directions * random_scales
if density is None:
density = torch.ones(self.num_points, 1)
self.spatial_lr_scale = spatial_lr_scale
fused_point_cloud = torch.tensor(xyz).float().cuda()
print(
"Initialize gaussians from {} estimated points".format(
fused_point_cloud.shape[0]
)
)
fused_density = (
self.density_inverse_activation(torch.tensor(density)).float().cuda()
)
dist = torch.sqrt(
torch.clamp_min(
distCUDA2(fused_point_cloud),
0.001**2,
)
)
if self.scale_bound is not None:
dist = torch.clamp(
dist, self.scale_bound[0] + EPS, self.scale_bound[1] - EPS
) # Avoid overflow
scales = self.scaling_inverse_activation(dist)[..., None].repeat(1, 3)
# scales = torch.ones_like(fused_point_cloud) * 0.01
# isotropic_scales_raw = self.scaling_inverse_activation(dist)[..., None]
rots = torch.zeros((fused_point_cloud.shape[0], 4), device="cuda")
rots[:, 0] = 1
self._xyz = nn.Parameter(fused_point_cloud.requires_grad_(True))
self._scaling = nn.Parameter(scales.requires_grad_(True))
# self._scaling = nn.Parameter(isotropic_scales_raw.requires_grad_(True)) # Store as (num_points, 1)
self._rotation = nn.Parameter(rots.requires_grad_(True))
self._density = nn.Parameter(fused_density.requires_grad_(True))
self.max_radii2D = torch.zeros((self.get_xyz.shape[0]), device="cuda")
#! Generate one gaussian for debugging purpose
if False:
print("Initialize one gaussian")
fused_xyz = (
torch.tensor([[0.0, 0.0, 0.0]]).float().cuda()
) # position: [0,0,0]
fused_density = self.density_inverse_activation(
torch.tensor([[0.8]]).float().cuda()
) # density: 0.8
scales = self.scaling_inverse_activation(
torch.tensor([[0.5, 0.5, 0.5]]).float().cuda()
) # scale: 0.5
rots = (
torch.tensor([[1.0, 0.0, 0.0, 0.0]]).float().cuda()
) # quaternion: [1, 0, 0, 0]
# rots = torch.tensor([[0.966, -0.259, 0, 0]]).float().cuda()
self._xyz = nn.Parameter(fused_xyz.requires_grad_(True))
self._scaling = nn.Parameter(scales.requires_grad_(True))
self._rotation = nn.Parameter(rots.requires_grad_(True))
self._density = nn.Parameter(fused_density.requires_grad_(True))
self.max_radii2D = torch.zeros((self.get_xyz.shape[0]), device="cuda")
def training_setup(self, training_args):
self.xyz_gradient_accum = torch.zeros((self.get_xyz.shape[0], 1), device="cuda")
self.denom = torch.zeros((self.get_xyz.shape[0], 1), device="cuda")
l = [
{
"params": [self._xyz],
"lr": training_args.position_lr_init * self.spatial_lr_scale,
"name": "xyz",
},
{
"params": [self._density],
"lr": training_args.density_lr_init * self.spatial_lr_scale,
"name": "density",
},
# {
# "params": [self._scaling],
# "lr": training_args.scaling_lr_init * self.spatial_lr_scale,
# "name": "scaling",
# },
# {
# "params": [self._rotation],
# "lr": training_args.rotation_lr_init * self.spatial_lr_scale,
# "name": "rotation",
# },
]
self.optimizer = torch.optim.Adam(l, lr=0.0, eps=1e-15)
self.xyz_scheduler_args = get_expon_lr_func(
lr_init=training_args.position_lr_init * self.spatial_lr_scale,
lr_final=training_args.position_lr_final * self.spatial_lr_scale,
max_steps=training_args.position_lr_max_steps,
)
self.density_scheduler_args = get_expon_lr_func(
lr_init=training_args.density_lr_init * self.spatial_lr_scale,
lr_final=training_args.density_lr_final * self.spatial_lr_scale,
max_steps=training_args.density_lr_max_steps,
)
self.scaling_scheduler_args = get_expon_lr_func(
lr_init=training_args.scaling_lr_init * self.spatial_lr_scale,
lr_final=training_args.scaling_lr_final * self.spatial_lr_scale,
max_steps=training_args.scaling_lr_max_steps,
)
self.rotation_scheduler_args = get_expon_lr_func(
lr_init=training_args.rotation_lr_init * self.spatial_lr_scale,
lr_final=training_args.rotation_lr_final * self.spatial_lr_scale,
max_steps=training_args.rotation_lr_max_steps,
)
def update_learning_rate(self, iteration):
"""Learning rate scheduling per step"""
for param_group in self.optimizer.param_groups:
if param_group["name"] == "xyz":
lr = self.xyz_scheduler_args(iteration)
param_group["lr"] = lr
if param_group["name"] == "density":
lr = self.density_scheduler_args(iteration)
param_group["lr"] = lr
if param_group["name"] == "scaling":
lr = self.scaling_scheduler_args(iteration)
param_group["lr"] = lr
if param_group["name"] == "rotation":
lr = self.rotation_scheduler_args(iteration)
param_group["lr"] = lr
def construct_list_of_attributes(self):
l = ["x", "y", "z", "nx", "ny", "nz"]
# All channels except the 3 DC
l.append("density")
for i in range(self._scaling.shape[1]):
l.append("scale_{}".format(i))
for i in range(self._rotation.shape[1]):
l.append("rot_{}".format(i))
return l
def save_ply(self, path):
# We save pickle files to store more information
mkdir_p(os.path.dirname(path))
xyz = t2a(self._xyz)
densities = t2a(self._density)
scale = t2a(self._scaling)
rotation = t2a(self._rotation)
out = {
"xyz": xyz,
"density": densities,
"scale": scale,
"rotation": rotation,
"scale_bound": self.scale_bound,
}
with open(path, "wb") as f:
pickle.dump(out, f, pickle.HIGHEST_PROTOCOL)
def reset_density(self, reset_density=1.0):
densities_new = self.density_inverse_activation(
torch.min(
self.get_density, torch.ones_like(self.get_density) * reset_density
)
)
optimizable_tensors = self.replace_tensor_to_optimizer(densities_new, "density")
self._density = optimizable_tensors["density"]
def load_ply(self, path):
# We load pickle file.
with open(path, "rb") as f:
data = pickle.load(f)
self._xyz = nn.Parameter(
torch.tensor(data["xyz"], dtype=torch.float, device="cuda").requires_grad_(
True
)
)
self._density = nn.Parameter(
torch.tensor(
data["density"], dtype=torch.float, device="cuda"
).requires_grad_(True)
)
self._scaling = nn.Parameter(
torch.tensor(
data["scale"], dtype=torch.float, device="cuda"
).requires_grad_(True)
)
self._rotation = nn.Parameter(
torch.tensor(
data["rotation"], dtype=torch.float, device="cuda"
).requires_grad_(True)
)
self.scale_bound = data["scale_bound"]
self.setup_functions() # Reset activation functions
def replace_tensor_to_optimizer(self, tensor, name):
optimizable_tensors = {}
for group in self.optimizer.param_groups:
if group["name"] == name:
stored_state = self.optimizer.state.get(group["params"][0], None)
stored_state["exp_avg"] = torch.zeros_like(tensor)
stored_state["exp_avg_sq"] = torch.zeros_like(tensor)
del self.optimizer.state[group["params"][0]]
group["params"][0] = nn.Parameter(tensor.requires_grad_(True))
self.optimizer.state[group["params"][0]] = stored_state
optimizable_tensors[group["name"]] = group["params"][0]
return optimizable_tensors
def _prune_optimizer(self, mask):
optimizable_tensors = {}
for group in self.optimizer.param_groups:
stored_state = self.optimizer.state.get(group["params"][0], None)
if stored_state is not None:
stored_state["exp_avg"] = stored_state["exp_avg"][mask]
stored_state["exp_avg_sq"] = stored_state["exp_avg_sq"][mask]
del self.optimizer.state[group["params"][0]]
group["params"][0] = nn.Parameter(
(group["params"][0][mask].requires_grad_(True))
)
self.optimizer.state[group["params"][0]] = stored_state
optimizable_tensors[group["name"]] = group["params"][0]
else:
group["params"][0] = nn.Parameter(
group["params"][0][mask].requires_grad_(True)
)
optimizable_tensors[group["name"]] = group["params"][0]
return optimizable_tensors
def prune_points(self, mask):
valid_points_mask = ~mask
optimizable_tensors = self._prune_optimizer(valid_points_mask)
self._xyz = optimizable_tensors["xyz"]
self._density = optimizable_tensors["density"]
self._scaling = optimizable_tensors["scaling"]
self._rotation = optimizable_tensors["rotation"]
self.xyz_gradient_accum = self.xyz_gradient_accum[valid_points_mask]
self.denom = self.denom[valid_points_mask]
self.max_radii2D = self.max_radii2D[valid_points_mask]
def cat_tensors_to_optimizer(self, tensors_dict):
optimizable_tensors = {}
for group in self.optimizer.param_groups:
assert len(group["params"]) == 1
extension_tensor = tensors_dict[group["name"]]
stored_state = self.optimizer.state.get(group["params"][0], None)
if stored_state is not None:
stored_state["exp_avg"] = torch.cat(
(stored_state["exp_avg"], torch.zeros_like(extension_tensor)), dim=0
)
stored_state["exp_avg_sq"] = torch.cat(
(stored_state["exp_avg_sq"], torch.zeros_like(extension_tensor)),
dim=0,
)
del self.optimizer.state[group["params"][0]]
group["params"][0] = nn.Parameter(
torch.cat(
(group["params"][0], extension_tensor), dim=0
).requires_grad_(True)
)
self.optimizer.state[group["params"][0]] = stored_state
optimizable_tensors[group["name"]] = group["params"][0]
else:
group["params"][0] = nn.Parameter(
torch.cat(
(group["params"][0], extension_tensor), dim=0
).requires_grad_(True)
)
optimizable_tensors[group["name"]] = group["params"][0]
return optimizable_tensors
def densification_postfix(
self,
new_xyz,
new_densities,
new_scaling,
new_rotation,
new_max_radii2D,
):
d = {
"xyz": new_xyz,
"density": new_densities,
"scaling": new_scaling,
"rotation": new_rotation,
}
optimizable_tensors = self.cat_tensors_to_optimizer(d)
self._xyz = optimizable_tensors["xyz"]
self._density = optimizable_tensors["density"]
self._scaling = optimizable_tensors["scaling"]
self._rotation = optimizable_tensors["rotation"]
self.xyz_gradient_accum = torch.zeros((self.get_xyz.shape[0], 1), device="cuda")
self.denom = torch.zeros((self.get_xyz.shape[0], 1), device="cuda")
self.max_radii2D = torch.cat([self.max_radii2D, new_max_radii2D], dim=-1)
def densify_and_split(self, grads, grad_threshold, densify_scale_threshold, N=2):
n_init_points = self.get_xyz.shape[0]
# Extract points that satisfy the gradient condition
padded_grad = torch.zeros((n_init_points), device="cuda")
padded_grad[: grads.shape[0]] = grads.squeeze()
selected_pts_mask = torch.where(padded_grad >= grad_threshold, True, False)
selected_pts_mask = torch.logical_and(
selected_pts_mask,
torch.max(self.get_scaling, dim=1).values > densify_scale_threshold,
)
stds = self.get_scaling[selected_pts_mask].repeat(N, 1)
means = torch.zeros((stds.size(0), 3), device="cuda")
samples = torch.normal(mean=means, std=stds)
rots = build_rotation(self._rotation[selected_pts_mask]).repeat(N, 1, 1)
new_xyz = torch.bmm(rots, samples.unsqueeze(-1)).squeeze(-1) + self.get_xyz[
selected_pts_mask
].repeat(N, 1)
new_scaling = self.scaling_inverse_activation(
self.get_scaling[selected_pts_mask].repeat(N, 1) / (0.8 * N)
)
# # self.get_scaling[selected_pts_mask] is (num_selected, 3) but isotropic
# # Take one component (e.g., the first) as the representative isotropic activated scale
# current_activated_scales_for_split = self.get_scaling[selected_pts_mask]
# isotropic_component_activated = current_activated_scales_for_split[:, 0:1] # Shape: (num_selected, 1)
# # Repeat for N splits and apply scaling factor
# new_target_activated_scales = isotropic_component_activated.repeat(N, 1) / (0.8 * N) # Shape: (num_selected*N, 1)
# # Apply inverse activation to get the raw values to be stored in self._scaling
# new_scaling = self.scaling_inverse_activation(new_target_activated_scales) # Shape: (num_selected*N, 1)
new_rotation = self._rotation[selected_pts_mask].repeat(N, 1)
# new_density = self._density[selected_pts_mask].repeat(N, 1)
new_density = self.density_inverse_activation(
self.get_density[selected_pts_mask].repeat(N, 1) * (1 / N)
)
new_max_radii2D = self.max_radii2D[selected_pts_mask].repeat(N)
self.densification_postfix(
new_xyz,
new_density,
new_scaling,
new_rotation,
new_max_radii2D,
)
prune_filter = torch.cat(
(
selected_pts_mask,
torch.zeros(N * selected_pts_mask.sum(), device="cuda", dtype=bool),
)
)
self.prune_points(prune_filter)
def densify_and_clone(self, grads, grad_threshold, densify_scale_threshold):
# Extract points that satisfy the gradient condition
selected_pts_mask = torch.where(
torch.norm(grads, dim=-1) >= grad_threshold, True, False
)
selected_pts_mask = torch.logical_and(
selected_pts_mask,
torch.max(self.get_scaling, dim=1).values <= densify_scale_threshold,
)
new_xyz = self._xyz[selected_pts_mask]
# new_densities = self._density[selected_pts_mask]
new_densities = self.density_inverse_activation(
self.get_density[selected_pts_mask] * 0.5
)
new_scaling = self._scaling[selected_pts_mask]
new_rotation = self._rotation[selected_pts_mask]
new_max_radii2D = self.max_radii2D[selected_pts_mask]
self._density[selected_pts_mask] = new_densities
self.densification_postfix(
new_xyz,
new_densities,
new_scaling,
new_rotation,
new_max_radii2D,
)
def densify_and_prune(
self,
max_grad,
min_density,
max_screen_size,
max_scale,
max_num_gaussians,
densify_scale_threshold,
bbox=None,
):
grads = self.xyz_gradient_accum / self.denom
grads[grads.isnan()] = 0.0
# Densify Gaussians if Gaussians are fewer than threshold
if densify_scale_threshold:
if not max_num_gaussians or (
max_num_gaussians and grads.shape[0] < max_num_gaussians
):
self.densify_and_clone(grads, max_grad, densify_scale_threshold)
self.densify_and_split(grads, max_grad, densify_scale_threshold)
# Prune gaussians with too small density
prune_mask = (self.get_density < min_density).squeeze()
# Prune gaussians outside the bbox
if bbox is not None:
xyz = self.get_xyz
prune_mask_xyz = (
(xyz[:, 0] < bbox[0, 0])
| (xyz[:, 0] > bbox[1, 0])
| (xyz[:, 1] < bbox[0, 1])
| (xyz[:, 1] > bbox[1, 1])
| (xyz[:, 2] < bbox[0, 2])
| (xyz[:, 2] > bbox[1, 2])
)
prune_mask = prune_mask | prune_mask_xyz
if max_screen_size:
big_points_vs = self.max_radii2D > max_screen_size
prune_mask = torch.logical_or(prune_mask, big_points_vs)
if max_scale:
big_points_ws = self.get_scaling.max(dim=1).values > max_scale
prune_mask = torch.logical_or(prune_mask, big_points_ws)
self.prune_points(prune_mask)
torch.cuda.empty_cache()
return grads
def add_densification_stats(self, viewspace_point_tensor, update_filter):
self.xyz_gradient_accum[update_filter] += torch.norm(
viewspace_point_tensor.grad[update_filter, :2], dim=-1, keepdim=True
)
self.denom[update_filter] += 1