-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.py
More file actions
289 lines (250 loc) · 11.3 KB
/
view.py
File metadata and controls
289 lines (250 loc) · 11.3 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
#
# This file is licensed under the Apache 2.0 license in viewer/LICENSE.md.
#
import os
from OpenGL.GL import *
import numpy as np
from threading import Lock
from argparse import ArgumentParser
from imgui_bundle import imgui_ctx, imgui
import gray.scene
from viewer import Viewer
from viewer.types import ViewerMode
from viewer.widgets.image import TorchImage
from viewer.widgets.cameras.fps import FPSCamera
from viewer.widgets.monitor import PerformanceMonitor
from viewer.widgets.ellipsoid_viewer import EllipsoidViewer
from dataclasses import dataclass
import tyro
from tyro.conf import subcommand, arg
from typing import Annotated, List, Optional
import json
@dataclass
class ViewerCLI:
model_path: Annotated[str, arg(aliases=["-m"])]
iteration: Annotated[int, arg(aliases=["-t"])] = -1
class GaussianViewer(Viewer):
def __init__(
self,
raytracer: "Raytracer",
train_cameras: List["CameraInfo"],
test_cameras: Optional[List["CameraInfo"]],
training=False,
):
super().__init__(ViewerMode.LOCAL)
self.window_title = "Gaussian Viewer"
self.gaussian_lock = Lock()
self.raytracer = raytracer
self.train_cameras = train_cameras
self.test_cameras = test_cameras
self.must_rebuild_bvh = False
self.training = training
def import_server_modules(self):
global torch
import torch
global gray
import gray
def create_widgets(self):
init_camera = self.test_cameras[0] if self.test_cameras else self.train_cameras[0]
self.camera_widget = FPSCamera(self.mode, 1297, 840, 47, 0.001, 100)
self.camera_widget.set(init_camera)
self.point_view = TorchImage(self.mode)
self.ellipsoid_viewer = EllipsoidViewer(self.mode)
# * Render modes
self.render_modes = ["Splats"]
if self.raytracer.cfg.render_depth:
self.render_modes.append("Depth")
if not self.training and not self.raytracer.cfg.post_mlp:
self.render_modes.append("Ellipsoids")
self.render_mode = "Splats"
# * Render settings
self.scaling_modifier = 1.0
# z-near for the renderer
self.znear = 0.0
# * Camera view
self.current_train_cam = -1
self.current_test_cam = -1
self.camera_widget.set(init_camera)
def step(self):
camera = gray.scene.CameraInfo(
uid=0,
R=self.camera_widget.to_world[:3, :3],
T=None,
origin=self.camera_widget.origin,
fov_x=self.camera_widget.fov_x,
fov_y=self.camera_widget.fov_y,
image_path=None,
image_name=None,
image_width=self.camera_widget.res_x,
image_height=self.camera_widget.res_y,
is_test=False,
)
if self.ellipsoid_viewer.num_gaussians is None and not self.raytracer.cfg.post_mlp:
gaussians = self.raytracer.cuda_module.get_gaussians()
if self.raytracer.cfg.sh:
colors = (gaussians.sh_coeffs_dc * 0.28209479177387814 + 0.5) / 3
else:
colors = gaussians.channels / 3
self.ellipsoid_viewer.upload(
gaussians.mean.detach().cpu().numpy(),
gaussians.rotation.detach().cpu().numpy(),
gaussians.scale.exp().detach().cpu().numpy(),
gaussians.opacity.sigmoid().detach().cpu().numpy(),
colors.detach().cpu().numpy(),
)
if self.render_mode in ["Splats", "Depth"]:
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
with torch.no_grad():
with self.gaussian_lock:
config = self.raytracer.cuda_module.get_config()
config.global_scale_factor.copy_(self.scaling_modifier)
if self.must_rebuild_bvh:
self.raytracer.cuda_module.rebuild_bvh()
self.must_rebuild_bvh = False
render = self.raytracer(camera, znear=self.znear).clamp(0, 1)
if self.render_mode == "Splats":
net_image = render.moveaxis(0, -1)
else:
framebuffer = self.raytracer.cuda_module.get_framebuffer()
net_image = framebuffer.output_depth.detach().clone().repeat(1, 1, 3)
net_image = (net_image - net_image.min()) / (net_image.max() - net_image.min())
end.record()
end.synchronize()
self.point_view.step(net_image)
render_time = start.elapsed_time(end)
if self.render_mode == "Ellipsoids":
self.ellipsoid_viewer.step(self.camera_widget)
render_time = glGetQueryObjectuiv(self.ellipsoid_viewer.query, GL_QUERY_RESULT) / 1e6
def show_gui(self):
with imgui_ctx.begin(f"Point View Settings"):
_, render_mode_choice = imgui.list_box(
"Render Mode", self.render_modes.index(self.render_mode), self.render_modes
)
self.render_mode = self.render_modes[render_mode_choice]
imgui.separator_text("Render Settings")
if self.render_mode in ["Splats", "Depth"]:
scaling_changed, self.scaling_modifier = imgui.drag_float(
"Scaling Modifier", self.scaling_modifier, v_min=0, v_max=2, v_speed=0.01
)
if scaling_changed:
self.must_rebuild_bvh = True
if imgui.is_item_hovered() and imgui.is_mouse_clicked(imgui.MouseButton_.right):
self.scaling_modifier = 1.0
znear_changed, self.znear = imgui.drag_float(
"Z Near", self.znear, v_min=0.0, v_max=1000.0, v_speed=0.01
)
if znear_changed:
# No BVH rebuild required, just update camera param next render
pass
if self.render_mode == "Ellipsoids":
_, self.ellipsoid_viewer.scaling_modifier = imgui.drag_float(
"Scaling Factor",
self.ellipsoid_viewer.scaling_modifier,
v_min=0,
v_max=10,
v_speed=0.01,
)
_, self.ellipsoid_viewer.render_floaters = imgui.checkbox(
"Render Floaters", self.ellipsoid_viewer.render_floaters
)
_, self.ellipsoid_viewer.limit = imgui.drag_float(
"Alpha Threshold", self.ellipsoid_viewer.limit, v_min=0, v_max=1, v_speed=0.01
)
imgui.separator_text("Camera Settings")
self.camera_widget.show_gui()
using_train_cam = self.current_train_cam != -1
if not using_train_cam:
imgui.push_style_color(imgui.Col_.frame_bg, (0.0, 0.0, 0.0, 0.0))
imgui.push_style_color(imgui.Col_.frame_bg_hovered, (0.0, 0.0, 0.0, 0.0))
imgui.push_style_color(imgui.Col_.frame_bg_active, (0.0, 0.0, 0.0, 0.0))
train_cam_changed, self.current_train_cam = imgui.input_int(
"Set Train View", self.current_train_cam, step=1, step_fast=10
)
if (
not train_cam_changed
and imgui.is_item_hovered()
and imgui.is_mouse_clicked(imgui.MouseButton_.right)
):
train_cam_changed = True
self.current_train_cam = 0
if not using_train_cam:
imgui.pop_style_color(3)
self.current_train_cam = max(
-1, min(len(self.train_cameras) - 1, self.current_train_cam)
)
using_test_cam = self.current_test_cam != -1
if not using_test_cam:
imgui.push_style_color(imgui.Col_.frame_bg, (0.0, 0.0, 0.0, 0.0))
imgui.push_style_color(imgui.Col_.frame_bg_hovered, (0.0, 0.0, 0.0, 0.0))
imgui.push_style_color(imgui.Col_.frame_bg_active, (0.0, 0.0, 0.0, 0.0))
test_cam_changed, self.current_test_cam = imgui.input_int(
"Set Test View", self.current_test_cam, step=1, step_fast=10
)
if (
not test_cam_changed
and imgui.is_item_hovered()
and imgui.is_mouse_clicked(imgui.MouseButton_.right)
):
test_cam_changed = True
self.current_test_cam = 0
if not using_test_cam:
imgui.pop_style_color(3)
self.current_test_cam = max(-1, min(len(self.test_cameras) - 1, self.current_test_cam))
if train_cam_changed:
self.camera_widget.set(self.train_cameras[self.current_train_cam])
self.current_test_cam = -1
elif test_cam_changed:
self.camera_widget.set(self.test_cameras[self.current_test_cam])
self.current_train_cam = -1
with imgui_ctx.begin("Point View"):
if self.render_mode in ["Splats", "Depth"]:
self.point_view.show_gui()
else:
self.ellipsoid_viewer.show_gui()
if imgui.is_item_hovered():
self.camera_widget.process_mouse_input()
if imgui.is_item_focused() or imgui.is_item_hovered():
self.camera_widget.process_keyboard_input()
def client_send(self):
return None, {
"scaling_modifier": self.scaling_modifier,
"render_mode": self.render_mode,
"znear": float(self.znear),
}
def server_recv(self, _, text):
self.scaling_modifier = text["scaling_modifier"]
self.render_mode = text["render_mode"]
if "znear" in text:
self.znear = float(text["znear"])
if __name__ == "__main__":
cli, unknown_args = tyro.cli(ViewerCLI, return_unknown_args=True)
# * Defer loading slower modules after CLI parsing
from gray.prelude import Config, search_for_max_iteration, Raytracer, CameraInfo
# * Load the config from JSON and allow for Config overrides
saved_cli_path = os.path.join(cli.model_path, "config.json")
cfg = tyro.cli(
Config, args=unknown_args, default=Config(**json.load(open(saved_cli_path, "r")))
)
# * Make it possible to point directly to a gaussians file
if cli.model_path.endswith(".safetensors"):
iteration = cfg.iteration
save_path = cli.model_path
elif cli.iteration != -1:
iteration = cli.iteration
save_path = os.path.join(cli.model_path, f"gaussians_{iteration:05d}.safetensors")
else:
iteration = search_for_max_iteration(cli.model_path)
save_path = os.path.join(cli.model_path, f"gaussians_{iteration:05d}.safetensors")
# * Load the cameras and raytracer
cameras = [
CameraInfo.from_json(x)
for x in json.load(open(os.path.join(cli.model_path, "cameras.json"), "r"))
]
image_width, image_height = cameras[0].image_width, cameras[0].image_height
raytracer = Raytracer.from_safetensors(cfg, save_path, image_width, image_height)
train_cameras = [c for c in cameras if not c.is_test]
test_cameras = [c for c in cameras if c.is_test]
viewer = GaussianViewer(raytracer, train_cameras, test_cameras)
viewer.run()