-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
68 lines (49 loc) · 2.05 KB
/
model.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
import torch
from torch import nn
import torch.nn.functional as F
def conv_block(in_channels, out_channels):
'''Convolution Block of 3x3 kernels + batch norm + maxpool of 2x2'''
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
def functional_conv_block(x, weights, biases, bn_weights, bn_biases):
'''Functional version of the conv_block'''
x = F.conv2d(x, weights, biases, padding=1)
x = F.batch_norm(x, running_mean=None, running_var=None, weight=bn_weights, bias=bn_biases, training=True)
x = F.relu(x)
x = F.max_pool2d(x, kernel_size=2, stride=2)
return x
class MAMLClassifier(nn.Module):
def __init__(self, n_way):
super(MAMLClassifier, self).__init__()
self.conv1 = conv_block(3, 64)
self.conv2 = conv_block(64, 64)
self.conv3 = conv_block(64, 64)
self.conv4 = conv_block(64, 64)
self.head = nn.Linear(64, n_way)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
# Features of shape (batch_size, 64)
feat = x.view(x.size(0), -1)
# Output
out = self.head(feat)
return out
def functional_forward(self, x, params):
'''Functional forward pass given the parameters'''
for block in [1,2,3,4]:
x = functional_conv_block(x,
params[f'conv{block}.0.weight'],
params[f'conv{block}.0.bias'],
params[f'conv{block}.1.weight'],
params[f'conv{block}.1.bias'])
# Features of shape (batch_size, 64)
feat = x.view(x.size(0), -1)
# Output
out = F.linear(feat, params['head.weight'], params['head.bias'])
return out