-
Notifications
You must be signed in to change notification settings - Fork 2
/
snl_finetune_unstructured.py
203 lines (166 loc) · 8.23 KB
/
snl_finetune_unstructured.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
# Selective Network Linearization unstructured method.
# Starting from the pretrained model.
import argparse
import os
from datasets import get_dataset, DATASETS, get_num_classes
from architectures_unstructured import ARCHITECTURES, get_architecture
from time import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torch.optim import SGD, Optimizer, Adam
from torch.optim.lr_scheduler import StepLR
import datetime
import time
import numpy as np
import copy
import types
from math import ceil
from train_utils import AverageMeter, accuracy, accuracy_list, init_logfile, log
from utils import *
import sys
parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
parser.add_argument('dataset', type=str, choices=DATASETS)
parser.add_argument('arch', type=str, choices=ARCHITECTURES)
parser.add_argument('outdir', type=str, help='folder to save model and training log)')
parser.add_argument('savedir', type=str, help='folder to load model')
parser.add_argument('--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('--finetune_epochs', default=100, type=int,
help='number of total epochs for the finetuning')
parser.add_argument('--epochs', default=2000, type=int)
parser.add_argument('--batch', default=256, type=int, metavar='N',
help='batchsize (default: 256)')
parser.add_argument('--logname', type=str, default='log.txt')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
help='initial learning rate', dest='lr')
parser.add_argument('--alpha', default=1e-5, type=float,
help='Lasso coefficient')
parser.add_argument('--threshold', default=1e-2, type=float)
parser.add_argument('--budegt_type', default='absolute', type=str, choices=['absolute', 'relative'])
parser.add_argument('--relu_budget', default=50000, type=int)
parser.add_argument('--lr_step_size', type=int, default=30,
help='How often to decrease learning by gamma.')
parser.add_argument('--gamma', type=float, default=0.1,
help='LR is multiplied by gamma on schedule.')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--weight-decay', '--wd', default=5e-4, type=float,
metavar='W', help='weight decay (default: 5e-4)')
parser.add_argument('--gpu', default=0, type=int,
help='id(s) for CUDA_VISIBLE_DEVICES')
parser.add_argument('--print-freq', default=100, type=int,
metavar='N', help='print frequency (default: 10)')
parser.add_argument('--stride', type=int, default=1, help='conv1 stride')
args = parser.parse_args()
if args.budegt_type == 'relative' and args.relu_budget > 1:
print(f'Warning: relative budget type is used, but the relu budget is {args.relu_budget} > 1.')
sys.exit(1)
def relu_counting(net, args):
relu_count = 0
for name, param in net.named_parameters():
if 'alpha' in name:
boolean_list = param.data > args.threshold
relu_count += (boolean_list == 1).sum()
return relu_count
def main():
if not os.path.exists(args.outdir):
os.makedirs(args.outdir)
device = torch.device("cuda")
torch.cuda.set_device(args.gpu)
logfilename = os.path.join(args.outdir, args.logname)
log(logfilename, "Hyperparameter List")
log(logfilename, "Finetune Epochs: {:}".format(args.finetune_epochs))
log(logfilename, "Learning Rate: {:}".format(args.lr))
log(logfilename, "Alpha: {:}".format(args.alpha))
log(logfilename, "ReLU Budget: {:}".format(args.relu_budget))
train_dataset = get_dataset(args.dataset, 'train')
test_dataset = get_dataset(args.dataset, 'test')
pin_memory = (args.dataset == "imagenet")
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=args.batch,
num_workers=args.workers, pin_memory=pin_memory)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=args.batch,
num_workers=args.workers, pin_memory=pin_memory)
# Loading the base_classifier
base_classifier = get_architecture(args.arch, args.dataset, device, args)
checkpoint = torch.load(args.savedir, map_location=device)
base_classifier.load_state_dict(checkpoint['state_dict'])
base_classifier.eval()
log(logfilename, "Loaded the base_classifier")
# Calculating the loaded model's test accuracy.
original_acc = model_inference(base_classifier, test_loader,
device, display=True)
log(logfilename, "Original Model Test Accuracy: {:.5}".format(original_acc))
# Creating a fresh copy of network not affecting the original network.
net = copy.deepcopy(base_classifier)
net = net.to(device)
relu_count = relu_counting(net, args)
log(logfilename, "Original ReLU Count: {}".format(relu_count))
# Alpha is the masking parameters initialized to 1. Enabling the grad.
for name, param in net.named_parameters():
if 'alpha' in name:
param.requires_grad = True
criterion = nn.CrossEntropyLoss().to(device)
optimizer = Adam(net.parameters(), lr=args.lr)
# counting number of ReLU.
total = relu_counting(net, args)
if args.budegt_type == 'relative':
args.relu_budget = int(total * args.relu_budget)
# Corresponds to Line 4-9
lowest_relu_count, relu_count = total, total
for epoch in range(args.epochs):
# Simultaneous tarining of w and alpha with KD loss.
train_loss = mask_train_kd_unstructured(train_loader, net, base_classifier, criterion, optimizer,
epoch, device, alpha=args.alpha, display=False)
acc = model_inference(net, test_loader, device, display=False)
# counting ReLU in the neural network by using threshold.
relu_count = relu_counting(net, args)
log(logfilename, 'Epochs: {}\t'
'Test Acc: {}\t'
'Relu Count: {}\t'
'Alpha: {:.6f}\t'.format(
epoch, acc, relu_count, args.alpha
)
)
if relu_count < lowest_relu_count:
lowest_relu_count = relu_count
elif relu_count >= lowest_relu_count and epoch >= 5:
args.alpha *= 1.1
if relu_count <= args.relu_budget:
print("Current epochs breaking loop at {:}".format(epoch))
break
log(logfilename, "After SNL Algorithm, the current ReLU Count: {}, rel. count:{}".format(relu_count, relu_count/total))
# Line 11: Threshold and freeze alpha
for name, param in net.named_parameters():
if 'alpha' in name:
boolean_list = param.data > args.threshold
param.data = boolean_list.float()
param.requires_grad = False
# Line 12: Finetuing the network
finetune_epoch = args.finetune_epochs
optimizer = SGD(net.parameters(), lr=1e-3, momentum=args.momentum, weight_decay=args.weight_decay)
criterion = nn.CrossEntropyLoss().to(device)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, finetune_epoch)
print("Finetuning the model")
log(logfilename, "Finetuning the model")
best_top1 = 0
for epoch in range(finetune_epoch):
train_loss, train_top1, train_top5 = train_kd(train_loader, net, base_classifier, optimizer, criterion, epoch, device)
test_loss, test_top1, test_top5 = test(test_loader, net, criterion, device, 100, display=True)
scheduler.step()
if best_top1 < test_top1:
best_top1 = test_top1
is_best = True
else:
is_best = False
if is_best:
torch.save({
'arch': args.arch,
'state_dict': net.state_dict(),
'optimizer': optimizer.state_dict(),
}, os.path.join(args.outdir, f'snl_best_checkpoint_{args.arch}_{args.dataset}_{args.relu_budget}.pth.tar'))
print("Final best Prec@1 = {}%".format(best_top1))
log(logfilename, "Final best Prec@1 = {}%".format(best_top1))
if __name__ == "__main__":
main()