-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel_gm.py
309 lines (273 loc) · 11.7 KB
/
model_gm.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import sys
PATH_TO_CIFAR = "./cifar_gm/"
sys.path.append(PATH_TO_CIFAR)
import train as cifar_train
def get_model_from_name(*, name='', args=None, idx=-1):
'''
[args.width_ratio] determines the portion that is remained after thining the network
if [width_ratio] = -1, then it means no thining is applied
else, [width_ratio] should be positive
if [idx] is -1, then [weight_ratio] is -1, which means no width resizing in hidden layers
Otherwise if [idx] is 0 and only one model is available, then [weight_ratio] is
[args.width_ratio] and width is resized
'''
from utils_gm import Namespace
if args == None:
args = Namespace( model_name=name )
if idx != -1 and idx == (args.num_models - 1):
# only passes for the second model
width_ratio = args.width_ratio
else:
width_ratio = -1
if args.model_name == 'net':
return Net(args)
elif args.model_name == 'simplenet':
return SimpleAhaNet(args)
elif args.model_name == 'smallmlpnet':
return SmallMlpNet(args)
elif args.model_name == 'mlpnet':
return MlpNet(args, width_ratio=width_ratio)
elif args.model_name == 'bigmlpnet':
return BigMlpNet(args)
elif args.model_name == 'cifarmlpnet':
return CifarMlpNet(args)
### my models begin
elif args.model_name == 'naivenet':
return naive_net()
elif args.model_name == 'naivecnn':
return naive_cnn()
elif args.model_name == 'simplemnistnet':
return SimpleNet( args )
elif args.model_name == 'lenet':
return LeNet()
elif args.model_name == 'smalllenet':
return SmallLeNet()
### my models end
elif args.model_name[0:3] == 'vgg' or args.model_name[0:3] == 'res':
if args.second_model_name is None or idx == 0:
barebone_config = {'model': args.model_name, 'dataset': args.dataset}
else:
barebone_config = {'model': args.second_model_name, 'dataset': args.dataset}
# if you want pre-relu acts, set relu_inplace to False
return cifar_train.get_model(barebone_config, args.gpu_id, relu_inplace=not args.prelu_acts)
else:
print( f"model name {args.model_name} not found!" )
class naive_net( nn.Module ):
def __init__( self ):
super( naive_net, self ).__init__()
self.lin1 = nn.Linear( 2, 3 )
self.lin2 = nn.Linear( 3, 2 )
def forward( self, x:torch.tensor ):
assert x.shape == torch.Size([2])
x = self.lin1( x )
x = F.relu( x )
x = self.lin2( x )
return x
class naive_cnn( nn.Module ):
def __init__( self ):
super( naive_cnn, self ).__init__()
self.conv1 = nn.Conv2d( 1, 2, 2 )
self.fc1 = nn.Linear( 8, 2 )
def forward( self, x:torch.tensor ):
x = F.relu( self.conv1( x ) )
x = self.fc1( x )
return x
class SimpleNet( nn.Module ):
def __init__( self, args ):
super( SimpleNet, self ).__init__()
# self.conv1 = nn.Conv2d( 1, args.hidden_size_1_conv, args.conv_kernel_size, padding=args.conv_padding, device=args.device, bias=args.bias )
# self.maxpool = nn.MaxPool2d( args.maxpool_kernel_size, padding=args.maxpool_padding )
# self.conv2 = nn.Conv2d( args.hidden_size_1_conv, args.hidden_size_2_conv, args.conv_kernel_size, padding=args.conv_padding, device=args.device, bias=args.bias )
# self.fc1 = nn.Linear( args.input_size_fc, args.num_classes, device=args.device, bias=args.bias )
self.conv1 = nn.Conv2d( 1, 32, 5, padding='same', device=args.device, bias=False )
self.maxpool = nn.MaxPool2d( 2, padding=0 )
self.conv2 = nn.Conv2d( 32, 64, 5, padding='same', device=args.device, bias=False )
self.fc1 = nn.Linear( 3136, 10, device=args.device, bias=False )
def forward( self, x ):
output = F.relu( self.conv1( x ) )
output = self.maxpool( output )
output = F.relu( self.conv2( output ) )
output = self.maxpool( output )
output = output.view( output.shape[0], -1 )
output = self.fc1( output )
return output
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(3, 6, kernel_size=(5, 5), bias=False)
self.conv2 = nn.Conv2d(6, 16, kernel_size=(5, 5), bias=False)
self.fc1 = nn.Linear(16 * 5 * 5, 120, bias=False)
self.fc2 = nn.Linear(120, 84, bias=False)
self.fc3 = nn.Linear(84, 10, bias=False)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class SmallLeNet(nn.Module):
def __init__(self):
super(SmallLeNet, self).__init__()
self.conv1 = nn.Conv2d(3, 3, kernel_size=(5, 5), bias=False)
self.conv2 = nn.Conv2d(3, 3, kernel_size=(5, 5), bias=False)
self.fc1 = nn.Linear(3 * 5 * 5, 12, bias=False)
self.fc2 = nn.Linear(12, 8, bias=False)
self.fc3 = nn.Linear(8, 10, bias=False)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class LogisticRegressionModel(nn.Module):
# default input and output dim for
def __init__(self, input_dim=784, output_dim=10):
super(LogisticRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
def forward(self, x):
out = F.softmax(self.linear(x))
return out
class Net(nn.Module):
def __init__(self, args):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5, bias= not args.disable_bias)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5, bias= not args.disable_bias)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50, bias= not args.disable_bias)
self.fc2 = nn.Linear(50, 10, bias= not args.disable_bias)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x)
class SimpleAhaNet(nn.Module):
def __init__(self, args):
super(SimpleAhaNet, self).__init__()
self.fc1 = nn.Linear(784, args.num_hidden_nodes, bias= not args.disable_bias)
self.fc2 = nn.Linear(args.num_hidden_nodes, 10, bias= not args.disable_bias)
self.enable_dropout = args.enable_dropout
def forward(self, x):
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x)
class MlpNet(nn.Module):
def __init__(self, args, width_ratio=-1):
super(MlpNet, self).__init__()
if args.dataset == 'mnist':
# 28 x 28 x 1
input_dim = 784
elif args.dataset.lower() == 'cifar10':
# 32 x 32 x 3
input_dim = 3072
if width_ratio != -1:
self.width_ratio = width_ratio
else:
self.width_ratio = 1
self.fc1 = nn.Linear(input_dim, int(args.num_hidden_nodes1/self.width_ratio), bias=not args.disable_bias)
self.fc2 = nn.Linear(int(args.num_hidden_nodes1/self.width_ratio), int(args.num_hidden_nodes2/self.width_ratio), bias=not args.disable_bias)
self.fc3 = nn.Linear(int(args.num_hidden_nodes2/self.width_ratio), int(args.num_hidden_nodes3/self.width_ratio), bias=not args.disable_bias)
self.fc4 = nn.Linear(int(args.num_hidden_nodes3/self.width_ratio), 10, bias=not args.disable_bias)
self.enable_dropout = args.enable_dropout
def forward(self, x, disable_logits=False):
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc2(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc3(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = self.fc4(x)
if disable_logits:
return x
else:
return F.log_softmax(x)
class SmallMlpNet(nn.Module):
def __init__(self, args):
super(SmallMlpNet, self).__init__()
self.fc1 = nn.Linear(784, args.num_hidden_nodes1, bias=not args.disable_bias)
self.fc2 = nn.Linear(args.num_hidden_nodes1, args.num_hidden_nodes2, bias=not args.disable_bias)
self.fc3 = nn.Linear(args.num_hidden_nodes2, 10, bias=not args.disable_bias)
self.enable_dropout = args.enable_dropout
def forward(self, x):
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc2(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = self.fc3(x)
return F.log_softmax(x)
class BigMlpNet(nn.Module):
def __init__(self, args):
super(BigMlpNet, self).__init__()
if args.dataset == 'mnist':
# 28 x 28 x 1
input_dim = 784
elif args.dataset.lower() == 'cifar10':
# 32 x 32 x 3
input_dim = 3072
self.fc1 = nn.Linear(input_dim, args.num_hidden_nodes1, bias=not args.disable_bias)
self.fc2 = nn.Linear(args.num_hidden_nodes1, args.num_hidden_nodes2, bias=not args.disable_bias)
self.fc3 = nn.Linear(args.num_hidden_nodes2, args.num_hidden_nodes3, bias=not args.disable_bias)
self.fc4 = nn.Linear(args.num_hidden_nodes3, args.num_hidden_nodes4, bias=not args.disable_bias)
self.fc5 = nn.Linear(args.num_hidden_nodes4, 10, bias=not args.disable_bias)
self.enable_dropout = args.enable_dropout
def forward(self, x):
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc2(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc3(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc4(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = self.fc5(x)
return F.log_softmax(x)
class CifarMlpNet(nn.Module):
def __init__(self, args):
super(CifarMlpNet, self).__init__()
input_dim = 3072
self.fc1 = nn.Linear(input_dim, 1024, bias=not args.disable_bias)
self.fc2 = nn.Linear(1024, 512, bias=not args.disable_bias)
self.fc3 = nn.Linear(512, 128, bias=not args.disable_bias)
self.fc4 = nn.Linear(128, 10, bias=not args.disable_bias)
self.enable_dropout = args.enable_dropout
def forward(self, x):
x = x.view(x.shape[0], -1)
x = F.relu(self.fc1(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc2(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = F.relu(self.fc3(x))
if self.enable_dropout:
x = F.dropout(x, training=self.training)
x = self.fc4(x)
return F.log_softmax(x)