-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgaussian_base.py
1469 lines (1275 loc) · 56.2 KB
/
gaussian_base.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
#
# 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 math
import os
import random
import sys
import argparse
from dataclasses import dataclass, field
from datetime import datetime
from typing import NamedTuple
import numpy as np
import cv2
from PIL import Image
import threestudio
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from transformers import pipeline
from plyfile import PlyData, PlyElement
from simple_knn._C import distCUDA2
import diffusers
from diffusers import StableDiffusionInpaintPipeline, AutoPipelineForInpainting
from threestudio.models.geometry.base import BaseGeometry
from threestudio.utils.misc import C
from threestudio.utils.typing import *
from segment_anything import sam_model_registry, SamPredictor
import matplotlib.pyplot as plt
from .gaussian_io import GaussianIO
import imageio
from scipy.spatial.transform import Rotation as R
REORDER_MTX = torch.tensor([
[0,0,0,1],
[1,0,0,0],
[0,1,0,0],
[0,0,1,0]
]).cuda().float()
def build_rotation(r):
norm = torch.sqrt(
r[:, 0] * r[:, 0] + r[:, 1] * r[:, 1] + r[:, 2] * r[:, 2] + r[:, 3] * r[:, 3]
)
q = r / norm[:, None]
R = torch.zeros((q.size(0), 3, 3), device="cuda")
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
def rotation_matrix(angle_x, angle_y, angle_z):
# Convert angles to radians
rad_x = torch.deg2rad(torch.tensor(angle_x))
rad_y = torch.deg2rad(torch.tensor(angle_y))
rad_z = torch.deg2rad(torch.tensor(angle_z))
# Compute sine and cosine of the angles
cos_x = torch.cos(rad_x)
sin_x = torch.sin(rad_x)
cos_y = torch.cos(rad_y)
sin_y = torch.sin(rad_y)
cos_z = torch.cos(rad_z)
sin_z = torch.sin(rad_z)
# Construct the rotation matrix
Rx = torch.tensor([[1, 0, 0],
[0, cos_x, -sin_x],
[0, sin_x, cos_x]])
Ry = torch.tensor([[cos_y, 0, sin_y],
[0, 1, 0],
[-sin_y, 0, cos_y]])
Rz = torch.tensor([[cos_z, -sin_z, 0],
[sin_z, cos_z, 0],
[0, 0, 1]])
# Combine the rotation matrices
rotation_matrix = Rz @ Ry @ Rx
return rotation_matrix
# from scipy.spatial import KDTree
#
# def distCUDA2(points):
# points_np = points.detach().cpu().float().numpy()
# dists, inds = KDTree(points_np).query(points_np, k=4)
# meanDists = (dists[:, 1:] ** 2).mean(1)
#
# return torch.tensor(meanDists, dtype=points.dtype, device=points.device)
sys.path.append('./utils/GeoWizard/geowizard')
from models.geowizard_pipeline import DepthNormalEstimationPipeline
C0 = 0.28209479177387814
def propagate(canvas):
H, W = canvas.shape
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
count = np.zeros_like(canvas)
while 1:
curr_mask = canvas > 0
if sum(sum(curr_mask)) == H * W:
break
expand_mask = (cv2.blur(curr_mask.astype(np.float32), (3, 3)) > 0)
x, y = np.where(np.logical_and(expand_mask, ~curr_mask))
old_canvas = canvas.copy()
for xx, yy in zip(x, y):
for i in range(4):
ref_x = xx + dx[i]
ref_y = yy + dy[i]
if 0<=ref_x<H and 0<=ref_y<W and old_canvas[ref_x, ref_y] != 0:
canvas[xx, yy] = old_canvas[ref_x, ref_y]
count[xx, yy] = count[ref_x, ref_y] + 1
weight = (count.max() - count) / count.max()
return canvas * weight
def save_pc(save_file, pts, color):
'''
pts: N, 3
color: N, 3
'''
if color.dtype == np.dtype('float64'):
color = (color * 255).astype(np.uint8)
with open(save_file, 'w') as f:
f.writelines((
"ply\n",
"format ascii 1.0\n",
"element vertex {}\n".format(pts.shape[0]),
"property float x\n",
"property float y\n",
"property float z\n",
"property uchar red\n",
"property uchar green\n",
"property uchar blue\n",
"end_header\n"))
for i in range(pts.shape[0]):
point = "%f %f %f %d %d %d\n" % (pts[i, 0], pts[i, 1], pts[i, 2], color[i, 0], color[i, 1], color[i, 2])
f.writelines(point)
threestudio.info(f"Saved point cloud to {save_file}.")
def RGB2SH(rgb):
return (rgb - 0.5) / C0
def SH2RGB(sh):
return sh * C0 + 0.5
def inverse_sigmoid(x):
return torch.log(x / (1 - x))
def strip_lowerdiag(L):
uncertainty = torch.zeros((L.shape[0], 6), dtype=torch.float, device="cuda")
uncertainty[:, 0] = L[:, 0, 0]
uncertainty[:, 1] = L[:, 0, 1]
uncertainty[:, 2] = L[:, 0, 2]
uncertainty[:, 3] = L[:, 1, 1]
uncertainty[:, 4] = L[:, 1, 2]
uncertainty[:, 5] = L[:, 2, 2]
return uncertainty
def strip_symmetric(sym):
return strip_lowerdiag(sym)
def gaussian_3d_coeff(xyzs, covs):
# xyzs: [N, 3]
# covs: [N, 6]
x, y, z = xyzs[:, 0], xyzs[:, 1], xyzs[:, 2]
a, b, c, d, e, f = (
covs[:, 0],
covs[:, 1],
covs[:, 2],
covs[:, 3],
covs[:, 4],
covs[:, 5],
)
# eps must be small enough !!!
inv_det = 1 / (
a * d * f + 2 * e * c * b - e**2 * a - c**2 * d - b**2 * f + 1e-24
)
inv_a = (d * f - e**2) * inv_det
inv_b = (e * c - b * f) * inv_det
inv_c = (e * b - c * d) * inv_det
inv_d = (a * f - c**2) * inv_det
inv_e = (b * c - e * a) * inv_det
inv_f = (a * d - b**2) * inv_det
power = (
-0.5 * (x**2 * inv_a + y**2 * inv_d + z**2 * inv_f)
- x * y * inv_b
- x * z * inv_c
- y * z * inv_e
)
power[power > 0] = -1e10 # abnormal values... make weights 0
return torch.exp(power)
def build_rotation(r):
norm = torch.sqrt(
r[:, 0] * r[:, 0] + r[:, 1] * r[:, 1] + r[:, 2] * r[:, 2] + r[:, 3] * r[:, 3]
)
q = r / norm[:, None]
R = torch.zeros((q.size(0), 3, 3), device="cuda")
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
def build_scaling_rotation(s, r):
L = torch.zeros((s.shape[0], 3, 3), dtype=torch.float, device="cuda")
R = build_rotation(r)
L[:, 0, 0] = s[:, 0]
L[:, 1, 1] = s[:, 1]
L[:, 2, 2] = s[:, 2]
L = R @ L
return L
def safe_state(silent):
old_f = sys.stdout
class F:
def __init__(self, silent):
self.silent = silent
def write(self, x):
if not self.silent:
if x.endswith("\n"):
old_f.write(
x.replace(
"\n",
" [{}]\n".format(
str(datetime.now().strftime("%d/%m %H:%M:%S"))
),
)
)
else:
old_f.write(x)
def flush(self):
old_f.flush()
sys.stdout = F(silent)
random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
torch.cuda.set_device(torch.device("cuda:0"))
class BasicPointCloud(NamedTuple):
points: np.array
colors: np.array
normals: np.array
class Camera(NamedTuple):
FoVx: torch.Tensor
FoVy: torch.Tensor
camera_center: torch.Tensor
image_width: int
image_height: int
world_view_transform: torch.Tensor
full_proj_transform: torch.Tensor
def fill_mask(mask):
mask = np.array(mask)
canvas = np.zeros_like(mask)
H, W = mask.shape
for i in range(H):
for p in range(0, W):
if mask[i, p]:
canvas[i, p] = 1
else:
break
for p in range(W-1, 0, -1):
if mask[i, p]:
canvas[i, p] = 1
else:
break
for i in range(W):
for p in range(0, H):
if mask[p, i]:
canvas[p, i] = 1
else:
break
for p in range(H-1, 0, -1):
if mask[p, i]:
canvas[p, i] = 1
else:
break
mask = np.logical_and(mask, canvas)
return Image.fromarray(mask)
def parse_wh(wh):
try:
W, H = wh
except:
W = H = wh
return W, H
@threestudio.register("gaussian-splatting")
class GaussianBaseModel(BaseGeometry, GaussianIO):
@dataclass
class Config(BaseGeometry.Config):
max_num: int = 500000
sh_degree: int = 0
position_lr: Any = 0.001
# scale_lr: Any = 0.003
feature_lr: Any = 0.01
opacity_lr: Any = 0.05
scaling_lr: Any = 0.005
rotation_lr: Any = 0.005
pred_normal: bool = False
normal_lr: Any = 0.001
lang_lr: float = 0.005
densification_interval: int = 50
prune_interval: int = 50
opacity_reset_interval: int = 100000
densify_from_iter: int = 100
prune_from_iter: int = 100
densify_until_iter: int = 2000
prune_until_iter: int = 2000
densify_grad_threshold: Any = 0.01
min_opac_prune: Any = 0.005
split_thresh: Any = 0.02
radii2d_thresh: Any = 1000
sphere: bool = False
prune_big_points: bool = False
color_clip: Any = 2.0
geometry_convert_from: str = ""
load_ply_only_vertex: bool = False
init_num_pts: int = 100
pc_init_radius: float = 0.8
opacity_init: float = 0.1
img_resolution: Any = 512
shap_e_guidance_config: dict = field(default_factory=dict)
max_scaling: float = 100
sam_ckpt_path: str = "ckpts/sam_vit_h_4b8939.pth"
ooi_bbox: Any = None
prompt: Any = None
empty_prompt: Any = None
lang_beta_1: float = 0.9
lang_beta_2: float = 0.999
inference_only: bool = False
pc_max_resolution: int = 512
use_sdxl_for_inpaint: bool = False
cfg: Config
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
self.scaling_activation = torch.exp
self.scaling_inverse_activation = torch.log
self.covariance_activation = build_covariance_from_scaling_rotation
self.opacity_activation = torch.sigmoid
self.inverse_opacity_activation = inverse_sigmoid
self.rotation_activation = torch.nn.functional.normalize
self.color_clip = C(self.cfg.color_clip, 0, 0)
self.fixed_xyz = None
self.fixed_rot = None
if not self.cfg.inference_only:
sam = sam_model_registry["vit_h"](checkpoint=self.cfg.sam_ckpt_path).to('cuda')
self.predictor = SamPredictor(sam)
def project_pc(self, c2w, H=256, W=None):
if W is None:
W = H
B = c2w.shape[0]
mask = torch.zeros([B, H, W], device='cuda')
depth_canvas = torch.zeros([B, H, W], device='cuda')
# for pc in [self.bg_point_cloud, self.point_cloud]:
pc_cam = torch.einsum('bxy,ny->bnx', torch.linalg.inv(c2w), self.point_cloud)
depth = -1 * pc_cam[..., 2].view(pc_cam.shape[0], -1)
pc_cam = (pc_cam / pc_cam[..., 2:3])[..., :3]
pc_2d = torch.einsum('xy,bny->bnx', self.proj_mtx, pc_cam).clamp(0, 1)
pc_2d[..., 0] = pc_2d[..., 0] * (W-1)
pc_2d[..., 1] = pc_2d[..., 1] * (H-1)
pc_2d = pc_2d.long()
for i in range(pc_2d.shape[0]):
x = (W - pc_2d[i, :, 0]).clamp(0, W-1)
y = (pc_2d[i, :, 1]).clamp(0, H-1)
unique_id = x * H + y
map_2d = np.zeros((W+1)*(H+1)) + 1e8
np.minimum.at(map_2d, unique_id.cpu(), depth[i].cpu())
map_2d[map_2d==1e8] = 0
positive_unique_id = np.where(map_2d>0)[0]
x, y = positive_unique_id // H, positive_unique_id % H
mask[i, y, x] = 1.0
depth_canvas[i, y, x] = torch.tensor(map_2d[positive_unique_id], device='cuda', dtype=torch.float)
# depth_canvas[i, y, x] = depth[i]
# pc_cam = torch.einsum('bxy,hwy->bhwx', torch.linalg.inv(c2w), self.point_cloud)
# depth = -1 * pc_cam[..., 2].view(pc_cam.shape[0], -1)
# pc_cam = (pc_cam / pc_cam[..., 2:3])[..., :3]
# pc_2d = torch.einsum('xy,bhwy->bhwx', self.proj_mtx, pc_cam).clamp(0, 1)
# pc_2d[..., 0] = pc_2d[..., 0] * (W-1)
# pc_2d[..., 1] = pc_2d[..., 1] * (H-1)
# pc_2d = (pc_2d.long()).view(pc_2d.shape[0], -1, pc_2d.shape[-1])
# mask = self.blur_kernel(mask) > 0
mask = torchvision.transforms.functional.gaussian_blur(mask, 3) > 0
# mask = mask > 0
return mask, depth_canvas
def img2pc_inpaint(self, img, c2w=None, gt_depth=None, mask=None, proj_func=None):
W, H = parse_wh(self.cfg.img_resolution)
if max(W, H) > self.cfg.pc_max_resolution:
W, H = int(W / max(W, H) * self.cfg.pc_max_resolution), int(H / max(W, H) * self.cfg.pc_max_resolution)
with torch.no_grad():
self.geowizard_pipe.to('cuda')
depth = self.geowizard_pipe(
img,
denoising_steps = 25,
ensemble_size = 3,
processing_res = 768,
match_input_res = False,
domain = 'outdoor',
color_map = 'Spectral',
gt_depth = gt_depth, mask = mask,
show_progress_bar = True)['depth_np']
self.geowizard_pipe.to('cpu')
ret_depth = depth.copy()
depth = torch.from_numpy(depth)[None]
depth = torch.nn.functional.interpolate(depth[None], size=(H, W), mode='bilinear', align_corners=True).squeeze()
depth = depth.cpu().numpy()
if proj_func is None:
depth = depth * 20 + 5
else:
depth = proj_func(depth)
depth = depth * -1
x, y = np.meshgrid(np.arange(W, dtype=np.float32), np.arange(H, dtype=np.float32), indexing='xy')
x = x / float(W-1)
y = y / float(H-1)
xyz = np.stack((x, y, np.ones_like(x)), 0).transpose(1, 2, 0)
xyz[..., 0] = 1 - xyz[..., 0]
fov = 60 / 180 * np.pi
proj_mtx = np.array([
[1 / (2 * np.tan(fov/2)), 0, 1/2],
[0, 1 / (2 * np.tan(fov/2)), 1/2],
[0, 0, 1],
])
self.proj_mtx = torch.from_numpy(proj_mtx).cuda().float()
if c2w is None:
c2w = np.array([0.0000, 0.0000, 1.0000, 2.5000, 1.0000, 0.0000, -0.0000, 0.0000, -0.0000, 1.0000, -0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000]).reshape(4, 4)
else:
c2w = c2w[0].cpu().numpy()
xyz = np.einsum('ab,hwb->hwa', np.linalg.inv(proj_mtx), xyz)
xyz = xyz * depth[..., None]
xyz = np.concatenate([xyz, np.ones_like(x)[..., None]], 2)
xyz = np.einsum('ab,hwb->hwa', c2w, xyz)
return xyz, ret_depth
def inpaint(self, img, mask, prompt):
# inpaint using base pipe
N = 512
img = img.convert("RGB").resize((N, N))
mask = mask.convert("RGB").resize((N, N))
self.base_inpainting_pipe.to("cuda")
img = self.base_inpainting_pipe(prompt=prompt, image=img, mask_image=mask, guidance_scale=7.5).images[0]
self.base_inpainting_pipe.to("cpu")
torch.cuda.empty_cache()
if self.cfg.use_sdxl_for_inpaint:
# inpaint using sdxl pipe
N = 1024
img = img.convert("RGB").resize((N, N))
mask = mask.convert("RGB").resize((N, N))
self.sdxl_inpainting_pipe.to("cuda")
img = self.sdxl_inpainting_pipe(prompt=prompt, image=img, mask_image=mask, guidance_scale=7.5, num_inference_steps=20, strength=0.99).images[0]
self.sdxl_inpainting_pipe.to("cpu")
return img
def configure(self) -> None:
super().configure()
self.active_sh_degree = 0
self.max_sh_degree = self.cfg.sh_degree
self._xyz = torch.empty(0)
self._features_dc = torch.empty(0)
self._features_rest = torch.empty(0)
self._scaling = torch.empty(0)
self._rotation = torch.empty(0)
self._opacity = torch.empty(0)
self._opacity_mask = None
self.max_radii2D = torch.empty(0)
self.xyz_gradient_accum = torch.empty(0)
self.denom = torch.empty(0)
self.noise_ratio = 0.0
if self.cfg.pred_normal:
self._normal = torch.empty(0)
self.optimizer = None
self.setup_functions()
self.save_path = None
self.fixed_xyz = None
self.fixed_rot = None
if self.cfg.inference_only:
return
# setup GeoWizard
geowizard_checkpoint_path = 'lemonaddie/geowizard'
self.geowizard_pipe = DepthNormalEstimationPipeline.from_pretrained(
geowizard_checkpoint_path, torch_dtype=torch.float32)
self.base_inpainting_pipe = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16)
# self.base_inpainting_pipe = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16, safety_checker=None)
if self.cfg.use_sdxl_for_inpaint:
self.sdxl_inpainting_pipe = AutoPipelineForInpainting.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype=torch.float16, variant="fp16")
self.sdxl_inpainting_pipe.scheduler = diffusers.EulerDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
if self.cfg.geometry_convert_from.startswith("depth:"):
# estimate depth
W, H = parse_wh(self.cfg.img_resolution)
if max(W, H) > self.cfg.pc_max_resolution:
W, H = int(W / max(W, H) * self.cfg.pc_max_resolution), int(H / max(W, H) * self.cfg.pc_max_resolution)
img = self.cfg.geometry_convert_from[len("depth:"):]
raw_img = img = Image.open(img).convert("RGB")
img = img.resize((W, H))
bg_xyz, bg_color = [], []
with torch.no_grad():
self.predictor.set_image(np.array(raw_img))
self.ooi_masks = []
total_inp_ooi_masks = None
total_ooi_masks = []
for i in range(len(self.cfg.ooi_bbox) // 4):
bbox = np.array(self.cfg.ooi_bbox[4*i:4*i+4])
masks, _, _ = self.predictor.predict(
point_coords=None,
point_labels=None,
box=bbox[None, :],
multimask_output=False,
)
# plt.imshow(masks[0])
# plt.savefig(os.path.join(self.save_path, f'mask_{i}.png'))
ooi_masks = np.array(Image.fromarray(masks[0]).resize((W, H), Image.NEAREST))
ooi_masks = (cv2.blur(ooi_masks.astype(np.float32), (5, 5)) > 0)
inp_ooi_masks = (cv2.blur(ooi_masks.astype(np.float32), (7, 7)) > 0)
if i == 0:
total_inp_ooi_masks = inp_ooi_masks
else:
total_inp_ooi_masks += inp_ooi_masks
total_ooi_masks.append(ooi_masks)
total_inp_ooi_masks = total_inp_ooi_masks > 0
original_wh = parse_wh(self.cfg.img_resolution)
bg_image = self.inpaint(img=img, mask=Image.fromarray(total_inp_ooi_masks), prompt=self.cfg.empty_prompt).resize((original_wh))
self.bg_image = np.array(bg_image)
self.bg_image_mask = np.array(Image.fromarray(total_inp_ooi_masks).resize((original_wh)))
xyz, depth = self.img2pc_inpaint(img)
self.point_cloud = torch.from_numpy(xyz).cuda().float().reshape(-1, 4)
for ooi_masks in total_ooi_masks:
transit_masks = np.logical_and(cv2.blur(ooi_masks.astype(np.float32), (3, 3)) > 0, ~ooi_masks)
depth_tensor = torch.from_numpy(depth)[None, None].cuda() * 2 - 1
self.ooi_masks.append(torch.tensor(ooi_masks.reshape(-1).astype(np.uint8), device='cuda').float().bool())
ooi_masks = cv2.blur(ooi_masks.astype(np.float32), (9, 9)) > 0
mask = torch.from_numpy(ooi_masks.astype(np.float32))[None, None].cuda()
bg_xyz_pc, _ = self.img2pc_inpaint(bg_image, gt_depth=depth_tensor, mask=1-mask)
bg_xyz.append(bg_xyz_pc[ooi_masks])
bg_color.append(np.array(bg_image.resize((W, H)))[ooi_masks] / 255)
# xyz = xyz[..., :3].reshape(-1, 3)
xyz = xyz.reshape(-1, 4)
color = np.array(img).reshape(-1, 3) / 255
bg_xyz = np.concatenate(bg_xyz, 0)
additional_pts_num = bg_xyz.shape[0]
xyz = np.concatenate([xyz, bg_xyz], 0)
self.point_cloud = torch.from_numpy(xyz).cuda().float()
color = np.concatenate([color, np.concatenate(bg_color, 0)], 0)
for i in range(len(self.ooi_masks)):
self.register_buffer(f"ooi_masks_{i}", torch.cat([self.ooi_masks[i], torch.zeros([additional_pts_num], device='cuda').bool()]) )
self.ooi_masks[i] = getattr(self, f"ooi_masks_{i}")
self.register_buffer(f"_delete_mask", torch.ones_like(self.ooi_masks[0].float()))
# project to 3D space
xyz = xyz[:, :3]
color = color
pcd = BasicPointCloud(
points=xyz, colors=color, normals=np.zeros((xyz.shape[0], 3))
)
self.create_from_pcd(pcd, 10)
self.training_setup()
elif self.cfg.geometry_convert_from.startswith("shap-e:"):
shap_e_guidance = threestudio.find("shap-e-guidance")(
self.cfg.shap_e_guidance_config
)
prompt = self.cfg.geometry_convert_from[len("shap-e:") :]
xyz, color = shap_e_guidance(prompt)
pcd = BasicPointCloud(
points=xyz, colors=color, normals=np.zeros((xyz.shape[0], 3))
)
self.create_from_pcd(pcd, 10)
self.training_setup()
# Support Initialization from OpenLRM, Please see https://github.com/Adamdad/threestudio-lrm
elif self.cfg.geometry_convert_from.startswith("lrm:"):
lrm_guidance = threestudio.find("lrm-guidance")(
self.cfg.shap_e_guidance_config
)
prompt = self.cfg.geometry_convert_from[len("lrm:") :]
xyz, color = lrm_guidance(prompt)
pcd = BasicPointCloud(
points=xyz, colors=color, normals=np.zeros((xyz.shape[0], 3))
)
self.create_from_pcd(pcd, 10)
self.training_setup()
elif os.path.exists(self.cfg.geometry_convert_from):
threestudio.info(
"Loading point cloud from %s" % self.cfg.geometry_convert_from
)
if self.cfg.geometry_convert_from.endswith(".ckpt"):
ckpt_dict = torch.load(self.cfg.geometry_convert_from)
num_pts = ckpt_dict["state_dict"]["geometry._xyz"].shape[0]
pcd = BasicPointCloud(
points=np.zeros((num_pts, 3)),
colors=np.zeros((num_pts, 3)),
normals=np.zeros((num_pts, 3)),
)
self.create_from_pcd(pcd, 10)
self.training_setup()
new_ckpt_dict = {}
for key in self.state_dict():
if ckpt_dict["state_dict"].__contains__("geometry." + key):
new_ckpt_dict[key] = ckpt_dict["state_dict"]["geometry." + key]
else:
new_ckpt_dict[key] = self.state_dict()[key]
self.load_state_dict(new_ckpt_dict)
elif self.cfg.geometry_convert_from.endswith(".ply"):
if self.cfg.load_ply_only_vertex:
plydata = PlyData.read(self.cfg.geometry_convert_from)
vertices = plydata["vertex"]
positions = np.vstack(
[vertices["x"], vertices["y"], vertices["z"]]
).T
if vertices.__contains__("red"):
colors = (
np.vstack(
[vertices["red"], vertices["green"], vertices["blue"]]
).T
/ 255.0
)
else:
shs = np.random.random((positions.shape[0], 3)) / 255.0
C0 = 0.28209479177387814
colors = shs * C0 + 0.5
normals = np.zeros_like(positions)
pcd = BasicPointCloud(
points=positions, colors=colors, normals=normals
)
self.create_from_pcd(pcd, 10)
else:
self.load_ply(self.cfg.geometry_convert_from)
self.training_setup()
else:
threestudio.info("Geometry not found, initilization with random points")
num_pts = self.cfg.init_num_pts
phis = np.random.random((num_pts,)) * 2 * np.pi
costheta = np.random.random((num_pts,)) * 2 - 1
thetas = np.arccos(costheta)
mu = np.random.random((num_pts,))
radius = self.cfg.pc_init_radius * np.cbrt(mu)
x = radius * np.sin(thetas) * np.cos(phis)
y = radius * np.sin(thetas) * np.sin(phis)
z = radius * np.cos(thetas)
xyz = np.stack((x, y, z), axis=1)
shs = np.random.random((num_pts, 3)) / 255.0
C0 = 0.28209479177387814
color = shs * C0 + 0.5
pcd = BasicPointCloud(
points=xyz, colors=color, normals=np.zeros((num_pts, 3))
)
self.create_from_pcd(pcd, 10)
self.training_setup()
def add_pc_from_novel_view(self, rgb, mask, depth, c2w, save_path=None):
W, H = parse_wh(self.cfg.img_resolution)
if max(W, H) > self.cfg.pc_max_resolution:
W, H = int(W / max(W, H) * self.cfg.pc_max_resolution), int(H / max(W, H) * self.cfg.pc_max_resolution)
# depth estimation -> add points.
mask = fill_mask(mask)
blur_mask = Image.fromarray(cv2.blur(np.array(mask).astype(np.float32), (7, 7)) > 0)
res = self.inpaint(img=rgb, mask=blur_mask, prompt=self.side_prompt)
self.geowizard_pipe.to('cuda')
depth_unaligned = self.geowizard_pipe(
res,
denoising_steps = 25,
ensemble_size = 3,
processing_res = 768,
match_input_res = False,
domain = 'outdoor',
color_map = 'Spectral',
gt_depth = None, mask = None,
show_progress_bar = True)['depth_np']
self.geowizard_pipe.to('cpu')
prev_depth = depth_unaligned[~np.array(mask.resize((768,768)))]
# inpaint the depth map
depth_nd = depth[0].cpu().numpy().astype(np.uint8)
inpaint_mask = np.logical_and(~np.array(mask) , depth[0].cpu().numpy().astype(np.uint8)==0 ).astype(np.uint8)
l, r = depth[depth>0].min().item(), depth.max().item()
depth = (depth - l) / (r - l) * 255
depth = cv2.inpaint(depth[0].cpu().numpy().astype(np.uint8), inpaint_mask, 3, cv2.INPAINT_TELEA)
depth = torch.tensor(depth)[None].cuda().float() / 255
reproj_func = lambda x: (x - prev_depth.min().item()) / (prev_depth.max().item() - prev_depth.min().item()) * (r-l) + l
depth = depth * (prev_depth.max() - prev_depth.min()) + prev_depth.min()
depth_tensor = torch.nn.functional.interpolate(depth[None].cuda(), 768, mode='nearest') * 2 - 1
_masks = cv2.blur(np.array(mask.resize((768, 768))).astype(float), (20, 20)) > 0
mask_tensor = torch.from_numpy(_masks.astype(np.float32))[None, None].cuda()
bg_xyz_pc, _ = self.img2pc_inpaint(res, gt_depth=depth_tensor, mask=1-mask_tensor, proj_func=reproj_func, c2w=c2w)
mask = np.array(Image.fromarray(_masks).resize((W, H)))
new_xyz = bg_xyz_pc[mask][:, :3]
res = res.resize((W, H))
new_color = np.array(res)[mask] / 255
pcd = BasicPointCloud(points=new_xyz, colors=new_color, normals=np.zeros((new_xyz.shape[0], 3)))
self.merge_from_pcd(pcd, 10)
original_wh = parse_wh(self.cfg.img_resolution)
return res.resize((original_wh)), Image.fromarray(_masks).resize((original_wh))
@property
def get_scaling(self):
if self.cfg.sphere:
return self.scaling_activation(
torch.mean(self._scaling, dim=-1).unsqueeze(-1).repeat(1, 3)
).clip(0, self.cfg.max_scaling)
return self.scaling_activation(self._scaling).clip(0, self.cfg.max_scaling)
@property
def get_rotation(self):
return self.rotation_activation(self._rotation)
@property
def get_language_feature(self):
return self._language_feature
@property
def get_xyz(self):
ret = self._xyz
if self.noise_ratio > 0.0:
offset = torch.zeros_like(ret)
for idx in range(len(self.ooi_masks)):
ooi_masks = getattr(self, f"ooi_masks_{idx}")
offset[ooi_masks] = torch.rand(3, device='cuda') * self.noise_ratio
return ret
@property
def get_features(self):
features_dc = self._features_dc
features_dc = features_dc.clip(-self.color_clip, self.color_clip)
features_rest = self._features_rest
return torch.cat((features_dc, features_rest), dim=1)
@property
def get_opacity(self):
if self._opacity_mask is None:
ret = self.opacity_activation(self._opacity)
else:
ret = self.opacity_activation(self._opacity) * self._opacity_mask.unsqueeze(-1)
if self._delete_mask is None:
return ret
else:
return ret * self._delete_mask.unsqueeze(-1)
@property
def get_normal(self):
if self.cfg.pred_normal:
return self._normal
else:
raise ValueError("Normal is not predicted")
def recover_xyzrot(self):
self._xyz = torch.nn.Parameter(self.fixed_xyz)
self._rotation = torch.nn.Parameter(self.fixed_rot)
def random_rotate(self, rotate_aug_scale, apply_rotate):
if self.fixed_xyz is None:
self.fixed_xyz = self.get_xyz.data
self.fixed_rot = self.get_rotation.data
if apply_rotate:
ooi_mask = self.ooi_masks_0.view(-1).byte().to(device='cuda').float()
rotate = random.randint(-rotate_aug_scale, rotate_aug_scale)
rot_matrix = rotation_matrix(0, 0, rotate).cuda()
prev_xyz = self.fixed_xyz.clone()
ooi_xyz = prev_xyz[ooi_mask.bool()]
mean = ooi_xyz.mean(0)
ooi_xyz = ooi_xyz - mean
after_xyz = torch.einsum('ab,nb->na', rot_matrix, ooi_xyz) + mean
prev_xyz[ooi_mask.bool()] = after_xyz
self._xyz = torch.nn.Parameter(prev_xyz)
prev_rotation = self.fixed_rot.clone()
prev_rotation_mtx = build_rotation(prev_rotation)
after_rotation_mtx = torch.einsum('ab,nbc->nac', rot_matrix, prev_rotation_mtx)
after_rotation = torch.from_numpy(R.from_matrix(after_rotation_mtx.detach().cpu()).as_quat()).cuda().float()
after_rotation = torch.einsum('ab,nb->na', REORDER_MTX, after_rotation)
prev_rotation[ooi_mask.bool()] = after_rotation[ooi_mask.bool()]
self._rotation = torch.nn.Parameter(prev_rotation)
else:
self.recover_xyzrot()
def get_covariance(self, scaling_modifier=1):
return self.covariance_activation(
self.get_scaling, scaling_modifier, self._rotation
)
def create_from_pcd(self, pcd: BasicPointCloud, spatial_lr_scale: float):
self.spatial_lr_scale = spatial_lr_scale
fused_point_cloud = torch.tensor(np.asarray(pcd.points)).float().cuda()
fused_color = RGB2SH(torch.tensor(np.asarray(pcd.colors)).float().cuda())
features = (
torch.zeros((fused_color.shape[0], 3, (self.max_sh_degree + 1) ** 2))
.float()
.cuda()
)
features[:, :3, 0] = fused_color
features[:, 3:, 1:] = 0.0
threestudio.info(
f"Number of points at initialisation:{fused_point_cloud.shape[0]}"
)
dist2 = torch.clamp_min(
distCUDA2(torch.from_numpy(np.asarray(pcd.points)).float().cuda()),
0.0000001,
)
scales = torch.log(torch.sqrt(dist2))[..., None].repeat(1, 3)
rots = torch.zeros((fused_point_cloud.shape[0], 4), device="cuda")
rots[:, 0] = 1
opacities = inverse_sigmoid(
self.cfg.opacity_init
* torch.ones(
(fused_point_cloud.shape[0], 1), dtype=torch.float, device="cuda"
)
)
self._xyz = nn.Parameter(fused_point_cloud.requires_grad_(True))
self._features_dc = nn.Parameter(
features[:, :, 0:1].transpose(1, 2).contiguous().requires_grad_(True)
)
self._features_rest = nn.Parameter(
features[:, :, 1:].transpose(1, 2).contiguous().requires_grad_(True)
)
self._scaling = nn.Parameter(scales.requires_grad_(True))
self._rotation = nn.Parameter(rots.requires_grad_(True))
self._opacity = nn.Parameter(opacities.requires_grad_(True))
if self.cfg.pred_normal:
normals = torch.zeros((fused_point_cloud.shape[0], 3), device="cuda")
self._normal = nn.Parameter(normals.requires_grad_(True))
self.max_radii2D = torch.zeros((self._xyz.shape[0]), device="cuda")
self.fused_point_cloud = fused_point_cloud.cpu().clone().detach()
self.features = features.cpu().clone().detach()
self.scales = scales.cpu().clone().detach()
self.rots = rots.cpu().clone().detach()
self.opacities = opacities.cpu().clone().detach()
language_feature = torch.zeros((self._xyz.shape[0], 3), device="cuda")
self._language_feature = torch.nn.Parameter(language_feature.requires_grad_(True))
def merge_from_pcd(self, pcd: BasicPointCloud, spatial_lr_scale: float):
self.spatial_lr_scale = spatial_lr_scale
fused_point_cloud = torch.tensor(np.asarray(pcd.points)).float().cuda()
fused_color = RGB2SH(torch.tensor(np.asarray(pcd.colors)).float().cuda())
features = (
torch.zeros((fused_color.shape[0], 3, (self.max_sh_degree + 1) ** 2))
.float()
.cuda()
)
features[:, :3, 0] = fused_color
features[:, 3:, 1:] = 0.0
threestudio.info(
f"Number of points at merging:{fused_point_cloud.shape[0]}"
)
dist2 = torch.clamp_min(
distCUDA2(torch.from_numpy(np.asarray(pcd.points)).float().cuda()),
0.0000001,
)
scales = torch.log(torch.sqrt(dist2))[..., None].repeat(1, 3)
rots = torch.zeros((fused_point_cloud.shape[0], 4), device="cuda")
rots[:, 0] = 1
opacities = inverse_sigmoid(
self.cfg.opacity_init
* torch.ones(
(fused_point_cloud.shape[0], 1), dtype=torch.float, device="cuda"
)
)
self.densification_postfix(
fused_point_cloud,
features[:, :, 0:1].transpose(1, 2).contiguous(),
features[:, :, 1:].transpose(1, 2).contiguous(),
opacities,
scales,
rots,
None,
torch.zeros((fused_point_cloud.shape[0], 3), device="cuda")
)
for idx in range(len(self.ooi_masks)):
# self.ooi_masks[idx] = torch.cat([self.ooi_masks[idx], torch.ones([fused_point_cloud.shape[0]], device='cuda') > 0])