This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCIFAR10_model_weight_list.py
394 lines (296 loc) · 12.8 KB
/
CIFAR10_model_weight_list.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
import torch.nn as nn
import torch.nn.functional as F
import torch
import sys
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data.sampler import SubsetRandomSampler
import torch
import torchvision
import torch.optim as optim
import torch.nn as nn
from torch.autograd import Variable
from six import add_metaclass
from contextlib import contextmanager
import random
import pickle
import os
import time
import functools
print("Python: %s" % sys.version)
print("Pytorch: %s" % torch.__version__)
batch_size = 128
# determine device to run network on (runs on gpu if available)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# define series of transforms to pre process images
transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
])
classes = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
# load training set
cifar10_trainset = torchvision.datasets.CIFAR10('/home/fmejia/fmejia/Cypercat/cyphercat/datasets//', train=True, transform=transform, download=True)
cifar10_trainloader = torch.utils.data.DataLoader(cifar10_trainset, batch_size=batch_size, shuffle=True, num_workers=2)
# load test set
cifar10_testset = torchvision.datasets.CIFAR10('/home/fmejia/fmejia/Cypercat/cyphercat/datasets//', train=False, transform=transform, download=True)
cifar10_testloader = torch.utils.data.DataLoader(cifar10_testset, batch_size=batch_size, shuffle=True, num_workers=2)
testset_list = []
test_batch_size = 128
for i in range(int(5 * test_batch_size / batch_size)):
testset_list.append(cifar10_testset)
cifar10_testset5 = torch.utils.data.ConcatDataset(testset_list)
cifar10_testloader5 = torch.utils.data.DataLoader(cifar10_testset5, batch_size=test_batch_size, shuffle=True, num_workers=2)
# helper function to unnormalize and plot image
def imshow(img, filename = None):
mean = torch.tensor((0.4914, 0.4822, 0.4465)).to(device)
mean = mean.view(-1,1,1).cpu().detach().numpy()
var = torch.tensor((0.2023, 0.1994, 0.2010)).to(device)
var = var.view(-1,1,1).cpu().detach().numpy()
img = np.array(img)
img = (img*var) + mean
img = np.moveaxis(img, 0, -1)
plt.imshow(img)
try:
plt.savefig(filename)
plt.show()
except:
plt.show()
##############################################################################
# ReparamModule
##############################################################################
class PatchModules(type):
def __call__(cls, *args, **kwargs):
r"""Called when you call ReparamModule(...) """
net = type.__call__(cls, *args, **kwargs)
# collect weight (module, name) pairs
# flatten weights
w_modules_names = []
for m in net.modules():
for n, p in m.named_parameters(recurse=False):
if p is not None:
w_modules_names.append((m, n))
for n, b in m.named_buffers(recurse=False):
if b is not None:
print((
'{} contains buffer {}. The buffer will be treated as '
'a constant and assumed not to change during gradient '
'steps. If this assumption is violated (e.g., '
'BatchNorm*d\'s running_mean/var), the computation will '
'be incorrect.').format(m.__class__.__name__, n))
net._weights_module_names = tuple(w_modules_names)
# Put to correct device before we do stuff on parameters
net = net.to(device)
ws = tuple(m._parameters[n].detach() for m, n in w_modules_names)
assert len(set(w.dtype for w in ws)) == 1
# reparam to a single flat parameter
net._weights_numels = tuple(w.numel() for w in ws)
net._weights_shapes = tuple(w.shape for w in ws)
with torch.no_grad():
flat_w = torch.cat([w.reshape(-1) for w in ws], 0)
# remove old parameters, assign the names as buffers
for m, n in net._weights_module_names:
delattr(m, n)
m.register_buffer(n, None)
# register the flat one
net.register_parameter('flat_w', nn.Parameter(flat_w, requires_grad=True))
return net
@add_metaclass(PatchModules)
class ReparamModule(nn.Module):
def _apply(self, *args, **kwargs):
rv = super(ReparamModule, self)._apply(*args, **kwargs)
return rv
def get_param(self, clone=False):
if clone:
return self.flat_w.detach().clone().requires_grad_(self.flat_w.requires_grad)
return self.flat_w
@contextmanager
def unflatten_weight(self, flat_w):
ws = (t.view(s) for (t, s) in zip(flat_w.split(self._weights_numels), self._weights_shapes))
for (m, n), w in zip(self._weights_module_names, ws):
setattr(m, n, w)
yield
for m, n in self._weights_module_names:
setattr(m, n, None)
def forward_with_param(self, inp, new_w):
with self.unflatten_weight(new_w):
return nn.Module.__call__(self, inp)
def __call__(self, inp):
return self.forward_with_param(inp, self.flat_w)
# make load_state_dict work on both
# singleton dicts containing a flattened weight tensor and
# full dicts containing unflattened weight tensors...
def load_state_dict(self, state_dict, *args, **kwargs):
if len(state_dict) == 1 and 'flat_w' in state_dict:
return super(ReparamModule, self).load_state_dict(state_dict, *args, **kwargs)
with self.unflatten_weight(self.flat_w):
flat_w = self.flat_w
del self.flat_w
super(ReparamModule, self).load_state_dict(state_dict, *args, **kwargs)
self.register_parameter('flat_w', flat_w)
def reset(self, inplace=True):
if inplace:
flat_w = self.flat_w
else:
flat_w = torch.empty_like(self.flat_w).requires_grad_()
with torch.no_grad():
with self.unflatten_weight(flat_w):
weights_init(self)
return flat_w
class VGG(ReparamModule):
def __init__(self, num_classes = 10):
super(VGG, self).__init__()
cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M']
layers = []
in_channels = 3
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
# layers += [nn.Conv2d(in_channels, in_channels, kernel_size = 2, stride = 2, padding = 0)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
self.features = nn.Sequential(*layers)
# self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
self.classifier = nn.Sequential(
nn.Linear(512, 64),
nn.ReLU(True),
# nn.Dropout(),
nn.Linear(64, 64),
nn.ReLU(True),
# nn.Dropout(),
nn.Linear(64, num_classes),
)
def forward(self, x):
x = self.features(x)
# x = self.avgpool(x)
# x = torch.flatten(x, 1)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
net = VGG()
def weights_init(m):
def init_func(m):
classname = m.__class__.__name__
if classname.startswith('Conv') or classname == 'Linear':
if getattr(m, 'bias', None) is not None:
nn.init.constant_(m.bias, 0.0)
if getattr(m, 'weight', None) is not None:
if classname == 'Linear':
nn.init.xavier_normal_(m.weight)
if classname.startswith('Conv'):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif 'Norm' in classname:
if getattr(m, 'weight', None) is not None:
m.weight.data.fill_(1)
if getattr(m, 'bias', None) is not None:
m.bias.data.zero_()
m.apply(init_func)
return(m)
# net.apply(weights_init)
net.reset()
# loss functions
dis_criterion = nn.BCELoss()
class_criterion = nn.CrossEntropyLoss()
class MyDataset():
def __init__(self, dataset):
self.dataset = dataset
def __getitem__(self, index):
data, target = self.dataset[index]
return data, target, index
def __len__(self):
return len(self.dataset)
cifar10_trainset = torchvision.datasets.CIFAR10('/home/fmejia/fmejia/Cypercat/cyphercat/datasets//', train=True, transform=transform, download=True)
trainset = MyDataset(cifar10_trainset)
cifar10_trainloader = torch.utils.data.DataLoader(trainset, batch_size = batch_size,
shuffle = True, num_workers=2)
label_list = torch.tensor(cifar10_trainset.targets)
label_ind = torch.argsort(label_list).int()
# label_ind = torch.linspace(0,49999,50000).long()
# print(label_ind)
count = 0
dataloader_list = []
for i in (classes):
train_idx = label_ind[count: count + 4999].tolist()
train_sampler = SubsetRandomSampler(train_idx)
trainloader = torch.utils.data.DataLoader(trainset, batch_size = 12,
sampler = train_sampler, num_workers=2)
dataloader_list.append(trainloader)
count += 5000
def train(net, data_loader, test_loader, optimizer, criterion, n_epochs, classes=None, verbose=False):
losses = []
train_accuracy = []
test_accuracy = []
for epoch in range(n_epochs):
net.train()
total = 0
correct = 0
for i, batch in enumerate(data_loader):
imgs, labels, _ = batch
imgs, labels = imgs.to(device), labels.to(device)
# if i == 0:
# imshow(imgs[0,:,:,:].squeeze().cpu().detach().numpy())
optimizer.zero_grad()
imgs0 = imgs
outputs = net(imgs0)
## accuracy calc
predicted = outputs.argmax(dim=1)
total += imgs.size(0)
correct += predicted.eq(labels).sum().item()
##
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
losses.append(loss.item())
# if verbose:
# print("[%d/%d][%d/%d] loss = %f" % (epoch, n_epochs, i, len(data_loader), loss.item()))
# evaluate performance on testset at the end of each epoch
print("[%d/%d]" %(epoch, n_epochs))
# train_accuracy.append(eval_target_net(net, data_loader, classes=classes))
train_accuracy.append(correct/total*100)
test_accuracy.append(eval_target_net(net, test_loader, classes=classes))
print("Train Accuracy %f" %(correct/total*100))
# print(train_accuracy)
plt.plot(losses)
plt.show()
plt.plot(train_accuracy,'bo-',label="train accuracy")
plt.plot(test_accuracy,'ro-',label="validation accuracy")
# Place a legend to the right of this smaller subplot.
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.show()
def eval_target_net(net, testloader, classes=None):
if classes is not None:
class_correct = np.zeros(10)
class_total = np.zeros(10)
total = 0
correct = 0
with torch.no_grad():
net.eval()
for i, (imgs, lbls) in enumerate(testloader):
imgs, lbls = imgs.to(device), lbls.to(device)
output = net(imgs)
predicted = output.argmax(dim=1)
total += imgs.size(0)
correct += predicted.eq(lbls).sum().item()
if classes is not None:
for prediction, lbl in zip(predicted, lbls):
class_correct[lbl] += prediction == lbl
class_total[lbl] += 1
if classes is not None:
for i in range(len(classes)):
print('Accuracy of %s : %.2f %%' % (classes[i], 100 * class_correct[i] / class_total[i]))
print("\nTotal accuracy = %.2f %%\n\n" % (100*(correct/total)) )
return((100*(correct/total)))
w_list = []
for i in range(128):
net = VGG()
net.reset()
criterion = nn.CrossEntropyLoss()
net.to(device)
optimizer_model = optim.SGD(net.parameters(), lr = 0.01, momentum=0.9)
net.train()
train(net, cifar10_trainloader, cifar10_testloader, optimizer_model, criterion, n_epochs = 30, classes=None, verbose=False)
w = torch.tensor(net.get_param().cpu().detach().numpy(), requires_grad = True).to(device)
w_list.append(w)
with open('w_list4.pickle', 'wb') as f:
pickle.dump([w_list], f)