-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.py
403 lines (331 loc) · 14.9 KB
/
render.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
import os
from argparse import ArgumentParser
from os import makedirs
import imageio
import numpy as np
import torch
import torchvision
from tqdm import tqdm
from arguments import ModelParams, PipelineParams, get_combined_args
from gaussian_renderer import GaussianModel
from gaussian_renderer import render
from scene import Scene, DeformModel, NonRigidDeformationModel
from utils.general_utils import safe_state
from utils.pose_utils import pose_spherical, render_wander_path
def render_set(
model_path,
load2gpu_on_the_fly,
name,
iteration,
views,
gaussians,
pipeline,
background,
deform,
fine_deform,
no_mlp
):
render_path = os.path.join(model_path, name, "ours_{}".format(iteration), "renders")
gts_path = os.path.join(model_path, name, "ours_{}".format(iteration), "gt")
depth_path = os.path.join(model_path, name, "ours_{}".format(iteration), "depth")
makedirs(render_path, exist_ok=True)
makedirs(gts_path, exist_ok=True)
makedirs(depth_path, exist_ok=True)
for idx, view in enumerate(tqdm(views, desc="Rendering progress")):
if load2gpu_on_the_fly:
view.load2device()
if idx == 0:
print(view.world_view_transform.T)
print(view.projection_matrix.T)
fid = view.fid
xyz = gaussians.get_xyz
time_input = fid.view(-1) # fid.unsqueeze(0).expand(xyz.shape[0], -1)
d_xyz, d_rotation, d_scaling = deform.step(xyz.detach(), time_input, use_mlp=not no_mlp)[0]
fd = None if fine_deform is None else fine_deform.step(xyz.detach(), d_xyz, d_rotation, time_input)
results = render(view, gaussians, pipeline, background, d_xyz, d_rotation, d_scaling, f_deform=fd)
rendering = results["render"]
depth = results["depth"]
gt = view.original_image[0:3, :, :]
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(idx) + ".png"))
torchvision.utils.save_image(gt, os.path.join(gts_path, '{0:05d}'.format(idx) + ".png"))
# torchvision.utils.save_image(rendering, os.path.join(render_path, view.image_name))
# torchvision.utils.save_image(gt, os.path.join(gts_path, view.image_name))
if depth is not None:
depth = depth / (depth.max() + 1e-5)
torchvision.utils.save_image(depth, os.path.join(depth_path, '{0:05d}'.format(idx) + ".png"))
def interpolate_time(
model_path,
load2gpt_on_the_fly,
name,
iteration,
views,
gaussians,
pipeline,
background,
deform,
fine_deform,
no_mlp,
):
render_path = os.path.join(model_path, name, "interpolate_{}".format(iteration), "renders")
depth_path = os.path.join(model_path, name, "interpolate_{}".format(iteration), "depth")
makedirs(render_path, exist_ok=True)
makedirs(depth_path, exist_ok=True)
to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8)
frame = 150
idx = torch.randint(0, len(views), (1,)).item()
view = views[idx]
renderings = []
for t in tqdm(range(0, frame, 1), desc="Rendering progress"):
fid = torch.Tensor([t / (frame - 1)]).cuda()
xyz = gaussians.get_xyz
time_input = fid.view(-1) # fid.unsqueeze(0).expand(xyz.shape[0], -1)
d_xyz, d_rotation, d_scaling = deform.step(xyz.detach(), time_input, use_mlp=not no_mlp)[0]
fd = None if fine_deform is None else fine_deform.step(xyz.detach(), d_xyz, d_rotation, time_input)
results = render(view, gaussians, pipeline, background, d_xyz, d_rotation, d_scaling, f_deform=fd)
rendering = results["render"]
renderings.append(to8b(rendering.cpu().numpy()))
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(t) + ".png"))
depth = results["depth"]
if depth is not None:
depth = depth / (depth.max() + 1e-5)
torchvision.utils.save_image(depth, os.path.join(depth_path, '{0:05d}'.format(t) + ".png"))
renderings = np.stack(renderings, 0).transpose(0, 2, 3, 1)
imageio.mimwrite(os.path.join(render_path, 'video.mp4'), renderings, fps=30, quality=8)
def interpolate_view(
model_path,
load2gpt_on_the_fly,
name,
iteration,
views,
gaussians,
pipeline,
background,
timer
):
render_path = os.path.join(model_path, name, "interpolate_view_{}".format(iteration), "renders")
depth_path = os.path.join(model_path, name, "interpolate_view_{}".format(iteration), "depth")
# acc_path = os.path.join(model_path, name, "interpolate_view_{}".format(iteration), "acc")
makedirs(render_path, exist_ok=True)
makedirs(depth_path, exist_ok=True)
# makedirs(acc_path, exist_ok=True)
frame = 150
to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8)
idx = torch.randint(0, len(views), (1,)).item()
view = views[idx] # Choose a specific time for rendering
render_poses = torch.stack(render_wander_path(view), 0)
# render_poses = torch.stack([pose_spherical(angle, -30.0, 4.0) for angle in np.linspace(-180, 180, frame + 1)[:-1]],
# 0)
renderings = []
for i, pose in enumerate(tqdm(render_poses, desc="Rendering progress")):
fid = view.fid
matrix = np.linalg.inv(np.array(pose))
R = -np.transpose(matrix[:3, :3])
R[:, 0] = -R[:, 0]
T = -matrix[:3, 3]
view.reset_extrinsic(R, T)
xyz = gaussians.get_xyz
time_input = fid.view(-1) # fid.unsqueeze(0).expand(xyz.shape[0], -1)
d_xyz, d_rotation, d_scaling = timer.step(xyz.detach(), time_input)[0]
results = render(view, gaussians, pipeline, background, d_xyz, d_rotation, d_scaling)
rendering = results["render"]
renderings.append(to8b(rendering.cpu().numpy()))
# acc = results["acc"]
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(i) + ".png"))
depth = results["depth"]
if depth is not None:
depth = depth / (depth.max() + 1e-5)
torchvision.utils.save_image(depth, os.path.join(depth_path, '{0:05d}'.format(i) + ".png"))
# torchvision.utils.save_image(acc, os.path.join(acc_path, '{0:05d}'.format(i) + ".png"))
renderings = np.stack(renderings, 0).transpose(0, 2, 3, 1)
imageio.mimwrite(os.path.join(render_path, 'video.mp4'), renderings, fps=30, quality=8)
def interpolate_all(
model_path,
load2gpt_on_the_fly,
name,
iteration,
views,
gaussians,
pipeline,
background,
deform,
fine_deform,
no_mlp,
):
render_path = os.path.join(model_path, name, "interpolate_all_{}".format(iteration), "renders")
depth_path = os.path.join(model_path, name, "interpolate_all_{}".format(iteration), "depth")
makedirs(render_path, exist_ok=True)
makedirs(depth_path, exist_ok=True)
frame = 150
render_poses = torch.stack([pose_spherical(angle, -30.0, 4.0) for angle in np.linspace(-180, 180, frame + 1)[:-1]],
0)
to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8)
idx = torch.randint(0, len(views), (1,)).item()
view = views[idx] # Choose a specific time for rendering
renderings = []
for i, pose in enumerate(tqdm(render_poses, desc="Rendering progress")):
fid = torch.Tensor([i / (frame - 1)]).cuda()
matrix = np.linalg.inv(np.array(pose))
R = -np.transpose(matrix[:3, :3])
R[:, 0] = -R[:, 0]
T = -matrix[:3, 3]
view.reset_extrinsic(R, T)
xyz = gaussians.get_xyz
time_input = fid.view(-1) # fid.unsqueeze(0).expand(xyz.shape[0], -1)
d_xyz, d_rotation, d_scaling = deform.step(xyz.detach(), time_input, use_mlp=not no_mlp)[0]
fd = None if fine_deform is None else fine_deform.step(xyz.detach(), d_xyz, d_rotation, time_input)
results = render(view, gaussians, pipeline, background, d_xyz, d_rotation, d_scaling, f_deform=fd)
rendering = results["render"]
renderings.append(to8b(rendering.cpu().numpy()))
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(i) + ".png"))
depth = results["depth"]
if depth is not None:
depth = depth / (depth.max() + 1e-5)
torchvision.utils.save_image(depth, os.path.join(depth_path, '{0:05d}'.format(i) + ".png"))
renderings = np.stack(renderings, 0).transpose(0, 2, 3, 1)
imageio.mimwrite(os.path.join(render_path, 'video.mp4'), renderings, fps=30, quality=8)
def interpolate_poses(
model_path,
load2gpt_on_the_fly,
name,
iteration,
views,
gaussians,
pipeline,
background,
timer
):
render_path = os.path.join(model_path, name, "interpolate_pose_{}".format(iteration), "renders")
depth_path = os.path.join(model_path, name, "interpolate_pose_{}".format(iteration), "depth")
makedirs(render_path, exist_ok=True)
makedirs(depth_path, exist_ok=True)
# makedirs(acc_path, exist_ok=True)
frame = 520
to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8)
idx = torch.randint(0, len(views), (1,)).item()
view_begin = views[0] # Choose a specific time for rendering
view_end = views[-1]
view = views[idx]
R_begin = view_begin.R
R_end = view_end.R
t_begin = view_begin.T
t_end = view_end.T
renderings = []
for i in tqdm(range(frame), desc="Rendering progress"):
fid = view.fid
ratio = i / (frame - 1)
R_cur = (1 - ratio) * R_begin + ratio * R_end
T_cur = (1 - ratio) * t_begin + ratio * t_end
view.reset_extrinsic(R_cur, T_cur)
xyz = gaussians.get_xyz
time_input = fid.view(-1) # fid.unsqueeze(0).expand(xyz.shape[0], -1)
d_xyz, d_rotation, d_scaling = timer.step(xyz.detach(), time_input)[0]
results = render(view, gaussians, pipeline, background, d_xyz, d_rotation, d_scaling)
rendering = results["render"]
renderings.append(to8b(rendering.cpu().numpy()))
# depth = results["depth"]
# depth = depth / (depth.max() + 1e-5)
renderings = np.stack(renderings, 0).transpose(0, 2, 3, 1)
imageio.mimwrite(os.path.join(render_path, 'video.mp4'), renderings, fps=60, quality=8)
def interpolate_view_original(
model_path, load2gpt_on_the_fly, name, iteration, views, gaussians, pipeline, background,
timer
):
render_path = os.path.join(model_path, name, "interpolate_hyper_view_{}".format(iteration), "renders")
depth_path = os.path.join(model_path, name, "interpolate_hyper_view_{}".format(iteration), "depth")
# acc_path = os.path.join(model_path, name, "interpolate_all_{}".format(iteration), "acc")
makedirs(render_path, exist_ok=True)
makedirs(depth_path, exist_ok=True)
frame = 1000
to8b = lambda x: (255 * np.clip(x, 0, 1)).astype(np.uint8)
R = []
T = []
for view in views:
R.append(view.R)
T.append(view.T)
view = views[0]
renderings = []
for i in tqdm(range(frame), desc="Rendering progress"):
fid = torch.Tensor([i / (frame - 1)]).cuda()
query_idx = i / frame * len(views)
begin_idx = int(np.floor(query_idx))
end_idx = int(np.ceil(query_idx))
if end_idx == len(views):
break
view_begin = views[begin_idx]
view_end = views[end_idx]
R_begin = view_begin.R
R_end = view_end.R
t_begin = view_begin.T
t_end = view_end.T
ratio = query_idx - begin_idx
R_cur = (1 - ratio) * R_begin + ratio * R_end
T_cur = (1 - ratio) * t_begin + ratio * t_end
view.reset_extrinsic(R_cur, T_cur)
xyz = gaussians.get_xyz
time_input = fid.view(-1) # fid.unsqueeze(0).expand(xyz.shape[0], -1)
d_xyz, d_rotation, d_scaling = timer.step(xyz.detach(), time_input)[0]
results = render(view, gaussians, pipeline, background, d_xyz, d_rotation, d_scaling)
rendering = results["render"]
renderings.append(to8b(rendering.cpu().numpy()))
# depth = results["depth"]
# depth = depth / (depth.max() + 1e-5)
renderings = np.stack(renderings, 0).transpose(0, 2, 3, 1)
imageio.mimwrite(os.path.join(render_path, 'video.mp4'), renderings, fps=60, quality=8)
def render_sets(
dataset: ModelParams, iteration: int, pipeline: PipelineParams, skip_train: bool, skip_test: bool,
mode: str, no_mlp: bool,
):
with torch.no_grad():
print(dataset.__dict__)
gaussians = GaussianModel(dataset.sh_degree)
scene = Scene(dataset, gaussians, load_iteration=iteration, shuffle=False)
deform = DeformModel(
num_superpoints=dataset.num_superpoints,
num_knn=dataset.num_knn,
sp_net_large=dataset.sp_net_large
)
deform.load_weights(dataset.model_path)
if os.path.exists(os.path.join(dataset.model_path, 'fine_deform')):
fine_deform = NonRigidDeformationModel(small=not dataset.fine_large, is_blender=dataset.is_blender)
fine_deform.load_weights(dataset.model_path)
print('Loaded fine_deform model')
else:
fine_deform = None
bg_color = [1, 1, 1] if dataset.white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
if mode == "render":
render_func = render_set
elif mode == "time":
render_func = interpolate_time
elif mode == "view":
render_func = interpolate_view
elif mode == "pose":
render_func = interpolate_poses
elif mode == "original":
render_func = interpolate_view_original
else:
render_func = interpolate_all
if not skip_train:
render_func(dataset.model_path, dataset.load2gpu_on_the_fly, "train", scene.loaded_iter,
scene.getTrainCameras(), gaussians, pipeline, background, deform, fine_deform, no_mlp)
if not skip_test:
render_func(dataset.model_path, dataset.load2gpu_on_the_fly, "test", scene.loaded_iter,
scene.getTestCameras(), gaussians, pipeline, background, deform, fine_deform, no_mlp)
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Testing script parameters")
model = ModelParams(parser, sentinel=True)
pipeline = PipelineParams(parser)
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--skip_train", action="store_true")
parser.add_argument("--skip_test", action="store_true")
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--mode", default='render', choices=['render', 'time', 'view', 'all', 'pose', 'original'])
parser.add_argument('--no-mlp', action='store_true')
args = get_combined_args(parser)
print("Rendering " + args.model_path)
# Initialize system state (RNG)
safe_state(args.quiet)
render_sets(model.extract(args), args.iteration, pipeline.extract(args),
args.skip_train, args.skip_test,
args.mode, args.no_mlp)