-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
220 lines (197 loc) · 8.04 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
import torch
from torch import nn
from torch.distributions.gamma import Gamma
import numpy as np
from einops import rearrange
@torch.jit.script
def gaussian_activation(x, a):
return torch.exp(-x**2/(2*a**2))
@torch.jit.script
def scaledsin_activation(x, a):
return torch.sin(x*a)
# encoding matrices, used in PE
E_2d = torch.eye(2)
# defined in mipnerf360 https://arxiv.org/pdf/2111.12077.pdf page11
E_3d = torch.FloatTensor([
0.8506508, 0, 0.5257311,
0.809017, 0.5, 0.309017,
0.5257311, 0.8506508, 0,
1, 0, 0,
0.809017, 0.5, -0.309017,
0.8506508, 0, -0.5257311,
0.309017, 0.809017, -0.5,
0, 0.5257311, -0.8506508,
0.5, 0.309017, -0.809017,
0, 1, 0,
-0.5257311, 0.8506508, 0,
-0.309017, 0.809017, -0.5,
0, 0.5257311, 0.8506508,
-0.309017, 0.809017, 0.5,
0.309017, 0.809017, 0.5,
0.5, 0.309017, 0.809017,
0.5, -0.309017, 0.809017,
0, 0, 1,
-0.5, 0.309017, 0.809017,
-0.809017, 0.5, 0.309017,
-0.809017, 0.5, -0.309017]).view(21, 3).T
class PE(nn.Module):
"""
positional encoding
"""
def __init__(self, P):
"""
P: (d, F) encoding matrix
"""
super().__init__()
self.register_buffer("P", P)
@property
def out_dim(self):
return self.P.shape[1]*2
def forward(self, x):
"""
x: (n_blocks, B, d)
"""
x_ = x @ self.P # (n_blocks, B, F)
return torch.cat([torch.sin(x_), torch.cos(x_)], -1) # (n_blocks, B, 2*F)
class BlockMLP(nn.Module):
"""
A BlockMLP consists of all the MLPs of a certain scale.
All MLPs are inferred at the same time without using for loop.
"""
def __init__(self, n_blocks, n_in, n_out,
n_layers, n_hidden, final_act,
a=0.1):
super().__init__()
self.n_layers = n_layers
self.final_act = final_act
for i in range(n_layers):
if i == 0: # first layer
wi = nn.Parameter(torch.empty(n_blocks, n_in, n_hidden))
bi = nn.Parameter(torch.empty(n_blocks, 1, n_hidden))
ai = nn.Parameter(a*torch.ones(n_blocks, 1, 1))
elif i < n_layers-1: # middle layers
wi = nn.Parameter(torch.empty(n_blocks, n_hidden, n_hidden))
bi = nn.Parameter(torch.empty(n_blocks, 1, n_hidden))
ai = nn.Parameter(a*torch.ones(n_blocks, 1, 1))
else: # last layer
wi = nn.Parameter(torch.empty(n_blocks, n_hidden, n_out))
bi = nn.Parameter(torch.empty(n_blocks, 1, n_out))
if final_act == 'sigmoid':
ai = nn.Sigmoid()
elif final_act == 'sin':
ai = nn.Parameter(a*torch.ones(n_blocks, 1, 1))
# layer initialization
if i == 0:
nn.init.uniform_(wi, -1/(n_in**0.5), 1/(n_in**0.5))
nn.init.uniform_(bi, -1/(n_in**0.5), 1/(n_in**0.5))
else:
nn.init.uniform_(wi, -1/(n_hidden**0.5), 1/(n_hidden**0.5))
nn.init.uniform_(bi, -1/(n_hidden**0.5), 1/(n_hidden**0.5))
setattr(self, f'w{i}', wi)
setattr(self, f'b{i}', bi)
setattr(self, f'a{i}', ai)
def forward(self, x, b_chunks=16384, to_cpu=False, **kwargs):
"""
Inputs:
x: (n_blocks, B, n_in)
b_chunks: int, @x is split into chunks of at most @b_chunks blocks
Outputs:
(n_blocks, B, n_out)
"""
out = []
for c in range(0, len(x), b_chunks):
x_ = x[c:c+b_chunks]
if 'pe' in kwargs: x_ = kwargs['pe'](x_)
for i in range(self.n_layers):
wi = getattr(self, f'w{i}')[c:c+b_chunks]
bi = getattr(self, f'b{i}')[c:c+b_chunks]
ai = getattr(self, f'a{i}')
if i<self.n_layers-1:
x_ = gaussian_activation(x_@wi+bi, ai[c:c+b_chunks])
else: # last layer
if self.final_act == 'sigmoid':
x_ = ai(x_@wi+bi)
elif self.final_act == 'sin':
x_ = scaledsin_activation(x_@wi+bi, ai[c:c+b_chunks])
if to_cpu: x_ = x_.cpu()
out += [x_]
return torch.cat(out)
# from https://github.com/boschresearch/multiplicative-filter-networks/blob/main/mfn/mfn.py
class BlockMLP_Gabor(nn.Module):
"""
A BlockMLP consists of all the MLPs of a certain scale.
All MLPs are inferred at the same time without using for loop.
"""
def __init__(self, n_blocks, n_in, n_out,
n_layers, n_hidden, final_act,
a=0.1, weight_scale=256.0, alpha=6.0, beta=1.0):
super().__init__()
self.n_layers = n_layers
self.final_act = final_act
weight_scale /= (n_layers-1)**0.5
for i in range(n_layers):
if i < n_layers-1:
fwi = nn.Parameter(torch.empty(n_blocks, n_in, n_hidden))
fbi = nn.Parameter(torch.empty(n_blocks, 1, n_hidden))
fmui = nn.Parameter(2*(torch.rand(n_blocks, 1, n_hidden, n_in)-0.5))
fgammai = \
nn.Parameter(
Gamma(alpha/(n_layers-1), beta).sample((n_blocks, n_hidden)))
nn.init.uniform_(fwi, -1/(n_in**0.5), 1/(n_in**0.5))
nn.init.uniform_(fbi, -np.pi, np.pi)
fwi.data *= weight_scale*rearrange(fgammai, 'n d -> n 1 d')**0.5
setattr(self, f'fw{i}', fwi)
setattr(self, f'fb{i}', fbi)
setattr(self, f'fmu{i}', fmui)
setattr(self, f'fgamma{i}', fgammai)
if i > 0:
o = n_hidden if i<n_layers-1 else n_out
wi = nn.Parameter(torch.empty(n_blocks, n_hidden, o))
bi = nn.Parameter(torch.empty(n_blocks, 1, o))
nn.init.uniform_(wi, -1/(n_hidden**0.5), 1/(n_hidden**0.5))
nn.init.uniform_(bi, -1/(n_hidden**0.5), 1/(n_hidden**0.5))
setattr(self, f'w{i}', wi)
setattr(self, f'b{i}', bi)
if i==n_layers-1:
if final_act == 'sigmoid':
ai = nn.Sigmoid()
elif final_act == 'sin':
ai = nn.Parameter(a*torch.ones(n_blocks, 1, 1))
setattr(self, f'a{i}', ai)
def forward(self, x, b_chunks=16384, to_cpu=False, **kwargs):
"""
Inputs:
x: (n_blocks, B, n_in)
b_chunks: int, @x is split into chunks of at most @b_chunks blocks
Outputs:
(n_blocks, B, n_out)
"""
out = []
for c in range(0, len(x), b_chunks):
x_ = xi = x[c:c+b_chunks]
for i in range(self.n_layers):
if i<self.n_layers-1:
fwi = getattr(self, f'fw{i}')[c:c+b_chunks]
fbi = getattr(self, f'fb{i}')[c:c+b_chunks]
fmui = getattr(self, f'fmu{i}')[c:c+b_chunks]
fgammai = getattr(self, f'fgamma{i}')[c:c+b_chunks]
D = torch.norm(rearrange(xi, 'n b d -> n b 1 d')-fmui, dim=-1)**2
if i>0:
wi = getattr(self, f'w{i}')[c:c+b_chunks]
bi = getattr(self, f'b{i}')[c:c+b_chunks]
if i==0:
x_ = torch.sin(xi@fwi+fbi) * \
torch.exp(-D/2*rearrange(fgammai, 'n h -> n 1 h'))
elif i<self.n_layers-1:
x_ = (x_@wi+bi) * \
torch.sin(xi@fwi+fbi) * \
torch.exp(-D/2*rearrange(fgammai, 'n h -> n 1 h'))
else: # last layer
ai = getattr(self, f'a{i}')
if self.final_act == 'sigmoid':
x_ = ai(x_@wi+bi)
elif self.final_act == 'sin':
x_ = scaledsin_activation(x_@wi+bi, ai[c:c+b_chunks])
if to_cpu: x_ = x_.cpu()
out += [x_]
return torch.cat(out)