-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
282 lines (239 loc) · 12.3 KB
/
main.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
import os
import time
import _pickle as pickle# to serialize objects
import gzip # to decompress
import argparse
import numpy as np
from termcolor import colored
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
import torch.backends.cudnn as cudnn
from Pruning import PruneNetwork
from Sharing import SharingWeightsNetwork
from deep_utils import * # CudaPytorchunique, CountAllWeights, CountZeroWeights, SumAllWeights
from models import *
from learning import *
import pdb
# lr1 = '0.05,0.025,0.025,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.005,0.005,0.005,0.0025,0.0025,0.001,0.0001,0.0001'
# lr2 = '0.00001,0.00001,0.0001,0.0001, 0.005,0.005,0.005 , 0.001,0.001,0.001,0.001'
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--lr1', default='0.02,0.02,0.015,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.005,0.005,0.005,0.0025,0.0025,0.001,0.0001,0.0001', type=str, help='learning rate for Stage 1')
parser.add_argument('--lr2', default='0.00001,0.00001,0.0001,0.0001, 0.005,0.005,0.005 , 0.001,0.001,0.001,0.001', type=str, help='learning rate for Stage 2')
parser.add_argument('--lrsch_1', default='4,11,17,22', type=str, help='learning rate schedule for Stage 1')
parser.add_argument('--lrsch_2', default='8,13,18,23', type=str, help='learning rate schedule for Stage 2')
parser.add_argument('--lcluster', default='4,5,7,9, 11,13,15, 18,20,25,35', type=str, help='cluster list for Stage 2')
parser.add_argument('--cluster_method', default='GMM', type=str, help='clustering method used for Stage 2 - available methods are: kmeans, GMM, GMM-kmeans')
parser.add_argument('--factor', default=0.2, type=float, help='Scale factor for optimizer scheduler')
parser.add_argument('--epoch', default=25, type=int, help='number of epochs per pruning iteration')
parser.add_argument('--iter', default=25, type=int, help='number of iterations for pruning')
parser.add_argument('--apply_pruning', default=False, type=bool, help='apply pruning stage')
parser.add_argument('--apply_sharing', default=True, type=bool, help='apply sharing stage')
parser.add_argument('--gpus', default=None, type=int, help='Use more gpus?')
# clusters_list = [4,5,7,9, 11,13,15, 18,20,25,35]
# list_lr = [0.00001, 0.00001, 0.0001, 0.0001, 0.00005, 0.00005, 0.00005, 0.0001, 0.0001, 0.0001, 0.0001]
# clusters_list = [35]
# list_lr = [0.00005]
start_iter = 0
args = parser.parse_args()
iterations = args.iter
Apply_Pruning = args.apply_pruning
Apply_Sharing = args.apply_sharing
cluster_method = args.cluster_method
list_lr1 = [float(i) for i in args.lr1.split(',')]
list_lr2 = [float(i) for i in args.lr2.split(',')]
schedule1 = [int(i) for i in args.lrsch_1.split(',')]
schedule2 = [int(i) for i in args.lrsch_2.split(',')]
clusters_list = [int(i) for i in args.lcluster.split(',')]
factor = args.factor
# torch.nn.Module.dump_patches = True
best_acc = 0 # best test accuracy
start_epoch = 0 # start from epoch 0 or last checkpoint epoch
print("learning rate:", list_lr1[0])
if len(list_lr1) < iterations:
raise Exception('lenght of the learning rate list must be equal to iteration numbers')
###############################################
################################################ Data and model name
###############################################
"""
A logger strategy is strongly needed =( . The future experiments have the research logger available in:
https://github.com/emedinac/Research-purpose_Logger
"""
original_namemodel='VGG19_base_10'
description = ['VGG for CIFAR10 database \n',
'optim.SGD(net.parameters(), lr=lr, momentum=0.9,) \n',
'run main.py \n',
'no L2, using data augmentation ',
]
# namemodel = 'VGG16'
# description = 'Model employed for Deep Compression'
###############################################
###############################################
###############################################
def DeepCompression_FirstStage(net):
global best_acc, namemodel
accuracies = []
total_weights, zero_weights = [], []
# Initial weights and accuracy of the model.
test_loss , test_acc = env.test(0)
accuracies.append(best_acc)
print('Current Network Accuracy: ', colored(test_acc,'blue'))
print(colored('Before Pruning','green'))
all_parameters,b,c = CountZeroWeights(net)
SumAllWeights(net)
total_weights.append(b)
zero_weights.append(c)
tr_loss = []; tr_acc = []; te_loss = []; te_acc = [];
for it in range(start_iter+1,iterations):
namemodel = original_namemodel+'_Pruned'+str(it).zfill(2)
print('=========================')
print("This will be the next file: ", namemodel)
print('=========================')
print(colored('Iteration: ','green'), colored(it,'green'))
lr = list_lr1[it]
env.lr(lr)
print(colored('Pruning network',"yellow"))
net = PruneNetwork(net) # Pruning magic is performed inside 8-)
net.Prune()
env.InsertNet(net)
print(colored('After Pruning','green'))
a,b,c = CountZeroWeights(env.net)
SumAllWeights(env.net)
test_loss , test_acc = env.test(0)
print('Accuracy: ', colored(test_acc,'red'))
total_weights.append(b)
zero_weights.append(c)
env.best_acc = 0
env.printlr()
for epoch in range(start_epoch, start_epoch+args.epoch):
if epoch in schedule1:
env.scheduler()
train_loss, train_acc = env.train(epoch,'Pruning')
test_loss , test_acc = env.test(epoch, namemodel)
tr_loss.append(train_loss); tr_acc.append(train_acc); te_loss.append(test_loss); te_acc.append(test_acc)
accuracies.append(env.best_acc) # save accuracy list
net = Load_model(namemodel)
CountZeroWeights(net)
SumAllWeights(net)
train_loss = np.array(tr_loss)[:,None]
train_acc = np.array(tr_acc)[:,None]
test_loss = np.array(te_loss)[:,None]
test_acc = np.array(te_acc)[:,None]
net = Load_model(namemodel)
print(colored('After Training','green'))
test_loss , test_acc = env.test(epoch, namemodel)
CountZeroWeights(net)
SumAllWeights(net)
return net, (train_loss, train_acc, test_loss, test_acc, description, accuracies, all_parameters, total_weights, zero_weights)
def DeepCompression_SecondStage(net):
global namemodel
# Initial weights and accuracy of the model.
accuracies = []
total_non_weights, unique_weights = [], []
accuracies.append(env.best_acc)
all_parameters,b,c = CountAllWeights(net)
total_non_weights.append(b)
unique_weights.append(c)
# schedule = [8,18]
tr_loss = []; tr_acc = []; te_loss = []; te_acc = [];
for lr_i, k in enumerate(clusters_list):
namemodel = original_namemodel+'_Shared'+str(k).zfill(2)
print('=========================')
print('=========================')
print(colored('Cluster: ','green'), colored(k,'green'))
lr = list_lr2[lr_i]
env.lr(lr)
print('Sharing weights in network')
net = SharingWeightsNetwork(net,cluster=k) # Sharing magic is performed inside 8-)
net.Sharing(cluster_method) # Method 1
env.InsertNet(net)
print(colored('After Sharing weights','green'))
a,b,c = CountAllWeights(env.net)
test_loss , test_acc = env.test(net, 0)
print('Accuracy: ', colored(test_acc,'red'))
total_non_weights.append(b)
unique_weights.append(c)
env.best_acc = 0
for epoch in range(start_epoch, start_epoch+args.epoch):
if epoch in schedule2:
env.scheduler()
train_loss, train_acc = env.train(epoch,'WeightSharing')
test_loss , test_acc = env.test(epoch, namemodel)
tr_loss.append(train_loss); tr_acc.append(train_acc); te_loss.append(test_loss); te_acc.append(test_acc)
accuracies.append(env.best_acc) # save accuracy list
namemodel = original_namemodel+'_Pruned'+str(iterations-1).zfill(2) # Last prunned network
net = Load_model(namemodel)
CountAllWeights(net)
SumAllWeights(net)
train_loss = np.array(tr_loss)[:,None]
train_acc = np.array(tr_acc)[:,None]
test_loss = np.array(te_loss)[:,None]
test_acc = np.array(te_acc)[:,None]
return net, (train_loss, train_acc, test_loss, test_acc, description, accuracies, all_parameters, total_non_weights, unique_weights)
# Load checkpoint.
def Load_model(namemodel):
global net_acc
assert os.path.isdir('./checkpoint/'), 'Error: no checkpoint directory found!'
checkpoint = torch.load('./checkpoint/'+namemodel+'.t7')
# net = models.CustomVGG('6ls3',[16,8,4,2,1])
net = VGG('VGG19',10).cuda()
print(colored('==> Resuming from checkpoint..'+str(namemodel),"green"))
net.load_state_dict(checkpoint['net']) # net is the state_dict
net_acc = checkpoint['acc']
if args.gpus: net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
cudnn.benchmark = True
return net
#################################################
################################################# MAIN
#################################################
if __name__ == "__main__":
# Load Model
namemodel = original_namemodel
if start_iter: net = Load_model(namemodel+'_Pruned'+str(start_iter).zfill(2))
else: net = Load_model(namemodel)
# Setting ENV #
DATA = 'CIFAR10'
optimizer = optim.SGD(net.parameters(), lr=0, momentum=0.9,) # LR can be set with any value. It is adapted in each network training
env = LearningProcess(DATA, nn.CrossEntropyLoss(), optimizer, gpus=args.gpus)
env.schedule_factor = factor
# For some models, not important, but useful to understand why Batch normalization goes to INF in the Running_Var (ref: BN paper)
# no_inf = net._modules.get('module')._modules.get('features')._modules.get('8')
# no_inf.running_var[no_inf.running_var==np.inf] = 1e5 # for INF problems during training. (in case model overfits)
print(colored('Original Model','green'))
env.InsertNet(net)
test_loss , test_acc = env.test(0)
CountZeroWeights(env.net)
if Apply_Pruning: # Apply or load...
print(colored('\n\nFisrt Stage: \n\n','green'))
net, Variables_to_save = DeepCompression_FirstStage(net)
train_loss, train_acc, test_loss, test_acc, description, accuracies, all_parameters, total_weights, zero_weights = Variables_to_save
pickle.dump(Variables_to_save, gzip.open('./checkpoint/'+namemodel+'.p.gz', 'wb'))
else:
print(colored('\n\nLoading Pruning Stage: \n\n','green'))
Variables_to_save = pickle.load(gzip.open('./checkpoint/'+namemodel+'_Pruned'+str(iterations-1).zfill(2)+'.p.gz', 'rb'))
train_loss, train_acc, test_loss, test_acc, description, accuracies, all_parameters, total_weights, zero_weights = Variables_to_save
namemodel = original_namemodel+'_Pruned'+str(iterations-1).zfill(2)
net = Load_model(namemodel)
print(colored('After Pruning','green'))
test_loss , test_acc = env.test(net, 0)
print(test_loss , test_acc)
CountZeroWeights(net)
pdb.set_trace() ########################################################################################################
if Apply_Sharing: # Apply or load...
print(colored('\n\nSecond Stage: \n\n','green'))
net, Variables_to_save = DeepCompression_SecondStage(net)
train_loss, train_acc, test_loss, test_acc, description, accuracies, all_parameters, total_non_weights, unique_weights = Variables_to_save
pickle.dump(Variables_to_save, gzip.open('./checkpoint/Sharing_'+namemodel+'.p.gz', 'wb'))
else:
print(colored('\n\nLoading Sharing Stage: \n\n','green'))
Variables_to_save = pickle.load(gzip.open('./checkpoint/Sharing_'+namemodel+'.p.gz', 'rb'))
train_loss, train_acc, test_loss, test_acc, description, accuracies, all_parameters, total_non_weights, unique_weights = Variables_to_save
namemodel = original_namemodel+'_Shared'+str(clusters_list[-1]).zfill(2)
net = Load_model(namemodel)
env.InsertNet(net)
print(colored('After Training','green'))
test_loss , test_acc = env.test(0)
CountAllWeights(net)