-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
executable file
·210 lines (176 loc) · 6.05 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
"""
Models for supervised meta-learning.
"""
import torch
from torch import nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.autograd import Variable
from args import argument_parser
from adamW import AdamW
args = argument_parser().parse_args()
class MiniimagenetModel(nn.Module):
"""
A model for Mini Imagenet classification.
"""
def __init__(self, num_classes):
super(MiniimagenetModel, self).__init__()
self.conv = nn.Sequential(
# 84 x 84 - 3
nn.Conv2d(3, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.MaxPool2d(2, stride=2, padding=0),
nn.ReLU(True),
# 42 x 42 - 32
nn.Conv2d(32, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.MaxPool2d(2, stride=2, padding=0),
nn.ReLU(True),
# 21 x 21 - 32
nn.Conv2d(32, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.MaxPool2d(2, stride=2, padding=1),
nn.ReLU(True),
# 11 x 11 - 32
nn.Conv2d(32, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.MaxPool2d(2, stride=2, padding=1),
nn.ReLU(True)
)
# 6 x 6 x 32 = 1152
self.classifier = nn.Linear(1152, num_classes)
def forward(self, x):
out = x.view(-1, 3, 84, 84)
out = self.conv(out)
out = out.view(len(out), -1)
out = self.classifier(out)
return F.log_softmax(out, 1)
#class MiniimagenetModel(nn.Module):
# """
# A model for Mini Imagenet classification.
# """
#
# def __init__(self, num_classes):
# super(MiniimagenetModel, self).__init__()
#
# self.conv = nn.Sequential(
# # 96 x 96 - 3
# nn.Conv2d(3, 32, 3, 1, 1),
# nn.BatchNorm2d(32),
# nn.MaxPool2d(2, stride=2, padding=0),
# nn.ReLU(True),
#
# # 48 x 48 - 32
# nn.Conv2d(32, 32, 3, 1, 1),
# nn.BatchNorm2d(32),
# nn.MaxPool2d(2, stride=2, padding=0),
# nn.ReLU(True),
#
# # 24 x 24 - 32
# nn.Conv2d(32, 32, 3, 1, 1),
# nn.BatchNorm2d(32),
# nn.MaxPool2d(2, stride=2, padding=0),
# nn.ReLU(True),
#
# # 12 x 12 - 32
# nn.Conv2d(32, 32, 3, 1, 1),
# nn.BatchNorm2d(32),
# nn.MaxPool2d(2, stride=2, padding=0),
# nn.ReLU(True)
# )
#
# # 6 x 6 x 32 = 1152
# self.classifier = nn.Linear(1152, num_classes)
#
# def forward(self, x):
# out = x.view(-1, 3, 96, 96)
# out = self.conv(out)
# out = out.view(len(out), -1)
# out = self.classifier(out)
# return F.log_softmax(out, 1)
class OmniglotModel(nn.Module):
"""
A model for Omniglot classification.
"""
def __init__(self, num_classes):
super(OmniglotModel, self).__init__()
self.conv = nn.Sequential(
# 28 x 28 - 1
nn.Conv2d(1, 64, 3, 2, 1),
nn.BatchNorm2d(64),
nn.ReLU(True),
# 14 x 14 - 64
nn.Conv2d(64, 64, 3, 2, 1),
nn.BatchNorm2d(64),
nn.ReLU(True),
# 7 x 7 - 64
nn.Conv2d(64, 64, 3, 2, 1),
nn.BatchNorm2d(64),
nn.ReLU(True),
# 4 x 4 - 64
nn.Conv2d(64, 64, 3, 2, 1),
nn.BatchNorm2d(64),
nn.ReLU(True),
# 2 x 2 - 64
)
# 2 x 2 x 64 = 256
self.classifier = nn.Linear(256, num_classes)
def forward(self, x):
out = x.view(-1, 1, 28, 28)
out = self.conv(out)
out = out.view(len(out), -1)
out = self.classifier(out)
return F.log_softmax(out, 1)
def get_optimizer(model, state=None):
if args.sgd:
# optimizer = torch.optim.SGD(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)
optimizer = torch.optim.SGD([
{'params': model.base.parameters()},
{'params': model.classifier.parameters(), 'lr': 1e-3}
], lr=1e-2, momentum=0.9, weight_decay=args.weight_decay)
else:
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, betas=(0, 0.999), weight_decay=args.weight_decay, amsgrad=False)
# optimizer = AdamW(model.parameters(), lr=args.learning_rate, betas=(0, 0.999), weight_decay=args.weight_decay, amsgrad=False)
if state is not None:
optimizer.load_state_dict(state)
return optimizer
def clone_model(num_classes, model_state=None):
model_clone = MiniimagenetModel(num_classes)
if args.cuda:
if args.parallel:
model_clone = nn.DataParallel(model_clone, device_ids=[0, 1]).cuda()
else:
model_clone.cuda()
if model_state is not None:
model_clone.load_state_dict(model_state)
return model_clone
def predict_label(prob):
__, argmax = prob.max(1)
return argmax
def truncated_normal_(tensor, mean=0.0, std=0.1):
size = tensor.shape
tmp = tensor.new_empty(size + (4,)).normal_()
valid = (tmp < 2) & (tmp > -2)
ind = valid.max(-1, keepdim=True)[1]
tensor.data.copy_(tmp.gather(-1, ind).squeeze(-1))
tensor.data.mul_(std).add_(mean)
def get_loss(prediction, labels):
loss = nn.NLLLoss()
return loss(prediction, labels)
#def clone(num_classes, model_state, op_state, cuda):
# model_clone = OmniglotModel(num_classes)
# model_dict = model_clone.state_dict()
#
# #Get trainable new vars.
# trainable=[]
# for name, param in model_clone.named_parameters():
# if param.requires_grad and "classifier" not in name:
# trainable.append(name)
#
# model_state = {name: model_state[name] for name in trainable}
#
# model_dict.update(model_state)
# model_clone.load_state_dict(model_dict)
# model_clone.cuda() if cuda else None
#
# return model_clone, get_optimizer(model_clone, state=op_state)