forked from OPTML-Group/Unlearn-Sparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_imp_ffcv.py
213 lines (173 loc) · 7.62 KB
/
main_imp_ffcv.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
'''
main process for a Lottery Tickets experiments
'''
import os
import pdb
import time
import pickle
import random
import shutil
import argparse
import numpy as np
from copy import deepcopy
import matplotlib.pyplot as plt
import torch
import torch.optim
import torch.nn as nn
import torch.utils.data
import torch.nn.functional as F
import torchvision.models as models
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data.sampler import SubsetRandomSampler
from utils import NormalizeByChannelMeanStd
from trainer import train, validate
from utils import *
from pruner import *
import arg_parser
best_sa = 0
def main():
global args, best_sa
args = arg_parser.parse_args()
print(args)
torch.cuda.set_device(int(args.gpu))
os.makedirs(args.save_dir, exist_ok=True)
if args.seed:
setup_seed(args.seed)
# prepare dataset
model, train_loader, val_loader, test_loader = setup_model_dataset(args)
model.cuda()
criterion = nn.CrossEntropyLoss()
decreasing_lr = list(map(int, args.decreasing_lr.split(',')))
if args.prune_type == 'lt':
print('lottery tickets setting (rewind to the same random init)')
initalization = deepcopy(model.state_dict())
elif args.prune_type == 'pt':
print('lottery tickets from best dense weight')
initalization = None
elif args.prune_type == 'rewind_lt':
print('lottery tickets with early weight rewinding')
initalization = None
else:
raise ValueError('unknown prune_type')
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=decreasing_lr, gamma=0.1) # 0.1 is fixed
if args.resume:
print('resume from checkpoint {}'.format(args.checkpoint))
checkpoint = torch.load(args.checkpoint, map_location = torch.device('cuda:'+str(args.gpu)))
best_sa = checkpoint['best_sa']
start_epoch = checkpoint['epoch']
all_result = checkpoint['result']
start_state = checkpoint['state']
if start_state>0:
current_mask = extract_mask(checkpoint['state_dict'])
prune_model_custom(model, current_mask)
check_sparsity(model)
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=decreasing_lr, gamma=0.1)
model.load_state_dict(checkpoint['state_dict'], strict=False)
# adding an extra forward process to enable the masks
x_rand = torch.rand(1,3,args.input_size, args.input_size).cuda()
model.eval()
with torch.no_grad():
model(x_rand)
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
initalization = checkpoint['init_weight']
print('loading state:', start_state)
print('loading from epoch: ',start_epoch, 'best_sa=', best_sa)
else:
all_result = {}
all_result['train_ta'] = []
all_result['test_ta'] = []
all_result['val_ta'] = []
start_epoch = 0
start_state = 0
print('######################################## Start Standard Training Iterative Pruning ########################################')
for state in range(start_state, args.pruning_times):
print('******************************************')
print('pruning state', state)
print('******************************************')
check_sparsity(model)
for epoch in range(start_epoch, args.epochs):
start_time = time.time()
print(optimizer.state_dict()['param_groups'][0]['lr'])
acc = train(train_loader, model, criterion, optimizer, epoch, args)
if state == 0:
if (epoch+1) == args.rewind_epoch:
torch.save(model.state_dict(), os.path.join(args.save_dir, 'epoch_{}_rewind_weight.pt'.format(epoch+1)))
if args.prune_type == 'rewind_lt':
initalization = deepcopy(model.state_dict())
# evaluate on validation set
tacc = validate(val_loader, model, criterion, args)
# evaluate on test set
test_tacc = validate(test_loader, model, criterion, args)
scheduler.step()
all_result['train_ta'].append(acc)
all_result['val_ta'].append(tacc)
all_result['test_ta'].append(test_tacc)
# remember best prec@1 and save checkpoint
is_best_sa = tacc > best_sa
best_sa = max(tacc, best_sa)
save_checkpoint({
'state': state,
'result': all_result,
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_sa': best_sa,
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'init_weight': initalization
}, is_SA_best=is_best_sa, pruning=state, save_path=args.save_dir)
# plot training curve
plt.plot(all_result['train_ta'], label='train_acc')
plt.plot(all_result['val_ta'], label='val_acc')
plt.plot(all_result['test_ta'], label='test_acc')
plt.legend()
plt.savefig(os.path.join(args.save_dir, str(state)+'net_train.png'))
plt.close()
print("one epoch duration:{}".format(time.time()-start_time))
#report result
check_sparsity(model)
print("Performance on the test data set")
test_tacc = validate(test_loader, model, criterion, args)
if len(all_result['val_ta'])!=0:
val_pick_best_epoch = np.argmax(np.array(all_result['val_ta']))
print('* best SA = {}, Epoch = {}'.format(all_result['test_ta'][val_pick_best_epoch], val_pick_best_epoch+1))
all_result = {}
all_result['train_ta'] = []
all_result['test_ta'] = []
all_result['val_ta'] = []
best_sa = 0
start_epoch = 0
if args.prune_type == 'pt':
print('* loading pretrained weight')
initalization = torch.load(os.path.join(args.save_dir, '0model_SA_best.pth.tar'), map_location = torch.device('cuda:'+str(args.gpu)))['state_dict']
#pruning and rewind
if args.random_prune:
print('random pruning')
pruning_model_random(model, args.rate)
else:
print('L1 pruning')
pruning_model(model, args.rate)
remain_weight = check_sparsity(model)
current_mask = extract_mask(model.state_dict())
remove_prune(model)
# weight rewinding
model.load_state_dict(initalization, strict=False) # rewind, initialization is a full model architecture without masks
prune_model_custom(model, current_mask)
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=decreasing_lr, gamma=0.1)
if args.rewind_epoch:
# learning rate rewinding
for _ in range(args.rewind_epoch):
scheduler.step()
if __name__ == '__main__':
main()