-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlayers.py
313 lines (232 loc) · 9.24 KB
/
layers.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
from collections import OrderedDict
import torch
import torch.nn as nn
from torchvision import models
def conv2d(nin, nout, activ, ks=3, s=2, p=1, bn=False, pn=False):
conv = nn.Conv2d(nin, nout, kernel_size=ks, stride=s, padding=p)
layers = OrderedDict()
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm2d(nout)
if activ:
layers['activ'] = activ
if pn:
layers['pn'] = PixelNormLayer()
return nn.Sequential(layers)
def conv3d(nin, nout, activ, ks=3, s=2, p=1, bn=False, pn=False):
conv = nn.Conv3d(nin, nout, kernel_size=ks, stride=s, padding=p)
layers = OrderedDict()
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm3d(nout)
if activ:
layers['activ'] = activ
if pn:
layers['pn'] = PixelNormLayer()
return nn.Sequential(layers)
def up_conv3d(nin, nout, activ, ks=3, s=1, p=1, up_factor=2, bn=False, pn=False):
layers = OrderedDict()
up = nn.Upsample(scale_factor=up_factor, mode='nearest')
layers['up'] = up
conv = nn.Conv3d(nin, nout, kernel_size=ks, stride=s, padding=p)
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm3d(nout)
if activ:
layers['activ'] = activ
if pn:
layers['pn'] = PixelNormLayer()
return nn.Sequential(layers)
def pad_conv3d(nin, nout, activ, ks=3, s=1, p=1, bn=False, pn=False):
layers = OrderedDict()
pd = nn.ReplicationPad3d(1)
layers['pd'] = pd
conv = nn.Conv3d(nin, nout, kernel_size=ks, stride=s, padding=p)
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm3d(nout)
if activ:
layers['activ'] = activ
if pn:
layers['pn'] = PixelNormLayer()
return nn.Sequential(layers)
def up_pad_conv3d(nin, nout, activ, ks=3, s=1, p=1, up_factor=2, bn=False, pn=False):
layers = OrderedDict()
up = nn.Upsample(scale_factor=up_factor, mode='nearest')
layers['up'] = up
pd = nn.ReplicationPad3d(1)
layers['pd'] = pd
conv = nn.Conv3d(nin, nout, kernel_size=ks, stride=s, padding=p)
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm3d(nout)
if activ:
layers['activ'] = activ
if pn:
layers['pn'] = PixelNormLayer()
return nn.Sequential(layers)
def reparameterize(mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def conv(nin, nout, kernel_size=3, stride=1, padding=1, layer=nn.Conv3d,
ws=False, bn=False, pn=False, activ=None, gainWS=2):
conv = layer(nin, nout, kernel_size, stride=stride, padding=padding, bias=False if bn else True)
layers = OrderedDict()
if ws:
layers['ws'] = WScaleLayer(conv, gain=gainWS)
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm3d(nout)
if activ:
if activ == nn.PReLU:
# to avoid sharing the same parameter, activ must be set to nn.PReLU (without '()') and initialized here
layers['activ'] = activ(num_parameters=1)
else:
layers['activ'] = activ
if pn:
layers['pn'] = PixelNormLayer()
return nn.Sequential(layers)
def linear(nin, nout, bn=False, activ=None):
fc = nn.Linear(nin, nout)
layers = OrderedDict()
layers['fc'] = fc
if bn:
layers['bn'] = nn.BatchNorm1d(nout)
if activ:
layers['activ'] = activ
return nn.Sequential(layers)
def conv_transpose(nin, nout, kernel_size=4, stride=2, padding=1, layer=nn.ConvTranspose3d,
ws=False, bn=False, pn=False, activ=None, gainWS=2):
conv = layer(nin, nout, kernel_size, stride=stride, padding=padding, bias=False if bn else True)
layers = OrderedDict()
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm3d(nout)
if activ:
if activ == nn.PReLU:
# to avoid sharing the same parameter, activ must be set to nn.PReLU (without '()') and initialized here
layers['activ'] = activ(num_parameters=1)
else:
layers['activ'] = activ
return nn.Sequential(layers)
def conv2d_transpose(nin, nout, activ=None, ks=4, s=2, p=1, bn=False, dropout=None):
layers = OrderedDict()
conv = nn.ConvTranspose2d(nin, nout, ks, stride=s, padding=p)
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm2d(nout)
if dropout is not None and dropout > 0:
layers['dropout'] = nn.Dropout(dropout)
if activ:
layers['activ'] = activ
return nn.Sequential(layers)
def conv2d_up(nin, nout, activ=None, ks=3, s=1, p=1, bn=False, scale=2, dropout=None):
layers = OrderedDict()
up = nn.Upsample(scale_factor=scale, mode='nearest')
layers['up'] = up
conv = nn.Conv2d(nin, nout, ks, stride=s, padding=p)
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm2d(nout)
if dropout is not None and dropout > 0:
layers['dropout'] = nn.Dropout(dropout)
if activ:
layers['activ'] = activ
return nn.Sequential(layers)
def conv3d_transpose(nin, nout, activ=None, ks=4, s=2, p=1, bn=False, dropout=None):
layers = OrderedDict()
conv = nn.ConvTranspose3d(nin, nout, ks, stride=s, padding=p)
layers['conv'] = conv
if bn:
layers['bn'] = nn.BatchNorm3d(nout)
if dropout is not None and dropout > 0:
layers['dropout'] = nn.Dropout(dropout)
if activ:
layers['activ'] = activ
return nn.Sequential(layers)
class PixelNormLayer(nn.Module):
def __init__(self):
super(PixelNormLayer, self).__init__()
def forward(self, x):
# seems correct
pn = x * torch.rsqrt(torch.mean(x ** 2, dim=1, keepdim=True) + 1e-8)
# print (pn.mean())
# pn = x / torch.sqrt(torch.mean(x ** 2, dim=1, keepdim=True) + 1e-8)
# print (pn.mean())
return pn
def __repr__(self):
return self.__class__.__name__
class WScaleLayer(nn.Module):
def __init__(self, incoming, gain=2):
super(WScaleLayer, self).__init__()
self.gain = gain
self.scale = (self.gain / incoming.weight[0].numel()) ** 0.5 # seems work
# print (self.scale)
# self.scale = 1
# self.scale = (torch.mean(incoming.weight.data ** 2)) ** 0.5
# # self.incoming.weight.data.copy_(self.incoming.weight.data / self.scale)
# print (self.scale)
def forward(self, input):
return input * self.scale
def __repr__(self):
return '{}(gain={})'.format(self.__class__.__name__, self.gain)
def load_pretrained_models(device, enc_type='densenet', is_grad=False, verbose=False):
if enc_type == 'resnet':
pretrained_model = models.resnet18(pretrained=True) # have some results
# pretrained_model = models.resnet34(pretrained=True) # new
# print (pretrained_model)
fe = nn.Sequential(
pretrained_model.conv1,
pretrained_model.bn1,
pretrained_model.relu,
pretrained_model.maxpool,
*list(pretrained_model.layer1.children())[:],
*list(pretrained_model.layer2.children())[:],
*list(pretrained_model.layer3.children())[:],
)
# print (pretrained_model)
elif enc_type == 'densenet':
# pretrained_model = models.densenet121(pretrained='imagenet')
# pretrained_model = models.densenet121(pretrained=True)
# print (pretrained_model)
# fe = nn.Sequential(
# pretrained_model.features.conv0,
# pretrained_model.features.norm0,
# pretrained_model.features.relu0,
# pretrained_model.features.pool0,
# *list(pretrained_model.features.denseblock1.children())[:],
# *list(pretrained_model.features.transition1.children())[:],
# *list(pretrained_model.features.denseblock2.children())[:],
# *list(pretrained_model.features.transition2.children())[:],
# )
pretrained_model = models.densenet161(pretrained=True)
# print (pretrained_model)
fe = nn.Sequential(
pretrained_model.features.conv0,
pretrained_model.features.norm0,
pretrained_model.features.relu0,
pretrained_model.features.pool0,
*list(pretrained_model.features.denseblock1.children())[:],
*list(pretrained_model.features.transition1.children())[:],
*list(pretrained_model.features.denseblock2.children())[:],
*list(pretrained_model.features.transition2.children())[:],
*list(pretrained_model.features.denseblock3.children())[:],
*list(pretrained_model.features.transition3.children())[:],
*list(pretrained_model.features.denseblock4.children())[:],
*list(pretrained_model.features.transition4.children())[:],
)
elif enc_type == 'vgg':
layer = 15 # 19
pretrained_model = models.vgg16(pretrained=True)
# print (pretrained_model)
fe = nn.Sequential(*list(pretrained_model.features.children())[:layer+1])
else:
raise ValueError('encoder type should be among fe, vgg or resnet')
if verbose:
print (pretrained_model)
for param in fe.parameters():
param.requires_grad = is_grad
fe = fe.to(device)
fe.eval()
return fe