-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrender_uncertainty.py
225 lines (179 loc) · 10.4 KB
/
render_uncertainty.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
#
# 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
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 numpy as np
from utils.camera_utils import rand_rotation_matrix
from scene.cameras import Camera
from gaussian_renderer import modified_render
from einops import reduce, repeat, rearrange
import seaborn as sns
import matplotlib.pyplot as plt
import itertools
from active.schema import schema_dict, override_test_idxs_dict, override_train_idxs_dict
def capture(self):
return (
self.active_sh_degree,
self._xyz,
self._features_dc,
self._features_rest,
self._scaling,
self._rotation,
self._opacity,
self.max_radii2D,
self.xyz_gradient_accum,
self.denom,
# self.optimizer.state_dict(),
# self.spatial_lr_scale,
)
@torch.no_grad()
def render_uncertainty(view, gaussians, pipeline, background, hessian_color):
render_pkg = modified_render(view, gaussians, pipeline, background)
pred_img = render_pkg["render"]
# pred_img.backward(gradient=torch.ones_like(pred_img))
pixel_gaussian_counter = render_pkg["pixel_gaussian_counter"]
render_pkg = modified_render(view, gaussians, pipeline, background, override_color=hessian_color)
uncertanity_map = reduce(render_pkg["render"], "c h w -> h w", "mean")
return pred_img, uncertanity_map, pixel_gaussian_counter, render_pkg["depth"]
def render_set(model_path, name, iteration, train_views, test_views, gaussians, pipeline, background, perturb_scale=1., camera_extent=None, args=None):
render_path = os.path.join(model_path, "renders")
eval_path = os.path.join(model_path, "eval")
makedirs(render_path, exist_ok=True)
makedirs(eval_path, exist_ok=True)
params = capture(gaussians)[1:7]
name2idx = {"xyz": 0, "rgb": 1, "sh": 2, "scale": 3, "rotation": 4, "opacity": 5}
xyz = params[0]
# filter_out_idx = [name2idx[k] for k in ["rotation", "rgb", "sh"]]
filter_out_idx = [name2idx[k] for k in ["rotation", "scale", "xyz", "opacity"]]
params = [p.requires_grad_(True) for i, p in enumerate(params) if i not in filter_out_idx]
optim = torch.optim.SGD(params, 0.)
gaussians.optimizer = optim
device = params[0].device
# H_train = torch.zeros(sum(p.numel() for p in params), device=params[0].device, dtype=params[0].dtype)
H_per_gaussian = torch.zeros(params[0].shape[0], device=params[0].device, dtype=params[0].dtype)
if not args.depth_only:
# TODO: We can also use all the views, here the train views are just a subset of training cameras
for idx, view in enumerate(tqdm(itertools.chain(train_views, test_views), desc="Rendering progress")):
# rendering = render(view, gaussians, pipeline, background)["render"]
render_pkg = modified_render(view, gaussians, pipeline, background)
pred_img = render_pkg["render"]
pred_img.backward(gradient=torch.ones_like(pred_img))
pixel_gaussian_counter = render_pkg["pixel_gaussian_counter"]
# render_pkg = modified_render(view, gaussians, pipeline, background, override_color=torch.ones_like(params[1]))
H_per_gaussian += sum([reduce(p.grad.detach(), "n ... -> n", "sum") for p in params])
# render_pkg = modified_render(view, gaussians, pipeline, background, override_color=H_per_gaussian.detach())
optim.zero_grad(set_to_none = True)
split = "train" if idx < len(train_views) else "test"
torchvision.utils.save_image(pred_img.detach(), os.path.join(render_path, f"{split}_{view.image_name}.png"))
else:
H_per_gaussian += 1
hessian_color = repeat(H_per_gaussian.detach(), "n -> n c", c=3)
with torch.no_grad():
for idx, view in enumerate(tqdm(test_views, desc="Rendering on test set")):
to_homo = lambda x: torch.cat([x, torch.ones(x.shape[:-1] + (1, ), dtype=x.dtype, device=x.device)], dim=-1)
pts3d_homo = to_homo(xyz)
pts3d_cam = pts3d_homo @ view.world_view_transform
gaussian_depths = pts3d_cam[:, 2, None]
cur_hessian_color = hessian_color * gaussian_depths.clamp(min=0)
pred_img, uncertanity_map, pixel_gaussian_counter, depth = render_uncertainty(view, gaussians, pipeline, background, cur_hessian_color)
# sns.heatmap(torch.log(uncertanity_map / pixel_gaussian_counter).clamp(min=0).detach().cpu(), square=True)
# plt.savefig(f"./uncern_all.jpg")
# torchvision.utils.save_image(pred_img.detach(), os.path.join(render_path, f"{split}_{idx:05d}.png"))
if args.depth_only:
sns.heatmap(depth.detach().cpu(), square=True)
plt.savefig(os.path.join(eval_path, f"depth_viz_{view.image_name}.jpg"))
else:
sns.heatmap(torch.log(uncertanity_map / pixel_gaussian_counter).detach().cpu(), square=True)
plt.savefig(os.path.join(eval_path, f"heatmap_{view.image_name}.jpg"))
plt.clf()
np.savez(os.path.join(eval_path, f"uncertainty_{idx:03d}_{view.image_name}.npz"),
uncertanity_map=uncertanity_map.cpu(), pixel_gaussian_counter=pixel_gaussian_counter.cpu(),
depth=depth.cpu(),
)
def render_set_current(model_path, name, iteration, train_views, test_views, gaussians, pipeline, background, perturb_scale=1., camera_extent=None, args=None):
eval_path = os.path.join(model_path, "eval")
makedirs(eval_path, exist_ok=True)
params = capture(gaussians)[1:7]
name2idx = {"xyz": 0, "rgb": 1, "sh": 2, "scale": 3, "rotation": 4, "opacity": 5}
filter_out_idx = [name2idx[k] for k in ["rotation"]]
params = [p.requires_grad_(True) for i, p in enumerate(params) if i not in filter_out_idx]
optim = torch.optim.SGD(params, 0.)
gaussians.optimizer = optim
device = params[0].device
for idx, view in enumerate(tqdm(test_views, desc="Rendering on test set")):
render_pkg = modified_render(view, gaussians, pipeline, background)
pred_img = render_pkg["render"]
pred_img.backward(gradient=torch.ones_like(pred_img))
pixel_gaussian_counter = render_pkg["pixel_gaussian_counter"]
H_per_gaussian = sum(reduce(p.grad.detach(), "n ... -> n", "sum") for p in params)
with torch.no_grad():
hessian_color = repeat(H_per_gaussian.detach(), "n -> n c", c=3)
# compute depth of gaussian in current view
to_homo = lambda x: torch.cat([x, torch.ones(x.shape[:-1] + (1, ), dtype=x.dtype, device=x.device)], dim=-1)
pts3d_homo = to_homo(params[0])
pts3d_cam = pts3d_homo @ view.world_view_transform
gaussian_depths = pts3d_cam[:, 2, None]
hessian_color = hessian_color * gaussian_depths
render_pkg = modified_render(view, gaussians, pipeline, background, override_color=hessian_color)
uncertanity_map = reduce(render_pkg["render"], "c h w -> h w", "mean")
depth = render_pkg["depth"]
# sns.heatmap(torch.log(uncertanity_map / pixel_gaussian_counter).clamp(min=0).detach().cpu(), square=True)
# plt.savefig(f"./uncern.jpg")
# plt.savefig(f"./uncern_all.jpg")
plt.clf()
torchvision.utils.save_image(pred_img.detach(), os.path.join(eval_path, f"render_{view.image_name}.png"))
sns.heatmap(torch.log(uncertanity_map / pixel_gaussian_counter).clamp(min=0).detach().cpu(), square=True)
plt.savefig(os.path.join(eval_path, f"heatmap_{view.image_name}.jpg"))
plt.clf()
np.savez(os.path.join(eval_path, f"uncertainty_{idx:03d}_{view.image_name}.npz"),
uncertanity_map=uncertanity_map.cpu(), pixel_gaussian_counter=pixel_gaussian_counter.cpu(),
depth=depth.cpu(),
)
optim.zero_grad(set_to_none = True)
def render_sets(dataset : ModelParams, iteration : int, pipeline : PipelineParams, agrs):
gaussians = GaussianModel(dataset.sh_degree)
# override_train_idxs = override_train_idxs_dict.get(args.override_idxs, None)
# use every frames
override_train_idxs = list(range(10_000))
override_test_idxs = override_test_idxs_dict[args.override_idxs]
scene = Scene(dataset, gaussians, load_iteration=iteration, shuffle=False, override_train_idxs=override_train_idxs, override_test_idxs=override_test_idxs)
bg_color = [1,1,1] if dataset.white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
if args.current:
render_set_current(dataset.model_path, "train", scene.loaded_iter, scene.getTrainCameras(), scene.getTestCameras(), gaussians, pipeline, background, camera_extent=scene.cameras_extent, args=args)
else:
render_set(dataset.model_path, "train", scene.loaded_iter, scene.getTrainCameras(), scene.getTestCameras(), gaussians, pipeline, background, camera_extent=scene.cameras_extent, args=args)
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("--quiet", action="store_true")
parser.add_argument("--perturb_scale", default=1., type=float)
parser.add_argument("--inflate_factor", default=5, type=int)
parser.add_argument("--override_idxs", type=str, help="speical test idxs on uncertainty evaluation")
parser.add_argument("--depth_only", action="store_true", help="render depth only")
parser.add_argument("--current", action="store_true", help="render uncertainty from current view")
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)