-
Notifications
You must be signed in to change notification settings - Fork 339
/
test_soft_pruning.py
71 lines (62 loc) · 2.5 KB
/
test_soft_pruning.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
import sys, os
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import torch
from torchvision.models import resnet50 as entry
import torch_pruning as tp
from torch import nn
import torch.nn.functional as F
def test_soft_pruning():
model = entry()
print(model)
# Global metrics
example_inputs = torch.randn(1, 3, 224, 224)
imp = tp.importance.MagnitudeImportance(p=2)
ignored_layers = []
# DO NOT prune the final classifier!
for m in model.modules():
if isinstance(m, torch.nn.Linear) and m.out_features == 1000:
ignored_layers.append(m)
iterative_steps = 1
pruner = tp.pruner.MagnitudePruner(
model,
example_inputs,
importance=imp,
global_pruning=True,
iterative_steps=iterative_steps,
pruning_ratio=0.5, # remove 50% channels, ResNet18 = {64, 128, 256, 512} => ResNet18_Half = {32, 64, 128, 256}
ignored_layers=ignored_layers,
)
base_macs, base_nparams = tp.utils.count_ops_and_params(model, example_inputs)
for i in range(iterative_steps):
# Soft Pruning
for group in pruner.step(interactive=True):
for dep, idxs in group:
target_layer = dep.target.module
pruning_fn = dep.handler
if pruning_fn in [tp.prune_conv_in_channels, tp.prune_linear_in_channels]:
target_layer.weight.data[:, idxs] *= 0
elif pruning_fn in [tp.prune_conv_out_channels, tp.prune_linear_out_channels]:
target_layer.weight.data[idxs] *= 0
if target_layer.bias is not None:
target_layer.bias.data[idxs] *= 0
elif pruning_fn in [tp.prune_batchnorm_out_channels]:
target_layer.weight.data[idxs] *= 0
target_layer.bias.data[idxs] *= 0
# group.prune() # <= disable hard pruning
print(model.conv1.weight)
macs, nparams = tp.utils.count_ops_and_params(model, example_inputs)
print(model)
print(model(example_inputs).shape)
print(
" Iter %d/%d, Params: %.2f M => %.2f M"
% (i+1, iterative_steps, base_nparams / 1e6, nparams / 1e6)
)
print(
" Iter %d/%d, MACs: %.2f G => %.2f G"
% (i+1, iterative_steps, base_macs / 1e9, macs / 1e9)
)
# finetune your model here
# finetune(model)
# ...
if __name__ == "__main__":
test_soft_pruning()