-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcustom_layers.py
More file actions
166 lines (123 loc) · 5.86 KB
/
Copy pathcustom_layers.py
File metadata and controls
166 lines (123 loc) · 5.86 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
import numpy as np
import geometry
from torchmeta.modules import (MetaModule, MetaSequential)
from collections import OrderedDict
import torch
from torch import nn
def init_weights_normal(m):
if type(m) == BatchLinear or type(m) == nn.Linear:
if hasattr(m, 'weight'):
nn.init.kaiming_normal_(m.weight, a=0.0, nonlinearity='relu', mode='fan_in')
def first_layer_sine_init(m):
with torch.no_grad():
if hasattr(m, 'weight'):
num_input = m.weight.size(-1)
# See paper sec. 3.2, final paragraph, and supplement Sec. 1.5 for discussion of factor 30
m.weight.uniform_(-1 / num_input, 1 / num_input)
class BatchLinear(nn.Linear, MetaModule):
'''A linear meta-layer that can deal with batched weight matrices and biases, as for instance output by a
hypernetwork.'''
__doc__ = nn.Linear.__doc__
def forward(self, input, params=None):
if params is None:
params = OrderedDict(self.named_parameters())
bias = params.get('bias', None)
weight = params['weight']
output = input.matmul(weight.permute(*[i for i in range(len(weight.shape) - 2)], -1, -2))
output += bias.unsqueeze(-2)
return output
class FCLayer(MetaModule):
def __init__(self, in_features, out_features, nonlinearity='relu', norm=None):
super().__init__()
self.net = [BatchLinear(in_features, out_features)]
if norm == 'layernorm':
self.net.append(nn.LayerNorm([out_features], elementwise_affine=True),)
elif norm == 'layernorm_na':
self.net.append(nn.LayerNorm([out_features], elementwise_affine=False),)
if nonlinearity == 'relu':
self.net.append(nn.ReLU(inplace=True))
elif nonlinearity == 'leaky_relu':
self.net.append(nn.LeakyReLU(0.2, inplace=True))
self.net = MetaSequential(*self.net)
self.net.apply(init_weights_normal)
def forward(self, input, params=None):
return self.net(input, params=self.get_subdict(params, 'net'))
class FCBlock(MetaModule):
def __init__(self,
hidden_ch,
num_hidden_layers,
in_features,
out_features,
outermost_linear=False,
norm=None,
activation='relu',
nonlinearity='relu'):
super().__init__()
self.net = []
self.net.append(FCLayer(in_features=in_features, out_features=hidden_ch, nonlinearity=nonlinearity, norm=norm))
for i in range(num_hidden_layers):
self.net.append(FCLayer(in_features=hidden_ch, out_features=hidden_ch, nonlinearity=nonlinearity, norm=norm))
if outermost_linear:
self.net.append(BatchLinear(in_features=hidden_ch, out_features=out_features))
else:
self.net.append(FCLayer(in_features=hidden_ch, out_features=out_features, nonlinearity=nonlinearity, norm=norm))
self.net = MetaSequential(*self.net)
self.net.apply(init_weights_normal)
def forward(self, input, params=None):
return self.net(input, params=self.get_subdict(params, 'net'))
class SineLayer(MetaModule):
def __init__(self, in_features, out_features, bias=True, is_first=False, omega_0=30):
super().__init__()
self.omega_0 = float(omega_0)
self.is_first = is_first
self.in_features = in_features
self.linear = BatchLinear(in_features, out_features, bias=bias)
self.init_weights()
def init_weights(self):
with torch.no_grad():
if self.is_first:
self.linear.weight.uniform_(-1 / self.in_features,
1 / self.in_features)
else:
self.linear.weight.uniform_(-np.sqrt(6 / self.in_features) / self.omega_0,
np.sqrt(6 / self.in_features) / self.omega_0)
def forward_with_film(self, input, gamma, beta):
intermed = self.linear(input)
return torch.sin(gamma * self.omega_0 * intermed + beta)
def forward(self, input, params=None):
intermed = self.linear(input, params=self.get_subdict(params, 'linear'))
return torch.sin(self.omega_0 * intermed)
class Siren(MetaModule):
def __init__(self, in_features, hidden_features, hidden_layers, out_features, outermost_linear=False,
first_omega_0=30, hidden_omega_0=30., special_first=True):
super().__init__()
self.hidden_omega_0 = hidden_omega_0
layer = SineLayer
self.net = []
self.net.append(layer(in_features, hidden_features,
is_first=special_first, omega_0=first_omega_0))
for i in range(hidden_layers):
self.net.append(layer(hidden_features, hidden_features,
is_first=False, omega_0=hidden_omega_0))
if outermost_linear:
final_linear = BatchLinear(hidden_features, out_features)
with torch.no_grad():
final_linear.weight.uniform_(-np.sqrt(6 / hidden_features) / 30.,
np.sqrt(6 / hidden_features) / 30.)
self.net.append(final_linear)
else:
self.net.append(layer(hidden_features, out_features, is_first=False, omega_0=hidden_omega_0))
self.net = nn.ModuleList(self.net)
def forward(self, coords, params=None):
x = coords
for i, layer in enumerate(self.net):
x = layer(x, params=self.get_subdict(params, f'net.{i}'))
return x
def forward_with_film(self, coords, film):
x = coords
for i, (layer, layer_film) in enumerate(zip(self.net, film)):
if i < len(self.net) - 1:
x = layer.forward_with_film(x, layer_film['gamma'], layer_film['beta'])
else:
x = layer.forward(x)
return x