-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
341 lines (311 loc) · 16 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
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
import torch
import torch.nn as nn
from nerf_helper import sample_along_rays, resample_along_rays, sample_along_rays_nerf, resample_along_rays_nerf
from nerf_render import volumetric_rendering, volumetric_rendering_nerf
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):
'''
Args
x : [B, N, 3]
y : [B, N, 3]
Return
x_ret : [B, N, E] (max_deg-min_deg)*(sin, cos)*(x,y,z) = E
y_ret : [B, N, E]
'''
shape = list(x.shape[:-1]) + [-1] # [B, N, -1]
x_enc = (x[..., None, :] * self.scales[:, None]).reshape(shape) # [B, N, L, 3] -> [B, N, 3L]
x_enc = torch.cat((x_enc, x_enc + 0.5 * torch.pi), -1) # [B, N, 6L]
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)
x_ret = torch.cat([x, x_ret], -1)
return x_ret, y_ret
else:
# PE
x_ret = torch.sin(x_enc)
x_ret = torch.cat([x, x_ret], -1)
return x_ret
class MipNeRF(nn.Module):
def __init__(self,
use_viewdirs=True,
randomized=False,
ray_shape="cone",
white_bkgd=True,
num_levels=2,
N_samples=64,
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.N_samples = N_samples
self.density_input = 3 + (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), # 0
nn.ReLU(True),
nn.Linear(hidden, hidden), # 1
nn.ReLU(True),
nn.Linear(hidden, hidden), # 2
nn.ReLU(True),
nn.Linear(hidden, hidden), # 3
nn.ReLU(True),
nn.Linear(hidden, hidden), # 4
nn.ReLU(True)
)
self.density_net1 = nn.Sequential(
nn.Linear(self.density_input+hidden, hidden), # 5
nn.ReLU(True),
nn.Linear(hidden, hidden), # 6
nn.ReLU(True),
nn.Linear(hidden, hidden), # 7
nn.ReLU(True)
)
self.final_density = nn.Sequential(
nn.Linear(hidden, 1),
)
input_shape = hidden
if self.use_viewdirs:
input_shape = hidden // 2
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, input_shape),
nn.ReLU(True),
)
self.final_rgb = nn.Sequential(
nn.Linear(input_shape, 3),
nn.Sigmoid()
)
_xavier_init(self)
self.to(device)
def forward(self, ray_batch):
comp_rgbs = []
distances = []
accs = []
rays_o, rays_d = ray_batch[:,0:3], ray_batch[:,3:6] # [N_rays, 3] each
bounds = torch.reshape(ray_batch[...,6:8], [-1,1,2]) # [N_rays, 1, 2]
near, far = bounds[...,0], bounds[...,1] # [N_rays, 1]
radii = torch.reshape(ray_batch[..., 8], [-1, 1]) # [N_rays, 1]
view_dirs = ray_batch[:,-3:] if ray_batch.shape[-1] > 9 else None # [N_rays, 3]
for l in range(self.num_levels):
# sample
if l == 0:
# t_vals : [N_rays, num_samples+1]
# mean, var : [N_rays, N_samples, 3]
N_samples = self.N_samples
t_vals, (mean, var) = sample_along_rays(rays_o, rays_d, radii, self.N_samples,
near, far, randomized=self.randomized, lindisp=False,
ray_shape=self.ray_shape)
else:
# new_t_vals : [N_rays, num_samples+1]
# new_mean, new_var : [N_rays, N_samples, 3]
t_vals, (mean, var) = resample_along_rays(rays_o, rays_d, radii, t_vals.to(rays_o.device),
weights.to(rays_o.device), randomized=self.randomized,
stop_grad=True, resample_padding=self.resample_padding, ray_shape=self.ray_shape)
N_samples = N_samples * 2 + 1
# do integrated positional encoding of samples
samples_enc = self.positional_encoding(mean, var)[0]
samples_enc = samples_enc.reshape([-1, samples_enc.shape[-1]]) # [N_rays*N_samples, 96]
# predict density
new_encodings = self.density_net0(samples_enc) # [N_rays*N_samples, 256]
new_encodings = torch.cat((new_encodings, samples_enc), -1) # [N_rays*N_samples, 256+96]
new_encodings = self.density_net1(new_encodings) # [N_rays*N_samples, 256]
raw_density = self.final_density(new_encodings).reshape((-1, N_samples, 1)) # [N_rays, N_samples, 1]
# predict rgb
if self.use_viewdirs:
# do positional encoding of viewdirs
viewdirs = self.viewdirs_encoding(view_dirs.to(self.device)) # [N_rays, 30]
#viewdirs = torch.cat((viewdirs, view_dirs.to(self.device)), -1) # [N_rays, 30]
viewdirs = torch.tile(viewdirs[:, None, :], (1, N_samples, 1)) # [N_rays, N_samples, 30]
viewdirs = viewdirs.reshape((-1, viewdirs.shape[-1])) # [N_rays*N_samples, 30]
new_encodings = self.rgb_net0(new_encodings) # [N_rays*N_samples, 256]
new_encodings = torch.cat((new_encodings, viewdirs), -1) # [N_rays*N_samples, 30+256]
new_encodings = self.rgb_net1(new_encodings) # [N_rays*N_samples, 286]
raw_rgb = self.final_rgb(new_encodings).reshape((-1, N_samples, 3)) # [N_rays, N_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_d.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)
class NeRF(nn.Module):
def __init__(self,
use_viewdirs=True,
randomized=False,
white_bkgd=True,
num_levels=2,
N_samples=64,
N_importance=128,
hidden=256,
density_noise=1,
min_deg=0, max_deg=10,
viewdirs_min_deg=0, viewdirs_max_deg=4,
device=torch.device("cpu"),
return_raw=False
):
super(NeRF, self).__init__()
self.use_viewdirs = use_viewdirs
self.init_randomized = randomized
self.randomized = randomized
self.white_bkgd = white_bkgd
self.num_levels = num_levels
self.N_samples = N_samples
self.N_importance = N_importance
self.density_input = 3 + (max_deg - min_deg) * 3 * 2
self.rgb_input = 3 + ((viewdirs_max_deg - viewdirs_min_deg) * 3 * 2)
self.density_noise = density_noise
self.hidden = hidden
self.device = device
self.return_raw = return_raw
self.density_activation = nn.ReLU(True)
self.positional_encoding = PositionalEncoding(min_deg, max_deg)
self.density_net0 = nn.Sequential(
nn.Linear(self.density_input, hidden), # 0
nn.ReLU(True),
nn.Linear(hidden, hidden), # 1
nn.ReLU(True),
nn.Linear(hidden, hidden), # 2
nn.ReLU(True),
nn.Linear(hidden, hidden), # 3
nn.ReLU(True),
nn.Linear(hidden, hidden), # 4
nn.ReLU(True)
)
# Concate input
self.density_net1 = nn.Sequential(
nn.Linear(self.density_input+hidden, hidden), # 5
nn.ReLU(True),
nn.Linear(hidden, hidden), # 6
nn.ReLU(True),
nn.Linear(hidden, hidden), # 7
nn.ReLU(True)
)
self.final_density = nn.Sequential(
nn.Linear(hidden, 1),
)
input_shape = hidden
if self.use_viewdirs:
input_shape = hidden // 2
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, input_shape),
nn.ReLU(True),
)
self.final_rgb = nn.Sequential(
nn.Linear(input_shape, 3),
nn.Sigmoid()
)
_xavier_init(self)
self.to(device)
def forward(self, ray_batch):
comp_rgbs = []
distances = []
accs = []
rays_o, rays_d = ray_batch[:,0:3], ray_batch[:,3:6] # [N_rays, 3] each
bounds = torch.reshape(ray_batch[...,6:8], [-1,1,2]) # [N_rays, 1, 2]
near, far = bounds[...,0], bounds[...,1] # [N_rays, 1]
view_dirs = ray_batch[:,-3:] if ray_batch.shape[-1] > 9 else None # [N_rays, 3]
for l in range(self.num_levels):
# sample
if l == 0:
num_samples = self.N_samples
t_vals, pts = sample_along_rays_nerf(rays_o, rays_d, self.N_samples,
near, far, randomized=self.randomized, lindisp=False)
# t_vals : [N_rays, N_samples]
# pts : [N_rays, N_samples, 3]
else:
num_samples = self.N_samples + self.N_importance
t_vals, pts = resample_along_rays_nerf(rays_o, rays_d, t_vals.to(rays_o.device),
weights.to(rays_o.device), self.N_importance, randomized=self.randomized)
# t_vals : [N_rays, N_samples+N_importance]
# pts : [N_rays, N_importance+N_samples, 3]
# do integrated positional encoding of samples
samples_enc = self.positional_encoding(pts) # [N_rays, N_samples, 60]
samples_enc = torch.cat([pts, samples_enc], -1) # [N_rays, N_samples, 63]
samples_enc = samples_enc.reshape([-1, samples_enc.shape[-1]]) # [N_rays*N_samples, 63]
# predict density
new_encodings = self.density_net0(samples_enc) # [N_rays*N_samples, 256]
new_encodings = torch.cat((samples_enc, new_encodings), -1) # [N_rays*N_samples, 256+63]
new_encodings = self.density_net1(new_encodings) # [N_rays*N_samples, 256]
raw_density = self.final_density(new_encodings).reshape((-1, num_samples, 1)) # [N_rays, N_samples, 1]
# predict rgb
if self.use_viewdirs:
# do positional encoding of viewdirs
viewdirs = self.viewdirs_encoding(view_dirs.to(self.device)) # [N_rays, 24]
viewdirs = torch.cat((view_dirs.to(self.device), viewdirs), -1) # [N_rays, 3+24]
viewdirs = torch.tile(viewdirs[:, None, :], (1, num_samples, 1)) # [N_rays, N_samples, 27]
viewdirs = viewdirs.reshape((-1, viewdirs.shape[-1])) # [N_rays*N_samples, 27]
new_encodings = self.rgb_net0(new_encodings) # [N_rays*N_samples, 256]
new_encodings = torch.cat((new_encodings, viewdirs), -1) # [N_rays*N_samples, 27+256]
new_encodings = self.rgb_net1(new_encodings) # [N_rays*N_samples, 128]
raw_rgb = self.final_rgb(new_encodings).reshape((-1, num_samples, 3)) # [N_rays, N_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
density = self.density_activation(raw_density)
comp_rgb, distance, acc, weights, alpha = volumetric_rendering_nerf(rgb, density, t_vals, rays_d.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 _xavier_init(model):
"""
Performs the Xavier weight initialization.
"""
for module in model.modules():
if isinstance(module, nn.Linear):
nn.init.xavier_uniform_(module.weight)