-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnerf.py
333 lines (254 loc) · 12.2 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from run_nerf import *
from Spline import se3_to_SE3
max_iter = 200000
T = max_iter+1
BOUNDARY = 20
class Embedder:
def __init__(self, **kwargs):
self.kwargs = kwargs
self.create_embedding_fn()
def create_embedding_fn(self):
embed_fns = []
d = self.kwargs['input_dims']
out_dim = 0
if self.kwargs['include_input']:
embed_fns.append(lambda x: x)
out_dim += d
max_freq = self.kwargs['max_freq_log2']
N_freqs = self.kwargs['num_freqs']
if self.kwargs['log_sampling']:
freq_bands = 2. ** torch.linspace(0., max_freq, steps=N_freqs)
else:
freq_bands = torch.linspace(2. ** 0., 2. ** max_freq, steps=N_freqs)
for freq in freq_bands:
for p_fn in self.kwargs['periodic_fns']:
embed_fns.append(lambda x, p_fn=p_fn, freq=freq: p_fn(x * freq))
out_dim += d
self.embed_fns = embed_fns
self.out_dim = out_dim
def embed(self, inputs):
return torch.cat([fn(inputs) for fn in self.embed_fns], -1)
def get_embedder(args, multires, i=0):
if i == -1:
return nn.Identity(), 3
embed_kwargs = {
'include_input': False if args.barf else True,
'input_dims': 3,
'max_freq_log2': multires - 1,
'num_freqs': multires,
'log_sampling': True,
'periodic_fns': [torch.sin, torch.cos],
}
embedder_obj = Embedder(**embed_kwargs)
embed = lambda x, eo=embedder_obj: eo.embed(x)
return embed, embedder_obj.out_dim
class Model():
def __init__(self):
super().__init__()
def build_network(self, args):
self.graph = Graph(args, D=8, W=256, input_ch=63, input_ch_views=27, output_ch=4, skips=[4], use_viewdirs=True)
return self.graph
def setup_optimizer(self, args):
grad_vars = list(self.graph.nerf.parameters())
if args.N_importance>0:
grad_vars += list(self.graph.nerf_fine.parameters())
self.optim = torch.optim.Adam(params=grad_vars, lr=args.lrate, betas=(0.9, 0.999))
return self.optim
class NeRF(nn.Module):
def __init__(self, D=8, W=256, input_ch=63, input_ch_views=27, output_ch=4, skips=[4], use_viewdirs=False):
super().__init__()
self.D = D
self.W = W
self.input_ch = input_ch
self.input_ch_views = input_ch_views
self.skips = skips
self.use_viewdirs = use_viewdirs
# network
self.pts_linears = nn.ModuleList(
[nn.Linear(input_ch, W)] + [nn.Linear(W, W) if i not in self.skips else nn.Linear(W + input_ch, W) for i in
range(D - 1)])
self.views_linears = nn.ModuleList([nn.Linear(input_ch_views + W, W // 2)])
if use_viewdirs:
self.feature_linear = nn.Linear(W, W)
self.alpha_linear = nn.Linear(W, 1)
self.rgb_linear = nn.Linear(W // 2, 3)
else:
self.output_linear = nn.Linear(W, output_ch)
def forward(self, barf_i, pts, viewdirs, args):
embed_fn, input_ch = get_embedder(args, args.multires, args.i_embed)
embeddirs_fn = None
if args.use_viewdirs:
embeddirs_fn, input_ch_views = get_embedder(args, args.multires_views, args.i_embed)
pts_flat = torch.reshape(pts, [-1, pts.shape[-1]])
embedded = embed_fn(pts_flat)
if viewdirs is not None:
input_dirs = viewdirs[:, None].expand(pts.shape)
input_dirs_flat = torch.reshape(input_dirs, [-1, input_dirs.shape[-1]])
embedded_dirs = embeddirs_fn(input_dirs_flat)
embedded = torch.cat([embedded, embedded_dirs], -1)
input_pts, input_views = torch.split(embedded, [self.input_ch, self.input_ch_views], dim=-1)
h = input_pts
for i, l in enumerate(self.pts_linears):
h = self.pts_linears[i](h)
h = F.relu(h)
if i in self.skips:
h = torch.cat([input_pts, h], -1)
if self.use_viewdirs:
alpha = self.alpha_linear(h)
feature = self.feature_linear(h)
h = torch.cat([feature, input_views], -1)
for i, l in enumerate(self.views_linears):
h = self.views_linears[i](h)
h = F.relu(h)
rgb = self.rgb_linear(h)
outputs = torch.cat([rgb, alpha], -1)
else:
outputs = self.output_linear(h)
outputs = torch.reshape(outputs, list(pts.shape[:-1]) + [outputs.shape[-1]])
return outputs
def raw2output(self, raw, z_vals, rays_d, raw_noise_std=0.0):
raw2alpha = lambda raw, dists, act_fn=F.relu: 1. - torch.exp(-act_fn(raw) * dists)
dists = z_vals[..., 1:] - z_vals[..., :-1]
dists = torch.cat([dists, torch.Tensor([1e10]).expand(dists[..., :1].shape)], -1)
dists = dists * torch.norm(rays_d[..., None, :], dim=-1)
rgb = torch.sigmoid(raw[..., :3])
noise = 0.
if raw_noise_std > 0.:
noise = torch.randn(raw[..., 3].shape) * raw_noise_std
alpha = raw2alpha(raw[..., 3] + noise, dists)
weights = alpha * torch.cumprod(torch.cat([torch.ones((alpha.shape[0], 1)), 1. - alpha + 1e-10], -1), -1)[:,:-1]
rgb_map = torch.sum(weights[..., None] * rgb, -2)
depth_map = torch.sum(weights * z_vals, -1)
# disp_map = 1. / torch.max(1e-10 * torch.ones_like(depth_map), depth_map / torch.sum(weights, -1))
disp_map = torch.max(1e-6 * torch.ones_like(depth_map), depth_map / (torch.sum(weights, -1)+1e-6))
acc_map = torch.sum(weights, -1)
sigma = F.relu(raw[..., 3] + noise)
return rgb_map, disp_map, acc_map, weights, depth_map, sigma
class Graph(nn.Module):
def __init__(self, args, D=8, W=256, input_ch=63, input_ch_views=27, output_ch=4, skips=[4], use_viewdirs=False):
super().__init__()
self.nerf = NeRF(D, W, input_ch, input_ch_views, output_ch, skips, use_viewdirs)
if args.N_importance > 0:
self.nerf_fine = NeRF(D, W, input_ch, input_ch_views, output_ch, skips, use_viewdirs)
def forward(self, i, img_idx, poses_num, H, W, K, args, novel_view=False):
if novel_view:
poses_sharp = se3_to_SE3(self.se3_sharp.weight)
ray_idx_sharp = torch.randperm(H * W)[:300]
ret = self.render(i, poses_sharp, ray_idx_sharp, H, W, K, args)
return ret, ray_idx_sharp, poses_sharp
spline_poses = self.get_pose(i, img_idx, args)
ray_idx = torch.randperm(H * W)[:args.N_rand // poses_num]
'''
# only used in distorted data
# aims to prevent the ray_idx lying on the edges
for j in range(ray_idx.shape[0]):
h = torch.randperm(H - 1)[0]
w = torch.randperm(W - 1)[0]
while (h < BOUNDARY or h > (H - 1 - BOUNDARY) or w < BOUNDARY or w > (W - 1 - BOUNDARY)):
h = torch.randperm(H - 1)[0]
w = torch.randperm(W - 1)[0]
index = h * W + w
ray_idx[j] = index
'''
ret = self.render(i, spline_poses, ray_idx, H, W, K, args, near=0, far=1.0, ray_idx_tv=None, training=True)
if (i % args.i_img == 0 or i % args.i_novel_view == 0) and i > 0:
if args.deblur_images % 2 == 0:
all_poses = self.get_pose_even(i, torch.arange(self.se3.weight.shape[0]), args.deblur_images)
else:
all_poses = self.get_pose(i, torch.arange(self.se3.weight.shape[0]), args)
return ret, ray_idx, spline_poses, all_poses
else:
return ret, ray_idx, spline_poses
def get_pose(self, i, img_idx, args):
return i
def get_gt_pose(self, poses, args):
return poses
def render(self, barf_i, poses, ray_idx, H, W, K, args, near=0., far=1., ray_idx_tv=None, training=False):
if training:
ray_idx_ = ray_idx.repeat(poses.shape[0])
poses = poses.unsqueeze(1).repeat(1, ray_idx.shape[0], 1, 1).reshape(-1, 3, 4)
j = ray_idx_.reshape(-1, 1).squeeze() // W
i = ray_idx_.reshape(-1, 1).squeeze() % W
rays_o_, rays_d_ = get_specific_rays(i, j, K, poses)
rays_o_d = torch.stack([rays_o_, rays_d_], 0)
batch_rays = torch.permute(rays_o_d, [1, 0, 2])
else:
rays_list = []
for p in poses[:, :3, :4]:
rays_o_, rays_d_ = get_rays(H, W, K, p)
rays_o_d = torch.stack([rays_o_, rays_d_], 0)
rays_list.append(rays_o_d)
rays = torch.stack(rays_list, 0)
rays = rays.reshape(-1, 2, H * W, 3)
rays = torch.permute(rays, [0, 2, 1, 3])
batch_rays = rays[:, ray_idx]
batch_rays = batch_rays.reshape(-1, 2, 3)
batch_rays = torch.transpose(batch_rays, 0, 1)
# get standard rays
rays_o, rays_d = batch_rays
if args.use_viewdirs:
viewdirs = rays_d
viewdirs = viewdirs / torch.norm(viewdirs, dim=-1, keepdim=True)
viewdirs = torch.reshape(viewdirs, [-1, 3]).float()
sh = rays_d.shape
if args.ndc:
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 args.use_viewdirs:
rays = torch.cat([rays, viewdirs], -1)
N_rays = rays.shape[0]
rays_o, rays_d = rays[:, 0:3], rays[:, 3:6]
viewdirs = rays[:, -3:] if rays.shape[-1] > 8 else None
bounds = torch.reshape(rays[..., 6:8], [-1, 1, 2])
near, far = bounds[..., 0], bounds[..., 1]
t_vals = torch.linspace(0., 1., steps=args.N_samples)
z_vals = near * (1. - t_vals) + far * (t_vals)
z_vals = z_vals.expand([N_rays, args.N_samples])
# perturb
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)
z_vals = lower + (upper - lower) * t_rand
pts = rays_o[..., None, :] + rays_d[..., None, :] * z_vals[..., :, None]
raw_output = self.nerf.forward(barf_i, pts, viewdirs, args)
rgb_map, disp_map, acc_map, weights, depth_map, sigma = self.nerf.raw2output(raw_output, z_vals, rays_d)
if args.N_importance > 0:
rgb_map_0, disp_map_0, acc_map_0 = rgb_map, disp_map, acc_map
z_vals_mid = .5 * (z_vals[..., 1:] + z_vals[..., :-1])
z_samples = sample_pdf(z_vals_mid, weights[..., 1:-1], args.N_importance)
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]
raw_output = self.nerf_fine.forward(barf_i, pts, viewdirs, args)
rgb_map, disp_map, acc_map, weights, depth_map, sigma = self.nerf_fine.raw2output(raw_output, z_vals, rays_d)
ret = {'rgb_map': rgb_map, 'disp_map': disp_map, 'acc_map': acc_map, 'depth_map': depth_map}
if args.N_importance > 0:
ret['rgb0'] = rgb_map_0
ret['disp0'] = disp_map_0
ret['acc0'] = acc_map_0
ret['sigma'] = sigma
return ret
@torch.no_grad()
def render_video(self, barf_i, poses, H, W, K, args):
all_ret = {}
ray_idx = torch.arange(0, H*W)
for i in range(0, ray_idx.shape[0], args.chunk):
ret = self.render(barf_i, poses, ray_idx[i:i+args.chunk], H, W, K, args)
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([H, W]) + list(all_ret[k].shape[1:])
all_ret[k] = torch.reshape(all_ret[k], k_sh)
return all_ret