forked from krrish94/nerf-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
256 lines (224 loc) · 9.55 KB
/
models.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
import torch
class VeryTinyNeRFModel(torch.nn.Module):
r"""Define a "very tiny" NeRF model comprising three fully connected layers.
"""
def __init__(self, filter_size=128, num_encoding_functions=6, use_viewdirs=True):
super(VeryTinyNeRFModel, self).__init__()
self.num_encoding_functions = num_encoding_functions
self.xyz_encoding_dims = 3 + 3 * 2 * num_encoding_functions
if use_viewdirs is True:
self.viewdir_encoding_dims = 3 + 3 * 2 * num_encoding_functions
else:
self.viewdir_encoding_dims = 0
# Input layer (default: 65 -> 128)
self.layer1 = torch.nn.Linear(
self.xyz_encoding_dims + self.viewdir_encoding_dims, filter_size
)
# Layer 2 (default: 128 -> 128)
self.layer2 = torch.nn.Linear(filter_size, filter_size)
# Layer 3 (default: 128 -> 4)
self.layer3 = torch.nn.Linear(filter_size, 4)
# Short hand for torch.nn.functional.relu
self.relu = torch.nn.functional.relu
def forward(self, x):
x = self.relu(self.layer1(x))
x = self.relu(self.layer2(x))
x = self.layer3(x)
return x
class MultiHeadNeRFModel(torch.nn.Module):
r"""Define a "multi-head" NeRF model (radiance and RGB colors are predicted by
separate heads).
"""
def __init__(self, hidden_size=128, num_encoding_functions=6, use_viewdirs=True):
super(MultiHeadNeRFModel, self).__init__()
self.num_encoding_functions = num_encoding_functions
self.xyz_encoding_dims = 3 + 3 * 2 * num_encoding_functions
if use_viewdirs is True:
self.viewdir_encoding_dims = 3 + 3 * 2 * num_encoding_functions
else:
self.viewdir_encoding_dims = 0
# Input layer (default: 39 -> 128)
self.layer1 = torch.nn.Linear(self.xyz_encoding_dims, hidden_size)
# Layer 2 (default: 128 -> 128)
self.layer2 = torch.nn.Linear(hidden_size, hidden_size)
# Layer 3_1 (default: 128 -> 1): Predicts radiance ("sigma")
self.layer3_1 = torch.nn.Linear(hidden_size, 1)
# Layer 3_2 (default: 128 -> 1): Predicts a feature vector (used for color)
self.layer3_2 = torch.nn.Linear(hidden_size, hidden_size)
# Layer 4 (default: 39 + 128 -> 128)
self.layer4 = torch.nn.Linear(
self.viewdir_encoding_dims + hidden_size, hidden_size
)
# Layer 5 (default: 128 -> 128)
self.layer5 = torch.nn.Linear(hidden_size, hidden_size)
# Layer 6 (default: 128 -> 3): Predicts RGB color
self.layer6 = torch.nn.Linear(hidden_size, 3)
# Short hand for torch.nn.functional.relu
self.relu = torch.nn.functional.relu
def forward(self, x):
x, view = x[..., : self.xyz_encoding_dims], x[..., self.xyz_encoding_dims :]
x = self.relu(self.layer1(x))
x = self.relu(self.layer2(x))
sigma = self.layer3_1(x)
feat = self.relu(self.layer3_2(x))
x = torch.cat((feat, view), dim=-1)
x = self.relu(self.layer4(x))
x = self.relu(self.layer5(x))
x = self.layer6(x)
return torch.cat((x, sigma), dim=-1)
class ReplicateNeRFModel(torch.nn.Module):
r"""NeRF model that follows the figure (from the supp. material of NeRF) to
every last detail. (ofc, with some flexibility)
"""
def __init__(
self,
hidden_size=256,
num_layers=4,
num_encoding_fn_xyz=6,
num_encoding_fn_dir=4,
include_input_xyz=True,
include_input_dir=True,
):
super(ReplicateNeRFModel, self).__init__()
# xyz_encoding_dims = 3 + 3 * 2 * num_encoding_functions
self.dim_xyz = (3 if include_input_xyz else 0) + 2 * 3 * num_encoding_fn_xyz
self.dim_dir = (3 if include_input_dir else 0) + 2 * 3 * num_encoding_fn_dir
self.layer1 = torch.nn.Linear(self.dim_xyz, hidden_size)
self.layer2 = torch.nn.Linear(hidden_size, hidden_size)
self.layer3 = torch.nn.Linear(hidden_size, hidden_size)
self.fc_alpha = torch.nn.Linear(hidden_size, 1)
self.layer4 = torch.nn.Linear(hidden_size + self.dim_dir, hidden_size // 2)
self.layer5 = torch.nn.Linear(hidden_size // 2, hidden_size // 2)
self.fc_rgb = torch.nn.Linear(hidden_size // 2, 3)
self.relu = torch.nn.functional.relu
def forward(self, x):
xyz, direction = x[..., : self.dim_xyz], x[..., self.dim_xyz :]
x_ = self.relu(self.layer1(xyz))
x_ = self.relu(self.layer2(x_))
feat = self.layer3(x_)
alpha = self.fc_alpha(x_)
y_ = self.relu(self.layer4(torch.cat((feat, direction), dim=-1)))
y_ = self.relu(self.layer5(y_))
rgb = self.fc_rgb(y_)
return torch.cat((rgb, alpha), dim=-1)
class PaperNeRFModel(torch.nn.Module):
r"""Implements the NeRF model as described in Fig. 7 (appendix) of the
arXiv submission (v0). """
def __init__(
self,
num_layers=8,
hidden_size=256,
skip_connect_every=4,
num_encoding_fn_xyz=6,
num_encoding_fn_dir=4,
include_input_xyz=True,
include_input_dir=True,
use_viewdirs=True,
):
super(PaperNeRFModel, self).__init__()
include_input_xyz = 3 if include_input_xyz else 0
include_input_dir = 3 if include_input_dir else 0
self.dim_xyz = include_input_xyz + 2 * 3 * num_encoding_fn_xyz
self.dim_dir = include_input_dir + 2 * 3 * num_encoding_fn_dir
self.layers_xyz = torch.nn.ModuleList()
self.use_viewdirs = use_viewdirs
self.layers_xyz.append(torch.nn.Linear(self.dim_xyz, 256))
for i in range(1, 8):
if i == 4:
self.layers_xyz.append(torch.nn.Linear(self.dim_xyz + 256, 256))
else:
self.layers_xyz.append(torch.nn.Linear(256, 256))
self.fc_feat = torch.nn.Linear(256, 256)
self.fc_alpha = torch.nn.Linear(256, 1)
self.layers_dir = torch.nn.ModuleList()
self.layers_dir.append(torch.nn.Linear(256 + self.dim_dir, 128))
for i in range(3):
self.layers_dir.append(torch.nn.Linear(128, 128))
self.fc_rgb = torch.nn.Linear(128, 3)
self.relu = torch.nn.functional.relu
def forward(self, x):
xyz, dirs = x[..., : self.dim_xyz], x[..., self.dim_xyz :]
for i in range(8):
if i == 4:
x = self.layers_xyz[i](torch.cat((xyz, x), -1))
else:
x = self.layers_xyz[i](x)
x = self.relu(x)
feat = self.fc_feat(x)
alpha = self.fc_alpha(feat)
if self.use_viewdirs:
x = self.layers_dir[0](torch.cat((feat, dirs), -1))
else:
x = self.layers_dir[0](feat)
x = self.relu(x)
for i in range(1, 3):
x = self.layers_dir[i](x)
x = self.relu(x)
rgb = self.fc_rgb(x)
return torch.cat((rgb, alpha), dim=-1)
class FlexibleNeRFModel(torch.nn.Module):
def __init__(
self,
num_layers=4,
hidden_size=128,
skip_connect_every=4,
num_encoding_fn_xyz=6,
num_encoding_fn_dir=4,
include_input_xyz=True,
include_input_dir=True,
use_viewdirs=True,
):
super(FlexibleNeRFModel, self).__init__()
include_input_xyz = 3 if include_input_xyz else 0
include_input_dir = 3 if include_input_dir else 0
self.dim_xyz = include_input_xyz + 2 * 3 * num_encoding_fn_xyz
self.dim_dir = include_input_dir + 2 * 3 * num_encoding_fn_dir
self.skip_connect_every = skip_connect_every
if not use_viewdirs:
self.dim_dir = 0
self.layer1 = torch.nn.Linear(self.dim_xyz, hidden_size)
self.layers_xyz = torch.nn.ModuleList()
for i in range(num_layers - 1):
if i % self.skip_connect_every == 0 and i > 0 and i != num_layers - 1:
self.layers_xyz.append(
torch.nn.Linear(self.dim_xyz + hidden_size, hidden_size)
)
else:
self.layers_xyz.append(torch.nn.Linear(hidden_size, hidden_size))
self.use_viewdirs = use_viewdirs
if self.use_viewdirs:
self.layers_dir = torch.nn.ModuleList()
# This deviates from the original paper, and follows the code release instead.
self.layers_dir.append(
torch.nn.Linear(self.dim_dir + hidden_size, hidden_size // 2)
)
self.fc_alpha = torch.nn.Linear(hidden_size, 1)
self.fc_rgb = torch.nn.Linear(hidden_size // 2, 3)
self.fc_feat = torch.nn.Linear(hidden_size, hidden_size)
else:
self.fc_out = torch.nn.Linear(hidden_size, 4)
self.relu = torch.nn.functional.relu
def forward(self, x):
if self.use_viewdirs:
xyz, view = x[..., : self.dim_xyz], x[..., self.dim_xyz :]
else:
xyz = x[..., : self.dim_xyz]
x = self.layer1(xyz)
for i in range(len(self.layers_xyz)):
if (
i % self.skip_connect_every == 0
and i > 0
and i != len(self.linear_layers) - 1
):
x = torch.cat((x, xyz), dim=-1)
x = self.relu(self.layers_xyz[i](x))
if self.use_viewdirs:
feat = self.relu(self.fc_feat(x))
alpha = self.fc_alpha(x)
x = torch.cat((feat, view), dim=-1)
for l in self.layers_dir:
x = self.relu(l(x))
rgb = self.fc_rgb(x)
return torch.cat((rgb, alpha), dim=-1)
else:
return self.fc_out(x)