forked from LaRiffle/ariann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
298 lines (251 loc) · 9.17 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
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
import os
import re
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
class Network1(nn.Module):
def __init__(self, dataset, out_features):
super(Network1, self).__init__()
self.fc1 = nn.Linear(784, 128)
self.fc2 = nn.Linear(128, 128)
self.fc3 = nn.Linear(128, out_features)
def forward(self, x):
x = x.reshape(-1, 784)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
x = self.fc3(x)
x = F.relu(x)
return x
class Network2(nn.Module):
def __init__(self, dataset, out_features):
super(Network2, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=0)
self.conv2 = nn.Conv2d(16, 16, kernel_size=5, stride=1, padding=0)
self.fc1 = nn.Linear(256, 100)
self.fc2 = nn.Linear(100, out_features)
def forward(self, x):
x = self.conv1(x)
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = F.relu(x) ## inverted!
x = self.conv2(x)
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = F.relu(x) ## inverted!
x = x.reshape(-1, 256)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
return x
class LeNet(nn.Module):
def __init__(self, dataset, out_features=10):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 20, kernel_size=5, stride=1, padding=0)
self.conv2 = nn.Conv2d(20, 50, kernel_size=5, stride=1, padding=0)
self.fc1 = nn.Linear(800, 500)
self.fc2 = nn.Linear(500, out_features)
def forward(self, x):
x = self.conv1(x)
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = F.relu(x) # switched!!
x = self.conv2(x)
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = F.relu(x) # switched!!
x = x.reshape(-1, 800)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
x = F.relu(x)
return x
class AlexNet_CIFAR10(nn.Module):
def __init__(self, out_features=10):
super(AlexNet_CIFAR10, self).__init__()
self.conv_base = nn.Sequential(
nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=10),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.ReLU(inplace=True), ## inverted!
nn.BatchNorm2d(96),
nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=1),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.ReLU(inplace=True), ## inverted!
nn.BatchNorm2d(256),
nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
nn.ReLU(inplace=True),
)
self.fc_base = nn.Sequential(
nn.Linear(256, 256),
nn.ReLU(inplace=True),
nn.Linear(256, 256),
nn.ReLU(inplace=True),
nn.Linear(256, out_features),
nn.ReLU(inplace=True),
)
def forward(self, x):
x = self.conv_base(x)
x = torch.flatten(x, 1)
x = self.fc_base(x)
return x
class AlexNet_FALCON(nn.Module):
"""
This is the AlexNet version used in FALCON, which is not the standard
of PyTorch
"""
def __init__(self, out_features=10):
super(AlexNet_FALCON, self).__init__()
self.conv_base = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3),
nn.Conv2d(64, 64, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(inplace=True), ## inverted!
nn.BatchNorm2d(64),
nn.Conv2d(64, 128, kernel_size=5, stride=1, padding=2),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(inplace=True), ## inverted!
nn.BatchNorm2d(128),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(inplace=True),
)
self.fc_base = nn.Sequential(
nn.Linear(16384, 1024), # 7*7*256
nn.ReLU(inplace=True),
nn.Linear(1024, 1024),
nn.ReLU(inplace=True),
nn.Linear(1024, 200),
nn.ReLU(inplace=True),
)
def forward(self, x):
x = self.conv_base(x)
x = torch.flatten(x, 1)
x = self.fc_base(x)
return x
def alexnet(dataset, out_features):
if dataset == "cifar10":
model = AlexNet_CIFAR10(out_features)
return model
elif dataset == "tiny-imagenet":
model = models.alexnet(pretrained=True)
class Empty(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x
model.avgpool = Empty()
model.classifier = nn.Sequential(
Empty(), # nn.Dropout(),
nn.Linear(256, 1024),
nn.ReLU(True),
Empty(), # nn.Dropout(),
nn.Linear(1024, 1024),
nn.ReLU(True),
nn.Linear(1024, out_features),
)
# Invert ReLU and MaxPool2d
for i, module in enumerate(model.features[:-1]):
next_module = model.features[i + 1]
if isinstance(module, nn.ReLU) and isinstance(next_module, nn.MaxPool2d):
model.features[i + 1] = module
model.features[i] = next_module
return model
else:
raise ValueError("VGG16 can't be built for this dataset, maybe modify it?")
def vgg16(dataset, out_features):
model = models.vgg16()
# Invert ReLU <-> Maxpool
for i, module in enumerate(model.features[:-1]):
next_module = model.features[i + 1]
if isinstance(module, nn.ReLU) and isinstance(next_module, nn.MaxPool2d):
model.features[i + 1] = module
model.features[i] = next_module
class Empty(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x
model.avgpool = Empty()
if dataset == "cifar10":
first_linear = nn.Linear(512, 4096)
elif dataset == "tiny-imagenet":
first_linear = nn.Linear(512 * 2 * 2, 4096)
else:
raise ValueError("VGG16 can't be built for this dataset, maybe modify it?")
model.classifier = nn.Sequential(
first_linear,
nn.ReLU(True),
Empty(), # nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
Empty(), # nn.Dropout(),
nn.Linear(4096, out_features),
)
return model
def resnet18(dataset, out_features):
model = models.resnet18()
model.maxpool, model.relu = model.relu, model.maxpool
model.fc = nn.Linear(in_features=512, out_features=out_features)
return model
model_zoo = {
"network1": Network1,
"network2": Network2,
"lenet": LeNet,
"alexnet": alexnet,
"vgg16": vgg16,
"resnet18": resnet18,
}
def get_model(model_name, dataset, out_features):
return model_zoo[model_name](dataset, out_features)
online_models = {
"lenet_mnist": {
"id": "1WWh_POWmgcBEDxk87t50DEZmTik9NRkg",
"file_name": "lenet_mnist_baseline_99.27.pt",
},
"alexnet_cifar10": {
"id": "1-M8SaF19EFSI1Zqmnr9KL5aQG2AEqWND",
"file_name": "alexnet_cifar10_baseline_70.23.pt",
},
"alexnet_tiny-imagenet": {
"id": "1Nygb3K8dbSBYMls3U6rngYIAYrRsLwR0",
"file_name": "alexnet_tiny-imagenet_baseline_37.8.pt",
},
"resnet18_hymenoptera": {
"id": "1bNHE91Fn32AGPNyk_hmGZuQdpnVmyOtR",
"file_name": "resnet18_hymenoptera_95.pt",
},
}
too_big_models = {
"vgg16_cifar10": "17k1nKItmp-4E1r5GFqfs8oH1Uhmp5e_0",
"vgg16_tiny-imagenet": "1uBiLpPi34Z3NywW3zwilMZpmb964oU8q",
}
def load_state_dict(model, model_name, dataset):
MODEL_PATH = "pretrained_models/"
base_name = f"{model_name}_{dataset}"
file_name = None
for file in os.scandir(MODEL_PATH):
if re.match(fr"^{base_name}", file.name):
file_name = file.name
if file_name is None:
if base_name in online_models:
id = online_models[base_name]["id"]
file_name = online_models[base_name]["file_name"]
print(f"Downloading model {file_name}... ")
os.system(
f"wget --no-check-certificate "
f"'https://docs.google.com/uc?export=download&id={id}' -O {MODEL_PATH+file_name}"
)
else:
if base_name in too_big_models:
id = too_big_models[base_name]
print(
f"Model {base_name} has to be downloaded manually :( \n\n"
f"https://docs.google.com/uc?export=download&id={id}\n"
)
raise FileNotFoundError(f"No pretrained model for {model_name} {dataset} was found!")
model.load_state_dict(torch.load(MODEL_PATH + file_name, map_location=torch.device("cpu")))
print(f"Pre-trained model loaded from {file_name}")