forked from VainF/Torch-Pruning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
395 lines (363 loc) · 16.4 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
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
import sys, os
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from functools import partial
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch_pruning as tp
import engine.utils as utils
import registry
parser = argparse.ArgumentParser()
# Basic options
parser.add_argument("--mode", type=str, required=True, choices=["pretrain", "prune", "test"])
parser.add_argument("--model", type=str, required=True)
parser.add_argument("--verbose", action="store_true", default=False)
parser.add_argument("--dataset", type=str, default="cifar100", choices=['cifar10', 'cifar100', 'modelnet40'])
parser.add_argument('--dataroot', default='data', help='path to your datasets')
parser.add_argument("--batch-size", type=int, default=128)
parser.add_argument("--total-epochs", type=int, default=100)
parser.add_argument("--lr-decay-milestones", default="60,80", type=str, help="milestones for learning rate decay")
parser.add_argument("--lr-decay-gamma", default=0.1, type=float)
parser.add_argument("--lr", default=0.01, type=float, help="learning rate")
parser.add_argument("--restore", type=str, default=None)
parser.add_argument('--output-dir', default='run', help='path where to save')
parser.add_argument("--finetune", action="store_true", default=False, help='whether finetune or not')
# For pruning
parser.add_argument("--method", type=str, default=None)
parser.add_argument("--speed-up", type=float, default=2)
parser.add_argument("--max-pruning-ratio", type=float, default=1.0)
parser.add_argument("--soft-keeping-ratio", type=float, default=0.0)
parser.add_argument("--reg", type=float, default=5e-4)
parser.add_argument("--delta_reg", type=float, default=1e-4, help='for growing regularization')
parser.add_argument("--weight-decay", type=float, default=5e-4)
parser.add_argument("--seed", type=int, default=None)
parser.add_argument("--global-pruning", action="store_true", default=False)
parser.add_argument("--sl-total-epochs", type=int, default=100, help="epochs for sparsity learning")
parser.add_argument("--sl-lr", default=0.01, type=float, help="learning rate for sparsity learning")
parser.add_argument("--sl-lr-decay-milestones", default="60,80", type=str, help="milestones for sparsity learning")
parser.add_argument("--sl-reg-warmup", type=int, default=0, help="epochs for sparsity learning")
parser.add_argument("--sl-restore", type=str, default=None)
parser.add_argument("--iterative-steps", default=400, type=int)
args = parser.parse_args()
def progressive_pruning(pruner, model, speed_up, example_inputs, train_loader=None):
model.eval()
base_ops, _ = tp.utils.count_ops_and_params(model, example_inputs=example_inputs)
current_speed_up = 1
while current_speed_up < speed_up:
if args.method == "obdc":
model.zero_grad()
imp=pruner.importance
imp._prepare_model(model, pruner)
for k, (imgs, lbls) in enumerate(train_loader):
if k>=10: break
imgs = imgs.to(args.device)
lbls = lbls.to(args.device)
output = model(imgs)
sampled_y = torch.multinomial(torch.nn.functional.softmax(output.cpu().data, dim=1),
1).squeeze().to(args.device)
loss_sample = F.cross_entropy(output, sampled_y)
loss_sample.backward()
imp.step()
pruner.step()
imp._rm_hooks(model)
imp._clear_buffer()
else:
pruner.step()
pruned_ops, _ = tp.utils.count_ops_and_params(model, example_inputs=example_inputs)
current_speed_up = float(base_ops) / pruned_ops
if pruner.current_step == pruner.iterative_steps:
break
return current_speed_up
def eval(model, test_loader, device=None):
correct = 0
total = 0
loss = 0
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
with torch.no_grad():
for i, (data, target) in enumerate(test_loader):
data, target = data.to(device), target.to(device)
out = model(data)
loss += F.cross_entropy(out, target, reduction="sum")
pred = out.max(1)[1]
correct += (pred == target).sum()
total += len(target)
return (correct / total).item(), (loss / total).item()
def train_model(
model,
train_loader,
test_loader,
epochs,
lr,
lr_decay_milestones,
lr_decay_gamma=0.1,
save_as=None,
# For pruning
weight_decay=5e-4,
save_state_dict_only=True,
pruner=None,
device=None,
):
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
optimizer = torch.optim.SGD(
model.parameters(),
lr=lr,
momentum=0.9,
weight_decay=weight_decay if pruner is None else 0,
)
milestones = [int(ms) for ms in lr_decay_milestones.split(",")]
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=milestones, gamma=lr_decay_gamma
)
model.to(device)
best_acc = -1
for epoch in range(epochs):
model.train()
for i, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
out = model(data)
loss = F.cross_entropy(out, target)
loss.backward()
if pruner is not None:
pruner.regularize(model) # for sparsity learning
optimizer.step()
if i % 10 == 0 and args.verbose:
args.logger.info(
"Epoch {:d}/{:d}, iter {:d}/{:d}, loss={:.4f}, lr={:.4f}".format(
epoch,
epochs,
i,
len(train_loader),
loss.item(),
optimizer.param_groups[0]["lr"],
)
)
if pruner is not None and isinstance(pruner, tp.pruner.GrowingRegPruner):
pruner.update_reg() # increase the strength of regularization
#print(pruner.group_reg[pruner._groups[0]])
model.eval()
acc, val_loss = eval(model, test_loader, device=device)
args.logger.info(
"Epoch {:d}/{:d}, Acc={:.4f}, Val Loss={:.4f}, lr={:.4f}".format(
epoch, epochs, acc, val_loss, optimizer.param_groups[0]["lr"]
)
)
if best_acc < acc:
os.makedirs(args.output_dir, exist_ok=True)
if args.mode == "prune":
if save_as is None:
save_as = os.path.join( args.output_dir, "{}_{}_{}.pth".format(args.dataset, args.model, args.method) )
if save_state_dict_only:
torch.save(model.state_dict(), save_as)
else:
torch.save(model, save_as)
elif args.mode == "pretrain":
if save_as is None:
save_as = os.path.join( args.output_dir, "{}_{}.pth".format(args.dataset, args.model) )
torch.save(model.state_dict(), save_as)
best_acc = acc
scheduler.step()
args.logger.info("Best Acc=%.4f" % (best_acc))
def get_pruner(model, example_inputs):
args.sparsity_learning = False
if args.method == "random":
imp = tp.importance.RandomImportance()
pruner_entry = partial(tp.pruner.MagnitudePruner, global_pruning=args.global_pruning)
elif args.method == "l1":
imp = tp.importance.MagnitudeImportance(p=1)
pruner_entry = partial(tp.pruner.MagnitudePruner, global_pruning=args.global_pruning)
elif args.method == "l2":
imp = tp.importance.MagnitudeImportance(p=2)
pruner_entry = partial(tp.pruner.MagnitudePruner, global_pruning=args.global_pruning)
elif args.method == "fpgm":
imp = tp.importance.FPGMImportance(p=2)
pruner_entry = partial(tp.pruner.MagnitudePruner, global_pruning=args.global_pruning)
elif args.method == "obdc":
imp = tp.importance.OBDCImportance(group_reduction='mean', num_classes=args.num_classes)
pruner_entry = partial(tp.pruner.MagnitudePruner, global_pruning=args.global_pruning)
elif args.method == "lamp":
imp = tp.importance.LAMPImportance(p=2)
pruner_entry = partial(tp.pruner.MagnitudePruner, global_pruning=args.global_pruning)
elif args.method == "slim":
args.sparsity_learning = True
imp = tp.importance.BNScaleImportance()
pruner_entry = partial(tp.pruner.BNScalePruner, reg=args.reg, global_pruning=args.global_pruning)
elif args.method == "group_slim":
args.sparsity_learning = True
imp = tp.importance.BNScaleImportance()
pruner_entry = partial(tp.pruner.BNScalePruner, reg=args.reg, global_pruning=args.global_pruning, group_lasso=True)
elif args.method == "group_norm":
imp = tp.importance.GroupNormImportance(p=2)
pruner_entry = partial(tp.pruner.GroupNormPruner, global_pruning=args.global_pruning)
elif args.method == "group_sl":
args.sparsity_learning = True
imp = tp.importance.GroupNormImportance(p=2, normalizer='max') # normalized by the maximum score for CIFAR
pruner_entry = partial(tp.pruner.GroupNormPruner, reg=args.reg, global_pruning=args.global_pruning)
elif args.method == "growing_reg":
args.sparsity_learning = True
imp = tp.importance.GroupNormImportance(p=2)
pruner_entry = partial(tp.pruner.GrowingRegPruner, reg=args.reg, delta_reg=args.delta_reg, global_pruning=args.global_pruning)
else:
raise NotImplementedError
#args.is_accum_importance = is_accum_importance
unwrapped_parameters = []
ignored_layers = []
pruning_ratio_dict = {}
# ignore output layers
for m in model.modules():
if isinstance(m, torch.nn.Linear) and m.out_features == args.num_classes:
ignored_layers.append(m)
elif isinstance(m, torch.nn.modules.conv._ConvNd) and m.out_channels == args.num_classes:
ignored_layers.append(m)
# Here we fix iterative_steps=200 to prune the model progressively with small steps
# until the required speed up is achieved.
pruner = pruner_entry(
model,
example_inputs,
importance=imp,
iterative_steps=args.iterative_steps,
pruning_ratio=1.0,
pruning_ratio_dict=pruning_ratio_dict,
max_pruning_ratio=args.max_pruning_ratio,
ignored_layers=ignored_layers,
unwrapped_parameters=unwrapped_parameters,
)
return pruner
def main():
if args.seed is not None:
torch.manual_seed(args.seed)
# Logger
if args.mode == "prune":
prefix = 'global' if args.global_pruning else 'local'
logger_name = "{}-{}-{}-{}".format(args.dataset, prefix, args.method, args.model)
args.output_dir = os.path.join(args.output_dir, args.dataset, args.mode, logger_name)
log_file = "{}/{}.txt".format(args.output_dir, logger_name)
elif args.mode == "pretrain":
args.output_dir = os.path.join(args.output_dir, args.dataset, args.mode)
logger_name = "{}-{}".format(args.dataset, args.model)
log_file = "{}/{}.txt".format(args.output_dir, logger_name)
elif args.mode == "test":
log_file = None
args.logger = utils.get_logger(logger_name, output=log_file)
# Model & Dataset
args.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
num_classes, train_dst, val_dst, input_size = registry.get_dataset(
args.dataset, data_root=args.dataroot
)
args.num_classes = num_classes
model = registry.get_model(args.model, num_classes=num_classes, pretrained=True, target_dataset=args.dataset)
train_loader = torch.utils.data.DataLoader(
train_dst,
batch_size=args.batch_size,
num_workers=4,
drop_last=True,
shuffle=True,
)
test_loader = torch.utils.data.DataLoader(
val_dst, batch_size=args.batch_size, num_workers=4
)
for k, v in utils.utils.flatten_dict(vars(args)).items(): # print args
args.logger.info("%s: %s" % (k, v))
if args.restore is not None:
loaded = torch.load(args.restore, map_location="cpu")
if isinstance(loaded, nn.Module):
model = loaded
else:
model.load_state_dict(loaded)
args.logger.info("Loading model from {restore}".format(restore=args.restore))
model = model.to(args.device)
######################################################
# Training / Pruning / Testing
example_inputs = train_dst[0][0].unsqueeze(0).to(args.device)
if args.mode == "pretrain":
ops, params = tp.utils.count_ops_and_params(
model, example_inputs=example_inputs,
)
args.logger.info("Params: {:.2f} M".format(params / 1e6))
args.logger.info("ops: {:.2f} M".format(ops / 1e6))
train_model(
model=model,
epochs=args.total_epochs,
lr=args.lr,
lr_decay_milestones=args.lr_decay_milestones,
train_loader=train_loader,
test_loader=test_loader
)
elif args.mode == "prune":
pruner = get_pruner(model, example_inputs=example_inputs)
# 0. Sparsity Learning
if args.sparsity_learning:
reg_pth = "reg_{}_{}_{}_{}.pth".format(args.dataset, args.model, args.method, args.reg)
reg_pth = os.path.join( os.path.join(args.output_dir, reg_pth) )
if not args.sl_restore:
args.logger.info("Regularizing...")
train_model(
model,
train_loader=train_loader,
test_loader=test_loader,
epochs=args.sl_total_epochs,
lr=args.sl_lr,
lr_decay_milestones=args.sl_lr_decay_milestones,
lr_decay_gamma=args.lr_decay_gamma,
pruner=pruner,
save_state_dict_only=True,
save_as = reg_pth,
)
args.logger.info("Loading the sparse model from {}...".format(reg_pth))
model.load_state_dict( torch.load( reg_pth, map_location=args.device) )
# 1. Pruning
model.eval()
ori_ops, ori_size = tp.utils.count_ops_and_params(model, example_inputs=example_inputs)
ori_acc, ori_val_loss = eval(model, test_loader, device=args.device)
args.logger.info("Pruning...")
progressive_pruning(pruner, model, speed_up=args.speed_up, example_inputs=example_inputs, train_loader=train_loader)
del pruner # remove reference
args.logger.info(model)
pruned_ops, pruned_size = tp.utils.count_ops_and_params(model, example_inputs=example_inputs)
pruned_acc, pruned_val_loss = eval(model, test_loader, device=args.device)
args.logger.info(
"Params: {:.2f} M => {:.2f} M ({:.2f}%)".format(
ori_size / 1e6, pruned_size / 1e6, pruned_size / ori_size * 100
)
)
args.logger.info(
"FLOPs: {:.2f} M => {:.2f} M ({:.2f}%, {:.2f}X )".format(
ori_ops / 1e6,
pruned_ops / 1e6,
pruned_ops / ori_ops * 100,
ori_ops / pruned_ops,
)
)
args.logger.info("Acc: {:.4f} => {:.4f}".format(ori_acc, pruned_acc))
args.logger.info(
"Val Loss: {:.4f} => {:.4f}".format(ori_val_loss, pruned_val_loss)
)
# 2. Finetuning
if args.finetune:
args.logger.info("Finetuning...")
train_model(
model,
epochs=args.total_epochs,
lr=args.lr,
lr_decay_milestones=args.lr_decay_milestones,
train_loader=train_loader,
test_loader=test_loader,
device=args.device,
save_state_dict_only=False,
)
elif args.mode == "test":
model.eval()
ops, params = tp.utils.count_ops_and_params(
model, example_inputs=example_inputs,
)
args.logger.info("Params: {:.2f} M".format(params / 1e6))
args.logger.info("ops: {:.2f} M".format(ops / 1e6))
acc, val_loss = eval(model, test_loader)
args.logger.info("Acc: {:.4f} Val Loss: {:.4f}\n".format(acc, val_loss))
if __name__ == "__main__":
main()