-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning_nerf.py
More file actions
493 lines (409 loc) · 16.4 KB
/
learning_nerf.py
File metadata and controls
493 lines (409 loc) · 16.4 KB
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
import os
import imageio
import json
import matplotlib.pyplot as plt
import numpy as np
import cv2
from typing import Dict,Tuple,Optional,List,Callable
import tqdm
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from nerf import NeRF
from dataset import NeRFData
def cumprod_exclusive(
tensor: torch.Tensor
) -> torch.Tensor:
r"""
(Courtesy of https://github.com/krrish94/nerf-pytorch)
Mimick functionality of tf.math.cumprod(..., exclusive=True), as it isn't available in PyTorch.
Args:
tensor (torch.Tensor): Tensor whose cumprod (cumulative product, see `torch.cumprod`) along dim=-1
is to be computed.
Returns:
cumprod (torch.Tensor): cumprod of Tensor along dim=-1, mimiciking the functionality of
tf.math.cumprod(..., exclusive=True) (see `tf.math.cumprod` for details).
"""
# Compute regular cumprod first (this is equivalent to `tf.math.cumprod(..., exclusive=False)`).
cumprod = torch.cumprod(tensor, -1)
# "Roll" the elements along dimension 'dim' by 1 element.
cumprod = torch.roll(cumprod, 1, -1)
# Replace the first element by "1" as this is what tf.cumprod(..., exclusive=True) does.
cumprod[..., 0] = 1.
return cumprod
def raw2outputs(
raw: torch.Tensor,
z_vals: torch.Tensor,
rays_d: torch.Tensor,
raw_noise_std: float = 0.0,
white_bkgd: bool = False
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
r"""
Convert the raw NeRF output into RGB and other maps.
"""
# Difference between consecutive elements of `z_vals`. [n_rays, n_samples]
dists = z_vals[..., 1:] - z_vals[..., :-1]
dists = torch.cat([dists, 1e10 * torch.ones_like(dists[..., :1])], dim=-1)
# Multiply each distance by the norm of its corresponding direction ray
# to convert to real world distance (accounts for non-unit directions).
dists = dists * torch.norm(rays_d[..., None, :], dim=-1)
# Add noise to model's predictions for density. Can be used to
# regularize network during training (prevents floater artifacts).
noise = 0.
if raw_noise_std > 0.:
noise = torch.randn(raw[..., 3].shape) * raw_noise_std
# Predict density of each sample along each ray. Higher values imply
# higher likelihood of being absorbed at this point. [n_rays, n_samples]
alpha = 1.0 - torch.exp(-nn.functional.relu(raw[..., 3] + noise) * dists)
# Compute weight for RGB of each sample along each ray. [n_rays, n_samples]
# The higher the alpha, the lower subsequent weights are driven.
weights = alpha * cumprod_exclusive(1. - alpha + 1e-10)
# Compute weighted RGB map.
rgb = torch.sigmoid(raw[..., :3]) # [n_rays, n_samples, 3]
rgb_map = torch.sum(weights[..., None] * rgb, dim=-2) # [n_rays, 3]
# Estimated depth map is predicted distance.
depth_map = torch.sum(weights * z_vals, dim=-1)
# Disparity map is inverse depth.
disp_map = 1. / torch.max(1e-10 * torch.ones_like(depth_map),
depth_map / torch.sum(weights, -1))
# Sum of weights along each ray. In [0, 1] up to numerical error.
acc_map = torch.sum(weights, dim=-1)
# To composite onto a white background, use the accumulated alpha map.
if white_bkgd:
rgb_map = rgb_map + (1. - acc_map[..., None])
return rgb_map, depth_map, acc_map, weights
def sample_pdf(
bins: torch.Tensor,
weights: torch.Tensor,
n_samples: int,
perturb: bool = False
) -> torch.Tensor:
r"""
Apply inverse transform sampling to a weighted set of points.
"""
# Normalize weights to get PDF.
pdf = (weights + 1e-5) / torch.sum(weights + 1e-5, -1, keepdims=True) # [n_rays, weights.shape[-1]]
# Convert PDF to CDF.
cdf = torch.cumsum(pdf, dim=-1) # [n_rays, weights.shape[-1]]
cdf = torch.concat([torch.zeros_like(cdf[..., :1]), cdf], dim=-1) # [n_rays, weights.shape[-1] + 1]
# Take sample positions to grab from CDF. Linear when perturb == 0.
if not perturb:
u = torch.linspace(0., 1., n_samples, device=cdf.device)
u = u.expand(list(cdf.shape[:-1]) + [n_samples]) # [n_rays, n_samples]
else:
u = torch.rand(list(cdf.shape[:-1]) + [n_samples], device=cdf.device) # [n_rays, n_samples]
# Find indices along CDF where values in u would be placed.
u = u.contiguous() # Returns contiguous tensor with same values.
inds = torch.searchsorted(cdf, u, right=True) # [n_rays, n_samples]
# Clamp indices that are out of bounds.
below = torch.clamp(inds - 1, min=0)
above = torch.clamp(inds, max=cdf.shape[-1] - 1)
inds_g = torch.stack([below, above], dim=-1) # [n_rays, n_samples, 2]
# Sample from cdf and the corresponding bin centers.
matched_shape = list(inds_g.shape[:-1]) + [cdf.shape[-1]]
cdf_g = torch.gather(cdf.unsqueeze(-2).expand(matched_shape), dim=-1,
index=inds_g)
bins_g = torch.gather(bins.unsqueeze(-2).expand(matched_shape), dim=-1,
index=inds_g)
# Convert samples to ray length.
denom = (cdf_g[..., 1] - cdf_g[..., 0])
denom = torch.where(denom < 1e-5, torch.ones_like(denom), denom)
t = (u - cdf_g[..., 0]) / denom
samples = bins_g[..., 0] + t * (bins_g[..., 1] - bins_g[..., 0])
return samples # [n_rays, n_samples]
def sample_hierarchical(
rays_o: torch.Tensor,
rays_d: torch.Tensor,
z_vals: torch.Tensor,
weights: torch.Tensor,
n_samples: int,
perturb: bool = False
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
r"""
Apply hierarchical sampling to the rays.
"""
# Draw samples from PDF using z_vals as bins and weights as probabilities.
z_vals_mid = .5 * (z_vals[..., 1:] + z_vals[..., :-1])
new_z_samples = sample_pdf(z_vals_mid, weights[..., 1:-1], n_samples,
perturb=perturb)
new_z_samples = new_z_samples.detach()
# Resample points from ray based on PDF.
z_vals_combined, _ = torch.sort(torch.cat([z_vals, new_z_samples], dim=-1), dim=-1)
pts = rays_o[..., None, :] + rays_d[..., None, :] * z_vals_combined[..., :, None] # [N_rays, N_samples + n_samples, 3]
return pts, z_vals_combined, new_z_samples
def get_chunks(
inputs: torch.Tensor,
chunksize: int = 2**15
) -> List[torch.Tensor]:
r"""
Divide an input into chunks.
"""
return [inputs[i:i + chunksize] for i in range(0, inputs.shape[0], chunksize)]
def prepare_chunks(
points: torch.Tensor,
chunksize: int = 2**15
) -> List[torch.Tensor]:
r"""
Encode and chunkify points to prepare for NeRF model.
"""
points = points.reshape((-1, 3))
points = get_chunks(points, chunksize=chunksize)
return points
def prepare_viewdirs_chunks(
points: torch.Tensor,
rays_d: torch.Tensor,
chunksize: int = 2**15
) -> List[torch.Tensor]:
r"""
Encode and chunkify viewdirs to prepare for NeRF model.
"""
# Prepare the viewdirs
viewdirs = rays_d / torch.norm(rays_d, dim=-1, keepdim=True)
viewdirs = viewdirs[:, :, None, :].expand(points.shape).reshape((-1, 3))
viewdirs = get_chunks(viewdirs, chunksize=chunksize)
return viewdirs
def nerf_forward(
data,
chunksize: int,
coarse_model: nn.Module,
kwargs_sample_stratified: dict=None,
kwargs_sample_hierarchical: dict=None
):
# Set no kwargs if none are given.
if kwargs_sample_stratified is None:
kwargs_sample_stratified = {}
if kwargs_sample_hierarchical is None:
kwargs_sample_hierarchical = {}
query_points = data['query_points']
rays_d = data['rays_d']
z_vals = data['z_vals']
batches = prepare_chunks(query_points, chunksize=chunksize)
# Coarse model pass.
# Split the encoded points into "chunks", run the model on all chunks, and
# concatenate the results (to avoid out-of-memory issues).
# query point is [b, n_rays, n_samples, 3]
predictions = []
for batch in batches:
predictions.append(coarse_model(batch))
raw = torch.cat(predictions, dim=0)
raw = raw.reshape(list(query_points.shape[1:3]) + [raw.shape[-1]])
# Perform differentiable volume rendering to re-synthesize the RGB image.
rgb_map, depth_map, acc_map, weights = raw2outputs(raw, z_vals, rays_d)
# rgb_map, depth_map, acc_map, weights = render_volume_density(raw, rays_o, z_vals)
outputs = {
'z_vals_stratified': z_vals
}
# Store outputs.
outputs['rgb_map'] = rgb_map
outputs['depth_map'] = depth_map
outputs['acc_map'] = acc_map
outputs['weights'] = weights
return outputs
def plot_samples(
z_vals: torch.Tensor,
z_hierarch: Optional[torch.Tensor] = None,
ax: Optional[np.ndarray] = None):
r"""
Plot stratified and (optional) hierarchical samples.
"""
y_vals = 1 + np.zeros_like(z_vals)
if ax is None:
ax = plt.subplot()
ax.plot(z_vals, y_vals, 'b-o')
if z_hierarch is not None:
y_hierarch = np.zeros_like(z_hierarch)
ax.plot(z_hierarch, y_hierarch, 'r-o')
ax.set_ylim([-1, 2])
ax.set_title('Stratified Samples (blue) and Hierarchical Samples (red)')
ax.axes.yaxis.set_visible(False)
ax.grid(True)
return ax
def crop_center(
img: torch.Tensor,
frac: float = 0.5
) -> torch.Tensor:
r"""
Crop center square from image.
"""
h_offset = round(img.shape[0] * (frac / 2))
w_offset = round(img.shape[1] * (frac / 2))
return img[h_offset:-h_offset, w_offset:-w_offset]
def init_models(
d_input,
n_freqs,
log_space,
n_freqs_views,
n_layers,
d_filter,
skip,
device='cpu'):
model = NeRF(d_input=d_input, n_layers=n_layers, d_filter=d_filter, skip=skip,
n_freqs=n_freqs, log_space=log_space, n_freqs_views=n_freqs_views)
model.to(device)
model_params = list(model.parameters())
# Optimizer
optimizer = torch.optim.Adam(model_params, lr=lr)
return model, optimizer
# datadir = 'data/nerf_synthetic/lego'
# visualize = False
# images, poses, render_poses, hwf, i_split = load_blender_data(datadir)
data = np.load('tiny_nerf_data.npz')
images = data['images']
poses = data['poses']
focal = data['focal']
height, width = images.shape[1:3]
near, far = 2., 6.
n_training = 100
testimg_idx = 101
testimg, testpose = images[testimg_idx], poses[testimg_idx]
plt.imshow(testimg)
print('Pose')
print(testpose)
# Gather as torch tensors
device = 'cuda:0' if torch.cuda.is_available else 'cpu'
images = torch.from_numpy(images)
poses = torch.from_numpy(poses)
# try the real model now
# first define a bunch of model paramers
# Encoders
d_input = 3 # Number of input dimensions
n_freqs = 10 # Number of encoding functions for samples
log_space = True # If set, frequencies scale in log space
n_freqs_views = 4 # Number of encoding functions for views
# Stratified sampling
n_samples = 64 # Number of spatial samples per ray
perturb = True # If set, applies noise to sample positions
inverse_depth = False # If set, samples points linearly in inverse depth
# Model
d_filter = 128 # Dimensions of linear layer filters
n_layers = 8 # Number of layers in network bottleneck
skip = [4] # Layers at which to apply input residual
# Hierarchical sampling
n_samples_hierarchical = 64 # Number of samples per ray
perturb_hierarchical = False # If set, applies noise to sample positions
# Optimizer
lr = 5e-4 # Learning rate
# Training
batch_size = 2**14 # Number of rays per gradient step (power of 2)
display_rate = 25 # Display test output every X epochs
chunksize = 2**14
# Early Stopping
warmup_iters = 100 # Number of iterations during warmup phase
warmup_min_fitness = 10.0 # Min val PSNR to continue training at warmup_iters
# We bundle the kwargs for various functions to pass all at once.
kwargs_sample_stratified = {
'n_samples': n_samples,
'perturb': perturb,
'inverse_depth': inverse_depth
}
kwargs_sample_hierarchical = {
'perturb': perturb
}
model, optimizer = init_models(
d_input,
n_freqs,
log_space,
n_freqs_views,
n_layers,
d_filter,
skip,
device)
train_psnrs = []
val_psnrs = []
iternums = []
# height, width = images.shape[1:3]
# height = round(height * keep_ratio)
# width = round(width * keep_ratio)
nerf_train_data = NeRFData(images[:n_training, ...],
poses[:n_training, ...],
focal,
near,
far,
kwargs_sample_stratified,
device)
nerf_test_data = NeRFData(images[n_training:, ...],
poses[n_training:, ...],
focal,
near,
far,
kwargs_sample_stratified,
device)
train_dataloader = DataLoader(nerf_train_data, shuffle=True)
test_dataloader = DataLoader(nerf_test_data)
n_epoch = 100
for epoch in range(n_epoch):
print('Training epoch {}'.format(epoch))
for i, data in tqdm.tqdm(enumerate(train_dataloader), total=len(train_dataloader)):
outputs = nerf_forward(
data,
chunksize,
coarse_model=model,
kwargs_sample_stratified=kwargs_sample_stratified,
kwargs_sample_hierarchical=kwargs_sample_hierarchical,
)
# Check for any numerical issues.
for k, v in outputs.items():
if torch.isnan(v).any():
print(f"! [Numerical Alert] {k} contains NaN.")
if torch.isinf(v).any():
print(f"! [Numerical Alert] {k} contains Inf.")
# Backprop!
rgb_predicted = outputs['rgb_map']
loss = torch.nn.functional.mse_loss(rgb_predicted, data['target_image'])
loss.backward()
optimizer.step()
optimizer.zero_grad()
psnr = -10. * torch.log10(loss)
train_psnrs.append(psnr.item())
# Evaluate testimg at given display rate.
if i % display_rate == 0:
test_data = next(iter(test_dataloader))
model.eval()
outputs = nerf_forward(
test_data,
chunksize,
coarse_model=model,
kwargs_sample_stratified=kwargs_sample_stratified,
kwargs_sample_hierarchical=kwargs_sample_hierarchical,
)
rgb_predicted = outputs['rgb_map']
loss = torch.nn.functional.mse_loss(rgb_predicted, test_data['target_image'].reshape(-1, 3))
print("Loss:", loss.item())
val_psnr = -10. * torch.log10(loss)
val_psnrs.append(val_psnr.item())
iternums.append(epoch * len(train_dataloader) + i)
# Plot example outputs
fig, ax = plt.subplots(1, 4, figsize=(20,6), gridspec_kw={'width_ratios': [1, 1, 1, 3]})
ax[0].imshow(rgb_predicted.reshape([height, width, 3]).detach().cpu().numpy())
ax[0].set_title(f'Epoch: {epoch} Iteration: {i}')
ax[1].imshow(test_data['target_image'].reshape([height, width, 3]).detach().cpu().numpy())
ax[1].set_title(f'Test Target')
ax[2].plot(range(0, epoch * len(train_dataloader) + i + 1), train_psnrs, 'r')
ax[2].plot(iternums, val_psnrs, 'b')
ax[2].set_title('PSNR (train=red, val=blue')
z_vals_strat = outputs['z_vals_stratified'].view((-1, n_samples))
z_sample_strat = z_vals_strat[z_vals_strat.shape[0] // 2].detach().cpu().numpy()
if 'z_vals_hierarchical' in outputs:
z_vals_hierarch = outputs['z_vals_hierarchical'].view((-1, n_samples_hierarchical))
z_sample_hierarch = z_vals_hierarch[z_vals_hierarch.shape[0] // 2].detach().cpu().numpy()
else:
z_sample_hierarch = None
_ = plot_samples(z_sample_strat, z_sample_hierarch, ax=ax[3])
ax[3].margins(0)
# plt.show()
plt.savefig('test.jpg')
# save an model every 50 epoch
# save model
if psnr > 30.0:
# good enough model
state = {'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'loss': loss}
torch.save(state, 'nerf_{:04d}.pth'.format(epoch))
print('')
print(f'Done!')
# always saving the last model
state = {'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'loss': loss}
torch.save(state, 'nerf_{:04d}.pth'.format(epoch))