-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_anytime_train.py
609 lines (513 loc) · 19.5 KB
/
main_anytime_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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
import argparse
import os
import pdb
import pickle
import random
import shutil
import time
from copy import deepcopy
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.multiprocessing
import torch.nn as nn
import torch.nn.functional as F
import torch.optim
import torch.utils.data
import torchvision.datasets as datasets
import torchvision.models as models
import torchvision.transforms as transforms
from advertorch.utils import NormalizeByChannelMeanStd
from torch.utils.data.sampler import SubsetRandomSampler
import utils
torch.multiprocessing.set_sharing_strategy("file_system")
from dataset import (Setup_RestrictedImageNet,
generate_anytime_cifar10_dataloader,
generate_anytime_cifar100_dataloader,
generate_anytime_res_img_dataloader,
generate_anytime_res_img_dataloader_few,
setup__cifar10_dataset, setup__cifar100_dataset)
from generate_mask import generate_mask_
from pruner import *
from utils import evaluate_cer, setup_model
from wb import WandBLogger
parser = argparse.ArgumentParser(description="PyTorch Anytime Training")
##################################### Dataset #################################################
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(
"--meta_batch_size",
type=int,
default=5000,
help="data number in each meta batch_size",
)
parser.add_argument("--meta_batch_number", type=int, default=10)
##################################### Architecture ############################################
parser.add_argument("--arch", type=str, default="resnet20s", help="model architecture")
parser.add_argument(
"--imagenet_arch",
action="store_true",
help="architecture for imagenet size samples",
)
parser.add_argument(
"--imagenet_path",
type=str,
default="../imagenet",
help="location of the imagenet folder",
)
##################################### General setting ############################################
parser.add_argument("--seed", default=None, type=int, help="random seed")
parser.add_argument("--gpu", type=int, default=0, help="gpu device id")
parser.add_argument(
"--workers", type=int, default=2, help="number of workers in dataloader"
)
parser.add_argument("--resume", action="store_true", help="resume from checkpoint")
parser.add_argument("--checkpoint", type=str, default=None, help="checkpoint file")
parser.add_argument(
"--save_dir",
help="The directory used to save the trained models",
default=None,
type=str,
)
parser.add_argument("-no_replay", action="store_true", help="Flag for No Replay")
parser.add_argument("-one_replay", action="store_true", help="Flag for No Replay")
parser.add_argument("-buffer_replay", action="store_true", help="Flag for No Replay")
parser.add_argument(
"--buffer_size_train",
default=182,
type=int,
help="number of Random Train examples to add in buffer",
)
parser.add_argument(
"--buffer_size_valid",
default=182,
type=int,
help="number of Random Valid examples to add in buffer",
)
parser.add_argument("-snip_no_replay", action="store_true", help="Flag for No Replay")
parser.add_argument("-few_shot", action="store_true", help="Flag for No Replay")
parser.add_argument(
"--n_shots",
default=100,
type=int,
help="number of Random Valid examples to add in buffer",
)
##################################### 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(
"--tickets_mask", default=None, type=str, help="mask for subnetworks"
)
parser.add_argument(
"--tickets_init", default=None, type=str, help="initilization for subnetworks"
)
parser.add_argument(
"--snip_size", default=0.20, type=float, help="the size for the snip"
)
parser.add_argument("--sparsity_level", default=0, type=float, help="sparsity level")
parser.add_argument(
"--pruner", default="snip", type=str, help="Pruner Type[mag,snip,GraSP,SynFlow]"
)
parser.add_argument(
"--scope", default="global", type=str, help="Scope of Pruner[local,global]"
)
##################################### W&B Logging setting #################################################
parser.add_argument("-wb", action="store_true", help="Flag for using W&B logging")
parser.add_argument(
"--project_name", default="APP", type=str, help="Name of the W&B project"
)
parser.add_argument(
"--run", default="Anytime_fixed", type=str, help="Name for the W&B run"
)
best_sa = 0
args = parser.parse_args()
print(args)
os.makedirs(args.save_dir, exist_ok=True)
if args.scope == "l":
args.scope = "local"
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)
model = setup_model(args)
if args.dataset == "cifar10":
whole_trainset = setup__cifar10_dataset(args)
elif args.dataset == "cifar100":
whole_trainset = setup__cifar100_dataset(args)
elif args.dataset == "restricted_imagenet":
whole_trainset, test_set = Setup_RestrictedImageNet(args, args.imagenet_path)
if args.tickets_init:
print("loading init from {}".format(args.tickets_init))
init_file = torch.load(args.tickets_init, map_location="cpu")
if "init_weight" in init_file:
init_file = init_file["init_weight"]
model.load_state_dict(init_file)
else:
torch.save(model.state_dict(), os.path.join(args.save_dir, "randinit.pth.tar"))
# setup initialization and mask
if args.tickets_mask:
print("loading mask from {}".format(args.tickets_mask))
mask_file = torch.load(args.tickets_mask, map_location="cpu")
if "state_dict" in mask_file:
mask_file = mask_file["state_dict"]
mask_file = extract_mask(mask_file)
print("pruning with {} masks".format(len(mask_file)))
prune_model_custom(model, mask_file)
model.cuda()
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
)
if args.wb:
wandb_logger = WandBLogger(
project_name=args.project_name,
run_name=args.run,
dir=args.save_dir,
config=vars(args),
model=model,
params={"resume": args.resume},
)
else:
wandb_logger = None
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"]
model.load_state_dict(checkpoint["state_dict"])
optimizer.load_state_dict(checkpoint["optimizer"])
scheduler.load_state_dict(checkpoint["scheduler"])
print(
"loading from state: {} epoch: {}, best_sa = {}".format(
start_state, start_epoch, best_sa
)
)
else:
all_result = {}
all_result["gen_gap"] = []
all_result["train_ta"] = []
all_result["val_ta"] = []
all_result["best_sa"] = []
all_result["gen_gap"] = []
all_result["train_loss"] = []
all_result["lr"] = []
all_result["val_loss"] = []
start_epoch = 0
start_state = 1
# sparsity = [1, 1.5,1.75,2, 2.5,3,3.5,4,4.5,5] # 32.768 remaining_weights=0.8**(sparsity)
if args.scope == "local":
sparsity = [args.sparsity_level for x in range(args.meta_batch_number)]
else:
sparsity = np.linspace(1, args.sparsity_level, args.meta_batch_number)
time_list = []
CER = []
CER_diff = []
for current_state in range(start_state, args.meta_batch_number + 1):
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=decreasing_lr, gamma=0.1
)
print("Current state = {}".format(current_state))
start_time = time.time()
if args.dataset == "cifar10":
print("Loading cifar10 dataset in anytime setting")
(
train_loader,
val_loader,
test_loader,
train_snip_set,
) = generate_anytime_cifar10_dataloader(args, whole_trainset, current_state)
elif args.dataset == "cifar100":
print("Loading cifar100 dataset in anytime setting")
(
train_loader,
val_loader,
test_loader,
train_snip_set,
) = generate_anytime_cifar100_dataloader(
args, whole_trainset, current_state
)
elif args.dataset == "restricted_imagenet":
print("Loading Restricted Imagenet dataset in anytime setting")
if args.meta_batch_number == 3:
(
train_loader,
val_loader,
test_loader,
train_snip_set,
) = generate_anytime_res_img_dataloader(
args, whole_trainset, test_set, 80565, current_state
)
elif args.meta_batch_number == 10:
# Few Shot Dataloader Example
(
train_loader,
val_loader,
test_loader,
train_snip_set,
) = generate_anytime_res_img_dataloader_few(
args, whole_trainset, test_set, 6800, current_state
)
# Generate Mask using SNIP
sparsity_level = sparsity[current_state - 1]
save_mask = (
args.save_dir
+ f"/{current_state}mask_{args.pruner}_{sparsity_level}.pth.tar"
)
if current_state == 1:
model_load_dir = (
args.save_dir + "/randinit.pth.tar"
) # 1st Meta Batch Randomly initialized model
else:
model_load_dir = args.save_dir + f"/{current_state-1}model_SA_best.pth.tar"
generate_mask_(
args,
train_snip_set,
args.pruner,
model_load_dir,
save=save_mask,
state=sparsity_level,
)
model.cpu()
# Load the Model by applying above mask
print("loading mask from {}".format(save_mask))
mask_file = torch.load(save_mask, map_location="cpu")
if "state_dict" in mask_file:
mask_file = mask_file["state_dict"]
mask_file = extract_mask(mask_file)
print("pruning with {} masks".format(len(mask_file)))
prune_model_custom(model, mask_file)
model.cuda()
for epoch in range(start_epoch, args.epochs):
print(optimizer.state_dict()["param_groups"][0]["lr"])
acc, loss = train(train_loader, model, criterion, optimizer, epoch)
# evaluate on validation set
tacc, vloss = validate(val_loader, model, criterion)
# evaluate on test set
# test_tacc = validate(test_loader, model, criterion)
scheduler.step()
# remember best prec@1 and save checkpoint
is_best_sa = tacc > best_sa
best_sa = max(tacc, best_sa)
gen_gap = acc - tacc
all_result["gen_gap"].append(gen_gap)
all_result["train_ta"].append(acc)
all_result["val_ta"].append(tacc)
all_result["best_sa"].append(best_sa)
all_result["train_loss"].append(loss)
all_result["val_loss"].append(vloss)
all_result["lr"].append(optimizer.state_dict()["param_groups"][0]["lr"])
save_checkpoint(
{
"state": current_state,
"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,
data_state=current_state,
save_path=args.save_dir,
)
if wandb_logger:
wandb_logger.log_metrics(all_result)
# report result
val_pick_best_epoch = np.argmax(np.array(all_result["val_ta"]))
print(
"* State = {} best SA = {} Epoch = {}".format(
current_state,
all_result["val_ta"][val_pick_best_epoch],
val_pick_best_epoch + 1,
)
)
all_result = {}
all_result["train_ta"] = []
all_result["val_ta"] = []
all_result["best_sa"] = []
all_result["gen_gap"] = []
all_result["train_loss"] = []
all_result["val_loss"] = []
all_result["lr"] = []
best_sa = 0
start_epoch = 0
best_checkpoint = torch.load(
os.path.join(args.save_dir, "{}model_SA_best.pth.tar".format(current_state))
)
print("Loading Best Weight")
model.load_state_dict(best_checkpoint["state_dict"])
end_time = time.time() - start_time
print("Total time elapsed: {:.4f}s".format(end_time))
time_list.append(end_time)
if args.dataset == "restricted_imagenet":
CER.append(evaluate_cer(model, args, test_loader))
else:
CER.append(evaluate_cer(model, args))
if current_state != 1:
diff = (CER[current_state - 1] - CER[current_state - 2]) / 10000
CER_diff.append(diff)
print("CER diff: {}".format(diff))
# Reset LR to 0.1 after each state
for g in optimizer.param_groups:
g["lr"] = 0.1
print("LR reset to 0.1")
print(optimizer.state_dict()["param_groups"][0]["lr"])
test_tacc, _ = validate(test_loader, model, criterion)
print("Test Acc = {}".format(test_tacc))
print("CER = {}".format(sum(CER)))
wandb_logger.log_metrics({"Test/test_acc": test_tacc})
wandb_logger.log_metrics({"Test/CER": sum(CER)})
print("Final Test Accuracy: ")
print(test_tacc)
print("CER")
print(CER)
print("Anytime Relative Error")
print(CER_diff)
print("Total time")
print(time_list)
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, losses.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, losses.avg
def save_checkpoint(
state, is_SA_best, data_state, save_path, filename="checkpoint.pth.tar"
):
filepath = os.path.join(save_path, str(data_state) + filename)
torch.save(state, filepath)
if is_SA_best:
shutil.copyfile(
filepath,
os.path.join(save_path, "{}model_SA_best.pth.tar".format(data_state)),
)
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):
print("setup random seed = {}".format(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()