-
Notifications
You must be signed in to change notification settings - Fork 14
/
model.py
211 lines (192 loc) · 8.9 KB
/
model.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
import torch
import torch.nn as nn
from ray_utils import sample_along_rays, resample_along_rays, volumetric_rendering, namedtuple_map
from pose_utils import to8b
class PositionalEncoding(nn.Module):
def __init__(self, min_deg, max_deg):
super(PositionalEncoding, self).__init__()
self.min_deg = min_deg
self.max_deg = max_deg
self.scales = nn.Parameter(torch.tensor([2 ** i for i in range(min_deg, max_deg)]), requires_grad=False)
def forward(self, x, y=None):
shape = list(x.shape[:-1]) + [-1]
x_enc = (x[..., None, :] * self.scales[:, None]).reshape(shape)
x_enc = torch.cat((x_enc, x_enc + 0.5 * torch.pi), -1)
if y is not None:
# IPE
y_enc = (y[..., None, :] * self.scales[:, None]**2).reshape(shape)
y_enc = torch.cat((y_enc, y_enc), -1)
x_ret = torch.exp(-0.5 * y_enc) * torch.sin(x_enc)
y_ret = torch.maximum(torch.zeros_like(y_enc), 0.5 * (1 - torch.exp(-2 * y_enc) * torch.cos(2 * x_enc)) - x_ret ** 2)
return x_ret, y_ret
else:
# PE
x_ret = torch.sin(x_enc)
return x_ret
class MipNeRF(nn.Module):
def __init__(self,
use_viewdirs=True,
randomized=False,
ray_shape="cone",
white_bkgd=True,
num_levels=2,
num_samples=128,
hidden=256,
density_noise=1,
density_bias=-1,
rgb_padding=0.001,
resample_padding=0.01,
min_deg=0,
max_deg=16,
viewdirs_min_deg=0,
viewdirs_max_deg=4,
device=torch.device("cpu"),
return_raw=False
):
super(MipNeRF, self).__init__()
self.use_viewdirs = use_viewdirs
self.init_randomized = randomized
self.randomized = randomized
self.ray_shape = ray_shape
self.white_bkgd = white_bkgd
self.num_levels = num_levels
self.num_samples = num_samples
self.density_input = (max_deg - min_deg) * 3 * 2
self.rgb_input = 3 + ((viewdirs_max_deg - viewdirs_min_deg) * 3 * 2)
self.density_noise = density_noise
self.rgb_padding = rgb_padding
self.resample_padding = resample_padding
self.density_bias = density_bias
self.hidden = hidden
self.device = device
self.return_raw = return_raw
self.density_activation = nn.Softplus()
self.positional_encoding = PositionalEncoding(min_deg, max_deg)
self.density_net0 = nn.Sequential(
nn.Linear(self.density_input, hidden),
nn.ReLU(True),
nn.Linear(hidden, hidden),
nn.ReLU(True),
nn.Linear(hidden, hidden),
nn.ReLU(True),
nn.Linear(hidden, hidden),
nn.ReLU(True),
)
self.density_net1 = nn.Sequential(
nn.Linear(self.density_input + hidden, hidden),
nn.ReLU(True),
nn.Linear(hidden, hidden),
nn.ReLU(True),
nn.Linear(hidden, hidden),
nn.ReLU(True),
nn.Linear(hidden, hidden),
nn.ReLU(True),
)
self.final_density = nn.Sequential(
nn.Linear(hidden, 1),
)
input_shape = hidden
if self.use_viewdirs:
input_shape = num_samples
self.rgb_net0 = nn.Sequential(
nn.Linear(hidden, hidden)
)
self.viewdirs_encoding = PositionalEncoding(viewdirs_min_deg, viewdirs_max_deg)
self.rgb_net1 = nn.Sequential(
nn.Linear(hidden + self.rgb_input, num_samples),
nn.ReLU(True),
)
self.final_rgb = nn.Sequential(
nn.Linear(input_shape, 3),
nn.Sigmoid()
)
_xavier_init(self)
self.to(device)
def forward(self, rays):
comp_rgbs = []
distances = []
accs = []
for l in range(self.num_levels):
# sample
if l == 0: # coarse grain sample
t_vals, (mean, var) = sample_along_rays(rays.origins, rays.directions, rays.radii, self.num_samples,
rays.near, rays.far, randomized=self.randomized, lindisp=False,
ray_shape=self.ray_shape)
else: # fine grain sample/s
t_vals, (mean, var) = resample_along_rays(rays.origins, rays.directions, rays.radii,
t_vals.to(rays.origins.device),
weights.to(rays.origins.device), randomized=self.randomized,
stop_grad=True, resample_padding=self.resample_padding,
ray_shape=self.ray_shape)
# do integrated positional encoding of samples
samples_enc = self.positional_encoding(mean, var)[0]
samples_enc = samples_enc.reshape([-1, samples_enc.shape[-1]])
# predict density
new_encodings = self.density_net0(samples_enc)
new_encodings = torch.cat((new_encodings, samples_enc), -1)
new_encodings = self.density_net1(new_encodings)
raw_density = self.final_density(new_encodings).reshape((-1, self.num_samples, 1))
# predict rgb
if self.use_viewdirs:
# do positional encoding of viewdirs
viewdirs = self.viewdirs_encoding(rays.viewdirs.to(self.device))
viewdirs = torch.cat((viewdirs, rays.viewdirs.to(self.device)), -1)
viewdirs = torch.tile(viewdirs[:, None, :], (1, self.num_samples, 1))
viewdirs = viewdirs.reshape((-1, viewdirs.shape[-1]))
new_encodings = self.rgb_net0(new_encodings)
new_encodings = torch.cat((new_encodings, viewdirs), -1)
new_encodings = self.rgb_net1(new_encodings)
raw_rgb = self.final_rgb(new_encodings).reshape((-1, self.num_samples, 3))
# Add noise to regularize the density predictions if needed.
if self.randomized and self.density_noise:
raw_density += self.density_noise * torch.rand(raw_density.shape, dtype=raw_density.dtype, device=raw_density.device)
# volumetric rendering
rgb = raw_rgb * (1 + 2 * self.rgb_padding) - self.rgb_padding
density = self.density_activation(raw_density + self.density_bias)
comp_rgb, distance, acc, weights, alpha = volumetric_rendering(rgb, density, t_vals, rays.directions.to(rgb.device), self.white_bkgd)
comp_rgbs.append(comp_rgb)
distances.append(distance)
accs.append(acc)
if self.return_raw:
raws = torch.cat((torch.clone(rgb).detach(), torch.clone(density).detach()), -1).cpu()
# Predicted RGB values for rays, Disparity map (inverse of depth), Accumulated opacity (alpha) along a ray
return torch.stack(comp_rgbs), torch.stack(distances), torch.stack(accs), raws
else:
# Predicted RGB values for rays, Disparity map (inverse of depth), Accumulated opacity (alpha) along a ray
return torch.stack(comp_rgbs), torch.stack(distances), torch.stack(accs)
def render_image(self, rays, height, width, chunks=8192):
"""
Return image, disparity map, accumulated opacity (shaped to height x width) created using rays as input.
Rays should be all of the rays that correspond to this one single image.
Batches the rays into chunks to not overload memory of device
"""
length = rays[0].shape[0]
rgbs = []
dists = []
accs = []
with torch.no_grad():
for i in range(0, length, chunks):
# put chunk of rays on device
chunk_rays = namedtuple_map(lambda r: r[i:i+chunks].to(self.device), rays)
rgb, distance, acc = self(chunk_rays)
rgbs.append(rgb[-1].cpu())
dists.append(distance[-1].cpu())
accs.append(acc[-1].cpu())
rgbs = to8b(torch.cat(rgbs, dim=0).reshape(height, width, 3).numpy())
dists = torch.cat(dists, dim=0).reshape(height, width).numpy()
accs = torch.cat(accs, dim=0).reshape(height, width).numpy()
return rgbs, dists, accs
def train(self, mode=True):
self.randomized = self.init_randomized
super().train(mode)
return self
def eval(self):
self.randomized = False
return super().eval()
def _xavier_init(model):
"""
Performs the Xavier weight initialization.
"""
for module in model.modules():
if isinstance(module, nn.Linear):
nn.init.xavier_uniform_(module.weight)