-
Notifications
You must be signed in to change notification settings - Fork 35
/
filter_pruning.py
69 lines (52 loc) · 1.8 KB
/
filter_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
"""
Pruning a ConvNet by filters iteratively
"""
import torch
import torch.nn as nn
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from pruning.methods import filter_prune
from pruning.utils import to_var, train, test, prune_rate
from models import ConvNet
# Hyper Parameters
param = {
'pruning_perc': 50.,
'batch_size': 128,
'test_batch_size': 100,
'num_epochs': 5,
'learning_rate': 0.001,
'weight_decay': 5e-4,
}
# Data loaders
train_dataset = datasets.MNIST(root='../data/',train=True, download=True,
transform=transforms.ToTensor())
loader_train = torch.utils.data.DataLoader(train_dataset,
batch_size=param['batch_size'], shuffle=True)
test_dataset = datasets.MNIST(root='../data/', train=False, download=True,
transform=transforms.ToTensor())
loader_test = torch.utils.data.DataLoader(test_dataset,
batch_size=param['test_batch_size'], shuffle=True)
# Load the pretrained model
net = ConvNet()
net.load_state_dict(torch.load('models/convnet_pretrained.pkl'))
if torch.cuda.is_available():
print('CUDA ensabled.')
net.cuda()
print("--- Pretrained network loaded ---")
test(net, loader_test)
# prune the weights
masks = filter_prune(net, param['pruning_perc'])
net.set_masks(masks)
print("--- {}% parameters pruned ---".format(param['pruning_perc']))
test(net, loader_test)
# Retraining
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.RMSprop(net.parameters(), lr=param['learning_rate'],
weight_decay=param['weight_decay'])
train(net, criterion, optimizer, param, loader_train)
# Check accuracy and nonzeros weights in each layer
print("--- After retraining ---")
test(net, loader_test)
prune_rate(net)
# Save and load the entire model
torch.save(net.state_dict(), 'models/convnet_pruned.pkl')