-
Notifications
You must be signed in to change notification settings - Fork 45
/
gaussian_utils.py
1452 lines (1198 loc) · 57.7 KB
/
gaussian_utils.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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import math
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from easyvolcap.utils.console_utils import *
from easyvolcap.utils.sh_utils import eval_sh
from easyvolcap.utils.timer_utils import timer
from easyvolcap.utils.blend_utils import batch_rodrigues
from easyvolcap.utils.net_utils import make_buffer, make_params, typed
from easyvolcap.utils.data_utils import to_x, add_batch, load_pts, to_tensor
from easyvolcap.utils.math_utils import torch_inverse_2x2, point_padding, affine_inverse, affine_padding
def render_diff_gauss(xyz3: torch.Tensor, rgb3: torch.Tensor, cov: torch.Tensor, occ1: torch.Tensor, camera: dotdict, tile_mask: torch.Tensor = None):
from diff_gauss import GaussianRasterizationSettings, GaussianRasterizer
# Prepare rasterization settings for gaussian
raster_settings = GaussianRasterizationSettings(
image_height=camera.image_height,
image_width=camera.image_width,
tanfovx=camera.tanfovx,
tanfovy=camera.tanfovy,
bg=torch.full([3], 0.0, device=xyz3.device), # GPU
scale_modifier=1.0,
viewmatrix=camera.world_view_transform,
projmatrix=camera.full_proj_transform,
sh_degree=0,
campos=camera.camera_center,
prefiltered=True,
debug=False,
)
# Rasterize visible Gaussians to image, obtain their radii (on screen).
scr = torch.zeros_like(xyz3, requires_grad=True) + 0 # gradient magic
rasterizer = GaussianRasterizer(raster_settings=raster_settings)
rendered_image, rendered_depth, rendered_alpha, radii = typed(torch.float, torch.float)(rasterizer)(
means3D=xyz3,
means2D=scr,
shs=None,
colors_precomp=rgb3,
opacities=occ1,
scales=None,
rotations=None,
cov3D_precomp=cov,
tile_mask=tile_mask,
)
# if xyz3.requires_grad and tile_mask is not None:
# from diff_gauss import interpret_geomBuffer, interpret_binningBuffer, interpret_imgBuffer
# colors_precomp, means3D, scales, rotations, cov3Ds_precomp, tile_mask, radii, sh, geomBuffer, binningBuffer, imgBuffer, alpha = rendered_image.grad_fn.saved_tensors
# raster_settings = rendered_image.grad_fn.raster_settings
# num_rendered = rendered_image.grad_fn.num_rendered
# BLOCK_X, BLOCK_Y = 16, 16
# tile_height, tile_width = (raster_settings.image_height + BLOCK_Y - 1) // BLOCK_Y, (raster_settings.image_width + BLOCK_X - 1) // BLOCK_X
# geom = interpret_geomBuffer(geomBuffer, len(means3D))
# binning = interpret_binningBuffer(binningBuffer, num_rendered)
# img = interpret_imgBuffer(imgBuffer, raster_settings.image_height * raster_settings.image_width, tile_height * tile_width)
# breakpoint()
rgb = rendered_image[None].permute(0, 2, 3, 1)
acc = rendered_alpha[None].permute(0, 2, 3, 1)
dpt = rendered_depth[None].permute(0, 2, 3, 1)
H = camera.image_height
W = camera.image_width
meta = dotdict({'radii': radii / float(max(H, W)), 'scr': scr, 'H': H, 'W': W})
return rgb, acc, dpt, meta
def render_fast_gauss(xyz3: torch.Tensor, rgb3: torch.Tensor, cov: torch.Tensor, occ1: torch.Tensor, camera: dotdict):
from fast_gauss import GaussianRasterizationSettings, GaussianRasterizer
# Prepare rasterization settings for gaussian
raster_settings = GaussianRasterizationSettings(
image_height=camera.image_height,
image_width=camera.image_width,
tanfovx=camera.tanfovx,
tanfovy=camera.tanfovy,
bg=torch.full([3], 0.0, device=xyz3.device), # GPU
scale_modifier=1.0,
viewmatrix=camera.world_view_transform,
projmatrix=camera.full_proj_transform,
sh_degree=0,
campos=camera.camera_center,
prefiltered=True,
debug=False,
)
# Rasterize visible Gaussians to image, obtain their radii (on screen).
scr = torch.zeros_like(xyz3, requires_grad=True) + 0 # gradient magic
rasterizer = GaussianRasterizer(raster_settings=raster_settings)
rendered_image, _ = rasterizer(
means3D=xyz3,
means2D=scr,
shs=None,
colors_precomp=rgb3,
opacities=occ1,
scales=None,
rotations=None,
cov3D_precomp=cov,
)
if rendered_image is not None:
rgb = rendered_image[None].permute(0, 2, 3, 1)
else:
rgb = None
return rgb, None, None, None
def render_fdgs(xyz3: torch.Tensor, rgb3: torch.Tensor, cov: torch.Tensor, occ1: torch.Tensor, camera: dotdict):
from diff_gaussian_rasterization import GaussianRasterizationSettings, GaussianRasterizer
# Prepare rasterization settings for gaussian
raster_settings = GaussianRasterizationSettings(
image_height=camera.image_height,
image_width=camera.image_width,
tanfovx=camera.tanfovx,
tanfovy=camera.tanfovy,
bg=torch.full([3], 0.0, device=xyz3.device), # GPU
scale_modifier=1.0,
viewmatrix=camera.world_view_transform,
projmatrix=camera.full_proj_transform,
sh_degree=0,
campos=camera.camera_center,
prefiltered=True,
debug=False
)
# Rasterize visible Gaussians to image, obtain their radii (on screen).
scr = torch.zeros_like(xyz3, requires_grad=True) + 0 # gradient magic
rasterizer = GaussianRasterizer(raster_settings=raster_settings)
rendered_image, radii = typed(torch.float, torch.float)(rasterizer)(
means3D=xyz3,
means2D=scr,
shs=None,
colors_precomp=rgb3,
opacities=occ1,
scales=None,
rotations=None,
cov3D_precomp=cov,
)
rgb = rendered_image[None].permute(0, 2, 3, 1)
acc = torch.ones_like(rgb[..., :1])
dpt = torch.zeros_like(rgb[..., :1])
H = camera.image_height
W = camera.image_width
meta = dotdict({'radii': radii / float(max(H, W)), 'scr': scr, 'H': H, 'W': W})
return rgb, acc, dpt, meta
@torch.jit.script
def in_frustum(xyz: torch.Tensor, full_proj_matrix: torch.Tensor, xy_padding: float = 0.5, padding: float = 0.01):
ndc = point_padding(xyz) @ full_proj_matrix.to(xyz, non_blocking=True) # this is now in clip space
ndc = ndc[..., :3] / ndc[..., 3:]
return (ndc[..., 2] > -1 - padding) & (ndc[..., 2] < 1 + padding) & (ndc[..., 0] > -1 - xy_padding) & (ndc[..., 0] < 1. + xy_padding) & (ndc[..., 1] > -1 - xy_padding) & (ndc[..., 1] < 1. + xy_padding) # N,
@torch.jit.script
def rgb2sh0(rgb: torch.Tensor):
C0 = 0.28209479177387814
return (rgb - 0.5) / C0
@torch.jit.script
def sh02rgb(sh: torch.Tensor):
C0 = 0.28209479177387814
return sh * C0 + 0.5
@torch.jit.script
def get_jacobian(pix_xyz: torch.Tensor, # B, P, 3, point in screen space
):
J = pix_xyz.new_zeros(pix_xyz.shape + (3, )) # B, P, 3, 3
J[..., 0, 0] = 1 / pix_xyz[..., 2]
J[..., 1, 1] = 1 / pix_xyz[..., 2]
J[..., 0, 2] = -pix_xyz[..., 0] / pix_xyz[..., 2]**2
J[..., 1, 2] = -pix_xyz[..., 1] / pix_xyz[..., 2]**2
J[..., 2, 2] = 1
return J
@torch.jit.script
def gaussian_2d(xy: torch.Tensor, # B, H, W, 2, screen pixel locations for evaluation
mean_xy: torch.Tensor, # B, H, W, K, 2, center of the gaussian in screen space
cov_xy: torch.Tensor, # B, H, W, 2, 2, covariance of the gaussian in screen space
# pow: float = 1, # when pow != 1, not a real gaussian, but easier to control fall off
# we want to the values at 3 sigma to zeros -> easier to control volume rendering?
):
inv_cov_xy = torch_inverse_2x2(cov_xy) # B, P, 2, 2
minus_mean = xy[..., None, :] - mean_xy # B, P, K, 2
# weight = torch.exp(-0.5 * torch.einsum('...d,...de,...e->...', x_minus_mean, inv_cov_xy, x_minus_mean)) # B, H, W, K
xTsigma_new = (minus_mean[..., None] * inv_cov_xy[..., None, :, :]).sum(dim=-2) # B, P, K, 2
xTsigma_x = (xTsigma_new * minus_mean).sum(dim=-1) # B, P, K
return xTsigma_x
@torch.jit.script
def gaussian_3d(scale3: torch.Tensor, # B, P, 3, the scale of the 3d gaussian in 3 dimensions
rot3: torch.Tensor, # B, P, 3, the rotation of the 3D gaussian (angle-axis)
R: torch.Tensor, # B, 3, 3, camera rotation
):
sigma0 = torch.diag_embed(scale3) # B, P, 3, 3
rotmat = batch_rodrigues(rot3) # B, P, 3, 3
R_sigma = rotmat @ sigma0
covariance = R @ R_sigma @ R_sigma.mT @ R.mT
return covariance # B, P, 3, 3
@torch.jit.script
def inverse_sigmoid(x):
return torch.log(x / (1 - x))
@torch.jit.script
def strip_lowerdiag(L: torch.Tensor):
# uncertainty = torch.zeros((L.shape[0], 6), dtype=L.dtype, device=L.device)
# uncertainty[:, 0] = L[:, 0, 0].clip(0.0) # sanitize covariance matrix
# uncertainty[:, 1] = L[:, 0, 1]
# uncertainty[:, 2] = L[:, 0, 2]
# uncertainty[:, 3] = L[:, 1, 1].clip(0.0) # sanitize covariance matrix
# uncertainty[:, 4] = L[:, 1, 2]
# uncertainty[:, 5] = L[:, 2, 2].clip(0.0) # sanitize covariance matrix
# return uncertainty
inds = torch.triu_indices(3, 3, device=L.device) # 2, 6
return L[:, inds[0], inds[1]]
def strip_symmetric(sym):
return strip_lowerdiag(sym)
@torch.jit.script
def build_rotation(q: torch.Tensor):
assert q.shape[-1] == 4
# norm = torch.sqrt(r[:, 0] * r[:, 0] + r[:, 1] * r[:, 1] + r[:, 2] * r[:, 2] + r[:, 3] * r[:, 3])
# q = r / norm[:, None]
q = F.normalize(q, dim=-1)
R = torch.zeros((q.size(0), 3, 3), dtype=q.dtype, device=q.device)
r = q[:, 0]
x = q[:, 1]
y = q[:, 2]
z = q[:, 3]
R[:, 0, 0] = 1 - 2 * (y * y + z * z)
R[:, 0, 1] = 2 * (x * y - r * z)
R[:, 0, 2] = 2 * (x * z + r * y)
R[:, 1, 0] = 2 * (x * y + r * z)
R[:, 1, 1] = 1 - 2 * (x * x + z * z)
R[:, 1, 2] = 2 * (y * z - r * x)
R[:, 2, 0] = 2 * (x * z - r * y)
R[:, 2, 1] = 2 * (y * z + r * x)
R[:, 2, 2] = 1 - 2 * (x * x + y * y)
return R
@torch.jit.script
def build_scaling_rotation(s: torch.Tensor, q: torch.Tensor):
L = torch.zeros((s.shape[0], 3, 3), dtype=s.dtype, device=s.device)
R = build_rotation(q)
L[:, 0, 0] = s[:, 0]
L[:, 1, 1] = s[:, 1]
L[:, 2, 2] = s[:, 2]
L = R @ L
return L
@torch.jit.script
def build_cov6(s: torch.Tensor, q: torch.Tensor):
L = build_scaling_rotation(s, q)
return strip_lowerdiag(L @ L.mT)
def fov2focal(fov, pixels):
return pixels / (2 * np.tan(fov / 2))
def focal2fov(focal, pixels):
return 2 * np.arctan(pixels / (2 * focal))
@torch.jit.script
def getWorld2View(R: torch.Tensor, t: torch.Tensor):
"""
R: ..., 3, 3
T: ..., 3, 1
"""
sh = R.shape[:-2]
T = torch.eye(4, dtype=R.dtype, device=R.device) # 4, 4
for i in range(len(sh)):
T = T.unsqueeze(0)
T = T.expand(sh + (4, 4))
T[..., :3, :3] = R
T[..., :3, 3:] = t
return T
def getWorld2ViewNumpy(R: np.ndarray, t: np.ndarray):
"""
R: ..., 3, 3
T: ..., 3, 1
"""
sh = R.shape[:-2]
T = np.eye(4, dtype=R.dtype) # 4, 4
# for i in range(len(sh)):
# T = T[None]
# T = np.broadcast_to(T, sh + (4, 4))
T[..., :3, :3] = R
T[..., :3, 3:] = t
return T
@torch.jit.script
def getProjectionMatrix(K: torch.Tensor, H: torch.Tensor, W: torch.Tensor, znear: torch.Tensor, zfar: torch.Tensor):
fx = K[0, 0]
fy = K[1, 1]
cx = K[0, 2]
cy = K[1, 2]
s = K[0, 1]
one = K[2, 2]
P = torch.zeros(4, 4, dtype=K.dtype, device=K.device)
P[0, 0] = 2 * fx / W
P[0, 1] = 2 * s / W
P[0, 2] = -1 + 2 * (cx / W)
P[1, 1] = 2 * fy / H
P[1, 2] = -1 + 2 * (cy / H)
P[2, 2] = -(zfar + znear) / (znear - zfar)
P[2, 3] = 2 * zfar * znear / (znear - zfar)
P[3, 2] = one
return P
def getProjectionMatrixNumpy(K: np.ndarray, H: int, W: int, znear: float, zfar: float):
fx = K[0, 0]
fy = K[1, 1]
cx = K[0, 2]
cy = K[1, 2]
s = K[0, 1]
one = K[2, 2]
P: np.ndarray = np.zeros((4, 4), dtype=K.dtype)
P[0, 0] = 2 * fx / W
P[0, 1] = 2 * s / W
P[0, 2] = -1 + 2 * (cx / W)
P[1, 1] = 2 * fy / H
P[1, 2] = -1 + 2 * (cy / H)
P[2, 2] = -(zfar + znear) / (znear - zfar)
P[2, 3] = 2 * zfar * znear / (znear - zfar)
P[3, 2] = one
return P
def prepare_gaussian_camera(batch):
output = dotdict()
H, W, K, R, T, n, f = batch.H[0], batch.W[0], batch.K[0], batch.R[0], batch.T[0], batch.n[0], batch.f[0]
cpu_H, cpu_W, cpu_K, cpu_R, cpu_T, cpu_n, cpu_f = batch.meta.H[0], batch.meta.W[0], batch.meta.K[0], batch.meta.R[0], batch.meta.T[0], batch.meta.n[0], batch.meta.f[0]
output.image_height = cpu_H
output.image_width = cpu_W
output.K = K
output.R = R
output.T = T
fl_x = cpu_K[0, 0] # use cpu K
fl_y = cpu_K[1, 1] # use cpu K
FoVx = focal2fov(fl_x, cpu_W)
FoVy = focal2fov(fl_y, cpu_H)
output.world_view_transform = getWorld2View(R, T).transpose(0, 1)
output.projection_matrix = getProjectionMatrix(K, H, W, n, f).transpose(0, 1)
output.full_proj_transform = torch.matmul(output.world_view_transform, output.projection_matrix)
output.camera_center = (-R.mT @ T)[..., 0] # B, 3, 1 -> 3,
# Set up rasterization configuration
output.tanfovx = math.tan(FoVx * 0.5)
output.tanfovy = math.tan(FoVy * 0.5)
return output
def prepare_cpu_gaussian_camera(batch):
output = dotdict()
cpu_H, cpu_W, cpu_K, cpu_R, cpu_T, cpu_n, cpu_f = batch.meta.H[0], batch.meta.W[0], batch.meta.K[0], batch.meta.R[0], batch.meta.T[0], batch.meta.n[0], batch.meta.f[0]
output.image_height = cpu_H
output.image_width = cpu_W
fl_x = cpu_K[0, 0] # use cpu K
fl_y = cpu_K[1, 1] # use cpu K
FoVx = focal2fov(fl_x, cpu_W)
FoVy = focal2fov(fl_y, cpu_H)
output.world_view_transform = getWorld2View(cpu_R, cpu_T).transpose(0, 1)
output.projection_matrix = getProjectionMatrix(cpu_K, cpu_H, cpu_W, cpu_n, cpu_f).transpose(0, 1)
output.full_proj_transform = torch.matmul(output.world_view_transform, output.projection_matrix)
output.camera_center = (-cpu_R.mT @ cpu_T)[..., 0] # B, 3, 1 -> 3,
# Set up rasterization configuration
output.tanfovx = math.tan(FoVx * 0.5)
output.tanfovy = math.tan(FoVy * 0.5)
return output
def convert_to_gaussian_camera(K: torch.Tensor,
R: torch.Tensor,
T: torch.Tensor,
H: torch.Tensor,
W: torch.Tensor,
n: torch.Tensor,
f: torch.Tensor,
cpu_K: torch.Tensor,
cpu_R: torch.Tensor,
cpu_T: torch.Tensor,
cpu_H: int,
cpu_W: int,
cpu_n: float = 0.01,
cpu_f: float = 100.,
):
output = dotdict()
output.K = K
output.R = R
output.T = T
output.H = H.item()
output.W = W.item()
output.n = n.item()
output.f = f.item()
output.image_height = cpu_H.item()
output.image_width = cpu_W.item()
FoVx = focal2fov(cpu_K[0, 0].item(), cpu_W.item()) # MARK: MIGHT SYNC IN DIST TRAINING, WHY?
FoVy = focal2fov(cpu_K[1, 1].item(), cpu_H.item()) # MARK: MIGHT SYNC IN DIST TRAINING, WHY?
# Use .float() to avoid AMP issues
output.world_view_transform = getWorld2View(R, T).transpose(0, 1).float() # this is now to be right multiplied
output.projection_matrix = getProjectionMatrix(K, H, W, n, f).transpose(0, 1).float() # this is now to be right multiplied
output.full_proj_transform = torch.matmul(output.world_view_transform, output.projection_matrix).float() # 4, 4
output.camera_center = (-R.mT @ T)[..., 0].float() # B, 3, 1 -> 3,
# Set up rasterization configuration
output.tanfovx = np.tan(FoVx * 0.5)
output.tanfovy = np.tan(FoVy * 0.5)
return output
def convert_to_cpu_gaussian_camera(K: torch.Tensor,
R: torch.Tensor,
T: torch.Tensor,
H: torch.Tensor,
W: torch.Tensor,
n: torch.Tensor,
f: torch.Tensor,
cpu_K: torch.Tensor,
cpu_R: torch.Tensor,
cpu_T: torch.Tensor,
cpu_H: int,
cpu_W: int,
cpu_n: float = 0.01,
cpu_f: float = 100.,
):
output = dotdict()
output.K = cpu_K.numpy()
output.R = cpu_R.numpy()
output.T = cpu_T.numpy()
output.H = cpu_H
output.W = cpu_W
output.n = cpu_n
output.f = cpu_f
output.image_height = cpu_H
output.image_width = cpu_W
FoVx = focal2fov(cpu_K[0, 0].item(), cpu_W) # MARK: MIGHT SYNC IN DIST TRAINING, WHY?
FoVy = focal2fov(cpu_K[1, 1].item(), cpu_H) # MARK: MIGHT SYNC IN DIST TRAINING, WHY?
# Use .float() to avoid AMP issues
output.world_view_transform = getWorld2ViewNumpy(output.R, output.T).T.astype(np.float32) # this is now to be right multiplied
output.projection_matrix = getProjectionMatrixNumpy(output.K, output.H, output.W, output.n, output.f).T.astype(np.float32) # this is now to be right multiplied
output.full_proj_transform = (output.world_view_transform @ output.projection_matrix).astype(np.float32) # 4, 4
output.camera_center = (-output.R.T @ output.T)[..., 0].astype(np.float32) # B, 3, 1 -> 3,
# Set up rasterization configuration
output.tanfovx = np.tan(FoVx * 0.5)
output.tanfovy = np.tan(FoVy * 0.5)
# output = to_tensor(output)
return output
def convert_to_gl_camera(K: torch.Tensor,
R: torch.Tensor,
T: torch.Tensor,
H: torch.Tensor,
W: torch.Tensor,
n: torch.Tensor,
f: torch.Tensor,
cpu_K: torch.Tensor,
cpu_R: torch.Tensor,
cpu_T: torch.Tensor,
cpu_H: int,
cpu_W: int,
cpu_n: float = 0.01,
cpu_f: float = 100.,
):
output = dotdict()
output.image_height = cpu_H.item()
output.image_width = cpu_W.item()
output.K = K
output.R = R
output.T = T
output.znear = cpu_n.item()
output.zfar = cpu_f.item()
output.FoVx = focal2fov(cpu_K[0, 0].item(), cpu_W.item()) # MARK: MIGHT SYNC IN DIST TRAINING, WHY?
output.FoVy = focal2fov(cpu_K[1, 1].item(), cpu_H.item()) # MARK: MIGHT SYNC IN DIST TRAINING, WHY?
# Use .float() to avoid AMP issues
output.world_view_transform = getWorld2View(R, T).transpose(0, 1).float() # this is now to be right multiplied
c2w = affine_inverse(output.world_view_transform.mT).mT
c2w[1] *= -1
c2w[2] *= -1
output.world_view_transform = affine_inverse(c2w.mT).mT
output.projection_matrix = getProjectionMatrix(K, H, W, n, f).transpose(0, 1).float() # this is now to be right multiplied
output.projection_matrix[2][0] *= -1
output.projection_matrix[2][2] *= -1
output.projection_matrix[2][3] *= -1
output.full_proj_transform = torch.matmul(output.world_view_transform, output.projection_matrix).float() # 4, 4
output.camera_center = (-R.mT @ T)[..., 0].float() # B, 3, 1 -> 3,
# Set up rasterization configuration
output.tanfovx = np.tan(output.FoVx * 0.5)
output.tanfovy = np.tan(output.FoVy * 0.5)
return output
def batch_to_camera(batch: dotdict, i: int = 0, is_cpu: bool = False, is_gl: bool = False, scene_scale: float = 1.0, no_batch: bool = False):
if not is_cpu:
if no_batch:
K = batch.K
R = batch.R
T = batch.T / scene_scale
H = batch.H.item()
W = batch.W.item()
n = batch.n.item() / scene_scale
f = batch.f.item() / scene_scale
else:
K = batch.K[i]
R = batch.R[i]
T = batch.T[i] / scene_scale
H = batch.H[i]
W = batch.W[i]
n = batch.n[i] / scene_scale
f = batch.f[i] / scene_scale
if no_batch:
cpu_K = batch.meta.K
cpu_R = batch.meta.R
cpu_T = batch.meta.T / scene_scale
cpu_H = batch.meta.H.item()
cpu_W = batch.meta.W.item()
cpu_n = batch.meta.n.item() / scene_scale
cpu_f = batch.meta.f.item() / scene_scale
else:
cpu_K = batch.meta.K[i]
cpu_R = batch.meta.R[i]
cpu_T = batch.meta.T[i] / scene_scale
cpu_H = batch.meta.H[i]
cpu_W = batch.meta.W[i]
cpu_n = batch.meta.n[i] / scene_scale
cpu_f = batch.meta.f[i] / scene_scale
if is_cpu:
if is_gl:
return convert_to_gl_camera(K, R, T, H, W, n, f, cpu_K, cpu_R, cpu_T, cpu_H, cpu_W, cpu_n, cpu_f)
else:
return convert_to_cpu_gaussian_camera(None, None, None, None, None, None, None, cpu_K, cpu_R, cpu_T, cpu_H, cpu_W, cpu_n, cpu_f)
else:
return convert_to_gaussian_camera(K, R, T, H, W, n, f, cpu_K, cpu_R, cpu_T, cpu_H, cpu_W, cpu_n, cpu_f)
class GaussianModel(nn.Module):
def __init__(self,
xyz: torch.Tensor = None,
colors: torch.Tensor = None,
init_occ: float = 0.1,
init_scale: torch.Tensor = None,
sh_deg: int = 3,
scale_min: float = 1e-4,
scale_max: float = 1e1,
):
super().__init__()
@torch.jit.script
def scaling_activation(x, scale_min: float = scale_min, scale_max: float = scale_max):
return torch.sigmoid(x) * (scale_max - scale_min) + scale_min
@torch.jit.script
def scaling_inverse_activation(x, scale_min: float = scale_min, scale_max: float = scale_max):
return torch.logit(((x - scale_min) / (scale_max - scale_min)).clamp(1e-5, 1 - 1e-5))
self.setup_functions(scaling_activation=scaling_activation, scaling_inverse_activation=scaling_inverse_activation)
# SH realte configs
self.active_sh_degree = make_buffer(torch.zeros(1))
self.max_sh_degree = sh_deg
# Initalize trainable parameters
self.create_from_pcd(xyz, colors, init_occ, init_scale)
# Densification related parameters
self.max_radii2D = make_buffer(torch.zeros(self.get_xyz.shape[0]))
self.xyz_gradient_accum = make_buffer(torch.zeros((self.get_xyz.shape[0], 1)))
self.denom = make_buffer(torch.zeros((self.get_xyz.shape[0], 1)))
# Perform some model messaging before loading
self._register_load_state_dict_pre_hook(self._load_state_dict_pre_hook)
def setup_functions(self,
scaling_activation=torch.exp,
scaling_inverse_activation=torch.log,
opacity_activation=torch.sigmoid,
inverse_opacity_activation=inverse_sigmoid,
rotation_activation=F.normalize,
):
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
self.scaling_activation = getattr(torch, scaling_activation) if isinstance(scaling_activation, str) else scaling_activation
self.opacity_activation = getattr(torch, opacity_activation) if isinstance(opacity_activation, str) else opacity_activation
self.rotation_activation = getattr(torch, rotation_activation) if isinstance(rotation_activation, str) else rotation_activation
self.scaling_inverse_activation = getattr(torch, scaling_inverse_activation) if isinstance(scaling_inverse_activation, str) else scaling_inverse_activation
self.opacity_inverse_activation = getattr(torch, inverse_opacity_activation) if isinstance(inverse_opacity_activation, str) else inverse_opacity_activation
self.covariance_activation = build_covariance_from_scaling_rotation
@property
def device(self):
return self.get_xyz.device
@property
def get_scaling(self):
return self.scaling_activation(self._scaling)
@property
def get_rotation(self):
return self.rotation_activation(self._rotation)
@property
def get_xyz(self):
return self._xyz
@property
def get_features(self):
features_dc = self._features_dc
features_rest = self._features_rest
return torch.cat((features_dc, features_rest), dim=1)
@property
def get_opacity(self):
return self.opacity_activation(self._opacity)
def get_covariance(self, scaling_modifier=1):
return self.covariance_activation(self.get_scaling, scaling_modifier, self._rotation)
def oneupSHdegree(self):
if self.active_sh_degree < self.max_sh_degree:
self.active_sh_degree += 1
def create_from_pcd(self, xyz: torch.Tensor, colors: torch.Tensor = None, opacities: float = 0.1, scales: torch.Tensor = None):
from simple_knn._C import distCUDA2
if xyz is None:
xyz = torch.empty(1, 3, device='cuda') # by default, init empty gaussian model on CUDA
features = torch.zeros((xyz.shape[0], 3, (self.max_sh_degree + 1) ** 2))
if colors is not None:
SH = rgb2sh0(colors)
features[:, :3, 0] = SH
features[:, 3: 1:] = 0
if scales is None:
dist2 = torch.clamp_min(distCUDA2(xyz.float().cuda()), 0.0000001)
scales = self.scaling_inverse_activation(torch.sqrt(dist2))[..., None].repeat(1, 3)
else:
scales = self.scaling_inverse_activation(scales)
rots = torch.rand((xyz.shape[0], 4))
rots[:, 0] = 1
if not isinstance(opacities, torch.Tensor) or len(opacities) != len(xyz):
opacities = opacities * torch.ones((xyz.shape[0], 1), dtype=torch.float)
opacities = self.opacity_inverse_activation(opacities)
self._xyz = make_params(xyz)
self._features_dc = make_params(features[:, :, :1].transpose(1, 2).contiguous())
self._features_rest = make_params(features[:, :, 1:].transpose(1, 2).contiguous())
self._scaling = make_params(scales)
self._rotation = make_params(rots)
self._opacity = make_params(opacities)
@torch.no_grad()
def _load_state_dict_pre_hook(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs):
# Supports loading points and features with different shapes
if prefix is not '' and not prefix.endswith('.'): prefix = prefix + '.' # special care for when we're loading the model directly
for name, params in self.named_parameters():
if f'{prefix}{name}' in state_dict:
params.data = params.data.new_empty(state_dict[f'{prefix}{name}'].shape)
def reset_opacity(self, optimizer_state):
for _, val in optimizer_state.items():
if val.name == '_opacity':
break
opacities_new = inverse_sigmoid(torch.min(self.get_opacity, torch.ones_like(self.get_opacity) * 0.01))
self._opacity.set_(opacities_new.detach())
self._opacity.grad = None
val.old_keep = torch.zeros_like(val.old_keep, dtype=torch.bool)
val.new_keep = torch.zeros_like(val.new_keep, dtype=torch.bool)
val.new_params = self._opacity
# optimizable_tensors = self.replace_tensor_to_optimizer(opacities_new, "opacity")
# self._opacity = optimizable_tensors["opacity"]
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: torch.Tensor):
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._features_dc = optimizable_tensors["f_dc"]
# self._features_rest = optimizable_tensors["f_rest"]
# self._opacity = optimizable_tensors["opacity"]
# self._scaling = optimizable_tensors["scaling"]
# self._rotation = optimizable_tensors["rotation"]
self._xyz.set_(self._xyz[valid_points_mask].detach())
self._xyz.grad = None
self._features_dc.set_(self._features_dc[valid_points_mask].detach())
self._features_dc.grad = None
self._features_rest.set_(self._features_rest[valid_points_mask].detach())
self._features_rest.grad = None
self._opacity.set_(self._opacity[valid_points_mask].detach())
self._opacity.grad = None
self._scaling.set_(self._scaling[valid_points_mask].detach())
self._scaling.grad = None
self._rotation.set_(self._rotation[valid_points_mask].detach())
self._rotation.grad = None
self.xyz_gradient_accum.set_(self.xyz_gradient_accum[valid_points_mask])
self.xyz_gradient_accum.grad = None
self.denom.set_(self.denom[valid_points_mask])
self.denom.grad = None
self.max_radii2D.set_(self.max_radii2D[valid_points_mask])
self.max_radii2D.grad = None
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_features_dc, new_features_rest, new_opacities, new_scaling, new_rotation, optimizer_state):
d = dotdict({
"_xyz": new_xyz,
"_features_dc": new_features_dc,
"_features_rest": new_features_rest,
"_opacity": new_opacities,
"_scaling": new_scaling,
"_rotation": new_rotation,
})
# optimizable_tensors = self.cat_tensors_to_optimizer(d)
# self._xyz = optimizable_tensors["xyz"]
# self._features_dc = optimizable_tensors["f_dc"]
# self._features_rest = optimizable_tensors["f_rest"]
# self._opacity = optimizable_tensors["opacity"]
# self._scaling = optimizable_tensors["scaling"]
# self._rotation = optimizable_tensors["rotation"]
for name, new_params in d.items():
params: nn.Parameter = getattr(self, name)
params.set_(torch.cat((params.data, new_params), dim=0).detach())
params.grad = None
device = self.get_xyz.device
self.xyz_gradient_accum.set_(torch.zeros((self.get_xyz.shape[0], 1), device=device))
self.xyz_gradient_accum.grad = None
self.denom.set_(torch.zeros((self.get_xyz.shape[0], 1), device=device))
self.denom.grad = None
self.max_radii2D.set_(torch.zeros((self.get_xyz.shape[0]), device=device))
self.max_radii2D.grad = None
for val in optimizer_state.values():
name = val.name
val.new_keep = torch.cat((val.new_keep, torch.zeros_like(d[name], dtype=torch.bool, requires_grad=False)), dim=0)
val.new_params = getattr(self, name)
assert val.new_keep.shape == val.new_params.shape
def densify_and_split(self, grads, grad_threshold, scene_extent, percent_dense, min_opacity, max_screen_size, optimizer_state, N=2):
n_init_points = self.get_xyz.shape[0]
device = self.get_xyz.device
# Extract points that satisfy the gradient condition
padded_grad = torch.zeros((n_init_points), device=device)
padded_grad[:grads.shape[0]] = grads.squeeze()
selected_pts_mask = padded_grad >= grad_threshold
selected_pts_mask = torch.logical_and(selected_pts_mask,
torch.max(self.get_scaling, dim=1).values > percent_dense * scene_extent)
stds = self.get_scaling[selected_pts_mask].repeat(N, 1)
means = torch.zeros((stds.size(0), 3), device=device)
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))
new_rotation = self._rotation[selected_pts_mask].repeat(N, 1)
new_features_dc = self._features_dc[selected_pts_mask].repeat(N, 1, 1)
new_features_rest = self._features_rest[selected_pts_mask].repeat(N, 1, 1)
new_opacity = self._opacity[selected_pts_mask].repeat(N, 1)
self.densification_postfix(new_xyz, new_features_dc, new_features_rest, new_opacity, new_scaling, new_rotation, optimizer_state)
prune_filter = torch.cat((selected_pts_mask, torch.zeros(N * selected_pts_mask.sum(), device=device, dtype=bool)))
self.prune_points(prune_filter)
old_keep_mask = ~prune_filter[:grads.shape[0]]
for val in optimizer_state.values():
name = val.name
val.old_keep[~old_keep_mask] = False
val.new_keep = val.new_keep[~prune_filter]
val.params = getattr(self, name)
assert val.old_keep.sum() == val.new_keep.sum()
assert val.new_keep.shape == val.new_params.shape
prune_mask = (self.get_opacity < min_opacity).squeeze()
if max_screen_size:
big_points_vs = self.max_radii2D > max_screen_size
big_points_ws = self.get_scaling.max(dim=1).values > 0.1 * scene_extent
prune_mask = torch.logical_or(torch.logical_or(prune_mask, big_points_vs), big_points_ws)
self.prune_points(prune_mask)
_old_keep_mask = old_keep_mask.clone()
mask_mask = old_keep_mask[old_keep_mask]
_mask = prune_mask[:mask_mask.shape[0]]
mask_mask[_mask] = False
old_keep_mask[_old_keep_mask] = mask_mask
for val in optimizer_state.values():
name = val.name
val.old_keep[~old_keep_mask] = False
val.new_keep = val.new_keep[~prune_mask]
val.params = getattr(self, name)
assert val.old_keep.sum() == val.new_keep.sum()
assert val.new_keep.shape == val.new_params.shape
def densify_and_clone(self, grads, grad_threshold, scene_extent, percent_dense, optimizer_state):
# Extract points that satisfy the gradient condition
selected_pts_mask = torch.norm(grads, dim=-1) >= grad_threshold
selected_pts_mask = torch.logical_and(selected_pts_mask,
torch.max(self.get_scaling, dim=1).values <= percent_dense * scene_extent)
new_xyz = self._xyz[selected_pts_mask]
new_features_dc = self._features_dc[selected_pts_mask]
new_features_rest = self._features_rest[selected_pts_mask]
new_opacities = self._opacity[selected_pts_mask]
new_scaling = self._scaling[selected_pts_mask]
new_rotation = self._rotation[selected_pts_mask]
self.densification_postfix(new_xyz, new_features_dc, new_features_rest, new_opacities, new_scaling, new_rotation, optimizer_state)
def densify_and_prune(self, max_grad, min_opacity, extent, max_screen_size, percent_dense, optimizer_state):
grads = self.xyz_gradient_accum / self.denom
grads[grads.isnan()] = 0.0
self.densify_and_clone(grads, max_grad, extent, percent_dense, optimizer_state)
self.densify_and_split(grads, max_grad, extent, percent_dense, min_opacity, max_screen_size, optimizer_state)
torch.cuda.empty_cache()
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
def construct_list_of_attributes(self):
l = ['x', 'y', 'z', 'nx', 'ny', 'nz']
# All channels except the 3 DC
for i in range(self._features_dc.shape[1] * self._features_dc.shape[2]):
l.append('f_dc_{}'.format(i))
for i in range(self._features_rest.shape[1] * self._features_rest.shape[2]):
l.append('f_rest_{}'.format(i))
l.append('opacity')
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):
from plyfile import PlyData, PlyElement
os.makedirs(dirname(path), exist_ok=True)
# The original gaussian model uses a different activation
# Normalization for rotation, so no conversion
# Exp on scaling, need to -> world space -> log
# Doing inverse_sigmoid here will lead to NaNs
_opacity = self._opacity
if self.opacity_activation != F.sigmoid and \
self.opacity_activation != torch.sigmoid and \
not isinstance(self.opacity_activation, nn.Sigmoid):
opacity = self.opacity_activation(opacity)
_opacity = inverse_sigmoid(opacity)
scale = self._scaling
scale = self.scaling_activation(scale)
_scale = torch.log(scale)
xyz = self._xyz.detach().cpu().numpy()
normals = np.zeros_like(xyz)
f_dc = self._features_dc.detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
f_rest = self._features_rest.detach().transpose(1, 2).flatten(start_dim=1).contiguous().cpu().numpy()
opacities = _opacity.detach().cpu().numpy()
scale = _scale.detach().cpu().numpy()
rotation = self._rotation.detach().cpu().numpy()
dtype_full = [(attribute, 'f4') for attribute in self.construct_list_of_attributes()]
elements = np.empty(xyz.shape[0], dtype=dtype_full)