forked from learning3d/assignment3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplicit.py
More file actions
241 lines (190 loc) · 6.7 KB
/
implicit.py
File metadata and controls
241 lines (190 loc) · 6.7 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
import torch
import torch.nn.functional as F
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, ray_bundle):
sample_points = ray_bundle.sample_points.view(-1, 3)
return torch.linalg.norm(
sample_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, ray_bundle):
sample_points = ray_bundle.sample_points.view(-1, 3)
diff = torch.abs(sample_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)
sdf_dict = {
'sphere': SphereSDF,
'box': BoxSDF,
}
# 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)
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
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 (3.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
volume_dict = {
'sdf_volume': SDFVolume,
'nerf': NeuralRadianceField,
}