-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
473 lines (364 loc) · 16.6 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
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
463
464
465
466
467
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision.models.resnet import ResNet, BasicBlock, conv3x3, conv1x1
device = 'cuda' if torch.cuda.is_available() else 'cpu'
channel_dict = {
"cifar10": 3,
"cinic10": 3,
"cifar100": 3,
"mnist": 1,
"fmnist": 1,
}
############################################################################################################
# MOBILENET
############################################################################################################
class MLP(nn.Module):
def __init__(self, num_classes=10, net_width=128, im_size = (28,28), dataset = 'cifar10'):
super(MLP, self).__init__()
channel = channel_dict.get(dataset)
self.fc1 = nn.Linear(im_size[0]*im_size[1]*channel, net_width)
self.fc2 = nn.Linear(net_width, net_width)
self.fc3 = nn.Linear(net_width, num_classes)
def forward(self, x):
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return self.fc3(x)
class Block(nn.Module):
'''expand + depthwise + pointwise'''
def __init__(self, in_planes, out_planes, expansion, stride, norm_layer):
super(Block, self).__init__()
self.stride = stride
planes = expansion * in_planes
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, stride=1, padding=0, bias=False)
self.bn1 = norm_layer(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, groups=planes, bias=False)
self.bn2 = norm_layer(planes)
self.conv3 = nn.Conv2d(planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
self.bn3 = norm_layer(out_planes)
self.shortcut = nn.Sequential()
if stride == 1 and in_planes != out_planes:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False),
norm_layer(out_planes),
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
out = out + self.shortcut(x) if self.stride==1 else out
return out
class MobileNetV2(nn.Module):
# (expansion, out_planes, num_blocks, stride)
def __init__(self, num_classes=10, norm_layer=nn.BatchNorm2d,shrink=1, dataset = 'cifar10'):
super(MobileNetV2, self).__init__()
# NOTE: change conv1 stride 2 -> 1 for CIFAR10
self.dataset = dataset
channel = channel_dict.get(dataset)
self.norm_layer = norm_layer
self.cfg = [(1, 16//shrink, 1, 1),
(6, 24//shrink, 2, 1), # NOTE: change stride 2 -> 1 for CIFAR10
(6, 32//shrink, 3, 2),
(6, 64//shrink, 4, 2),
(6, 96//shrink, 3, 1),
(6, 160//shrink, 3, 2),
(6, 320//shrink, 1, 1)]
self.conv1 = nn.Conv2d(channel, 32, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = self.norm_layer(32)
self.layers = self._make_layers(in_planes=32)
self.conv2 = nn.Conv2d(self.cfg[-1][1], 1280//shrink, kernel_size=1, stride=1, padding=0, bias=False)
self.bn2 = self.norm_layer(1280//shrink)
self.classification_layer = nn.Linear(1280//shrink, num_classes)
def _make_layers(self, in_planes):
layers = []
for expansion, out_planes, num_blocks, stride in self.cfg:
strides = [stride] + [1]*(num_blocks-1)
for stride in strides:
layers.append(Block(in_planes, out_planes, expansion, stride, self.norm_layer))
in_planes = out_planes
return nn.Sequential(*layers)
def extract_features(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.layers(out)
out = F.relu(self.bn2(self.conv2(out)))
out = F.avg_pool2d(out, 4)
out = out.view(out.size(0), -1)
return out
def forward(self, x):
feature = self.extract_features(x)
out = self.classification_layer(feature)
return out
def mobilenetv2(num_classes=10, dataset = 'cifar10'):
return MobileNetV2(norm_layer=nn.BatchNorm2d, shrink=2, num_classes=num_classes, dataset = 'cifar10')
############################################################################################################
# RESNET
############################################################################################################
class basic_noskip(BasicBlock):
expansion: int = 1
def __init__(
self,
*args,
**kwargs
) -> None:
super(basic_noskip, self).__init__(*args, **kwargs)
def forward(self, x):
out = self.conv1(x)
# out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
# out = self.bn2(out)
out = self.relu(out)
return out
class Model_noskip(nn.Module):
def __init__(self, channel=3, feature_dim=128, group_norm=False):
super(Model_noskip, self).__init__()
self.f = []
for name, module in ResNet(basic_noskip, [1,1,1,1], num_classes=10).named_children():
if name == 'conv1':
module = nn.Conv2d(channel, 64, kernel_size=3, stride=1, padding=1, bias=False)
if not isinstance(module, nn.Linear) and not isinstance(module, nn.MaxPool2d):
self.f.append(module)
# encoder
self.f = nn.Sequential(*self.f)
# projection head
self.g = nn.Sequential(nn.Linear(512, 512, bias=False), nn.BatchNorm1d(512),
nn.ReLU(inplace=True), nn.Linear(512, feature_dim, bias=True))
if group_norm:
apply_gn(self)
def forward(self, x):
x = self.f(x)
feature = torch.flatten(x, start_dim=1)
out = self.g(feature)
return F.normalize(feature, dim=-1), F.normalize(out, dim=-1)
class resnet8_noskip(nn.Module):
def __init__(self, num_classes=10, pretrained_path=None, group_norm=False, dataset = 'cifar10'):
super(resnet8_noskip, self).__init__()
channel = channel_dict.get(dataset)
# encoder
self.f = Model_noskip(channel = channel, group_norm=group_norm).f
# classifier
self.classification_layer = nn.Linear(512, num_classes, bias=True)
if pretrained_path:
self.load_state_dict(torch.load(pretrained_path, map_location='cpu'), strict=False)
def extract_features(self, x):
return torch.flatten(self.f(x), start_dim=1)
def forward(self, x):
feature = self.extract_features(x)
out = self.classification_layer(feature)
return out
class Model(nn.Module):
def __init__(self, channel=3, feature_dim=128, group_norm=False):
super(Model, self).__init__()
self.f = []
for name, module in ResNet(BasicBlock, [1,1,1,1], num_classes=10).named_children():
if name == 'conv1':
module = nn.Conv2d(channel, 64, kernel_size=3, stride=1, padding=1, bias=False)
if not isinstance(module, nn.Linear) and not isinstance(module, nn.MaxPool2d):
self.f.append(module)
# encoder
self.f = nn.Sequential(*self.f)
# projection head
self.g = nn.Sequential(nn.Linear(512, 512, bias=False), nn.BatchNorm1d(512),
nn.ReLU(inplace=True), nn.Linear(512, feature_dim, bias=True))
if group_norm:
apply_gn(self)
def forward(self, x):
x = self.f(x)
feature = torch.flatten(x, start_dim=1)
out = self.g(feature)
return F.normalize(feature, dim=-1), F.normalize(out, dim=-1)
class resnet8(nn.Module):
def __init__(self, num_classes=10, pretrained_path=None, group_norm=False, dataset = 'cifar10'):
super(resnet8, self).__init__()
channel = channel_dict.get(dataset)
# encoder
self.f = Model(channel = channel, group_norm=group_norm).f
# classifier
self.classification_layer = nn.Linear(512, num_classes, bias=True)
if pretrained_path:
self.load_state_dict(torch.load(pretrained_path, map_location='cpu'), strict=False)
def extract_features(self, x):
return torch.flatten(self.f(x), start_dim=1)
def forward(self, x):
feature = self.extract_features(x)
out = self.classification_layer(feature)
return out
############################################################################################################
# SHUFFLENET
############################################################################################################
class ShuffleBlock(nn.Module):
def __init__(self, groups):
super(ShuffleBlock, self).__init__()
self.groups = groups
def forward(self, x):
'''Channel shuffle: [N,C,H,W] -> [N,g,C/g,H,W] -> [N,C/g,g,H,w] -> [N,C,H,W]'''
N,C,H,W = x.size()
g = self.groups
return x.view(N,g,C//g,H,W).permute(0,2,1,3,4).reshape(N,C,H,W)
class Bottleneck(nn.Module):
def __init__(self, in_planes, out_planes, stride, groups):
super(Bottleneck, self).__init__()
self.stride = stride
mid_planes = out_planes//4
g = 1 if in_planes==24 else groups
self.conv1 = nn.Conv2d(in_planes, mid_planes, kernel_size=1, groups=g, bias=False)
self.bn1 = nn.BatchNorm2d(mid_planes)
self.shuffle1 = ShuffleBlock(groups=g)
self.conv2 = nn.Conv2d(mid_planes, mid_planes, kernel_size=3, stride=stride, padding=1, groups=mid_planes, bias=False)
self.bn2 = nn.BatchNorm2d(mid_planes)
self.conv3 = nn.Conv2d(mid_planes, out_planes, kernel_size=1, groups=groups, bias=False)
self.bn3 = nn.BatchNorm2d(out_planes)
self.shortcut = nn.Sequential()
if stride == 2:
self.shortcut = nn.Sequential(nn.AvgPool2d(3, stride=2, padding=1))
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.shuffle1(out)
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
res = self.shortcut(x)
out = F.relu(torch.cat([out,res], 1)) if self.stride==2 else F.relu(out+res)
return out
class ShuffleNet(nn.Module):
def __init__(self, num_classes=10):
super(ShuffleNet, self).__init__()
cfg = {'out_planes': [200,400,800],'num_blocks': [4,8,4],'groups': 2}
out_planes = cfg['out_planes']
num_blocks = cfg['num_blocks']
groups = cfg['groups']
self.conv1 = nn.Conv2d(3, 24, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(24)
self.in_planes = 24
self.layer1 = self._make_layer(out_planes[0], num_blocks[0], groups)
self.layer2 = self._make_layer(out_planes[1], num_blocks[1], groups)
self.layer3 = self._make_layer(out_planes[2], num_blocks[2], groups)
self.classification_layer = nn.Linear(out_planes[2], num_classes)
def _make_layer(self, out_planes, num_blocks, groups):
layers = []
for i in range(num_blocks):
stride = 2 if i == 0 else 1
cat_planes = self.in_planes if i == 0 else 0
layers.append(Bottleneck(self.in_planes, out_planes-cat_planes, stride=stride, groups=groups))
self.in_planes = out_planes
return nn.Sequential(*layers)
def extract_features(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = F.avg_pool2d(out, 4)
feature = out.view(out.size(0), -1)
return feature
def forward(self, x):
feature = self.extract_features(x)
out = self.classification_layer(feature)
return out
''' ConvNet '''
class ConvNet(nn.Module):
def __init__(self, num_classes=10, net_width=128, net_depth=3, net_act='relu', net_norm='instancenorm', net_pooling='avgpooling', im_size = (32,32), dataset = 'cifar10'):
super(ConvNet, self).__init__()
channel = channel_dict.get(dataset)
self.features, shape_feat = self._make_layers(channel, net_width, net_depth, net_norm, net_act, net_pooling, im_size)
num_feat = shape_feat[0]*shape_feat[1]*shape_feat[2]
print(f"num feat {num_feat}")
self.classifier = nn.Linear(num_feat, num_classes)
def forward(self, x):
# print("MODEL DATA ON: ", x.get_device(), "MODEL PARAMS ON: ", self.classifier.weight.data.get_device())
out = self.get_feature(x)
out = self.classifier(out)
return out
def get_feature(self,x):
out = self.features(x)
out = out.view(out.size(0), -1)
return out
def _get_activation(self, net_act):
if net_act == 'sigmoid':
return nn.Sigmoid()
elif net_act == 'relu':
return nn.ReLU(inplace=True)
elif net_act == 'leakyrelu':
return nn.LeakyReLU(negative_slope=0.01)
else:
exit('unknown activation function: %s'%net_act)
def _get_pooling(self, net_pooling):
if net_pooling == 'maxpooling':
return nn.MaxPool2d(kernel_size=2, stride=2)
elif net_pooling == 'avgpooling':
return nn.AvgPool2d(kernel_size=2, stride=2)
elif net_pooling == 'none':
return None
else:
exit('unknown net_pooling: %s'%net_pooling)
def _get_normlayer(self, net_norm, shape_feat):
# shape_feat = (c*h*w)
if net_norm == 'batchnorm':
return nn.BatchNorm2d(shape_feat[0], affine=True)
elif net_norm == 'layernorm':
return nn.LayerNorm(shape_feat, elementwise_affine=True)
elif net_norm == 'instancenorm':
return nn.GroupNorm(shape_feat[0], shape_feat[0], affine=True)
elif net_norm == 'groupnorm':
return nn.GroupNorm(4, shape_feat[0], affine=True)
elif net_norm == 'none':
return None
else:
exit('unknown net_norm: %s'%net_norm)
def _make_layers(self, channel, net_width, net_depth, net_norm, net_act, net_pooling, im_size):
layers = []
in_channels = channel
if im_size[0] == 28:
im_size = (32, 32)
shape_feat = [in_channels, im_size[0], im_size[1]]
for d in range(net_depth):
layers += [nn.Conv2d(in_channels, net_width, kernel_size=3, padding=3 if channel == 1 and d == 0 else 1)]
shape_feat[0] = net_width
if net_norm != 'none':
layers += [self._get_normlayer(net_norm, shape_feat)]
layers += [self._get_activation(net_act)]
in_channels = net_width
if net_pooling != 'none':
layers += [self._get_pooling(net_pooling)]
shape_feat[1] //= 2
shape_feat[2] //= 2
return nn.Sequential(*layers), shape_feat
class TextModel(nn.Module):
def __init__(self, vocab_size=95811, embed_dim=64, num_classes=4):
super(TextModel, self).__init__()
self.embedding = nn.EmbeddingBag(vocab_size, embed_dim)
self.fc = nn.Linear(embed_dim, num_classes)
self.init_weights()
def init_weights(self):
initrange = 0.5
self.embedding.weight.data.uniform_(-initrange, initrange)
self.fc.weight.data.uniform_(-initrange, initrange)
self.fc.bias.data.zero_()
def forward(self, text, offsets):
embedded = self.embedding(text, offsets)
return self.fc(embedded)
class LogisticRegression(nn.Module):
def __init__(self, input_dim=130107, num_classes=20):
super(LogisticRegression, self).__init__()
self.fc = torch.nn.Parameter(torch.zeros(input_dim, num_classes))
def forward(self, x):
out = x @ self.fc
return out
def get_model(model):
return { "mobilenetv2" : (mobilenetv2, optim.Adam, {"lr" : 0.001}),
"shufflenet" : (ShuffleNet, optim.Adam, {"lr" : 0.001}),
"resnet8" : (resnet8, optim.Adam, {"lr" : 0.001}),
"resnet8_noskip" : (resnet8_noskip, optim.Adam, {"lr" : 0.001}),
"ConvNet" : (ConvNet, optim.Adam, {"lr" : 0.001}),
"MLP" : (MLP, optim.Adam, {"lr" : 0.001}),
"TextModel" : (TextModel, optim.Adam, {"lr" : 1}),
"LogisticRegression" : (LogisticRegression, optim.Adam, {"lr" : 0.001}),
}[model]
def print_model(model):
n = 0
print("Model:")
for key, value in model.named_parameters():
print(' -', '{:30}'.format(key), list(value.shape), "Requires Grad:", value.requires_grad)
n += value.numel()
print("Total number of Parameters: ", n)
print()