-
Notifications
You must be signed in to change notification settings - Fork 19
/
implicit.py
380 lines (311 loc) · 10.4 KB
/
implicit.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
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
import torch
import torch.nn.functional as F
from torch import autograd
from ray_utils import RayBundle
# Sphere SDF class
class SphereSDF(torch.nn.Module):
def __init__(
self,
cfg
):
super().__init__()
self.radius = torch.nn.Parameter(
torch.tensor(cfg.radius.val).float(), requires_grad=cfg.radius.opt
)
self.center = torch.nn.Parameter(
torch.tensor(cfg.center.val).float().unsqueeze(0), requires_grad=cfg.center.opt
)
def forward(self, points):
points = points.view(-1, 3)
return torch.linalg.norm(
points - self.center,
dim=-1,
keepdim=True
) - self.radius
# Box SDF class
class BoxSDF(torch.nn.Module):
def __init__(
self,
cfg
):
super().__init__()
self.center = torch.nn.Parameter(
torch.tensor(cfg.center.val).float().unsqueeze(0), requires_grad=cfg.center.opt
)
self.side_lengths = torch.nn.Parameter(
torch.tensor(cfg.side_lengths.val).float().unsqueeze(0), requires_grad=cfg.side_lengths.opt
)
def forward(self, points):
points = points.view(-1, 3)
diff = torch.abs(points - self.center) - self.side_lengths / 2.0
signed_distance = torch.linalg.norm(
torch.maximum(diff, torch.zeros_like(diff)),
dim=-1
) + torch.minimum(torch.max(diff, dim=-1)[0], torch.zeros_like(diff[..., 0]))
return signed_distance.unsqueeze(-1)
# Torus SDF class
class TorusSDF(torch.nn.Module):
def __init__(
self,
cfg
):
super().__init__()
self.center = torch.nn.Parameter(
torch.tensor(cfg.center.val).float().unsqueeze(0), requires_grad=cfg.center.opt
)
self.radii = torch.nn.Parameter(
torch.tensor(cfg.radii.val).float().unsqueeze(0), requires_grad=cfg.radii.opt
)
def forward(self, points):
points = points.view(-1, 3)
diff = points - self.center
q = torch.stack(
[
torch.linalg.norm(diff[..., :2], dim=-1) - self.radii[..., 0],
diff[..., -1],
],
dim=-1
)
return (torch.linalg.norm(q, dim=-1) - self.radii[..., 1]).unsqueeze(-1)
sdf_dict = {
'sphere': SphereSDF,
'box': BoxSDF,
'torus': TorusSDF,
}
# Converts SDF into density/feature volume
class SDFVolume(torch.nn.Module):
def __init__(
self,
cfg
):
super().__init__()
self.sdf = sdf_dict[cfg.sdf.type](
cfg.sdf
)
self.rainbow = cfg.feature.rainbow if 'rainbow' in cfg.feature else False
self.feature = torch.nn.Parameter(
torch.ones_like(torch.tensor(cfg.feature.val).float().unsqueeze(0)), requires_grad=cfg.feature.opt
)
self.alpha = torch.nn.Parameter(
torch.tensor(cfg.alpha.val).float(), requires_grad=cfg.alpha.opt
)
self.beta = torch.nn.Parameter(
torch.tensor(cfg.beta.val).float(), requires_grad=cfg.beta.opt
)
def _sdf_to_density(self, signed_distance):
# Convert signed distance to density with alpha, beta parameters
return torch.where(
signed_distance > 0,
0.5 * torch.exp(-signed_distance / self.beta),
1 - 0.5 * torch.exp(signed_distance / self.beta),
) * self.alpha
def forward(self, ray_bundle):
sample_points = ray_bundle.sample_points.view(-1, 3)
depth_values = ray_bundle.sample_lengths[..., 0]
deltas = torch.cat(
(
depth_values[..., 1:] - depth_values[..., :-1],
1e10 * torch.ones_like(depth_values[..., :1]),
),
dim=-1,
).view(-1, 1)
# Transform SDF to density
signed_distance = self.sdf(ray_bundle.sample_points)
density = self._sdf_to_density(signed_distance)
# Outputs
if self.rainbow:
base_color = torch.clamp(
torch.abs(sample_points - self.sdf.center),
0.02,
0.98
)
else:
base_color = 1.0
out = {
'density': -torch.log(1.0 - density) / deltas,
'feature': base_color * self.feature * density.new_ones(sample_points.shape[0], 1)
}
return out
# Converts SDF into density/feature volume
class SDFSurface(torch.nn.Module):
def __init__(
self,
cfg
):
super().__init__()
self.sdf = sdf_dict[cfg.sdf.type](
cfg.sdf
)
self.rainbow = cfg.feature.rainbow if 'rainbow' in cfg.feature else False
self.feature = torch.nn.Parameter(
torch.ones_like(torch.tensor(cfg.feature.val).float().unsqueeze(0)), requires_grad=cfg.feature.opt
)
def get_distance(self, points):
points = points.view(-1, 3)
return self.sdf(points)
def get_color(self, points):
points = points.view(-1, 3)
# Outputs
if self.rainbow:
base_color = torch.clamp(
torch.abs(points - self.sdf.center),
0.02,
0.98
)
else:
base_color = 1.0
return base_color * self.feature * points.new_ones(points.shape[0], 1)
def forward(self, points):
return self.get_distance(points)
class HarmonicEmbedding(torch.nn.Module):
def __init__(
self,
in_channels: int = 3,
n_harmonic_functions: int = 6,
omega0: float = 1.0,
logspace: bool = True,
include_input: bool = True,
) -> None:
super().__init__()
if logspace:
frequencies = 2.0 ** torch.arange(
n_harmonic_functions,
dtype=torch.float32,
)
else:
frequencies = torch.linspace(
1.0,
2.0 ** (n_harmonic_functions - 1),
n_harmonic_functions,
dtype=torch.float32,
)
self.register_buffer("_frequencies", omega0 * frequencies, persistent=False)
self.include_input = include_input
self.output_dim = n_harmonic_functions * 2 * in_channels
if self.include_input:
self.output_dim += in_channels
def forward(self, x: torch.Tensor):
embed = (x[..., None] * self._frequencies).view(*x.shape[:-1], -1)
if self.include_input:
return torch.cat((embed.sin(), embed.cos(), x), dim=-1)
else:
return torch.cat((embed.sin(), embed.cos()), dim=-1)
class LinearWithRepeat(torch.nn.Linear):
def forward(self, input):
n1 = input[0].shape[-1]
output1 = F.linear(input[0], self.weight[:, :n1], self.bias)
output2 = F.linear(input[1], self.weight[:, n1:], None)
return output1 + output2.unsqueeze(-2)
class MLPWithInputSkips(torch.nn.Module):
def __init__(
self,
n_layers: int,
input_dim: int,
output_dim: int,
skip_dim: int,
hidden_dim: int,
input_skips,
):
super().__init__()
layers = []
for layeri in range(n_layers):
if layeri == 0:
dimin = input_dim
dimout = hidden_dim
elif layeri in input_skips:
dimin = hidden_dim + skip_dim
dimout = hidden_dim
else:
dimin = hidden_dim
dimout = hidden_dim
linear = torch.nn.Linear(dimin, dimout)
layers.append(torch.nn.Sequential(linear, torch.nn.ReLU(True)))
self.mlp = torch.nn.ModuleList(layers)
self._input_skips = set(input_skips)
def forward(self, x: torch.Tensor, z: torch.Tensor) -> torch.Tensor:
y = x
for li, layer in enumerate(self.mlp):
if li in self._input_skips:
y = torch.cat((y, z), dim=-1)
y = layer(y)
return y
# TODO (Q3.1): Implement NeRF MLP
class NeuralRadianceField(torch.nn.Module):
def __init__(
self,
cfg,
):
super().__init__()
self.harmonic_embedding_xyz = HarmonicEmbedding(3, cfg.n_harmonic_functions_xyz)
self.harmonic_embedding_dir = HarmonicEmbedding(3, cfg.n_harmonic_functions_dir)
embedding_dim_xyz = self.harmonic_embedding_xyz.output_dim
embedding_dim_dir = self.harmonic_embedding_dir.output_dim
pass
class NeuralSurface(torch.nn.Module):
def __init__(
self,
cfg,
):
super().__init__()
# TODO (Q6): Implement Neural Surface MLP to output per-point SDF
# TODO (Q7): Implement Neural Surface MLP to output per-point color
def get_distance(
self,
points
):
'''
TODO: Q6
Output:
distance: N X 1 Tensor, where N is number of input points
'''
points = points.view(-1, 3)
pass
def get_color(
self,
points
):
'''
TODO: Q7
Output:
distance: N X 3 Tensor, where N is number of input points
'''
points = points.view(-1, 3)
pass
def get_distance_color(
self,
points
):
'''
TODO: Q7
Output:
distance, points: N X 1, N X 3 Tensors, where N is number of input points
You may just implement this by independent calls to get_distance, get_color
but, depending on your MLP implementation, it maybe more efficient to share some computation
'''
def forward(self, points):
return self.get_distance(points)
def get_distance_and_gradient(
self,
points
):
has_grad = torch.is_grad_enabled()
points = points.view(-1, 3)
# Calculate gradient with respect to points
with torch.enable_grad():
points = points.requires_grad_(True)
distance = self.get_distance(points)
gradient = autograd.grad(
distance,
points,
torch.ones_like(distance, device=points.device),
create_graph=has_grad,
retain_graph=has_grad,
only_inputs=True
)[0]
return distance, gradient
implicit_dict = {
'sdf_volume': SDFVolume,
'nerf': NeuralRadianceField,
'sdf_surface': SDFSurface,
'neural_surface': NeuralSurface,
}