-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
349 lines (278 loc) · 15.3 KB
/
utils.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
import time, torch
from argparse import ArgumentTypeError
from prefetch_generator import BackgroundGenerator
class WeightedSubset(torch.utils.data.Subset):
def __init__(self, dataset, indices, weights) -> None:
self.dataset = dataset
assert len(indices) == len(weights)
self.indices = indices
self.weights = weights
def __getitem__(self, idx):
if isinstance(idx, list):
return self.dataset[[self.indices[i] for i in idx]], self.weights[[i for i in idx]]
return self.dataset[self.indices[idx]], self.weights[idx]
def train(train_loader, network, criterion, optimizer, scheduler, epoch, args, rec, if_weighted: bool = False):
"""Train for one epoch on the training set"""
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
# switch to train mode
network.train()
end = time.time()
#train_features, train_labels = print(next(iter(train_loader)))
print("len trainloader:", len(train_loader))
for i, contents in enumerate(train_loader):
optimizer.zero_grad()
if if_weighted:
target = contents[0][1].to(args.device)
input = contents[0][0].to(args.device)
# Compute output
output = network(input)
weights = contents[1].to(args.device).requires_grad_(False)
loss = torch.sum(criterion(output, target) * weights) / torch.sum(weights)
else:
target = contents[1].to(args.device)
input = contents[0].to(args.device)
# Compute output
output = network(input)
loss = criterion(output, target).mean()
# Measure accuracy and record loss
prec1 = accuracy(output.data, target, topk=(1,))[0]
losses.update(loss.data.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
# Compute gradient and do SGD step
loss.backward()
optimizer.step()
scheduler.step()
# Measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format(
epoch, i, len(train_loader), batch_time=batch_time,
loss=losses, top1=top1))
record_train_stats(rec, epoch, losses.avg, top1.avg, optimizer.state_dict()['param_groups'][0]['lr'])
def test(test_loader, network, criterion, epoch, args, rec):
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
top5 = AverageMeter('Acc@5', ':6.2f')
# Switch to evaluate mode
network.eval()
network.no_grad = True
end = time.time()
for i, (input, target) in enumerate(test_loader):
target = target.to(args.device)
input = input.to(args.device)
# Compute output
with torch.no_grad():
output = network(input)
loss = criterion(output, target).mean()
# Measure accuracy and record loss
#prec1 = accuracy(output.data, target, topk=(1,))[0]
prec1, prec5 = accuracy(output.data, target, topk=(1,5))
losses.update(loss.data.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
# Measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format(
i, len(test_loader), batch_time=batch_time, loss=losses,
top1=top1))
print(' * Prec@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'.format(top1=top1, top5=top5))
network.no_grad = False
record_test_stats(rec, epoch, losses.avg, top1.avg)
return top1.avg
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
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 __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
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].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def str_to_bool(v):
# Handle boolean type in arguments.
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise ArgumentTypeError('Boolean value expected.')
def save_checkpoint(state, path, epoch, prec):
print("=> Saving checkpoint for epoch %d, with Prec@1 %f." % (epoch, prec))
torch.save(state, path)
def init_recorder():
from types import SimpleNamespace
rec = SimpleNamespace()
rec.train_step = []
rec.train_loss = []
rec.train_acc = []
rec.lr = []
rec.test_step = []
rec.test_loss = []
rec.test_acc = []
rec.ckpts = []
return rec
def record_train_stats(rec, step, loss, acc, lr):
rec.train_step.append(step)
rec.train_loss.append(loss)
rec.train_acc.append(acc)
rec.lr.append(lr)
return rec
def record_test_stats(rec, step, loss, acc):
rec.test_step.append(step)
rec.test_loss.append(loss)
rec.test_acc.append(acc)
return rec
def record_ckpt(rec, step):
rec.ckpts.append(step)
return rec
class DataLoaderX(torch.utils.data.DataLoader):
def __iter__(self):
return BackgroundGenerator(super().__iter__())
def add_sparse_args(parser):
parser.add_argument('--sparse', action='store_true', help='Enable sparse mode. Default: True.')
parser.add_argument('--fix', action='store_true', help='Fix sparse connectivity during training. Default: True.')
parser.add_argument('--sparse_init', type=str, default='ERK', help='sparse initialization')
parser.add_argument('--growth', type=str, default='random', help='Growth mode. Choose from: momentum, random, random_unfired, and gradient.')
parser.add_argument('--death', type=str, default='magnitude', help='Death mode / pruning mode. Choose from: magnitude, SET, threshold.')
parser.add_argument('--redistribution', type=str, default='none', help='Redistribution mode. Choose from: momentum, magnitude, nonzeros, or none.')
parser.add_argument('--death-rate', type=float, default=0.50, help='The pruning rate / death rate.')
parser.add_argument('--density', type=float, default=0.05, help='The density of the overall sparse network.')
parser.add_argument('--update_frequency', type=int, default=100, metavar='N', help='how many iterations to train between parameter exploration')
parser.add_argument('--decay-schedule', type=str, default='cosine', help='The decay schedule for the pruning rate. Default: cosine. Choose from: cosine, linear.')
parser.add_argument('--multiplier', type=int, default=1)
def add_general_args(parser):
# Basic arguments
parser.add_argument('--dataset', type=str, default='CIFAR10', help='dataset')
parser.add_argument('--model', type=str, default='ResNet18', help='model')
parser.add_argument('--selection', type=str, default="uniform", help="selection method")
parser.add_argument('--num_exp', type=int, default=1, help='the number of experiments')
parser.add_argument('--num_eval', type=int, default=10, help='the number of evaluating randomly initialized models')
parser.add_argument('--epochs', default=200, type=int, help='number of total epochs to run')
parser.add_argument('--data_path', type=str, default='data', help='dataset path')
parser.add_argument('--gpu', default=None, nargs="+", type=int, help='GPU id to use')
parser.add_argument('--print_freq', '-p', default=20, type=int, help='print frequency (default: 20)')
parser.add_argument('--fraction', default=0.1, type=float, help='fraction of data to be selected (default: 0.1)')
parser.add_argument('--seed', default=int(time.time() * 1000) % 100000, type=int, help="random seed")
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument("--cross", type=str, nargs="+", default=None, help="models for cross-architecture experiments")
# Optimizer and scheduler
parser.add_argument('--optimizer', default="SGD", help='optimizer to use, e.g. SGD, Adam')
parser.add_argument('--lr', type=float, default=0.1, help='learning rate for updating network parameters')
parser.add_argument('--min_lr', type=float, default=1e-4, help='minimum learning rate')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum (default: 0.9)')
parser.add_argument('-wd', '--weight_decay', default=5e-4, type=float,
metavar='W', help='weight decay (default: 5e-4)',
dest='weight_decay')
parser.add_argument("--nesterov", default=True, type=str_to_bool, help="if set nesterov")
parser.add_argument("--scheduler", default="CosineAnnealingLR", type=str, help=
"Learning rate scheduler")
parser.add_argument("--gamma", type=float, default=.5, help="Gamma value for StepLR")
parser.add_argument("--step_size", type=float, default=30, help="Step size for StepLR")
# Training
parser.add_argument('--batch', '--batch-size', "-b", default=256, type=int, metavar='N',
help='mini-batch size (default: 256)')
parser.add_argument("--train_batch", "-tb", default=None, type=int,
help="batch size for training, if not specified, it will equal to batch size in argument --batch")
parser.add_argument("--selection_batch", "-sb", default=None, type=int,
help="batch size for selection, if not specified, it will equal to batch size in argument --batch")
# Testing
parser.add_argument("--test_interval", '-ti', default=1, type=int, help=
"the number of training epochs to be preformed between two test epochs; a value of 0 means no test will be run (default: 1)")
parser.add_argument("--test_fraction", '-tf', type=float, default=1.,
help="proportion of test dataset used for evaluating the model (default: 1.)")
# Checkpoint and resumption
parser.add_argument('--save_path', "-sp", type=str, default='', help='path to save results (default: do not save)')
parser.add_argument('--resume', '-r', type=str, default='', help="path to latest checkpoint (default: do not load)")
parser.add_argument('--save_indices', action='store_true')
parser.add_argument('--indices_path', type=str, default='/work/08604/yro/maverick2/indices', help='indices path')
def add_selection_args(parser):
# Selecting
parser.add_argument("--selection_epochs", "-se", default=40, type=int,
help="number of epochs whiling performing selection on full dataset")
parser.add_argument('--selection_momentum', '-sm', default=0.9, type=float, metavar='M',
help='momentum whiling performing selection (default: 0.9)')
parser.add_argument('--selection_weight_decay', '-swd', default=5e-4, type=float,
metavar='W', help='weight decay whiling performing selection (default: 5e-4)',
dest='selection_weight_decay')
parser.add_argument('--selection_optimizer', "-so", default="SGD",
help='optimizer to use whiling performing selection, e.g. SGD, Adam')
parser.add_argument("--selection_nesterov", "-sn", default=True, type=str_to_bool,
help="if set nesterov whiling performing selection")
parser.add_argument('--selection_lr', '-slr', type=float, default=0.1, help='learning rate for selection')
parser.add_argument("--selection_test_interval", '-sti', default=1, type=int, help=
"the number of training epochs to be preformed between two test epochs during selection (default: 1)")
parser.add_argument("--selection_test_fraction", '-stf', type=float, default=1.,
help="proportion of test dataset used for evaluating the model while preforming selection (default: 1.)")
parser.add_argument('--balance', default=True, type=str_to_bool,
help="whether balance selection is performed per class")
parser.add_argument('--use_selected', dest='use_selected', action='store_true')
parser.add_argument('--debug', dest='debug', action='store_true')
parser.add_argument("--easy_epochs", default=100, type=int)
# Algorithm
parser.add_argument('--submodular', default="GraphCut", help="specifiy submodular function to use")
parser.add_argument('--submodular_greedy', default="LazyGreedy", help="specifiy greedy algorithm for submodular optimization")
parser.add_argument('--uncertainty', default="Entropy", help="specifiy uncertanty score to use")
parser.add_argument('--diff_granularity', default=10, type=int)
parser.add_argument('--wa', dest='wa', action='store_true')
parser.add_argument('--wa_num_models', default=10, type=int)
parser.add_argument('--selection_coeff', default=1, type=int)
parser.add_argument('--wa_pre_epochs', default=5, type=int)
parser.add_argument('--wa_pretrain', action='store_true')
parser.add_argument('--wa_replacement', action='store_true')
parser.add_argument('--prune_first', action='store_true')
parser.add_argument('--prune_second', action='store_true')
parser.add_argument('--dropout', type=float, default=0.0)
parser.add_argument('--rand_augment', action='store_true')
parser.add_argument('--aug_n', default=1, type=int)
parser.add_argument('--aug_m', default=5, type=int)
parser.add_argument('--augment_coeff', default=1, type=int)
parser.add_argument('--augment_first', action='store_true')
parser.add_argument('--augment_second', action='store_true')
parser.add_argument('--augment_third', action='store_true')
parser.add_argument('--rand_pruning', action='store_true')
parser.add_argument('--pruning_method', default="Uniform")
parser.add_argument('--pruning_rate', type=float, default=0.0)
parser.add_argument('--dnc', action='store_true')
parser.add_argument('--dynamic', action='store_true')
parser.add_argument('--reselect', default=-1, type=int)
parser.add_argument('--ensemble', action='store_true')
parser.add_argument('--ensemble_size', default=5, type=int)