-
Notifications
You must be signed in to change notification settings - Fork 26
/
render.py
405 lines (333 loc) · 20.8 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
404
405
#
# 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 torch
from scene import Scene
import os
from tqdm import tqdm
from os import makedirs
from gaussian_renderer import render, render_edit
import torchvision
from utils.general_utils import safe_state
from argparse import ArgumentParser
from arguments import ModelParams, PipelineParams, get_combined_args
from gaussian_renderer import GaussianModel
import cv2
import matplotlib.pyplot as plt
from utils.graphics_utils import getWorld2View2
from utils.pose_utils import render_path_spiral
import sklearn
import sklearn.decomposition
import numpy as np
from PIL import Image
import torch.nn as nn
import torch.nn.functional as F
from utils.clip_utils import CLIPEditor
import yaml
from models.networks import CNN_decoder, MLP_encoder
def feature_visualize_saving(feature):
fmap = feature[None, :, :, :] # torch.Size([1, 512, h, w])
fmap = nn.functional.normalize(fmap, dim=1)
pca = sklearn.decomposition.PCA(3, random_state=42)
f_samples = fmap.permute(0, 2, 3, 1).reshape(-1, fmap.shape[1])[::3].cpu().numpy()
transformed = pca.fit_transform(f_samples)
feature_pca_mean = torch.tensor(f_samples.mean(0)).float().cuda()
feature_pca_components = torch.tensor(pca.components_).float().cuda()
q1, q99 = np.percentile(transformed, [1, 99])
feature_pca_postprocess_sub = q1
feature_pca_postprocess_div = (q99 - q1)
del f_samples
vis_feature = (fmap.permute(0, 2, 3, 1).reshape(-1, fmap.shape[1]) - feature_pca_mean[None, :]) @ feature_pca_components.T
vis_feature = (vis_feature - feature_pca_postprocess_sub) / feature_pca_postprocess_div
vis_feature = vis_feature.clamp(0.0, 1.0).float().reshape((fmap.shape[2], fmap.shape[3], 3)).cpu()
return vis_feature
def parse_edit_config_and_text_encoding(edit_config):
edit_dict = {}
if edit_config is not None:
with open(edit_config, 'r') as f:
edit_config = yaml.safe_load(f)
print(edit_config)
objects = edit_config["edit"]["objects"]
targets = edit_config["edit"]["targets"].split(",")
edit_dict["positive_ids"] = [objects.index(t) for t in targets if t in objects]
edit_dict["score_threshold"] = edit_config["edit"]["threshold"]
# text encoding
clip_editor = CLIPEditor()
text_feature = clip_editor.encode_text([obj.replace("_", " ") for obj in objects])
# setup editing
op_dict = {}
for operation in edit_config["edit"]["operations"].split(","):
if operation == "extraction":
op_dict["extraction"] = True
elif operation == "deletion":
op_dict["deletion"] = True
elif operation == "color_func":
op_dict["color_func"] = eval(edit_config["edit"]["colorFunc"])
else:
raise NotImplementedError
edit_dict["operations"] = op_dict
idx = edit_dict["positive_ids"][0]
return edit_dict, text_feature, targets[idx]
def render_set(model_path, name, iteration, views, gaussians, pipeline, background, edit_config, speedup):
if edit_config != "no editing":
edit_dict, text_feature, target = parse_edit_config_and_text_encoding(edit_config)
edit_render_path = os.path.join(model_path, name, "ours_{}_{}_{}".format(iteration, next(iter(edit_dict["operations"])), target), "renders")
edit_gts_path = os.path.join(model_path, name, "ours_{}_{}_{}".format(iteration, next(iter(edit_dict["operations"])), target), "gt")
edit_feature_map_path = os.path.join(model_path, name, "ours_{}_{}_{}".format(iteration, next(iter(edit_dict["operations"])), target), "feature_map")
edit_gt_feature_map_path = os.path.join(model_path, name, "ours_{}_{}_{}".format(iteration, next(iter(edit_dict["operations"])), target), "gt_feature_map")
makedirs(edit_render_path, exist_ok=True)
makedirs(edit_gts_path, exist_ok=True)
makedirs(edit_feature_map_path, exist_ok=True)
makedirs(edit_gt_feature_map_path, exist_ok=True)
else:
render_path = os.path.join(model_path, name, "ours_{}".format(iteration), "renders")
gts_path = os.path.join(model_path, name, "ours_{}".format(iteration), "gt")
feature_map_path = os.path.join(model_path, name, "ours_{}".format(iteration), "feature_map")
gt_feature_map_path = os.path.join(model_path, name, "ours_{}".format(iteration), "gt_feature_map")
saved_feature_path = os.path.join(model_path, name, "ours_{}".format(iteration), "saved_feature")
#encoder_ckpt_path = os.path.join(model_path, "encoder_chkpnt{}.pth".format(iteration))
decoder_ckpt_path = os.path.join(model_path, "decoder_chkpnt{}.pth".format(iteration))
depth_path = os.path.join(model_path, name, "ours_{}".format(iteration), "depth") ###
if speedup:
gt_feature_map = views[0].semantic_feature.cuda()
feature_out_dim = gt_feature_map.shape[0]
feature_in_dim = int(feature_out_dim/4)
cnn_decoder = CNN_decoder(feature_in_dim, feature_out_dim)
cnn_decoder.load_state_dict(torch.load(decoder_ckpt_path))
makedirs(render_path, exist_ok=True)
makedirs(gts_path, exist_ok=True)
makedirs(feature_map_path, exist_ok=True)
makedirs(gt_feature_map_path, exist_ok=True)
makedirs(saved_feature_path, exist_ok=True)
makedirs(depth_path, exist_ok=True) ###
for idx, view in enumerate(tqdm(views, desc="Rendering progress")):
if edit_config != "no editing":
render_pkg = render_edit(view, gaussians, pipeline, background, text_feature, edit_dict)
gt = view.original_image[0:3, :, :]
gt_feature_map = view.semantic_feature.cuda()
torchvision.utils.save_image(render_pkg["render"], os.path.join(edit_render_path, '{0:05d}'.format(idx) + ".png"))
torchvision.utils.save_image(gt, os.path.join(edit_gts_path, '{0:05d}'.format(idx) + ".png"))
# visualize feature map
feature_map = render_pkg["feature_map"]
feature_map = F.interpolate(feature_map.unsqueeze(0), size=(gt_feature_map.shape[1], gt_feature_map.shape[2]), mode='bilinear', align_corners=True).squeeze(0) ###
if speedup:
feature_map = cnn_decoder(feature_map)
feature_map_vis = feature_visualize_saving(feature_map)
Image.fromarray((feature_map_vis.cpu().numpy() * 255).astype(np.uint8)).save(os.path.join(edit_feature_map_path, '{0:05d}'.format(idx) + "_feature_vis.png"))
gt_feature_map_vis = feature_visualize_saving(gt_feature_map)
Image.fromarray((gt_feature_map_vis.cpu().numpy() * 255).astype(np.uint8)).save(os.path.join(edit_gt_feature_map_path, '{0:05d}'.format(idx) + "_feature_vis.png"))
else:
render_pkg = render(view, gaussians, pipeline, background)
gt = view.original_image[0:3, :, :]
gt_feature_map = view.semantic_feature.cuda()
torchvision.utils.save_image(render_pkg["render"], 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"))
### depth ###
depth = render_pkg["depth"]
scale_nor = depth.max().item()
depth_nor = depth / scale_nor
depth_tensor_squeezed = depth_nor.squeeze() # Remove the channel dimension
colormap = plt.get_cmap('jet')
depth_colored = colormap(depth_tensor_squeezed.cpu().numpy())
depth_colored_rgb = depth_colored[:, :, :3]
depth_image = Image.fromarray((depth_colored_rgb * 255).astype(np.uint8))
output_path = os.path.join(depth_path, '{0:05d}'.format(idx) + ".png")
depth_image.save(output_path)
##############
# visualize feature map
feature_map = render_pkg["feature_map"]
feature_map = F.interpolate(feature_map.unsqueeze(0), size=(gt_feature_map.shape[1], gt_feature_map.shape[2]), mode='bilinear', align_corners=True).squeeze(0) ###
if speedup:
feature_map = cnn_decoder(feature_map)
feature_map_vis = feature_visualize_saving(feature_map)
Image.fromarray((feature_map_vis.cpu().numpy() * 255).astype(np.uint8)).save(os.path.join(feature_map_path, '{0:05d}'.format(idx) + "_feature_vis.png"))
gt_feature_map_vis = feature_visualize_saving(gt_feature_map)
Image.fromarray((gt_feature_map_vis.cpu().numpy() * 255).astype(np.uint8)).save(os.path.join(gt_feature_map_path, '{0:05d}'.format(idx) + "_feature_vis.png"))
# save feature map
feature_map = feature_map.cpu().numpy().astype(np.float16)
torch.save(torch.tensor(feature_map).half(), os.path.join(saved_feature_path, '{0:05d}'.format(idx) + "_fmap_CxHxW.pt"))
def render_video(model_path, iteration, views, gaussians, pipeline, background, edit_config): ###
render_path = os.path.join(model_path, 'video', "ours_{}".format(iteration))
makedirs(render_path, exist_ok=True)
view = views[0]
render_poses = render_path_spiral(views)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
size = (view.original_image.shape[2], view.original_image.shape[1])
final_video = cv2.VideoWriter(os.path.join(render_path, 'final_video.mp4'), fourcc, 10, size)
if edit_config != "no editing":
edit_dict, text_feature = parse_edit_config_and_text_encoding(edit_config)
for idx, pose in enumerate(tqdm(render_poses, desc="Rendering progress")):
view.world_view_transform = torch.tensor(getWorld2View2(pose[:3, :3].T, pose[:3, 3], view.trans, view.scale)).transpose(0, 1).cuda()
view.full_proj_transform = (view.world_view_transform.unsqueeze(0).bmm(view.projection_matrix.unsqueeze(0))).squeeze(0)
view.camera_center = view.world_view_transform.inverse()[3, :3]
if edit_config != "no editing":
rendering = torch.clamp(render_edit(view, gaussians, pipeline, background, text_feature, edit_dict)["render"], min=0., max=1.) ###
else:
rendering = torch.clamp(render(view, gaussians, pipeline, background)["render"], min=0., max=1.)
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(idx) + ".png"))
final_video.write((rendering.permute(1, 2, 0).detach().cpu().numpy() * 255.).astype(np.uint8)[..., ::-1])
final_video.release()
def interpolate_matrices(start_matrix, end_matrix, steps):
# Generate interpolation factors
interpolation_factors = np.linspace(0, 1, steps)
# Interpolate between the matrices
interpolated_matrices = []
for factor in interpolation_factors:
interpolated_matrix = (1 - factor) * start_matrix + factor * end_matrix
interpolated_matrices.append(interpolated_matrix)
return np.array(interpolated_matrices)
def multi_interpolate_matrices(matrix, num_interpolations):
interpolated_matrices = []
for i in range(matrix.shape[0] - 1):
start_matrix = matrix[i]
end_matrix = matrix[i + 1]
for j in range(num_interpolations):
t = (j + 1) / (num_interpolations + 1)
interpolated_matrix = (1 - t) * start_matrix + t * end_matrix
interpolated_matrices.append(interpolated_matrix)
return np.array(interpolated_matrices)
###
def render_novel_views(model_path, name, iteration, views, gaussians, pipeline, background,
edit_config, speedup, multi_interpolate, num_views):
if multi_interpolate:
name = name + "_multi_interpolate"
# make dirs
if edit_config != "no editing":
edit_dict, text_feature, target = parse_edit_config_and_text_encoding(edit_config)
# edit
edit_render_path = os.path.join(model_path, name, "ours_{}_{}_{}".format(iteration, next(iter(edit_dict["operations"])), target), "renders")
edit_feature_map_path = os.path.join(model_path, name, "ours_{}_{}_{}".format(iteration, next(iter(edit_dict["operations"])), target), "feature_map")
makedirs(edit_render_path, exist_ok=True)
makedirs(edit_feature_map_path, exist_ok=True)
else:
# non-edit
render_path = os.path.join(model_path, name, "ours_{}".format(iteration), "renders")
feature_map_path = os.path.join(model_path, name, "ours_{}".format(iteration), "feature_map")
saved_feature_path = os.path.join(model_path, name, "ours_{}".format(iteration), "saved_feature")
#encoder_ckpt_path = os.path.join(model_path, "encoder_chkpnt{}.pth".format(iteration))
decoder_ckpt_path = os.path.join(model_path, "decoder_chkpnt{}.pth".format(iteration))
if speedup:
gt_feature_map = views[0].semantic_feature.cuda()
feature_out_dim = gt_feature_map.shape[0]
feature_in_dim = int(feature_out_dim/4)
cnn_decoder = CNN_decoder(feature_in_dim, feature_out_dim)
cnn_decoder.load_state_dict(torch.load(decoder_ckpt_path))
makedirs(render_path, exist_ok=True)
makedirs(feature_map_path, exist_ok=True)
makedirs(saved_feature_path, exist_ok=True)
view = views[0]
# create novel poses
render_poses = []
for cam in views:
pose = np.concatenate([cam.R, cam.T.reshape(3, 1)], 1)
render_poses.append(pose)
if not multi_interpolate:
poses = interpolate_matrices(render_poses[0], render_poses[-1], num_views)
else:
poses = multi_interpolate_matrices(np.array(render_poses), 2)
# rendering process
for idx, pose in enumerate(tqdm(poses, desc="Rendering progress")):
view.world_view_transform = torch.tensor(getWorld2View2(pose[:, :3], pose[:, 3], view.trans, view.scale)).transpose(0, 1).cuda()
view.full_proj_transform = (view.world_view_transform.unsqueeze(0).bmm(view.projection_matrix.unsqueeze(0))).squeeze(0)
view.camera_center = view.world_view_transform.inverse()[3, :3]
if edit_config != "no editing":
render_pkg = render_edit(view, gaussians, pipeline, background, text_feature, edit_dict)
gt = view.original_image[0:3, :, :]
gt_feature_map = view.semantic_feature.cuda()
torchvision.utils.save_image(render_pkg["render"], os.path.join(edit_render_path, '{0:05d}'.format(idx) + ".png"))
# visualize feature map
feature_map = render_pkg["feature_map"]
feature_map = F.interpolate(feature_map.unsqueeze(0), size=(gt_feature_map.shape[1], gt_feature_map.shape[2]), mode='bilinear', align_corners=True).squeeze(0) ###
if speedup:
feature_map = cnn_decoder(feature_map)
feature_map_vis = feature_visualize_saving(feature_map)
Image.fromarray((feature_map_vis.cpu().numpy() * 255).astype(np.uint8)).save(os.path.join(edit_feature_map_path, '{0:05d}'.format(idx) + "_feature_vis.png"))
else:
# mlp encoder
render_pkg = render(view, gaussians, pipeline, background)
gt_feature_map = view.semantic_feature.cuda()
torchvision.utils.save_image(render_pkg["render"], os.path.join(render_path, '{0:05d}'.format(idx) + ".png"))
# visualize feature map
feature_map = render_pkg["feature_map"]
feature_map = F.interpolate(feature_map.unsqueeze(0), size=(gt_feature_map.shape[1], gt_feature_map.shape[2]), mode='bilinear', align_corners=True).squeeze(0) ###
if speedup:
feature_map = cnn_decoder(feature_map)
feature_map_vis = feature_visualize_saving(feature_map)
Image.fromarray((feature_map_vis.cpu().numpy() * 255).astype(np.uint8)).save(os.path.join(feature_map_path, '{0:05d}'.format(idx) + "_feature_vis.png"))
# save feature map
feature_map = feature_map.cpu().numpy().astype(np.float16)
torch.save(torch.tensor(feature_map).half(), os.path.join(saved_feature_path, '{0:05d}'.format(idx) + "_fmap_CxHxW.pt"))
def render_novel_video(model_path, name, iteration, views, gaussians, pipeline, background, edit_config):
render_path = os.path.join(model_path, name, "ours_{}".format(iteration))
makedirs(render_path, exist_ok=True)
view = views[0]
fourcc = cv2.VideoWriter_fourcc(*'XVID')
size = (view.original_image.shape[2], view.original_image.shape[1])
final_video = cv2.VideoWriter(os.path.join(render_path, 'final_video.mp4'), fourcc, 10, size)
if edit_config != "no editing":
edit_dict, text_feature = parse_edit_config_and_text_encoding(edit_config)
render_poses = [(cam.R, cam.T) for cam in views]
render_poses = []
for cam in views:
pose = np.concatenate([cam.R, cam.T.reshape(3, 1)], 1)
render_poses.append(pose)
# create novel poses
poses = interpolate_matrices(render_poses[0], render_poses[-1], 200)
# rendering process
for idx, pose in enumerate(tqdm(poses, desc="Rendering progress")):
view.world_view_transform = torch.tensor(getWorld2View2(pose[:, :3], pose[:, 3], view.trans, view.scale)).transpose(0, 1).cuda()
view.full_proj_transform = (view.world_view_transform.unsqueeze(0).bmm(view.projection_matrix.unsqueeze(0))).squeeze(0)
view.camera_center = view.world_view_transform.inverse()[3, :3]
if edit_config != "no editing":
rendering = torch.clamp(render_edit(view, gaussians, pipeline, background, text_feature, edit_dict)["render"], min=0., max=1.)
else:
rendering = torch.clamp(render(view, gaussians, pipeline, background)["render"], min=0., max=1.)
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(idx) + ".png"))
final_video.write((rendering.permute(1, 2, 0).detach().cpu().numpy() * 255.).astype(np.uint8)[..., ::-1])
final_video.release()
def render_sets(dataset : ModelParams, iteration : int, pipeline : PipelineParams, skip_train : bool, skip_test : bool, novel_view : bool,
video : bool , edit_config: str, novel_video : bool, multi_interpolate : bool, num_views : int):
with torch.no_grad():
gaussians = GaussianModel(dataset.sh_degree)
scene = Scene(dataset, gaussians, load_iteration=iteration, shuffle=False)
bg_color = [1,1,1] if dataset.white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
if not skip_train:
render_set(dataset.model_path, "train", scene.loaded_iter, scene.getTrainCameras(), gaussians, pipeline, background, edit_config, dataset.speedup)
if not skip_test:
render_set(dataset.model_path, "test", scene.loaded_iter, scene.getTestCameras(), gaussians, pipeline, background, edit_config, dataset.speedup)
if novel_view:
render_novel_views(dataset.model_path, "novel_views", scene.loaded_iter, scene.getTrainCameras(), gaussians, pipeline, background,
edit_config, dataset.speedup, multi_interpolate, num_views)
if video:
render_video(dataset.model_path, scene.loaded_iter, scene.getTrainCameras(), gaussians, pipeline, background, edit_config)
if novel_video:
render_novel_video(dataset.model_path, "novel_views_video", scene.loaded_iter, scene.getTrainCameras(), gaussians, pipeline, background, edit_config, dataset.speedup)
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("--novel_view", action="store_true") ###
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--video", action="store_true") ###
parser.add_argument("--novel_video", action="store_true") ###
parser.add_argument('--edit_config', default="no editing", type=str)
parser.add_argument("--multi_interpolate", action="store_true") ###
parser.add_argument("--num_views", default=200, type=int)
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.novel_view,
args.video, args.edit_config, args.novel_video, args.multi_interpolate, args.num_views) ###