-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnerf_train_test.py
More file actions
999 lines (844 loc) · 31.9 KB
/
nerf_train_test.py
File metadata and controls
999 lines (844 loc) · 31.9 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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
import os
from typing import Optional, Tuple, List, Union, Callable
import numpy as np
import torch
from torch import nn
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from tqdm import trange
# For repeatability
# seed = 3407
# torch.manual_seed(seed)
# np.random.seed(seed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# if not os.path.exists('tiny_nerf_data.npz'):
# !wget http://cseweb.ucsd.edu/~viscomp/projects/LF/papers/ECCV20/nerf/tiny_nerf_data.npz
script_dir = os.path.dirname(os.path.realpath(__file__))
dataname='dozer'
datapath='data/'+dataname+'_data.npz'
data = np.load(os.path.join(script_dir,datapath))
#data = np.load(os.path.join(script_dir,"tiny_nerf_data.npz"))
images = data["images"]
poses = data["poses"]
focal = data["focal"]
print(f"Images shape: {images.shape}")
print(f"Poses shape: {poses.shape}")
print(f"Focal length: {focal}")
height, width = images.shape[1:3]
near, far = 2.0, 6.0
n_training = 100
testimg_idx = 101
testimg, testpose = images[testimg_idx], poses[testimg_idx]
plt.imshow(testimg)
print("Pose")
print(testpose)
plt.show()
dirs = np.stack([np.sum([0, 0, -1] * pose[:3, :3], axis=-1) for pose in poses])
origins = poses[:, :3, -1]
ax = plt.figure(figsize=(12, 8)).add_subplot(projection="3d")
_ = ax.quiver(
origins[..., 0].flatten(),
origins[..., 1].flatten(),
origins[..., 2].flatten(),
dirs[..., 0].flatten(),
dirs[..., 1].flatten(),
dirs[..., 2].flatten(),
length=0.5,
normalize=True,
)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("z")
plt.show()
def get_rays(
height: int, width: int, focal_length: float, c2w: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
r"""
Find origin and direction of rays through every pixel and camera origin.
"""
# print(c2w.shape)
# Apply pinhole camera model to gather directions at each pixel
i, j = torch.meshgrid(
torch.arange(width, dtype=torch.float32).to(c2w),
torch.arange(height, dtype=torch.float32).to(c2w),
indexing="ij",
)
i, j = i.transpose(-1, -2), j.transpose(-1, -2)
directions = torch.stack(
[
(i - width * 0.5) / focal_length,
-(j - height * 0.5) / focal_length,
-torch.ones_like(i),
],
dim=-1,
)
# Apply camera pose to directions
rays_d = torch.sum(directions[..., None, :] * c2w[:3, :3], dim=-1)
# Origin is same for all directions (the optical center)
rays_o = c2w[:3, -1].expand(rays_d.shape)
return rays_o, rays_d
# Gather as torch tensors
images = torch.from_numpy(data["images"][:n_training]).to(device)
poses = torch.from_numpy(data["poses"]).to(device)
focal = torch.from_numpy(data["focal"]).to(device)
testimg = torch.from_numpy(data["images"][testimg_idx]).to(device)
testpose = torch.from_numpy(data["poses"][testimg_idx]).to(device)
# Grab rays from sample image
height, width = images.shape[1:3]
with torch.no_grad():
ray_origin, ray_direction = get_rays(height, width, focal, testpose)
print("Ray Origin")
print(ray_origin.shape)
print(ray_origin[height // 2, width // 2, :])
print("")
print("Ray Direction")
print(ray_direction.shape)
print(ray_direction[height // 2, width // 2, :])
print("")
def sample_stratified(
rays_o: torch.Tensor,
rays_d: torch.Tensor,
near: float,
far: float,
n_samples: int,
perturb: Optional[bool] = True,
inverse_depth: bool = False,
) -> Tuple[torch.Tensor, torch.Tensor]:
r"""
Sample along ray from regularly-spaced bins.
"""
# Grab samples for space integration along ray
t_vals = torch.linspace(0.0, 1.0, n_samples, device=rays_o.device)
if not inverse_depth:
# Sample linearly between `near` and `far`
z_vals = near * (1.0 - t_vals) + far * (t_vals)
else:
# Sample linearly in inverse depth (disparity)
z_vals = 1.0 / (1.0 / near * (1.0 - t_vals) + 1.0 / far * (t_vals))
# Draw uniform samples from bins along ray
if perturb:
mids = 0.5 * (z_vals[1:] + z_vals[:-1])
upper = torch.concat([mids, z_vals[-1:]], dim=-1)
lower = torch.concat([z_vals[:1], mids], dim=-1)
t_rand = torch.rand([n_samples], device=z_vals.device)
z_vals = lower + (upper - lower) * t_rand
z_vals = z_vals.expand(list(rays_o.shape[:-1]) + [n_samples])
# Apply scale from `rays_d` and offset from `rays_o` to samples
# pts: (width, height, n_samples, 3)
pts = rays_o[..., None, :] + rays_d[..., None, :] * z_vals[..., :, None]
return pts, z_vals
# Draw stratified samples from example
rays_o = ray_origin.view([-1, 3])
rays_d = ray_direction.view([-1, 3])
n_samples = 8
perturb = True
inverse_depth = False
with torch.no_grad():
pts, z_vals = sample_stratified(
rays_o,
rays_d,
near,
far,
n_samples,
perturb=perturb,
inverse_depth=inverse_depth,
)
print("Input Points")
print(pts.shape)
print("")
print("Distances Along Ray")
print(z_vals.shape)
# Draw stratified samples from example
rays_o = ray_origin.view([-1, 3])
rays_d = ray_direction.view([-1, 3])
n_samples = 8
perturb = True
inverse_depth = False
with torch.no_grad():
pts, z_vals = sample_stratified(
rays_o,
rays_d,
near,
far,
n_samples,
perturb=perturb,
inverse_depth=inverse_depth,
)
print("Input Points")
print(pts.shape)
print("")
print("Distances Along Ray")
print(z_vals.shape)
y_vals = torch.zeros_like(z_vals)
_, z_vals_unperturbed = sample_stratified(
rays_o, rays_d, near, far, n_samples, perturb=False, inverse_depth=inverse_depth
)
plt.plot(z_vals_unperturbed[0].cpu().numpy(), 1 + y_vals[0].cpu().numpy(), "b-o")
plt.plot(z_vals[0].cpu().numpy(), y_vals[0].cpu().numpy(), "r-o")
plt.ylim([-1, 2])
plt.title("Stratified Sampling (blue) with Perturbation (red)")
ax = plt.gca()
ax.axes.yaxis.set_visible(False)
plt.grid(True)
plt.show()
class PositionalEncoder(nn.Module):
r"""
Sine-cosine positional encoder for input points.
"""
def __init__(self, d_input: int, n_freqs: int, log_space: bool = False):
super().__init__()
self.d_input = d_input
self.n_freqs = n_freqs
self.log_space = log_space
self.d_output = d_input * (1 + 2 * self.n_freqs)
self.embed_fns = [lambda x: x]
# Define frequencies in either linear or log scale
if self.log_space:
freq_bands = 2.0 ** torch.linspace(0.0, self.n_freqs - 1, self.n_freqs)
else:
freq_bands = torch.linspace(
2.0**0.0, 2.0 ** (self.n_freqs - 1), self.n_freqs
)
# Alternate sin and cos
for freq in freq_bands:
self.embed_fns.append(lambda x, freq=freq: torch.sin(x * freq))
self.embed_fns.append(lambda x, freq=freq: torch.cos(x * freq))
def forward(self, x) -> torch.Tensor:
r"""
Apply positional encoding to input.
"""
return torch.concat([fn(x) for fn in self.embed_fns], dim=-1)
# Create encoders for points and view directions
encoder = PositionalEncoder(3, 10)
viewdirs_encoder = PositionalEncoder(3, 4)
# Grab flattened points and view directions
pts_flattened = pts.reshape(-1, 3)
viewdirs = rays_d / torch.norm(rays_d, dim=-1, keepdim=True)
flattened_viewdirs = viewdirs[:, None, ...].expand(pts.shape).reshape((-1, 3))
# Encode inputs
encoded_points = encoder(pts_flattened)
encoded_viewdirs = viewdirs_encoder(flattened_viewdirs)
print("Encoded Points")
print(encoded_points.shape)
print(torch.min(encoded_points), torch.max(encoded_points), torch.mean(encoded_points))
print("")
print(encoded_viewdirs.shape)
print("Encoded Viewdirs")
print(
torch.min(encoded_viewdirs),
torch.max(encoded_viewdirs),
torch.mean(encoded_viewdirs),
)
print("")
class NeRF(nn.Module):
r"""
Neural radiance fields module.
"""
def __init__(
self,
d_input: int = 3,
n_layers: int = 8,
d_filter: int = 256,
skip: Tuple[int] = (4,),
d_viewdirs: Optional[int] = None,
):
super().__init__()
self.d_input = d_input
self.skip = skip
self.act = nn.functional.relu
self.d_viewdirs = d_viewdirs
# Create model layers
self.layers = nn.ModuleList(
[nn.Linear(self.d_input, d_filter)]
+ [
(
nn.Linear(d_filter + self.d_input, d_filter)
if i in skip
else nn.Linear(d_filter, d_filter)
)
for i in range(n_layers - 1)
]
)
# Bottleneck layers
if self.d_viewdirs is not None:
# If using viewdirs, split alpha and RGB
self.alpha_out = nn.Linear(d_filter, 1)
self.rgb_filters = nn.Linear(d_filter, d_filter)
self.branch = nn.Linear(d_filter + self.d_viewdirs, d_filter // 2)
self.output = nn.Linear(d_filter // 2, 3)
else:
# If no viewdirs, use simpler output
self.output = nn.Linear(d_filter, 4)
def forward(
self, x: torch.Tensor, viewdirs: Optional[torch.Tensor] = None
) -> torch.Tensor:
r"""
Forward pass with optional view direction.
"""
# Cannot use viewdirs if instantiated with d_viewdirs = None
if self.d_viewdirs is None and viewdirs is not None:
raise ValueError("Cannot input x_direction if d_viewdirs was not given.")
# Apply forward pass up to bottleneck
x_input = x
for i, layer in enumerate(self.layers):
x = self.act(layer(x))
if i in self.skip:
x = torch.cat([x, x_input], dim=-1)
# Apply bottleneck
if self.d_viewdirs is not None:
# Split alpha from network output
alpha = self.alpha_out(x)
# Pass through bottleneck to get RGB
x = self.rgb_filters(x)
x = torch.concat([x, viewdirs], dim=-1)
x = self.act(self.branch(x))
x = self.output(x)
# Concatenate alphas to output
x = torch.concat([x, alpha], dim=-1)
else:
# Simple output
x = self.output(x)
return x
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.0
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.0
if raw_noise_std > 0.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.0 - 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.0 / 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.0 - 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.0, 1.0, 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 = 0.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,
encoding_function: Callable[[torch.Tensor], 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 = encoding_function(points)
points = get_chunks(points, chunksize=chunksize)
return points
def prepare_viewdirs_chunks(
points: torch.Tensor,
rays_d: torch.Tensor,
encoding_function: Callable[[torch.Tensor], 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 = encoding_function(viewdirs)
viewdirs = get_chunks(viewdirs, chunksize=chunksize)
return viewdirs
def nerf_forward(
rays_o: torch.Tensor,
rays_d: torch.Tensor,
near: float,
far: float,
encoding_fn: Callable[[torch.Tensor], torch.Tensor],
coarse_model: nn.Module,
kwargs_sample_stratified: dict = None,
n_samples_hierarchical: int = 0,
kwargs_sample_hierarchical: dict = None,
fine_model=None,
viewdirs_encoding_fn: Optional[Callable[[torch.Tensor], torch.Tensor]] = None,
chunksize: int = 2**15,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, dict]:
r"""
Compute forward pass through model(s).
"""
# 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 = {}
# Sample query points along each ray.
query_points, z_vals = sample_stratified(
rays_o, rays_d, near, far, **kwargs_sample_stratified
)
# Prepare batches.
batches = prepare_chunks(query_points, encoding_fn, chunksize=chunksize)
if viewdirs_encoding_fn is not None:
batches_viewdirs = prepare_viewdirs_chunks(
query_points, rays_d, viewdirs_encoding_fn, chunksize=chunksize
)
else:
batches_viewdirs = [None] * len(batches)
# 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).
predictions = []
for batch, batch_viewdirs in zip(batches, batches_viewdirs):
predictions.append(coarse_model(batch, viewdirs=batch_viewdirs))
raw = torch.cat(predictions, dim=0)
raw = raw.reshape(list(query_points.shape[:2]) + [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}
# Fine model pass.
if n_samples_hierarchical > 0:
# Save previous outputs to return.
rgb_map_0, depth_map_0, acc_map_0 = rgb_map, depth_map, acc_map
# Apply hierarchical sampling for fine query points.
query_points, z_vals_combined, z_hierarch = sample_hierarchical(
rays_o,
rays_d,
z_vals,
weights,
n_samples_hierarchical,
**kwargs_sample_hierarchical,
)
# Prepare inputs as before.
batches = prepare_chunks(query_points, encoding_fn, chunksize=chunksize)
if viewdirs_encoding_fn is not None:
batches_viewdirs = prepare_viewdirs_chunks(
query_points, rays_d, viewdirs_encoding_fn, chunksize=chunksize
)
else:
batches_viewdirs = [None] * len(batches)
# Forward pass new samples through fine model.
fine_model = fine_model if fine_model is not None else coarse_model
predictions = []
for batch, batch_viewdirs in zip(batches, batches_viewdirs):
predictions.append(fine_model(batch, viewdirs=batch_viewdirs))
raw = torch.cat(predictions, dim=0)
raw = raw.reshape(list(query_points.shape[:2]) + [raw.shape[-1]])
# Perform differentiable volume rendering to re-synthesize the RGB image.
rgb_map, depth_map, acc_map, weights = raw2outputs(raw, z_vals_combined, rays_d)
# Store outputs.
outputs["z_vals_hierarchical"] = z_hierarch
outputs["rgb_map_0"] = rgb_map_0
outputs["depth_map_0"] = depth_map_0
outputs["acc_map_0"] = acc_map_0
# Store outputs.
outputs["rgb_map"] = rgb_map
outputs["depth_map"] = depth_map
outputs["acc_map"] = acc_map
outputs["weights"] = weights
return outputs
# 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
use_viewdirs = True # If set, use view direction as input
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 = 2 # Number of layers in network bottleneck
skip = [] # Layers at which to apply input residual
use_fine_model = True # If set, creates a fine model
d_filter_fine = 128 # Dimensions of linear layer filters of fine network
n_layers_fine = 2 # Number of layers in fine network bottleneck
# 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
n_iters = 20000
batch_size = 2**14 # Number of rays per gradient step (power of 2)
one_image_per_step = False # One image per gradient step (disables batching)
chunksize = 2**14 # Modify as needed to fit in GPU memory
center_crop = True # Crop the center of image (one_image_per_)
center_crop_iters = 50 # Stop cropping center after this many epochs
display_rate = 25 # Display test output every X epochs
# 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
n_restarts = 10 # Number of times to restart if training stalls
# 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}
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]
class EarlyStopping:
r"""
Early stopping helper based on fitness criterion.
"""
def __init__(self, patience: int = 30, margin: float = 1e-4):
self.best_fitness = 0.0 # In our case PSNR
self.best_iter = 0
self.margin = margin
self.patience = patience or float(
"inf"
) # epochs to wait after fitness stops improving to stop
def __call__(self, iter: int, fitness: float):
r"""
Check if criterion for stopping is met.
"""
if (fitness - self.best_fitness) > self.margin:
self.best_iter = iter
self.best_fitness = fitness
delta = iter - self.best_iter
stop = delta >= self.patience # stop training if patience exceeded
return stop
def init_models():
r"""
Initialize models, encoders, and optimizer for NeRF training.
"""
# Encoders
encoder = PositionalEncoder(d_input, n_freqs, log_space=log_space)
encode = lambda x: encoder(x)
# View direction encoders
if use_viewdirs:
encoder_viewdirs = PositionalEncoder(
d_input, n_freqs_views, log_space=log_space
)
encode_viewdirs = lambda x: encoder_viewdirs(x)
d_viewdirs = encoder_viewdirs.d_output
else:
encode_viewdirs = None
d_viewdirs = None
# Models
model = NeRF(
encoder.d_output,
n_layers=n_layers,
d_filter=d_filter,
skip=skip,
d_viewdirs=d_viewdirs,
)
model.to(device)
model_params = list(model.parameters())
if use_fine_model:
fine_model = NeRF(
encoder.d_output,
n_layers=n_layers,
d_filter=d_filter,
skip=skip,
d_viewdirs=d_viewdirs,
)
fine_model.to(device)
model_params = model_params + list(fine_model.parameters())
else:
fine_model = None
# Optimizer
optimizer = torch.optim.Adam(model_params, lr=lr)
# Early Stopping
warmup_stopper = EarlyStopping(patience=50)
return model, fine_model, encode, encode_viewdirs, optimizer, warmup_stopper
# model, fine_model, encode, encode_viewdirs, optimizer, warmup_stopper = init_models()
def train():
r"""
Launch training session for NeRF.
"""
# Shuffle rays across all images.
if not one_image_per_step:
height, width = images.shape[1:3]
all_rays = torch.stack(
[
torch.stack(get_rays(height, width, focal, p), 0)
for p in poses[:n_training]
],
0,
)
rays_rgb = torch.cat([all_rays, images[:, None]], 1)
rays_rgb = torch.permute(rays_rgb, [0, 2, 3, 1, 4])
rays_rgb = rays_rgb.reshape([-1, 3, 3])
rays_rgb = rays_rgb.type(torch.float32)
rays_rgb = rays_rgb[torch.randperm(rays_rgb.shape[0])]
i_batch = 0
train_psnrs = []
val_psnrs = []
iternums = []
for i in trange(n_iters):
model.train()
if one_image_per_step:
# Randomly pick an image as the target.
target_img_idx = np.random.randint(images.shape[0])
target_img = images[target_img_idx].to(device)
if center_crop and i < center_crop_iters:
target_img = crop_center(target_img)
height, width = target_img.shape[:2]
target_pose = poses[target_img_idx].to(device)
rays_o, rays_d = get_rays(height, width, focal, target_pose)
rays_o = rays_o.reshape([-1, 3])
rays_d = rays_d.reshape([-1, 3])
else:
# Random over all images.
batch = rays_rgb[i_batch : i_batch + batch_size]
batch = torch.transpose(batch, 0, 1)
rays_o, rays_d, target_img = batch
height, width = target_img.shape[:2]
i_batch += batch_size
# Shuffle after one epoch
if i_batch >= rays_rgb.shape[0]:
rays_rgb = rays_rgb[torch.randperm(rays_rgb.shape[0])]
i_batch = 0
target_img = target_img.reshape([-1, 3])
# Run one iteration of TinyNeRF and get the rendered RGB image.
outputs = nerf_forward(
rays_o,
rays_d,
near,
far,
encode,
model,
kwargs_sample_stratified=kwargs_sample_stratified,
n_samples_hierarchical=n_samples_hierarchical,
kwargs_sample_hierarchical=kwargs_sample_hierarchical,
fine_model=fine_model,
viewdirs_encoding_fn=encode_viewdirs,
chunksize=chunksize,
)
# 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, target_img)
loss.backward()
optimizer.step()
optimizer.zero_grad()
psnr = -10.0 * torch.log10(loss)
train_psnrs.append(psnr.item())
val_psnr = -10. * torch.log10(loss)
val_psnrs.append(val_psnr.item())
# Check PSNR for issues and stop if any are found.
if i == warmup_iters - 1:
if val_psnr < warmup_min_fitness:
print(
f"Val PSNR {val_psnr} below warmup_min_fitness {warmup_min_fitness}. Stopping..."
)
return False, train_psnrs, val_psnrs
elif i < warmup_iters:
if warmup_stopper is not None and warmup_stopper(i, psnr):
print(
f"Train PSNR flatlined at {psnr} for {warmup_stopper.patience} iters. Stopping..."
)
return False, train_psnrs, val_psnrs
if i%100==0:
feature=str(dataname)+"_"+str(n_freqs)+"_"+str(n_freqs_views)+"_"+str(d_filter)+"_"+str(n_layers)+"_"+str(n_iters)#+"_"+str(n_samples)
torch.save(model.state_dict(), "pts/nerf_"+feature+".pt")
torch.save(fine_model.state_dict(), "pts/nerf-fine_"+feature+".pt")
return True, train_psnrs, val_psnrs
# Run training session(s)
for _ in range(n_restarts):
model, fine_model, encode, encode_viewdirs, optimizer, warmup_stopper = (
init_models()
)
success, train_psnrs, val_psnrs = train()
if success and val_psnrs[-1] >= warmup_min_fitness:
print("Training successful!")
break
print("")
print(f"Done!")
model.eval()
height, width = testimg.shape[:2]
rays_o, rays_d = get_rays(height, width, focal, testpose)
rays_o = rays_o.reshape([-1, 3])
rays_d = rays_d.reshape([-1, 3])
outputs = nerf_forward(
rays_o,
rays_d,
near,
far,
encode,
model,
kwargs_sample_stratified=kwargs_sample_stratified,
n_samples_hierarchical=n_samples_hierarchical,
kwargs_sample_hierarchical=kwargs_sample_hierarchical,
fine_model=fine_model,
viewdirs_encoding_fn=encode_viewdirs,
chunksize=chunksize,
)
rgb_predicted = outputs["rgb_map"]
loss = torch.nn.functional.mse_loss(rgb_predicted, testimg.reshape(-1, 3))
print("Loss:", loss.item())
val_psnr = -10.0 * torch.log10(loss)
val_psnrs.append(val_psnr.item())
# iternums.append(i)
# Plot example outputs
fig, ax = plt.subplots(
1, 2, figsize=(8, 4), gridspec_kw={"width_ratios": [1, 1]}
)
ax[0].imshow(
rgb_predicted.reshape([height, width, 3]).detach().cpu().numpy()
)
ax[0].set_title(f"Iteration: {n_iters}")
ax[1].imshow(testimg.detach().cpu().numpy())
ax[1].set_title(f"Target")
# ax[2].plot(range(0, 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[2].margins(0)
plt.show()