-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinitialize_pcd.py
172 lines (153 loc) · 5.96 KB
/
initialize_pcd.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
import os
import numpy as np
import tigre.algorithms as algs
import open3d as o3d
import sys
import argparse
import os.path as osp
import json
import pickle
from tqdm import trange
import copy
import torch
sys.path.append("./")
from r2_gaussian.utils.ct_utils import get_geometry_tigre, recon_volume
from r2_gaussian.arguments import ParamGroup, ModelParams, PipelineParams
from r2_gaussian.utils.plot_utils import show_one_volume, show_two_volume
from r2_gaussian.gaussian import GaussianModel, query, initialize_gaussian
from r2_gaussian.utils.image_utils import metric_vol
from r2_gaussian.dataset import Scene
from r2_gaussian.utils.general_utils import t2a
np.random.seed(0)
class InitParams(ParamGroup):
def __init__(self, parser):
self.recon_method = "fdk"
self.n_points = 50000
self.density_thresh = 0.05
self.density_rescale = 0.15
self.random_density_max = 1.0 # Parameters for random mode
super().__init__(parser, "Initialization Parameters")
def init_pcd(
projs,
angles,
geo,
scanner_cfg,
args: InitParams,
save_path,
):
"Initialize Gaussians."
recon_method = args.recon_method
n_points = args.n_points
assert recon_method in ["random", "fdk"], "--recon_method not supported."
if recon_method == "random":
print(f"Initialize random point clouds.")
sampled_positions = np.array(scanner_cfg["offOrigin"])[None, ...] + np.array(
scanner_cfg["sVoxel"]
)[None, ...] * (np.random.rand(n_points, 3) - 0.5)
sampled_densities = (
np.random.rand(
n_points,
)
* args.random_density_max
)
else:
# Use traditional algorithms for initialization
print(
f"Initialize point clouds with the volume reconstructed from {recon_method}."
)
vol = recon_volume(projs, angles, copy.deepcopy(geo), recon_method)
# show_one_volume(vol)
density_mask = vol > args.density_thresh
valid_indices = np.argwhere(density_mask)
offOrigin = np.array(scanner_cfg["offOrigin"])
dVoxel = np.array(scanner_cfg["dVoxel"])
sVoxel = np.array(scanner_cfg["sVoxel"])
assert (
valid_indices.shape[0] >= n_points
), "Valid voxels less than target number of sampling. Check threshold"
sampled_indices = valid_indices[
np.random.choice(len(valid_indices), n_points, replace=False)
]
sampled_positions = sampled_indices * dVoxel - sVoxel / 2 + offOrigin
sampled_densities = vol[
sampled_indices[:, 0],
sampled_indices[:, 1],
sampled_indices[:, 2],
]
sampled_densities = sampled_densities * args.density_rescale
out = np.concatenate([sampled_positions, sampled_densities[:, None]], axis=-1)
np.save(save_path, out)
print(f"Initialization saved in {save_path}.")
def main(
args, init_args: InitParams, model_args: ModelParams, pipe_args: PipelineParams
):
# Read scene
data_path = args.data
model_args.source_path = data_path
scene = Scene(model_args, False) #! Here we scale the scene to [-1,1]^3 space.
train_cameras = scene.getTrainCameras()
projs_train = np.concatenate(
[t2a(cam.original_image) for cam in train_cameras], axis=0
)
angles_train = np.stack([t2a(cam.angle) for cam in train_cameras], axis=0)
scanner_cfg = scene.scanner_cfg
geo = get_geometry_tigre(scanner_cfg)
save_path = args.output
if not save_path:
if osp.exists(osp.join(data_path, "meta_data.json")):
save_path = osp.join(data_path, "init_" + osp.basename(data_path) + ".npy")
elif data_path.split(".")[-1] in ["pickle", "pkl"]:
save_path = osp.join(
osp.dirname(data_path),
"init_" + osp.basename(data_path).split(".")[0] + ".npy",
)
else:
assert False, f"Could not recognize scene type: {args.source_path}."
assert not osp.exists(
save_path
), f"Initialization file {save_path} exists! Delete it first."
os.makedirs(osp.dirname(save_path), exist_ok=True)
init_pcd(
projs=projs_train,
angles=angles_train,
geo=geo,
scanner_cfg=scanner_cfg,
args=init_args,
save_path=save_path,
)
# Evaluate using ground truth volume (for debug only)
if args.evaluate:
with torch.no_grad():
model_args.ply_path = save_path
scale_bound = None
volume_to_world = max(scanner_cfg["sVoxel"])
if model_args.scale_min and model_args.scale_max:
scale_bound = (
np.array([model_args.scale_min, model_args.scale_max])
* volume_to_world
)
gaussians = GaussianModel(scale_bound)
initialize_gaussian(gaussians, model_args, None)
vol_pred = query(
gaussians,
scanner_cfg["offOrigin"],
scanner_cfg["nVoxel"],
scanner_cfg["sVoxel"],
pipe_args,
)["vol"]
vol_gt = scene.vol_gt.cuda()
psnr_3d, _ = metric_vol(vol_gt, vol_pred, "psnr")
print(f"3D PSNR for initial Gaussians: {psnr_3d}")
# show_two_volume(vol_gt, vol_pred, title1="gt", title2="init")
if __name__ == "__main__":
# fmt: off
parser = argparse.ArgumentParser(description="Generate initialization parameters")
init_parser = InitParams(parser)
lp = ModelParams(parser)
pp = PipelineParams(parser)
parser.add_argument("--data", type=str, help="Path to data.")
parser.add_argument("--output", default=None, type=str, help="Path to output.")
parser.add_argument("--evaluate", default=False, action="store_true", help="Add this flag to evaluate quality (given GT volume, for debug only)")
# fmt: on
args = parser.parse_args()
main(args, init_parser.extract(args), lp.extract(args), pp.extract(args))