-
Notifications
You must be signed in to change notification settings - Fork 28
/
NeRF.py
635 lines (527 loc) · 28 KB
/
NeRF.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
import torch
import torch.nn as nn
from run_nerf_helpers import *
import os
import imageio
import time
def init_linear_weights(m):
if isinstance(m, nn.Linear):
if m.weight.shape[0] in [2, 3]:
nn.init.xavier_normal_(m.weight, 0.1)
else:
nn.init.xavier_normal_(m.weight)
# nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.ConvTranspose2d):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 0)
class DSKnet(nn.Module):
def __init__(self, num_img, poses, num_pt, kernel_hwindow, *, random_hwindow=0.25,
in_embed=3, random_mode='input', img_embed=32, spatial_embed=0, depth_embed=0,
num_hidden=3, num_wide=64, short_cut=False, pattern_init_radius=0.1,
isglobal=False, optim_trans=False, optim_spatialvariant_trans=False):
"""
num_img: number of image, used for deciding the view embedding
poses: the original poses, used for generating new rays, len(poses) == num_img
num_pt: number of sparse point, we use 5 in the paper
kernel_hwindow: the size of physically equivalent blur kernel, the sparse points are bounded inside the blur kernel.
Can be a very big number
random_hwindow: in training, we randomly perturb the sparse point to model a smooth manifold
random_mode: 'input' or 'output', it controls whether the random perturb is added to the input of DSK or output of DSK
// the above two parameters do not have big impact on the results
in_embed: embedding for the canonical kernel location
img_embed: the length of the view embedding
spatial_embed: embedding for the pixel location of the blur kernel inside an image
depth_embed: (deprecated) the embedding for the depth of current rays
num_hidden, num_wide, short_cut: control the structure of the MLP
pattern_init_radius: the little gain add to the deform location described in Sec. 4.4
isglobal: control whether the canonical kernel should be shared by all the input views or not, does not have big impact on the results
optim_trans: whether to optimize the ray origin described in Sec. 4.3
optim_spatialvariant_trans: whether to optimize the ray origin for each view or each kernel point.
"""
super().__init__()
self.num_pt = num_pt
self.num_img = num_img
self.short_cut = short_cut
self.kernel_hwindow = kernel_hwindow
self.random_hwindow = random_hwindow # about 1 pix
self.random_mode = random_mode
self.isglobal = isglobal
pattern_num = 1 if isglobal else num_img
assert self.random_mode in ['input', 'output'], f"DSKNet::random_mode {self.random_mode} unrecognized, " \
f"should be input/output"
self.register_buffer("poses", poses)
self.register_parameter("pattern_pos",
nn.Parameter(torch.randn(pattern_num, num_pt, 2)
.type(torch.float32) * pattern_init_radius, True))
self.optim_trans = optim_trans
self.optim_sv_trans = optim_spatialvariant_trans
if optim_trans:
self.register_parameter("pattern_trans",
nn.Parameter(torch.zeros(pattern_num, num_pt, 2)
.type(torch.float32), True))
if in_embed > 0:
self.in_embed_fn, self.in_embed_cnl = get_embedder(in_embed, input_dim=2)
else:
self.in_embed_fn, self.in_embed_cnl = None, 0
self.img_embed_cnl = img_embed
if spatial_embed > 0:
self.spatial_embed_fn, self.spatial_embed_cnl = get_embedder(spatial_embed, input_dim=2)
else:
self.spatial_embed_fn, self.spatial_embed_cnl = None, 0
if depth_embed > 0:
self.require_depth = True
self.depth_embed_fn, self.depth_embed_cnl = get_embedder(depth_embed, input_dim=1)
else:
self.require_depth = False
self.depth_embed_fn, self.depth_embed_cnl = None, 0
in_cnl = self.in_embed_cnl + self.img_embed_cnl + self.depth_embed_cnl + self.spatial_embed_cnl
out_cnl = 1 + 2 + 2 if self.optim_sv_trans else 1 + 2 # u, v, w or u, v, w, dx, dy
hiddens = [nn.Linear(num_wide, num_wide) if i % 2 == 0 else nn.ReLU()
for i in range((num_hidden - 1) * 2)]
# hiddens = [nn.Linear(num_wide, num_wide), nn.ReLU()] * num_hidden
self.linears = nn.Sequential(
nn.Linear(in_cnl, num_wide), nn.ReLU(),
*hiddens,
)
self.linears1 = nn.Sequential(
nn.Linear((num_wide + in_cnl) if short_cut else num_wide, num_wide), nn.ReLU(),
nn.Linear(num_wide, out_cnl)
)
self.linears.apply(init_linear_weights)
self.linears1.apply(init_linear_weights)
if img_embed > 0:
self.register_parameter("img_embed",
nn.Parameter(torch.zeros(num_img, img_embed).type(torch.float32), True))
else:
self.img_embed = None
def forward(self, H, W, K, rays, rays_info):
"""
inputs: all input has shape (ray_num, cnl)
outputs: output shape (ray_num, ptnum, 3, 2) last two dim: [ray_o, ray_d]
"""
img_idx = rays_info['images_idx'].squeeze(-1)
img_embed = self.img_embed[img_idx] if self.img_embed is not None else \
torch.tensor([]).reshape(len(img_idx), self.img_embed_cnl)
pt_pos = self.pattern_pos.expand(len(img_idx), -1, -1) if self.isglobal \
else self.pattern_pos[img_idx]
pt_pos = torch.tanh(pt_pos) * self.kernel_hwindow
if self.random_hwindow > 0 and self.random_mode == "input":
random_pos = torch.randn_like(pt_pos) * self.random_hwindow
pt_pos = pt_pos + random_pos
input_pos = pt_pos # the first point is the reference point
if self.in_embed_fn is not None:
pt_pos = pt_pos * (np.pi / self.kernel_hwindow)
pt_pos = self.in_embed_fn(pt_pos)
img_embed_expand = img_embed[:, None].expand(len(img_embed), self.num_pt, self.img_embed_cnl)
x = torch.cat([pt_pos, img_embed_expand], dim=-1)
rays_x, rays_y = rays_info['rays_x'], rays_info['rays_y']
if self.spatial_embed_fn is not None:
spatialx = rays_x / (W / 2 / np.pi) - np.pi
spatialy = rays_y / (H / 2 / np.pi) - np.pi # scale 2pi to match the freq in the embedder
spatial = torch.cat([spatialx, spatialy], dim=-1)
spatial = self.spatial_embed_fn(spatial)
spatial = spatial[:, None].expand(len(img_idx), self.num_pt, self.spatial_embed_cnl)
x = torch.cat([x, spatial], dim=-1)
if self.depth_embed_fn is not None:
depth = rays_info['ray_depth']
depth = depth * np.pi # TODO: please always check that the depth lies between [0, 1)
depth = self.depth_embed_fn(depth)
depth = depth[:, None].expand(len(img_idx), self.num_pt, self.depth_embed_cnl)
x = torch.cat([x, depth], dim=-1)
# forward
x1 = self.linears(x)
x1 = torch.cat([x, x1], dim=-1) if self.short_cut else x1
x1 = self.linears1(x1)
delta_trans = None
if self.optim_sv_trans:
delta_trans, delta_pos, weight = torch.split(x1, [2, 2, 1], dim=-1)
else:
delta_pos, weight = torch.split(x1, [2, 1], dim=-1)
if self.optim_trans:
delta_trans = self.pattern_trans.expand(len(img_idx), -1, -1) if self.isglobal \
else self.pattern_trans[img_idx]
if delta_trans is None:
delta_trans = torch.zeros_like(delta_pos)
delta_trans = delta_trans * 0.01
new_rays_xy = delta_pos + input_pos
weight = torch.softmax(weight[..., 0], dim=-1)
if self.random_hwindow > 0 and self.random_mode == 'output':
raise NotImplementedError(f"{self.random_mode} for self.random_mode is not implemented")
poses = self.poses[img_idx]
# get rays from offsetted pt position
rays_x = (rays_x - K[0, 2] + new_rays_xy[..., 0]) / K[0, 0]
rays_y = -(rays_y - K[1, 2] + new_rays_xy[..., 1]) / K[1, 1]
dirs = torch.stack([rays_x - delta_trans[..., 0],
rays_y - delta_trans[..., 1],
-torch.ones_like(rays_x)], -1)
# Rotate ray directions from camera frame to the world frame
rays_d = torch.sum(dirs[..., None, :] * poses[..., None, :3, :3],
-1) # dot product, equals to: [c2w.dot(dir) for dir in dirs]
# Translate camera frame's origin to the world frame. It is the origin of all rays.
translation = torch.stack([
delta_trans[..., 0],
delta_trans[..., 1],
torch.zeros_like(rays_x),
torch.ones_like(rays_x)
], dim=-1)
rays_o = torch.sum(translation[..., None, :] * poses[:, None], dim=-1)
# rays_o = poses[..., None, :3, -1].expand_as(rays_d)
align = new_rays_xy[:, 0, :].abs().mean()
align += (delta_trans[:, 0, :].abs().mean() * 10)
return torch.stack([rays_o, rays_d], dim=-1), weight, align
class NeRFAll(nn.Module):
def __init__(self, args, kernelsnet=None):
super().__init__()
self.args = args
self.embed_fn, self.input_ch = get_embedder(args.multires, args.i_embed)
self.input_ch_views = 0
self.kernelsnet = kernelsnet
self.embeddirs_fn = None
if args.use_viewdirs:
self.embeddirs_fn, self.input_ch_views = get_embedder(args.multires_views, args.i_embed)
self.output_ch = 5 if args.N_importance > 0 else 4
skips = [4]
self.mlp_coarse = NeRF(
D=args.netdepth, W=args.netwidth,
input_ch=self.input_ch, output_ch=self.output_ch, skips=skips,
input_ch_views=self.input_ch_views, use_viewdirs=args.use_viewdirs)
self.mlp_fine = None
if args.N_importance > 0:
self.mlp_fine = NeRF(
D=args.netdepth_fine, W=args.netwidth_fine,
input_ch=self.input_ch, output_ch=self.output_ch, skips=skips,
input_ch_views=self.input_ch_views, use_viewdirs=args.use_viewdirs)
activate = {'relu': torch.relu, 'sigmoid': torch.sigmoid, 'exp': torch.exp, 'none': lambda x: x,
'sigmoid1': lambda x: 1.002 / (torch.exp(-x) + 1) - 0.001,
'softplus': lambda x: nn.Softplus()(x - 1)}
self.rgb_activate = activate[args.rgb_activate]
self.sigma_activate = activate[args.sigma_activate]
self.tonemapping = ToneMapping(args.tone_mapping_type)
def mlpforward(self, inputs, viewdirs, mlp, netchunk=1024 * 64):
"""Prepares inputs and applies network 'fn'.
"""
inputs_flat = torch.reshape(inputs, [-1, inputs.shape[-1]])
embedded = self.embed_fn(inputs_flat)
if viewdirs is not None:
input_dirs = viewdirs[:, None].expand(inputs.shape)
input_dirs_flat = torch.reshape(input_dirs, [-1, input_dirs.shape[-1]])
embedded_dirs = self.embeddirs_fn(input_dirs_flat)
embedded = torch.cat([embedded, embedded_dirs], -1)
# batchify execution
if netchunk is None:
outputs_flat = mlp(embedded)
else:
outputs_flat = torch.cat([mlp(embedded[i:i + netchunk]) for i in range(0, embedded.shape[0], netchunk)], 0)
outputs = torch.reshape(outputs_flat, list(inputs.shape[:-1]) + [outputs_flat.shape[-1]])
return outputs
def raw2outputs(self, raw, z_vals, rays_d, raw_noise_std=0, white_bkgd=False, pytest=False):
"""Transforms model's predictions to semantically meaningful values.
Args:
raw: [num_rays, num_samples along ray, 4]. Prediction from model.
z_vals: [num_rays, num_samples along ray]. Integration time.
rays_d: [num_rays, 3]. Direction of each ray.
Returns:
rgb_map: [num_rays, 3]. Estimated RGB color of a ray.
disp_map: [num_rays]. Disparity map. Inverse of depth map.
acc_map: [num_rays]. Sum of weights along each ray.
weights: [num_rays, num_samples]. Weights assigned to each sampled color.
depth_map: [num_rays]. Estimated distance to object.
"""
def raw2alpha(raw_, dists_, act_fn):
alpha_ = - torch.exp(-act_fn(raw_) * dists_) + 1.
return torch.cat([alpha_, torch.ones_like(alpha_[:, 0:1])], dim=-1)
dists = z_vals[..., 1:] - z_vals[..., :-1] # [N_rays, N_samples - 1]
# dists = torch.cat([dists, torch.tensor([1e10]).expand(dists[..., :1].shape)], -1)
dists = dists * torch.norm(rays_d[..., None, :], dim=-1)
rgb = self.rgb_activate(raw[..., :3])
noise = 0.
if raw_noise_std > 0.:
noise = torch.randn_like(raw[..., :-1, 3]) * raw_noise_std
# Overwrite randomly sampled data if pytest
if pytest:
np.random.seed(0)
noise = np.random.rand(*list(raw[..., 3].shape)) * raw_noise_std
noise = torch.tensor(noise)
density = self.sigma_activate(raw[..., :-1, 3] + noise)
if not self.training and self.args.render_rmnearplane > 0:
mask = z_vals[:, 1:]
mask = mask > self.args.render_rmnearplane / 128
mask = mask.type_as(density)
density = mask * density
alpha = - torch.exp(- density * dists) + 1.
alpha = torch.cat([alpha, torch.ones_like(alpha[:, 0:1])], dim=-1)
# alpha = raw2alpha(raw[..., :-1, 3] + noise, dists, act_fn=self.sigma_activate) # [N_rays, N_samples]
# weights = alpha * tf.math.cumprod(1.-alpha + 1e-10, -1, exclusive=True)
weights = alpha * \
torch.cumprod(torch.cat([torch.ones((alpha.shape[0], 1)), - alpha + (1. + 1e-10)], -1), -1)[:, :-1]
rgb_map = torch.sum(weights[..., None] * rgb, -2) # [N_rays, 3]
depth_map = torch.sum(weights * z_vals, -1)
# disp_map = 1. / torch.clamp_min(depth_map, 1e-10)
acc_map = torch.sum(weights, -1)
if white_bkgd:
rgb_map = rgb_map + (1. - acc_map[..., None])
return rgb_map, density, acc_map, weights, depth_map
def render_rays(self,
ray_batch,
N_samples,
retraw=False,
lindisp=False,
perturb=0.,
N_importance=0,
white_bkgd=False,
raw_noise_std=0.,
pytest=False):
"""Volumetric rendering.
Args:
ray_batch: array of shape [batch_size, ...]. All information necessary
for sampling along a ray, including: ray origin, ray direction, min
dist, max dist, and unit-magnitude viewing direction.
N_samples: int. Number of different times to sample along each ray.
retraw: bool. If True, include model's raw, unprocessed predictions.
lindisp: bool. If True, sample linearly in inverse depth rather than in depth.
perturb: float, 0 or 1. If non-zero, each ray is sampled at stratified
random points in time.
N_importance: int. Number of additional times to sample along each ray.
These samples are only passed to network_fine.
white_bkgd: bool. If True, assume a white background.
raw_noise_std: ...
verbose: bool. If True, print more debugging info.
"""
N_rays = ray_batch.shape[0]
rays_o, rays_d = ray_batch[:, 0:3], ray_batch[:, 3:6] # [N_rays, 3] each
viewdirs = ray_batch[:, -3:] if ray_batch.shape[-1] > 8 else None
bounds = torch.reshape(ray_batch[..., 6:8], [-1, 1, 2])
near, far = bounds[..., 0], bounds[..., 1] # [-1,1]
t_vals = torch.linspace(0., 1., steps=N_samples).type_as(rays_o)
if not lindisp:
z_vals = near * (1. - t_vals) + far * (t_vals)
else:
z_vals = 1. / (1. / near * (1. - t_vals) + 1. / far * (t_vals))
z_vals = z_vals.expand([N_rays, N_samples])
if perturb > 0.:
# get intervals between samples
mids = .5 * (z_vals[..., 1:] + z_vals[..., :-1])
upper = torch.cat([mids, z_vals[..., -1:]], -1)
lower = torch.cat([z_vals[..., :1], mids], -1)
# stratified samples in those intervals
t_rand = torch.rand(z_vals.shape).type_as(rays_o)
# Pytest, overwrite u with numpy's fixed random numbers
if pytest:
np.random.seed(0)
t_rand = np.random.rand(*list(z_vals.shape))
t_rand = torch.tensor(t_rand)
z_vals = lower + (upper - lower) * t_rand
pts = rays_o[..., None, :] + rays_d[..., None, :] * z_vals[..., :, None] # [N_rays, N_samples, 3]
# raw = run_network(pts)
raw = self.mlpforward(pts, viewdirs, self.mlp_coarse)
rgb_map, density_map, acc_map, weights, depth_map = self.raw2outputs(raw, z_vals, rays_d, raw_noise_std,
white_bkgd, pytest=pytest)
if N_importance > 0:
rgb_map_0, depth_map_0, acc_map_0, density_map0 = rgb_map, depth_map, acc_map, density_map
z_vals_mid = .5 * (z_vals[..., 1:] + z_vals[..., :-1])
z_samples = sample_pdf(z_vals_mid, weights[..., 1:-1], N_importance, det=(perturb == 0.), pytest=pytest)
z_samples = z_samples.detach()
z_vals, _ = torch.sort(torch.cat([z_vals, z_samples], -1), -1)
pts = rays_o[..., None, :] + rays_d[..., None, :] * z_vals[..., :,
None] # [N_rays, N_samples + N_importance, 3]
mlp = self.mlp_coarse if self.mlp_fine is None else self.mlp_fine
raw = self.mlpforward(pts, viewdirs, mlp)
rgb_map, density_map, acc_map, weights, depth_map = self.raw2outputs(raw, z_vals, rays_d, raw_noise_std,
white_bkgd, pytest=pytest)
ret = {'rgb_map': rgb_map, 'depth_map': depth_map, 'acc_map': acc_map, 'density_map': density_map}
if retraw:
ret['raw'] = raw
if N_importance > 0:
ret['rgb0'] = rgb_map_0
ret['depth0'] = depth_map_0
ret['acc0'] = acc_map_0
ret['density0'] = density_map0
ret['z_std'] = torch.std(z_samples, dim=-1, unbiased=False) # [N_rays]
for k in ret:
if torch.isnan(ret[k]).any():
print(f"! [Numerical Error] {k} contains nan.")
if torch.isinf(ret[k]).any():
print(f"! [Numerical Error] {k} contains inf.")
return ret
def forward(self, H, W, K, chunk=1024 * 32, rays=None, rays_info=None, poses=None, **kwargs):
"""
render rays or render poses, rays and poses should atleast specify one
calling model.train() to render rays, where rays, rays_info, should be specified
calling model.eval() to render an image, where poses should be specified
optional args:
force_naive: when True, will only run the naive NeRF, even if the kernelsnet is specified
"""
# training
if self.training:
assert rays is not None, "Please specify rays when in the training mode"
force_baseline = kwargs.pop("force_naive", True)
# kernel mode, run multiple rays to get result of one ray
if self.kernelsnet is not None and not force_baseline:
if self.kernelsnet.require_depth:
with torch.no_grad():
rgb, depth, acc, extras = self.render(H, W, K, chunk, rays, **kwargs)
rays_info["ray_depth"] = depth[:, None]
# time0 = time.time()
new_rays, weight, align_loss = self.kernelsnet(H, W, K, rays, rays_info)
ray_num, pt_num = new_rays.shape[:2]
# time1 = time.time()
rgb, depth, acc, extras = self.render(H, W, K, chunk, new_rays.reshape(-1, 3, 2), **kwargs)
rgb_pts = rgb.reshape(ray_num, pt_num, 3)
rgb0_pts = extras['rgb0'].reshape(ray_num, pt_num, 3)
# time2 = time.time()
rgb = torch.sum(rgb_pts * weight[..., None], dim=1)
rgb0 = torch.sum(rgb0_pts * weight[..., None], dim=1)
rgb = self.tonemapping(rgb)
rgb0 = self.tonemapping(rgb0)
# time3 = time.time()
# print(f"Time| kernel: {time1-time0:.5f}, nerf: {time2-time1:.5f}, fuse: {time3-time2}")
other_loss = {}
# compute align loss, some priors of the ray pattern
# ========================
if align_loss is not None:
other_loss["align"] = align_loss.reshape(1, 1)
return rgb, rgb0, other_loss
else:
rgb, depth, acc, extras = self.render(H, W, K, chunk, rays, **kwargs)
return self.tonemapping(rgb), self.tonemapping(extras['rgb0']), {}
# evaluation
else:
assert poses is not None, "Please specify poses when in the eval model"
if "render_point" in kwargs.keys():
rgbs, depths, weights = self.render_subpath(H, W, K, chunk, poses, **kwargs)
depths = weights * 2
else:
rgbs, depths = self.render_path(H, W, K, chunk, poses, **kwargs)
return self.tonemapping(rgbs), depths
def render(self, H, W, K, chunk, rays=None, c2w=None, ndc=True,
near=0., far=1.,
use_viewdirs=False, c2w_staticcam=None,
**kwargs): # the render function
"""Render rays
Args:
H: int. Height of image in pixels.
W: int. Width of image in pixels.
focal: float. Focal length of pinhole camera.
chunk: int. Maximum number of rays to process simultaneously. Used to
control maximum memory usage. Does not affect final results.
rays: array of shape [2, batch_size, 3]. Ray origin and direction for
each example in batch.
c2w: array of shape [3, 4]. Camera-to-world transformation matrix.
ndc: bool. If True, represent ray origin, direction in NDC coordinates.
near: float or array of shape [batch_size]. Nearest distance for a ray.
far: float or array of shape [batch_size]. Farthest distance for a ray.
use_viewdirs: bool. If True, use viewing direction of a point in space in model.
c2w_staticcam: array of shape [3, 4]. If not None, use this transformation matrix for
camera while using other c2w argument for viewing directions.
Returns:
rgb_map: [batch_size, 3]. Predicted RGB values for rays.
disp_map: [batch_size]. Disparity map. Inverse of depth.
acc_map: [batch_size]. Accumulated opacity (alpha) along a ray.
extras: dict with everything returned by render_rays().
"""
rays_o, rays_d = rays[..., 0], rays[..., 1]
if use_viewdirs:
# provide ray directions as input
viewdirs = rays_d
if c2w_staticcam is not None:
# special case to visualize effect of viewdirs
rays_o, rays_d = get_rays(H, W, K, c2w_staticcam)
viewdirs = viewdirs / torch.norm(viewdirs, dim=-1, keepdim=True)
viewdirs = torch.reshape(viewdirs, [-1, 3]).float()
sh = rays_d.shape # [..., 3]
if ndc:
# for forward facing scenes
rays_o, rays_d = ndc_rays(H, W, K[0][0], 1., rays_o, rays_d)
# Create ray batch
rays_o = torch.reshape(rays_o, [-1, 3]).float()
rays_d = torch.reshape(rays_d, [-1, 3]).float()
near, far = near * torch.ones_like(rays_d[..., :1]), far * torch.ones_like(rays_d[..., :1])
rays = torch.cat([rays_o, rays_d, near, far], -1)
if use_viewdirs:
rays = torch.cat([rays, viewdirs], -1)
# Batchfy and Render and reshape
all_ret = {}
for i in range(0, rays.shape[0], chunk):
ret = self.render_rays(rays[i:i + chunk], **kwargs)
for k in ret:
if k not in all_ret:
all_ret[k] = []
all_ret[k].append(ret[k])
all_ret = {k: torch.cat(all_ret[k], 0) for k in all_ret}
for k in all_ret:
k_sh = list(sh[:-1]) + list(all_ret[k].shape[1:])
all_ret[k] = torch.reshape(all_ret[k], k_sh)
k_extract = ['rgb_map', 'depth_map', 'acc_map']
ret_list = [all_ret[k] for k in k_extract]
ret_dict = {k: all_ret[k] for k in all_ret if k not in k_extract}
return ret_list + [ret_dict]
def render_path(self, H, W, K, chunk, render_poses, render_kwargs, render_factor=0, ):
"""
render image specified by the render_poses
"""
if render_factor != 0:
# Render downsampled for speed
H = H // render_factor
W = W // render_factor
rgbs = []
depths = []
t = time.time()
for i, c2w in enumerate(render_poses):
print(i, time.time() - t)
t = time.time()
rays = get_rays(H, W, K, c2w)
rays = torch.stack(rays, dim=-1)
rgb, depth, acc, extras = self.render(H, W, K, chunk=chunk, rays=rays, c2w=c2w[:3, :4], **render_kwargs)
rgbs.append(rgb)
depths.append(depth)
if i == 0:
print(rgb.shape, depth.shape)
rgbs = torch.stack(rgbs, 0)
depths = torch.stack(depths, 0)
return rgbs, depths
def render_subpath(self, H, W, K, chunk, render_poses, render_point, images_indices, render_kwargs,
render_factor=0):
"""
"""
if render_factor != 0:
# Render downsampled for speed
H = H // render_factor
W = W // render_factor
rgbs = []
depths = []
weights = []
t = time.time()
rayx, rayy = torch.meshgrid(torch.linspace(0, W - 1, W),
torch.linspace(0, H - 1, H))
rayx = rayx.t().reshape(-1, 1) + HALF_PIX
rayy = rayy.t().reshape(-1, 1) + HALF_PIX
for imgidx, c2w in zip(images_indices, render_poses):
i = int(imgidx.item())
print(i, time.time() - t)
t = time.time()
rays = get_rays(H, W, K, c2w)
rays = torch.stack(rays, dim=-1).reshape(H * W, 3, 2)
rays_info = {}
if self.kernelsnet.require_depth:
with torch.no_grad():
rgb, depth, acc, extras = self.render(H, W, K, chunk, rays, **render_kwargs)
rays_info["ray_depth"] = depth[..., None]
i = i if i < self.kernelsnet.num_img else 1
rays_info["images_idx"] = torch.ones_like(rays[:, 0:1, 0]).type(torch.long) * i
rays_info["rays_x"] = rayx
rays_info["rays_y"] = rayy
new_rays, weight, _ = self.kernelsnet(H, W, K, rays, rays_info)
new_rays = new_rays[:, render_point]
weight = weight[:, render_point]
rgb, depth, acc, extras = self.render(H, W, K, chunk=chunk, rays=new_rays.reshape(-1, 3, 2),
c2w=c2w[:3, :4], **render_kwargs)
rgbs.append(rgb.reshape(H, W, 3))
depths.append(depth.reshape(H, W))
weights.append(weight.reshape(H, W))
if i == 0:
print(rgb.shape, depth.shape)
rgbs = torch.stack(rgbs, 0)
depths = torch.stack(depths, 0)
weights = torch.stack(weights, 0)
return rgbs, depths, weights