-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_train.py
300 lines (231 loc) · 9.62 KB
/
main_train.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
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 advertorch.utils import NormalizeByChannelMeanStd
from utils.pruner import *
from utils.setup import *
parser = argparse.ArgumentParser(description='PyTorch Evaluation Tickets')
##################################### general setting #################################################
parser.add_argument('--data', type=str, default='../data', help='location of the data corpus')
parser.add_argument('--dataset', type=str, default='cifar10', help='dataset')
parser.add_argument('--arch', type=str, default='res20s', help='model architecture')
parser.add_argument('--split_file', type=str, default=None, help='dataset index')
parser.add_argument('--seed', default=None, type=int, help='random seed')
parser.add_argument('--save_dir', help='The directory used to save the trained models', default=None, type=str)
parser.add_argument('--gpu', type=int, default=0, help='gpu device id')
parser.add_argument('--save_model', action="store_true", help="whether to save model")
##################################### training setting #################################################
parser.add_argument('--batch_size', type=int, default=128, help='batch size')
parser.add_argument('--lr', default=0.1, type=float, help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float, help='momentum')
parser.add_argument('--weight_decay', default=1e-4, type=float, help='weight decay')
parser.add_argument('--epochs', default=182, type=int, help='number of total epochs to run')
parser.add_argument('--warmup', default=0, type=int, help='warm up epochs')
parser.add_argument('--print_freq', default=50, type=int, help='print frequency')
parser.add_argument('--decreasing_lr', default='91,136', help='decreasing strategy')
##################################### Pruning setting #################################################
parser.add_argument('--init_dir', default=None, type=str, help='init weight')
parser.add_argument('--mask_dir', default=None, type=str, help='mask direction for ticket')
best_sa = 0
def main():
global args, best_sa
args = 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()
#loading tickets
load_ticket(model, args)
criterion = nn.CrossEntropyLoss()
decreasing_lr = list(map(int, args.decreasing_lr.split(',')))
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)
all_result = {}
all_result['train'] = []
all_result['test_ta'] = []
all_result['ta'] = []
start_epoch = 0
print(model.normalize)
remain_weight = check_sparsity(model)
for epoch in range(start_epoch, args.epochs):
print(optimizer.state_dict()['param_groups'][0]['lr'])
acc = train(train_loader, model, criterion, optimizer, epoch)
# evaluate on validation set
tacc = validate(val_loader, model, criterion)
# evaluate on test set
test_tacc = validate(test_loader, model, criterion)
scheduler.step()
all_result['train'].append(acc)
all_result['ta'].append(tacc)
all_result['test_ta'].append(test_tacc)
all_result['remain_weight'] = remain_weight
# remember best prec@1 and save checkpoint
is_best_sa = tacc > best_sa
best_sa = max(tacc, best_sa)
if args.save_model:
save_checkpoint({
'result': all_result,
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_sa': best_sa,
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict()
}, is_SA_best=is_best_sa, save_path=args.save_dir)
else:
save_checkpoint({
'result': all_result
}, is_SA_best=False, save_path=args.save_dir)
plt.plot(all_result['train'], label='train_acc')
plt.plot(all_result['ta'], label='val_acc')
plt.plot(all_result['test_ta'], label='test_acc')
plt.legend()
plt.savefig(os.path.join(args.save_dir, 'net_train.png'))
plt.close()
check_sparsity(model)
print('* best SA={}'.format(all_result['test_ta'][np.argmax(np.array(all_result['ta']))]))
def train(train_loader, model, criterion, optimizer, epoch):
losses = AverageMeter()
top1 = AverageMeter()
# switch to train mode
model.train()
start = time.time()
for i, (image, target) in enumerate(train_loader):
if epoch < args.warmup:
warmup_lr(epoch, i+1, optimizer, one_epoch_step=len(train_loader))
image = image.cuda()
target = target.cuda()
# compute output
output_clean = model(image)
loss = criterion(output_clean, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
output = output_clean.float()
loss = loss.float()
# measure accuracy and record loss
prec1 = accuracy(output.data, target)[0]
losses.update(loss.item(), image.size(0))
top1.update(prec1.item(), image.size(0))
if i % args.print_freq == 0:
end = time.time()
print('Epoch: [{0}][{1}/{2}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Accuracy {top1.val:.3f} ({top1.avg:.3f})\t'
'Time {3:.2f}'.format(
epoch, i, len(train_loader), end-start, loss=losses, top1=top1))
start = time.time()
print('train_accuracy {top1.avg:.3f}'.format(top1=top1))
return top1.avg
def validate(val_loader, model, criterion):
"""
Run evaluation
"""
losses = AverageMeter()
top1 = AverageMeter()
# switch to evaluate mode
model.eval()
for i, (image, target) in enumerate(val_loader):
image = image.cuda()
target = target.cuda()
# compute output
with torch.no_grad():
output = model(image)
loss = criterion(output, target)
output = output.float()
loss = loss.float()
# measure accuracy and record loss
prec1 = accuracy(output.data, target)[0]
losses.update(loss.item(), image.size(0))
top1.update(prec1.item(), image.size(0))
if i % args.print_freq == 0:
print('Test: [{0}/{1}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Accuracy {top1.val:.3f} ({top1.avg:.3f})'.format(
i, len(val_loader), loss=losses, top1=top1))
print('valid_accuracy {top1.avg:.3f}'
.format(top1=top1))
return top1.avg
def save_checkpoint(state, is_SA_best, save_path, filename='checkpoint.pth.tar'):
filepath = os.path.join(save_path, filename)
torch.save(state, filepath)
if is_SA_best:
shutil.copyfile(filepath, os.path.join(save_path, 'model_SA_best.pth.tar'))
def load_ticket(model, args):
# weight
if args.init_dir:
initalization = torch.load(args.init_dir, map_location = torch.device('cuda:'+str(args.gpu)))
if 'init_weight' in initalization.keys():
initalization = initalization['init_weight']
model.load_state_dict(initalization)
# mask
if args.mask_dir:
current_mask_weight = torch.load(args.mask_dir, map_location = torch.device('cuda:'+str(args.gpu)))
if 'state_dict' in current_mask_weight.keys():
current_mask_weight = current_mask_weight['state_dict']
current_mask = extract_mask(current_mask_weight)
prune_model_custom(model, current_mask)
check_sparsity(model)
def warmup_lr(epoch, step, optimizer, one_epoch_step):
overall_steps = args.warmup*one_epoch_step
current_steps = epoch*one_epoch_step + step
lr = args.lr * current_steps/overall_steps
lr = min(lr, args.lr)
for p in optimizer.param_groups:
p['lr']=lr
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
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 setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
if __name__ == '__main__':
main()