-
Notifications
You must be signed in to change notification settings - Fork 1
/
pruning_kmeans_cifar10.py
554 lines (463 loc) · 23.4 KB
/
pruning_kmeans_cifar10.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
from __future__ import division
import os, sys, shutil, time, random
import argparse
import torch
import torch.backends.cudnn as cudnn
import torchvision.datasets as dset
import torchvision.transforms as transforms
from utils import AverageMeter, RecorderMeter, time_string, convert_secs2time, timing
import models
import numpy as np
import pickle
from scipy.spatial import distance
import pdb
from sklearn.cluster import KMeans
import math
model_names = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name]))
parser = argparse.ArgumentParser(description='Trains ResNeXt on CIFAR or ImageNet',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('data_path', type=str, help='Path to dataset')
parser.add_argument('--dataset', type=str, choices=['cifar10', 'cifar100', 'imagenet', 'svhn', 'stl10'],
help='Choose between Cifar10/100 and ImageNet.')
parser.add_argument('--arch', metavar='ARCH', default='resnet18', choices=model_names,
help='model architecture: ' + ' | '.join(model_names) + ' (default: resnext29_8_64)')
# Optimization options
parser.add_argument('--epochs', type=int, default=200, help='Number of epochs to train.')
parser.add_argument('--batch_size', type=int, default=128, help='Batch size.')
parser.add_argument('--learning_rate', type=float, default=0.1, help='The Learning Rate.')
parser.add_argument('--momentum', type=float, default=0.9, help='Momentum.')
parser.add_argument('--decay', type=float, default=0.0005, help='Weight decay (L2 penalty).')
parser.add_argument('--schedule', type=int, nargs='+', default=[150, 225],
help='Decrease learning rate at these epochs.')
parser.add_argument('--gammas', type=float, nargs='+', default=[0.1, 0.1],
help='LR is multiplied by gamma on schedule, number of gammas should be equal to schedule')
# Checkpoints
parser.add_argument('--print_freq', default=200, type=int, metavar='N', help='print frequency (default: 200)')
parser.add_argument('--save_path', type=str, default='./', help='Folder to save checkpoints and log.')
parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to latest checkpoint (default: none)')
parser.add_argument('--start_epoch', default=0, type=int, metavar='N', help='manual epoch number (useful on restarts)')
parser.add_argument('--evaluate', dest='evaluate', action='store_true', help='evaluate model on validation set')
# Acceleration
parser.add_argument('--ngpu', type=int, default=1, help='0 = CPU.')
parser.add_argument('--workers', type=int, default=2, help='number of data loading workers (default: 2)')
# random seed
parser.add_argument('--manualSeed', type=int, help='manual seed')
# compress rate
parser.add_argument('--pruning_rate', type=float, default=0.1, help='the reducing ratio of pruning based on Distance')
parser.add_argument('--layer_begin', type=int, default=1, help='compress layer of model')
parser.add_argument('--layer_end', type=int, default=1, help='compress layer of model')
parser.add_argument('--layer_inter', type=int, default=1, help='compress layer of model')
parser.add_argument('--use_state_dict', dest='use_state_dict', action='store_true', help='use state dcit or not')
parser.add_argument('--n_clusters', type=int, default=4, help='number of clusters for kmeans')
parser.add_argument('--cos', dest='cos', action='store_true', help='use cos update lr')
args = parser.parse_args()
args.use_cuda = args.ngpu > 0 and torch.cuda.is_available()
if args.manualSeed is None:
args.manualSeed = random.randint(1, 10000)
random.seed(args.manualSeed)
torch.manual_seed(args.manualSeed)
if args.use_cuda:
torch.cuda.manual_seed_all(args.manualSeed)
cudnn.benchmark = True
def main():
best_prec1 = 0
# Init logger
if not os.path.isdir(args.save_path):
os.makedirs(args.save_path)
log = open(os.path.join(args.save_path, 'log_seed_{}.txt'.format(args.manualSeed)), 'w')
print_log('save path : {}'.format(args.save_path), log)
state = {k: v for k, v in args._get_kwargs()}
print_log(state, log)
print_log("Random Seed: {}".format(args.manualSeed), log)
print_log("python version : {}".format(sys.version.replace('\n', ' ')), log)
print_log("torch version : {}".format(torch.__version__), log)
print_log("cudnn version : {}".format(torch.backends.cudnn.version()), log)
print_log("Pruning Rate: {}".format(args.pruning_rate), log)
print_log("Layer Begin: {}".format(args.layer_begin), log)
print_log("Layer End: {}".format(args.layer_end), log)
print_log("Layer Inter: {}".format(args.layer_inter), log)
print_log("Checkpoint Path:{}".format(args.resume), log)
# Init dataset
if not os.path.isdir(args.data_path):
os.makedirs(args.data_path)
if args.dataset == 'cifar10':
mean = [x / 255 for x in [125.3, 123.0, 113.9]]
std = [x / 255 for x in [63.0, 62.1, 66.7]]
elif args.dataset == 'cifar100':
mean = [x / 255 for x in [129.3, 124.1, 112.4]]
std = [x / 255 for x in [68.2, 65.4, 70.4]]
else:
assert False, "Unknow dataset : {}".format(args.dataset)
train_transform = transforms.Compose(
[transforms.RandomHorizontalFlip(), transforms.RandomCrop(32, padding=4), transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
if args.dataset == 'cifar10':
train_data = dset.CIFAR10(args.data_path, train=True, transform=train_transform, download=True)
test_data = dset.CIFAR10(args.data_path, train=False, transform=test_transform, download=True)
num_classes = 10
elif args.dataset == 'cifar100':
train_data = dset.CIFAR100(args.data_path, train=True, transform=train_transform, download=True)
test_data = dset.CIFAR100(args.data_path, train=False, transform=test_transform, download=True)
num_classes = 100
elif args.dataset == 'svhn':
train_data = dset.SVHN(args.data_path, split='train', transform=train_transform, download=True)
test_data = dset.SVHN(args.data_path, split='test', transform=test_transform, download=True)
num_classes = 10
elif args.dataset == 'stl10':
train_data = dset.STL10(args.data_path, split='train', transform=train_transform, download=True)
test_data = dset.STL10(args.data_path, split='test', transform=test_transform, download=True)
num_classes = 10
elif args.dataset == 'imagenet':
assert False, 'Do not finish imagenet code'
else:
assert False, 'Do not support dataset : {}'.format(args.dataset)
train_loader = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=True)
print_log("=> creating model '{}'".format(args.arch), log)
# Init model, criterion, and optimizer
net = models.__dict__[args.arch](num_classes)
print_log("=> network :\n {}".format(net), log)
net = torch.nn.DataParallel(net, device_ids=list(range(args.ngpu)))
# define loss function (criterion) and optimizer
criterion = torch.nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(net.parameters(), state['learning_rate'], momentum=state['momentum'],
weight_decay=state['decay'], nesterov=True)
if args.use_cuda:
net.cuda()
criterion.cuda()
recorder = RecorderMeter(args.epochs)
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print_log("=> loading checkpoint '{}'".format(args.resume), log)
checkpoint = torch.load(args.resume)
# recorder = checkpoint['recorder']
# args.start_epoch = checkpoint['epoch']
if args.use_state_dict:
net.load_state_dict(checkpoint['state_dict'])
else:
net = checkpoint['state_dict']
# optimizer.load_state_dict(checkpoint['optimizer'])
print_log("=> loaded checkpoint '{}' (epoch {})".format(args.resume, checkpoint['epoch']), log)
else:
print_log("=> no checkpoint found at '{}'".format(args.resume), log)
else:
print_log("=> do not use any checkpoint for {} model".format(args.arch), log)
if args.evaluate:
time1 = time.time()
validate(test_loader, net, criterion, log)
time2 = time.time()
print('function took %0.3f ms' % ((time2 - time1) * 1000.0))
return
m = Mask(net)
m.init_length()
print("-" * 10 + "one epoch begin" + "-" * 10)
print("reducing ratio of pruning : %f" % args.pruning_rate)
print("total remaining ratio is %f" % (1 - args.pruning_rate))
val_acc_1, val_los_1 = validate(test_loader, net, criterion, log)
print(" accu before is: %.3f %%" % val_acc_1)
m.model = net
m.init_mask(args.pruning_rate, args.n_clusters)
m.do_similar_mask()
net = m.model
m.if_zero()
if args.use_cuda:
net = net.cuda()
val_acc_2, val_los_2= validate(test_loader, net, criterion, log)
print(" accu after is: %s %%" % val_acc_2)
# Main loop
start_time = time.time()
epoch_time = AverageMeter()
small_filter_index = []
large_filter_index = []
for epoch in range(args.start_epoch, args.epochs):
if args.cos:
print_log('Using cos lr',log)
current_learning_rate = cos_learning_rate(optimizer, epoch, args.epochs)
else:
current_learning_rate = adjust_learning_rate(optimizer, epoch, args.gammas, args.schedule)
need_hour, need_mins, need_secs = convert_secs2time(epoch_time.avg * (args.epochs - epoch))
need_time = '[Need: {:02d}:{:02d}:{:02d}]'.format(need_hour, need_mins, need_secs)
print_log(
'\n==>>{:s} [Epoch={:03d}/{:03d}] {:s} [learning_rate={:6.4f}]'.format(time_string(), epoch, args.epochs,
need_time, current_learning_rate) \
+ ' [Best : Accuracy={:.2f}, Error={:.2f}]'.format(recorder.max_accuracy(False),
100 - recorder.max_accuracy(False)), log)
# train for one epoch
train_acc, train_los = train(train_loader, net, criterion, optimizer, epoch, log, m)
# evaluate on validation set
val_acc_2, val_los_2 = validate(test_loader, net, criterion, log)
# is_best = recorder.update(epoch, train_los, train_acc, val_los_2, val_acc_2)
is_best = val_acc_2 > best_prec1
best_prec1 = max(val_acc_2, best_prec1)
save_checkpoint({
'epoch': epoch + 1,
'arch': args.arch,
'state_dict': net.state_dict(),
'recorder': recorder,
'optimizer': optimizer.state_dict(),
}, is_best, args.save_path, 'checkpoint.pth.tar')
# # save checkpiont
# if epoch==9 or epoch==29 or epoch==59 or epoch==119 or epoch==159:
# tmp_str = 'checkpoint'+ str(epoch+1)+'.pth.tar'
# save_checkpoint({
# 'epoch': epoch + 1,
# 'arch': args.arch,
# 'state_dict': net.state_dict(),
# 'recorder': recorder,
# 'optimizer': optimizer.state_dict(),
# }, False, args.save_path, tmp_str)
# measure elapsed time
epoch_time.update(time.time() - start_time)
start_time = time.time()
# recorder.plot_curve(os.path.join(args.save_path, 'curve.png'))
print_log('Best prec:{:.2f}'.format(best_prec1),log)
log.close()
# train function (forward, backward, update)
def train(train_loader, model, criterion, optimizer, epoch, log, m):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to train mode
model.train()
end = time.time()
for i, (input, target) in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
target = target.cuda()
input_var = input.cuda()
target_var = target
# compute output
output = model(input_var)
loss = criterion(output, target_var)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
# Mask grad for iteration
m.do_grad_mask()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print_log(' Epoch: [{:03d}][{:03d}/{:03d}] '
'Time {batch_time.val:.3f} ({batch_time.avg:.3f}) '
'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
'Loss {loss.val:.4f} ({loss.avg:.4f}) '
'Prec@1 {top1.val:.3f} ({top1.avg:.3f}) '
'Prec@5 {top5.val:.3f} ({top5.avg:.3f}) '.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses, top1=top1, top5=top5) + time_string(), log)
print_log(
' **Train** Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f} Error@1 {error1:.3f}'.format(top1=top1, top5=top5,
error1=100 - top1.avg),
log)
return top1.avg, losses.avg
def validate(val_loader, model, criterion, log):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
for i, (input, target) in enumerate(val_loader):
if args.use_cuda:
target = target.cuda()
input = input.cuda()
input_var = torch.autograd.Variable(input, volatile=True)
target_var = torch.autograd.Variable(target, volatile=True)
# compute output
output = model(input_var)
loss = criterion(output, target_var)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
print_log(' **Test** Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f} Error@1 {error1:.3f}'.format(top1=top1, top5=top5,
error1=100 - top1.avg),
log)
return top1.avg, losses.avg
def print_log(print_string, log):
print("{}".format(print_string))
log.write('{}\n'.format(print_string))
log.flush()
def save_checkpoint(state, is_best, save_path, filename):
filename = os.path.join(save_path, filename)
torch.save(state, filename)
if is_best:
bestname = os.path.join(save_path, 'model_best.pth.tar')
shutil.copyfile(filename, bestname)
def adjust_learning_rate(optimizer, epoch, gammas, schedule):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
lr = args.learning_rate
assert len(gammas) == len(schedule), "length of gammas and schedule should be equal"
for (gamma, step) in zip(gammas, schedule):
if (epoch >= step):
lr = lr * gamma
else:
break
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def cos_learning_rate(optimizer, epoch, epochs):
if epoch <= epochs-40 and epoch > 60:
min_lr = args.learning_rate * 0.001
tmp_lr = min_lr + 0.5*(args.learning_rate-min_lr)*(1+math.cos(math.pi*(epoch-60)*1./\
(epochs-60-40)))
elif epoch > epochs-40:
tmp_lr = args.learning_rate * 0.001
else:
tmp_lr = args.learning_rate
for param_group in optimizer.param_groups:
param_group['lr'] = tmp_lr
return tmp_lr
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def save_obj(obj, name):
with open('obj/' + name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name):
with open('obj/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
class Mask:
def __init__(self, model):
self.model_size = {}
self.model_length = {}
self.distance_rate = {}
self.model = model
self.mask_index = []
self.filter_small_index = {}
self.filter_large_index = {}
self.similar_matrix = {}
self.norm_matrix = {}
def get_filter_kmeans(self, weight_torch, distance_rate, length, num_clusters):
codebook = np.ones(length) # length = Ni+1*Ni*k*k
label_sum = [] # num_filters for each cluster
if len(weight_torch.size()) == 4:
similar_pruned_num = []
scale = []
weight_vec = weight_torch.view(weight_torch.size()[0], -1)
# num_clusters = int(weight_vec.size()[0]/num_clusters)
# KMeans to cluster filters
kmeans = KMeans(n_clusters=num_clusters, max_iter=300).fit(weight_vec.cpu().numpy())
centroids = torch.from_numpy(kmeans.cluster_centers_) # return torch.size(n_cluster, N_i*k*k)
labels = kmeans.labels_ # return size=N_i+1
# get a list for how many filters in each cluster, define prune num in each cluster
for i in range(num_clusters):
label_sum.append(sum(labels==i))
similar_pruned_num.append(int(label_sum[i]* distance_rate))
# for distance similar: get the filter index with largest similarity == small distance to centroids
for i in range(num_clusters):
weight_sub = weight_vec.cpu()[labels==i] # size=N_i+1*cluster_num
weight_index = np.where(labels==i)[0] # get index of weight_sub in weight_vec
norm2_np = torch.norm(centroids[i]-weight_sub,2,1).numpy() # using broadcast
filter_large_index = norm2_np.argsort()[similar_pruned_num[i]:]
filter_small_index = norm2_np.argsort()[:similar_pruned_num[i]]
if i != 0 :
kmeans_index_for_filter = np.append(kmeans_index_for_filter, weight_index[filter_small_index])
else:
kmeans_index_for_filter = weight_index[filter_small_index]
# assert pruning ratio
if sum(similar_pruned_num) < sum(label_sum)*distance_rate:
norm_prune_num = int((sum(label_sum)*distance_rate - sum(similar_pruned_num)))
left_index = [x for x in np.array(range(weight_vec.size()[0])) if x not in kmeans_index_for_filter]
weight_sub_1 = weight_vec.cpu()[left_index]
norm2_np = torch.norm(weight_sub_1,2,1).numpy()
norm_small_index = norm2_np.argsort()[:norm_prune_num]
norm_index_for_filter = np.array(left_index)[norm_small_index]
kernel_length = weight_torch.size()[1] * weight_torch.size()[2] * weight_torch.size()[3]
codebook = torch.FloatTensor(codebook.reshape(sum(label_sum),kernel_length))
codebook[kmeans_index_for_filter,:] = 0
codebook[norm_index_for_filter,:] = 0
codebook = codebook.view(length).numpy()
print("kmeans index done")
else:
pass
return codebook, weight_torch, labels, num_clusters, scale
def convert2tensor(self, x):
x = torch.FloatTensor(x)
return x
def init_length(self):
for index, item in enumerate(self.model.parameters()):
self.model_size[index] = item.size()
for index1 in self.model_size:
for index2 in range(0, len(self.model_size[index1])):
if index2 == 0:
self.model_length[index1] = self.model_size[index1][0]
else:
self.model_length[index1] *= self.model_size[index1][index2]
def init_rate(self, rate_dist_per_layer):
for index, item in enumerate(self.model.parameters()):
self.distance_rate[index] = 1
for key in range(args.layer_begin, args.layer_end + 1, args.layer_inter):
self.distance_rate[key] = rate_dist_per_layer
# different setting for different architecture
if args.arch == 'resnet20':
last_index = 57
elif args.arch == 'resnet32':
last_index = 93
elif args.arch == 'resnet56':
last_index = 165
elif args.arch == 'resnet110':
last_index = 327
# to jump the last fc layer
self.mask_index = [x for x in range(0, last_index, 3)]
# self.mask_index = [x for x in range (0,330,3)]
def init_mask(self, rate_dist_per_layer, num_clusters):
self.init_rate(rate_dist_per_layer)
for index, item in enumerate(self.model.parameters()):
if index in self.mask_index:
# mask for distance criterion
self.similar_matrix[index],weight_torch, labels, num_clusters_l, scale = self.get_filter_kmeans(item.data, self.distance_rate[index], self.model_length[index],num_clusters )
self.similar_matrix[index] = self.convert2tensor(self.similar_matrix[index])
if args.use_cuda:
self.similar_matrix[index] = self.similar_matrix[index].cuda()
print("mask Ready")
def do_similar_mask(self):
for index, item in enumerate(self.model.parameters()):
if index in self.mask_index:
a = item.data.view(self.model_length[index])
b = a * self.similar_matrix[index]
item.data = b.view(self.model_size[index])
print("mask similar Done")
def do_grad_mask(self):
for index, item in enumerate(self.model.parameters()):
if index in self.mask_index:
a = item.grad.data.view(self.model_length[index])
# reverse the mask of model
b = a * self.similar_matrix[index]
item.grad.data = b.view(self.model_size[index])
# print("grad zero Done")
def if_zero(self):
for index, item in enumerate(self.model.parameters()):
if (index in self.mask_index):
# if index == 0:
a = item.data.view(self.model_length[index])
b = a.cpu().numpy()
print(
"number of nonzero weight is %d, zero is %d" % (np.count_nonzero(b), len(b) - np.count_nonzero(b)))
if __name__ == '__main__':
main()