-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
287 lines (264 loc) · 12.1 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
279
280
281
282
283
284
285
286
287
import os
import time
import math
import random
import shutil
import logging
import torch
import torch.nn as nn
import models
import numpy as np
from options import parser
from collections import OrderedDict
from dataloader import getDataLoader
from utils import *
from regularization import *
args = parser.parse_args()
state = {k: v for k, v in args._get_kwargs()}
print('Parameters:')
for key, value in state.items():
print(' {key} : {value}'.format(key=key, value=value))
# Use CUDA
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
use_cuda = torch.cuda.is_available()
# Random seed
if args.manualSeed is None:
args.manualSeed = random.randint(1, 10000)
random.seed(args.manualSeed)
torch.manual_seed(args.manualSeed)
np.random.seed(args.manualSeed)
if use_cuda:
torch.cuda.manual_seed_all(args.manualSeed)
torch.backends.cudnn.deterministic = True
best_acc = 0 # best test accuracy
# Get loggers and save the config information
train_log, test_log, checkpoint_dir, log_dir = get_loggers(args)
def main():
global best_acc, train_log, test_log, checkpoint_dir, log_dir
# create model
logging.info("=" * 89)
logging.info("=> creating model '{}'".format(args.arch))
model = models.get_model(pretrained=args.pretrained, dataset = args.dataset,
arch = args.arch, bias=args.bias)
# define loss function (criterion) and optimizer
criterion = Loss()
model.set_criterion(criterion)
# Data loader
trainloader, testloader = getDataLoader(args.data, args.dataset, args.batch_size,
args.workers)
# to cuda
if torch.cuda.is_available() and args.gpu_id != -1:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id
model = torch.nn.DataParallel(model).cuda()
logging.info('=> running the model on gpu{}.'.format(args.gpu_id))
else:
logging.info('=> running the model on cpu.')
# define optimizer
param_dict = dict(model.named_parameters())
params = []
BN_name_pool = []
for m_name, m in model.named_modules():
if isinstance(m, nn.BatchNorm2d):
BN_name_pool.append(m_name + '.weight')
BN_name_pool.append(m_name + '.bias')
for key, value in param_dict.items():
if (key in BN_name_pool and 'mobilenet' in args.arch) or 'mask' in key:
params += [{'params': [value], 'lr': args.learning_rate, 'weight_decay': 0.}]
else:
params += [{'params':[value]}]
optimizer = torch.optim.SGD(params, lr=args.learning_rate,weight_decay=args.weight_decay,
momentum=args.momentum, nesterov=True)
p_anneal = ExpAnnealing(0, 1, 0, alpha=args.alpha)
# ready
logging.info("=" * 89)
# Evaluate
if args.evaluate:
logging.info('Evaluate model')
top1, top5 = validate(testloader, model, criterion, 0, use_cuda,
(args.lbda, 0), args.den_target)
logging.info('Test Acc (Top-1): %.2f, Test Acc (Top-5): %.2f' % (top1, top5))
return
# training
logging.info('\n Train for {} epochs'.format(args.epochs))
train_process(model, args.epochs, testloader, trainloader, criterion, optimizer,
use_cuda, args.lbda, args.gamma, p_anneal, checkpoint_dir, args.den_target)
train_log.close()
test_log.close()
logging.info('Best acc: {}'.format(best_acc))
return
def train_process(model, total_epochs, testloader, trainloader, criterion, optimizer,
use_cuda, lbda, gamma, p_anneal, checkpoint_dir, den_target):
global best_acc
for epoch in range(total_epochs):
p = p_anneal.get_lr(epoch)
# get target density
state['den_target'] = den_target
# update lr
adjust_learning_rate(optimizer, epoch=epoch)
# Training
train(trainloader, model, criterion, optimizer, epoch, use_cuda, (lbda, gamma),
den_target, p)
test_acc, _ = validate(testloader, model, criterion, epoch, use_cuda,
(lbda, gamma), den_target, p=p)
# save checkpoint
if checkpoint_dir is not None:
is_best = test_acc > best_acc
best_acc = max(test_acc, best_acc)
model_dict = model.module.state_dict() if use_cuda else model.state_dict()
save_checkpoint(
{
'epoch': epoch + 1,
'state_dict': model_dict,
'acc': test_acc,
'best_acc': best_acc,
'optimizer': optimizer.state_dict()
},
is_best=is_best,
checkpoint_dir=checkpoint_dir)
return
def train(train_loader, model, criterion, optimizer, epoch, use_cuda, param,
den_target, p):
lbda, gamma = param
# switch to train mode
model.train()
logging.info("=" * 89)
batch_time, data_time, closses, rlosses, blosses, losses, top1, top5 = getAvgMeter(8)
end = time.time()
bar = Bar('Processing', max=len(train_loader))
for batch_idx, (x, targets) in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
# get inputs
if use_cuda:
x, targets = x.cuda(), targets.cuda()
x, targets = torch.autograd.Variable(x), torch.autograd.Variable(targets)
batch_size = x.size(0)
# inference
inputs = {"x": x, "label": targets, "den_target": den_target, "lbda": lbda,
"gamma": gamma, "p": p}
outputs= model(**inputs)
loss = outputs["closs"].mean() + outputs["rloss"].mean() + outputs["bloss"].mean()
# measure accuracy and record loss
prec1, prec5 = accuracy(outputs["out"].data, targets.data, topk=(1, 5))
closses.update(outputs["closs"].mean().item(), batch_size)
rlosses.update(outputs["rloss"].mean().item(), batch_size)
blosses.update(outputs["bloss"].mean().item(), batch_size)
losses.update(loss.item(), batch_size)
top1.update(prec1.item(), batch_size)
top5.update(prec5.item(), batch_size)
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# plot progress
bar.suffix = '({batch}/{size}) Data: {data:.3f}s | Batch: {bt:.3f}s | '.format(
batch=batch_idx+1, size=len(train_loader), data=data_time.val, bt=batch_time.val,
)+'Total: {total:} | (C,R,B)Loss: {closs:.2f}, {rloss:.2f}, {bloss:.2f}'.format(
total=bar.elapsed_td, closs=closses.avg, rloss=rlosses.avg, bloss=blosses.avg,
)+' | Loss: {loss:.2f} | top1: {top1:.2f} | top5: {top5:.2f}'.format(top1=top1.avg,
top5=top5.avg, loss=losses.avg)
bar.next()
bar.finish()
train_log.write(content="{epoch}\t{top1.avg:.4f}\t{top5.avg:.4f}\t{loss.avg:.4f}\t"
"{closs.avg:.4f}\t{rloss.avg:.4f}\t{bloss.avg:.4f}".format(
epoch=epoch, top1=top1, top5=top5,loss=losses, closs=closses,
rloss=rlosses, bloss=blosses),
wrap=True, flush=True)
return
def validate(val_loader, model, criterion, epoch, use_cuda, param, den_target, p=0):
global log_dir
lbda, gamma = param
# switch to evaluate mode
model.eval()
logging.info("=" * 89)
(batch_time, data_time, closses, rlosses, blosses, losses,
top1, top5, block_flops)= getAvgMeter(9)
with torch.no_grad():
end = time.time()
bar = Bar('Processing', max=len(val_loader))
for batch_idx, (x, targets) in enumerate(val_loader):
# measure data loading time
data_time.update(time.time() - end)
# get inputs
if use_cuda:
x, targets = x.cuda(), targets.cuda(non_blocking=True)
x, targets = torch.autograd.Variable(x), torch.autograd.Variable(targets)
batch_size = x.size(0)
# inference
inputs = {"x": x, "label": targets, "den_target": den_target, "lbda": lbda,
"gamma": gamma, "p": p}
outputs= model(**inputs)
loss = outputs["closs"].mean() + outputs["rloss"].mean() + outputs["bloss"].mean()
# measure accuracy and record loss
prec1, prec5 = accuracy(outputs["out"].data, targets.data, topk=(1, 5))
closses.update(outputs["closs"].mean().item(), batch_size)
rlosses.update(outputs["rloss"].mean().item(), batch_size)
blosses.update(outputs["bloss"].mean().item(), batch_size)
losses.update(loss.item(), batch_size)
top1.update(prec1.item(), batch_size)
top5.update(prec5.item(), batch_size)
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# get flops
flops_real = outputs["flops_real"]
flops_mask = outputs["flops_mask"]
flops_ori = outputs["flops_ori"]
flops_conv, flops_mask, flops_ori, flops_conv1, flops_fc = analyse_flops(
flops_real, flops_mask, flops_ori, batch_size)
block_flops.update(flops_conv, batch_size)
# plot progress
bar.suffix = '({batch}/{size}) Batch: {bt:.3f}s | Total: {total:}'.format(
batch=batch_idx+1, size=len(val_loader), bt=batch_time.avg, total=bar.elapsed_td
)+' | (C,R,B)Loss: {closs:.2f}, {rloss:.2f}, {bloss:.2f}'.format(
closs=closses.avg, rloss=rlosses.avg, bloss=blosses.avg,
)+' | Loss: {loss:.2f} | top1: {top1:.2f} | top5: {top5:.2f}'.format(
top1=top1.avg, top5=top5.avg, loss=losses.avg)
bar.next()
bar.finish()
# log
if use_cuda:
model.module.record_flops(block_flops.avg, flops_mask, flops_ori, flops_conv1, flops_fc)
else:
model.record_flops(block_flops.avg, flops_mask, flops_ori, flops_conv1, flops_fc)
flops = (block_flops.avg[-1]+flops_mask[-1]+flops_conv1.mean()+flops_fc.mean())/1024
flops_per = (block_flops.avg[-1]+flops_mask[-1]+flops_conv1.mean()+flops_fc.mean())/(
flops_ori[-1]+flops_conv1.mean()+flops_fc.mean())*100
test_log.write(content="{epoch}\t{top1.avg:.4f}\t{top5.avg:.4f}\t{loss.avg:.4f}\t"
"{closs.avg:.4f}\t{rloss.avg:.4f}\t{bloss.avg:.4f}\t"
"{flops_per:.2f}%\t{flops:.2f}K\t".format(epoch=epoch, top1=top1,
top5=top5, loss=losses, closs=closses, rloss=rlosses,
bloss=blosses, flops_per=flops_per, flops=flops),
wrap=True, flush=True)
return (top1.avg, top5.avg)
def getAvgMeter(num):
return [AverageMeter() for _ in range(num)]
def adjust_learning_rate(optimizer, epoch):
global state
if args.lr_mode == 'cosine':
lr = 0.5*args.learning_rate*(1+math.cos(math.pi*float(epoch)/float(args.epochs)))
state['learning_rate'] = lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
elif args.lr_mode == 'step':
if epoch in args.schedule:
state['learning_rate'] *= args.lr_decay
for param_group in optimizer.param_groups:
param_group['lr'] = state['learning_rate']
else:
raise NotImplementedError('can not support lr mode {}'.format(args.lr_mode))
logging.info("\nEpoch: {epoch:3d} | learning rate = {lr:.6f}".format(
epoch=epoch, lr=state['learning_rate']))
def save_checkpoint(state,
is_best,
filename='checkpoint.pth.tar',
checkpoint_dir='.'):
filename = os.path.join(checkpoint_dir, filename)
torch.save(state, filename, pickle_protocol=4)
if is_best:
shutil.copyfile(filename, os.path.join(checkpoint_dir, 'model_best.pth.tar'))
if __name__ == "__main__":
main()