-
Notifications
You must be signed in to change notification settings - Fork 11
/
meta_models.py
462 lines (384 loc) · 16.2 KB
/
meta_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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torchvision
from torch.autograd import Variable
import itertools
def to_var(x, requires_grad=True):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x, requires_grad=requires_grad)
def weights_init(m):
if isinstance(m, (nn.Conv2d, nn.Linear)):
nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
class MetaModule(nn.Module):
# adopted from: Adrien Ecoffet https://github.com/AdrienLE
def parameters(self):
for name, param in self.named_params(self):
yield param
def named_leaves(self):
return []
def named_submodules(self):
return []
def named_params(self, curr_module=None, memo=None, prefix=''):
if memo is None:
memo = set()
if hasattr(curr_module, 'named_leaves'):
for name, p in curr_module.named_leaves():
if p is not None and p not in memo:
memo.add(p)
yield prefix + ('.' if prefix else '') + name, p
else:
for name, p in curr_module._parameters.items():
if p is not None and p not in memo:
memo.add(p)
yield prefix + ('.' if prefix else '') + name, p
for mname, module in curr_module.named_children():
submodule_prefix = prefix + ('.' if prefix else '') + mname
for name, p in self.named_params(module, memo, submodule_prefix):
yield name, p
def update_params(self, lr_inner, first_order=False, source_params=None, detach=False):
if source_params is not None:
for tgt, src in zip(self.named_params(self), source_params):
name_t, param_t = tgt
# name_s, param_s = src
# grad = param_s.grad
# name_s, param_s = src
grad = src
if first_order:
grad = to_var(grad.detach().data)
if grad is None:
print(name_t)
tmp = param_t
else:
tmp = param_t - lr_inner * grad
self.set_param(self, name_t, tmp)
else:
for name, param in self.named_params(self):
if not detach:
grad = param.grad
if first_order:
grad = to_var(grad.detach().data)
if grad is None:
print(name)
tmp = param_t
else:
tmp = param_t - lr_inner * grad
self.set_param(self, name, tmp)
else:
param = param.detach_()
self.set_param(self, name, param)
def set_param(self,curr_mod, name, param):
if '.' in name:
n = name.split('.')
module_name = n[0]
rest = '.'.join(n[1:])
for name, mod in curr_mod.named_children():
if module_name == name:
self.set_param(mod, rest, param)
break
else:
setattr(curr_mod, name, param)
def detach_params(self):
for name, param in self.named_params(self):
self.set_param(self, name, param.detach())
def copy(self, other, same_var=False):
for name, param in other.named_params():
if not same_var:
param = to_var(param.data.clone(), requires_grad=True)
self.set_param(name, param)
class MetaLinear(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Linear(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
if ignore.bias is not None: self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
else: self.bias = None
def forward(self, x):
if self.bias is not None:
return F.linear(x, self.weight, self.bias)
else:
return F.linear(x, self.weight)
def named_leaves(self):
if self.bias is not None:
return [('weight', self.weight), ('bias', self.bias)]
else:
return [('weight', self.weight)]
class MetaConv2d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Conv2d(*args, **kwargs)
self.stride = ignore.stride
self.padding = ignore.padding
self.dilation = ignore.dilation
self.groups = ignore.groups
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
if ignore.bias is not None:
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
else:
self.register_buffer('bias', None)
def forward(self, x):
return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaConv3d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Conv3d(*args, **kwargs)
self.stride = ignore.stride
self.padding = ignore.padding
self.dilation = ignore.dilation
self.groups = ignore.groups
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
if ignore.bias is not None:
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
else:
self.register_buffer('bias', None)
def forward(self, x):
return F.conv3d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaConvTranspose2d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.ConvTranspose2d(*args, **kwargs)
self.stride = ignore.stride
self.padding = ignore.padding
self.dilation = ignore.dilation
self.groups = ignore.groups
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
if ignore.bias is not None:
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
else:
self.register_buffer('bias', None)
def forward(self, x, output_size=None):
output_padding = self._output_padding(x, output_size)
return F.conv_transpose2d(x, self.weight, self.bias, self.stride, self.padding,
output_padding, self.groups, self.dilation)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaBatchNorm3d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.BatchNorm3d(*args, **kwargs)
self.num_features = ignore.num_features
self.eps = ignore.eps
self.momentum = ignore.momentum
self.affine = ignore.affine
self.track_running_stats = ignore.track_running_stats
if self.affine:
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
if self.track_running_stats:
self.register_buffer('running_mean', torch.zeros(self.num_features))
self.register_buffer('running_var', torch.ones(self.num_features))
else:
self.register_parameter('running_mean', None)
self.register_parameter('running_var', None)
def forward(self, x):
return F.batch_norm(x, self.running_mean, self.running_var, self.weight, self.bias,
self.training or not self.track_running_stats, self.momentum, self.eps)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaBatchNorm2d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.BatchNorm2d(*args, **kwargs)
self.num_features = ignore.num_features
self.eps = ignore.eps
self.momentum = ignore.momentum
self.affine = ignore.affine
self.track_running_stats = ignore.track_running_stats
if self.affine:
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
if self.track_running_stats:
self.register_buffer('running_mean', torch.zeros(self.num_features))
self.register_buffer('running_var', torch.ones(self.num_features))
else:
self.register_parameter('running_mean', None)
self.register_parameter('running_var', None)
def forward(self, x):
return F.batch_norm(x, self.running_mean, self.running_var, self.weight, self.bias,
self.training or not self.track_running_stats, self.momentum, self.eps)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaBatchNorm1d(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.BatchNorm1d(*args, **kwargs)
self.num_features = ignore.num_features
self.eps = ignore.eps
self.momentum = ignore.momentum
self.affine = ignore.affine
self.track_running_stats = ignore.track_running_stats
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
if self.track_running_stats:
self.register_buffer('running_mean', torch.zeros(self.num_features))
self.register_buffer('running_var', torch.ones(self.num_features))
else:
self.register_parameter('running_mean', None)
self.register_parameter('running_var', None)
def forward(self, x):
return F.batch_norm(x, self.running_mean, self.running_var, self.weight, self.bias,
self.training or not self.track_running_stats, self.momentum, self.eps)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class LeNet(MetaModule):
def __init__(self, n_out):
super(LeNet, self).__init__()
layers = []
layers.append(MetaConv2d(1, 6, kernel_size=5))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.MaxPool2d(kernel_size=2,stride=2))
layers.append(MetaConv2d(6, 16, kernel_size=5))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.MaxPool2d(kernel_size=2,stride=2))
layers.append(MetaConv2d(16, 120, kernel_size=5))
layers.append(nn.ReLU(inplace=True))
self.main = nn.Sequential(*layers)
layers = []
layers.append(MetaLinear(120, 84))
layers.append(nn.ReLU(inplace=True))
layers.append(MetaLinear(84, n_out))
self.fc_layers = nn.Sequential(*layers)
def forward(self, x):
x = self.main(x)
x = x.view(-1, 120)
return self.fc_layers(x).squeeze()
class MetaMLP(MetaModule):
def __init__(self, dim):
super(MetaMLP, self).__init__()
layers = []
self.l1 = MetaLinear(dim, 300, bias=False)
self.bn1 = MetaBatchNorm1d(num_features = 300)
self.l2 = MetaLinear(300, 300, bias=False)
self.bn2 = MetaBatchNorm1d(num_features = 300)
self.l3 = MetaLinear(300, 300, bias=False)
self.bn3 = MetaBatchNorm1d(num_features = 300)
self.l4 = MetaLinear(300, 300, bias=False)
self.bn4 = MetaBatchNorm1d(num_features = 300)
self.l5 = MetaLinear(300, 1)
self.apply(weights_init)
def forward(self, x):
x = self.l1(x)
x = self.bn1(x)
x = F.relu(x)
x = self.l2(x)
x = self.bn2(x)
x = F.relu(x)
x = self.l3(x)
x = self.bn3(x)
x = F.relu(x)
x = self.l4(x)
x = self.bn4(x)
x = F.relu(x)
x = x.view(-1, 300)
x = self.l5(x)
return x
class MetaCNN(MetaModule):
def __init__(self):
super(MetaCNN, self).__init__()
self.conv1 = MetaConv2d(3, 96, kernel_size = 3, padding=1)
self.bn1 = MetaBatchNorm2d(96)
self.conv2 = MetaConv2d(96, 96, kernel_size = 3, padding=1)
self.bn2 = MetaBatchNorm2d(96)
self.conv3 = MetaConv2d(96, 96, kernel_size = 3, stride = 2, padding=1)
self.bn3 = MetaBatchNorm2d(96)
self.conv4 = MetaConv2d(96, 192, kernel_size = 3, padding=1)
self.bn4 = MetaBatchNorm2d(192)
self.conv5 = MetaConv2d(192, 192, kernel_size = 3, padding=1)
self.bn5 = MetaBatchNorm2d(192)
self.conv6 = MetaConv2d(192, 192, kernel_size = 3, stride = 2, padding=1)
self.bn6 = MetaBatchNorm2d(192)
self.conv7 = MetaConv2d(192, 192, kernel_size = 3, padding=1)
self.bn7 = MetaBatchNorm2d(192)
self.conv8 = MetaConv2d(192, 192, kernel_size = 1)
self.bn8 = MetaBatchNorm2d(192)
self.conv9 = MetaConv2d(192, 10, kernel_size = 1)
self.bn9 = MetaBatchNorm2d(10)
self.l1 = MetaLinear(640, 1000)
self.l2 = MetaLinear(1000, 1000)
self.l3 = MetaLinear(1000, 1)
self.apply(weights_init)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = F.relu(x)
x = self.conv2(x)
x = self.bn2(x)
x = F.relu(x)
x = self.conv3(x)
x = self.bn3(x)
x = F.relu(x)
x = self.conv4(x)
x = self.bn4(x)
x = F.relu(x)
x = self.conv5(x)
x = self.bn5(x)
x = F.relu(x)
x = self.conv6(x)
x = self.bn6(x)
x = F.relu(x)
x = self.conv7(x)
x = self.bn7(x)
x = F.relu(x)
x = self.conv8(x)
x = self.bn8(x)
x = F.relu(x)
x = self.conv9(x)
x = self.bn9(x)
x = F.relu(x)
x = x.view(-1, 640)
x = self.l1(x)
x = F.relu(x)
x = self.l2(x)
x = F.relu(x)
x = self.l3(x)
return x
class MetaBasic2Conv(MetaModule):
def __init__(self):
super(MetaBasic2Conv, self).__init__()
self.conv1 = MetaConv3d(1, 16, kernel_size=3, stride=1, padding=1) # b, 16, 61, 73, 61
self.bn1 = MetaBatchNorm3d(16)
self.conv2 = MetaConv3d(16, 32, kernel_size=3, stride=1, padding=1) # b, 32, 31, 37, 31
self.bn2 = MetaBatchNorm3d(32)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = F.relu(x, inplace = True)
x = F.max_pool3d(x, kernel_size = 3, stride = 2, padding = 1)
x = self.conv2(x)
x = self.bn2(x)
x = F.relu(x, inplace = True)
x = F.max_pool3d(x, kernel_size = 3, stride = 2, padding = 1)
return x
class MetaLenet3D(MetaModule):
def __init__(self):
super(MetaLenet3D, self).__init__()
self.conv_mri = MetaBasic2Conv().cuda()
self.conv_left = MetaBasic2Conv().cuda()
self.conv_right = MetaBasic2Conv().cuda()
self.fc1 = MetaLinear(32 * (13*13*13 + 2*8*8*8), 256)
self.bn1 = MetaBatchNorm1d(256)
self.fc2 = MetaLinear(256, 1)
def forward(self, mri, left, right):
mri = self.conv_mri(mri)
left = self.conv_left(left)
right = self.conv_right(right)
# print(mri.size(), left.size(), right.size())
mri = mri.view(-1, 32 * 13 * 13 * 13)
left = left.view(-1, 32 * 8 * 8 * 8)
right = right.view(-1, 32 * 8 * 8 * 8)
x = torch.cat((mri, left, right), dim=1)
x = self.fc1(x)
x = self.bn1(x)
x = F.relu(x, inplace = True)
x = self.fc2(x)
#x = F.softmax(x, 1)
#x = torch.log(x[:, 0] / (1- x[: ,0])).view(-1, 1)
return x