forked from Xtra-Computing/NIID-Bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
453 lines (366 loc) · 14.7 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class FcNet(nn.Module):
"""
Fully connected network for MNIST classification
"""
def __init__(self, input_dim, hidden_dims, output_dim, dropout_p=0.0):
super().__init__()
self.input_dim = input_dim
self.hidden_dims = hidden_dims
self.output_dim = output_dim
self.dropout_p = dropout_p
self.dims = [self.input_dim]
self.dims.extend(hidden_dims)
self.dims.append(self.output_dim)
self.layers = nn.ModuleList([])
for i in range(len(self.dims) - 1):
ip_dim = self.dims[i]
op_dim = self.dims[i + 1]
self.layers.append(
nn.Linear(ip_dim, op_dim, bias=True)
)
self.__init_net_weights__()
def __init_net_weights__(self):
for m in self.layers:
m.weight.data.normal_(0.0, 0.1)
m.bias.data.fill_(0.1)
def forward(self, x):
x = x.view(-1, self.input_dim)
for i, layer in enumerate(self.layers):
x = layer(x)
# Do not apply ReLU on the final layer
if i < (len(self.layers) - 1):
x = F.relu(x)
if i < (len(self.layers) - 1): # No dropout on output layer
x = F.dropout(x, p=self.dropout_p, training=self.training)
return x
class ConvBlock(nn.Module):
def __init__(self):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
return x
class FCBlock(nn.Module):
def __init__(self, input_dim, hidden_dims, output_dim=10):
super(FCBlock, self).__init__()
self.fc1 = nn.Linear(input_dim, hidden_dims[0])
self.fc2 = nn.Linear(hidden_dims[0], hidden_dims[1])
self.fc3 = nn.Linear(hidden_dims[1], output_dim)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class VGGConvBlocks(nn.Module):
'''
VGG model
'''
def __init__(self, features, num_classes=10):
super(VGGConvBlocks, self).__init__()
self.features = features
# Initialize weights
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
m.bias.data.zero_()
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
return x
class FCBlockVGG(nn.Module):
def __init__(self, input_dim, hidden_dims, output_dim=10):
super(FCBlockVGG, self).__init__()
self.fc1 = nn.Linear(input_dim, hidden_dims[0])
self.fc2 = nn.Linear(hidden_dims[0], hidden_dims[1])
self.fc3 = nn.Linear(hidden_dims[1], output_dim)
def forward(self, x):
x = F.dropout(x)
x = F.relu(self.fc1(x))
x = F.dropout(x)
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class SimpleCNN(nn.Module):
def __init__(self, input_dim, hidden_dims, output_dim=10):
super(SimpleCNN, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
# for now, we hard coded this network
# i.e. we fix the number of hidden layers i.e. 2 layers
self.fc1 = nn.Linear(input_dim, hidden_dims[0])
self.fc2 = nn.Linear(hidden_dims[0], hidden_dims[1])
self.fc3 = nn.Linear(hidden_dims[1], output_dim)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
# a simple perceptron model for generated 3D data
class PerceptronModel(nn.Module):
def __init__(self, input_dim=3, output_dim=2):
super(PerceptronModel, self).__init__()
self.fc1 = nn.Linear(input_dim, output_dim)
def forward(self, x):
x = self.fc1(x)
return x
class SimpleCNNMNIST(nn.Module):
def __init__(self, input_dim, hidden_dims, output_dim=10):
super(SimpleCNNMNIST, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
# for now, we hard coded this network
# i.e. we fix the number of hidden layers i.e. 2 layers
self.fc1 = nn.Linear(input_dim, hidden_dims[0])
self.fc2 = nn.Linear(hidden_dims[0], hidden_dims[1])
self.fc3 = nn.Linear(hidden_dims[1], output_dim)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class SimpleCNNContainer(nn.Module):
def __init__(self, input_channel, num_filters, kernel_size, input_dim, hidden_dims, output_dim=10):
super(SimpleCNNContainer, self).__init__()
'''
A testing cnn container, which allows initializing a CNN with given dims
num_filters (list) :: number of convolution filters
hidden_dims (list) :: number of neurons in hidden layers
Assumptions:
i) we use only two conv layers and three hidden layers (including the output layer)
ii) kernel size in the two conv layers are identical
'''
self.conv1 = nn.Conv2d(input_channel, num_filters[0], kernel_size)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(num_filters[0], num_filters[1], kernel_size)
# for now, we hard coded this network
# i.e. we fix the number of hidden layers i.e. 2 layers
self.fc1 = nn.Linear(input_dim, hidden_dims[0])
self.fc2 = nn.Linear(hidden_dims[0], hidden_dims[1])
self.fc3 = nn.Linear(hidden_dims[1], output_dim)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, x.size()[1] * x.size()[2] * x.size()[3])
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
############## LeNet for MNIST ###################
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5, 1)
self.conv2 = nn.Conv2d(20, 50, 5, 1)
self.fc1 = nn.Linear(4 * 4 * 50, 500)
self.fc2 = nn.Linear(500, 10)
self.ceriation = nn.CrossEntropyLoss()
def forward(self, x):
x = self.conv1(x)
x = F.max_pool2d(x, 2, 2)
x = F.relu(x)
x = self.conv2(x)
x = F.max_pool2d(x, 2, 2)
x = F.relu(x)
x = x.view(-1, 4 * 4 * 50)
x = self.fc1(x)
x = self.fc2(x)
return x
class LeNetContainer(nn.Module):
def __init__(self, num_filters, kernel_size, input_dim, hidden_dims, output_dim=10):
super(LeNetContainer, self).__init__()
self.conv1 = nn.Conv2d(1, num_filters[0], kernel_size, 1)
self.conv2 = nn.Conv2d(num_filters[0], num_filters[1], kernel_size, 1)
self.fc1 = nn.Linear(input_dim, hidden_dims[0])
self.fc2 = nn.Linear(hidden_dims[0], output_dim)
def forward(self, x):
x = self.conv1(x)
x = F.max_pool2d(x, 2, 2)
x = F.relu(x)
x = self.conv2(x)
x = F.max_pool2d(x, 2, 2)
x = F.relu(x)
x = x.view(-1, x.size()[1] * x.size()[2] * x.size()[3])
x = self.fc1(x)
x = self.fc2(x)
return x
### Moderate size of CNN for CIFAR-10 dataset
class ModerateCNN(nn.Module):
def __init__(self, output_dim=10):
super(ModerateCNN, self).__init__()
self.conv_layer = nn.Sequential(
# Conv Layer block 1
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
# Conv Layer block 2
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout2d(p=0.05),
# Conv Layer block 3
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Dropout(p=0.1),
# nn.Linear(4096, 1024),
nn.Linear(4096, 512),
nn.ReLU(inplace=True),
# nn.Linear(1024, 512),
nn.Linear(512, 512),
nn.ReLU(inplace=True),
nn.Dropout(p=0.1),
nn.Linear(512, output_dim)
)
def forward(self, x):
x = self.conv_layer(x)
x = x.view(x.size(0), -1)
x = self.fc_layer(x)
return x
### Moderate size of CNN for CIFAR-10 dataset
class ModerateCNNCeleba(nn.Module):
def __init__(self):
super(ModerateCNNCeleba, self).__init__()
self.conv_layer = nn.Sequential(
# Conv Layer block 1
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
# Conv Layer block 2
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
# nn.Dropout2d(p=0.05),
# Conv Layer block 3
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Dropout(p=0.1),
# nn.Linear(4096, 1024),
nn.Linear(4096, 512),
nn.ReLU(inplace=True),
# nn.Linear(1024, 512),
nn.Linear(512, 512),
nn.ReLU(inplace=True),
nn.Dropout(p=0.1),
nn.Linear(512, 2)
)
def forward(self, x):
x = self.conv_layer(x)
# x = x.view(x.size(0), -1)
x = x.view(-1, 4096)
x = self.fc_layer(x)
return x
class ModerateCNNMNIST(nn.Module):
def __init__(self):
super(ModerateCNNMNIST, self).__init__()
self.conv_layer = nn.Sequential(
# Conv Layer block 1
nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
# Conv Layer block 2
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout2d(p=0.05),
# Conv Layer block 3
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Dropout(p=0.1),
nn.Linear(2304, 1024),
nn.ReLU(inplace=True),
nn.Linear(1024, 512),
nn.ReLU(inplace=True),
nn.Dropout(p=0.1),
nn.Linear(512, 10)
)
def forward(self, x):
x = self.conv_layer(x)
x = x.view(x.size(0), -1)
x = self.fc_layer(x)
return x
class ModerateCNNContainer(nn.Module):
def __init__(self, input_channels, num_filters, kernel_size, input_dim, hidden_dims, output_dim=10):
super(ModerateCNNContainer, self).__init__()
##
self.conv_layer = nn.Sequential(
# Conv Layer block 1
nn.Conv2d(in_channels=input_channels, out_channels=num_filters[0], kernel_size=kernel_size, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=num_filters[0], out_channels=num_filters[1], kernel_size=kernel_size, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
# Conv Layer block 2
nn.Conv2d(in_channels=num_filters[1], out_channels=num_filters[2], kernel_size=kernel_size, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=num_filters[2], out_channels=num_filters[3], kernel_size=kernel_size, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout2d(p=0.05),
# Conv Layer block 3
nn.Conv2d(in_channels=num_filters[3], out_channels=num_filters[4], kernel_size=kernel_size, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=num_filters[4], out_channels=num_filters[5], kernel_size=kernel_size, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.fc_layer = nn.Sequential(
nn.Dropout(p=0.1),
nn.Linear(input_dim, hidden_dims[0]),
nn.ReLU(inplace=True),
nn.Linear(hidden_dims[0], hidden_dims[1]),
nn.ReLU(inplace=True),
nn.Dropout(p=0.1),
nn.Linear(hidden_dims[1], output_dim)
)
def forward(self, x):
x = self.conv_layer(x)
x = x.view(x.size(0), -1)
x = self.fc_layer(x)
return x
def forward_conv(self, x):
x = self.conv_layer(x)
x = x.view(x.size(0), -1)
return x