-
Notifications
You must be signed in to change notification settings - Fork 2
/
prune.py
188 lines (163 loc) · 9.24 KB
/
prune.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
import torch
import torch.nn as nn
def prune_net(net, independentflag, prune_layers, prune_channels, net_name, shortcutflag):
print("pruning:")
if net_name == 'vgg16':
return prune_vgg(net, independentflag, prune_layers, prune_channels)
elif net_name == "resnet34":
return prune_resnet(net, independentflag, prune_layers, prune_channels, shortcutflag)
else:
print("The net is not provided.")
exit(0)
def prune_vgg(net, independentflag, prune_layers, prune_channels):
last_prune_flag = 0
arg_index = 0
conv_index = 1
residue = None
for i in range(len(net.module.features)):
if isinstance(net.module.features[i], nn.Conv2d):
# prune next layer's filter in dim=1
if last_prune_flag:
net.module.features[i], residue = get_new_conv(net.module.features[i], remove_channels, 1)
last_prune_flag = 0
# prune this layer's filter in dim=0
if "conv_%d" % conv_index in prune_layers:
remove_channels = channels_index(net.module.features[i].weight.data, prune_channels[arg_index], residue,
independentflag)
print(prune_layers[arg_index], remove_channels)
net.module.features[i] = get_new_conv(net.module.features[i], remove_channels, 0)
last_prune_flag = 1
arg_index += 1
else:
residue = None
conv_index += 1
elif isinstance(net.module.features[i], nn.BatchNorm2d) and last_prune_flag:
# prune bn
net.module.features[i] = get_new_norm(net.module.features[i], remove_channels)
# prune linear
if "conv_13" in prune_layers:
net.module.classifier[0] = get_new_linear(net.module.classifier[0], remove_channels)
net = net.cuda()
print(net)
return net
def prune_resnet(net, independentflag, prune_layers, prune_channels, shortcutflag):
# init
last_prune_flag = 0
arg_index = 0
residue = None
layers = [net.module.layer1, net.module.layer2, net.module.layer3, net.module.layer4]
# prune shortcut
if shortcutflag:
downsample_index = 1
for layer_index in range(len(layers)):
for block_index in range(len(layers[layer_index])):
if last_prune_flag:
# prune next block's filter in dim=1
layers[layer_index][block_index].conv1, residue = get_new_conv(
layers[layer_index][block_index].conv1, remove_channels, 1)
if layer_index >= 1 and block_index == 0:
if last_prune_flag:
# prune next downsample's filter in dim=1
layers[layer_index][block_index].downsample[0], residue = get_new_conv(
layers[layer_index][block_index].downsample[0], remove_channels, 1)
else:
residue = None
if "downsample_%d" % downsample_index in prune_layers:
# identify channels to remove
remove_channels = channels_index(layers[layer_index][block_index].downsample[0].weight.data,
prune_channels[arg_index], residue, independentflag)
print(prune_layers[arg_index], remove_channels)
# prune downsample's filter in dim=0
layers[layer_index][block_index].downsample[0] = get_new_conv(layers[layer_index][block_index].
downsample[0], remove_channels, 0)
# prune downsample's bn
layers[layer_index][block_index].downsample[1] = get_new_norm(layers[layer_index][block_index].
downsample[1], remove_channels)
arg_index += 1
last_prune_flag = 1
else:
last_prune_flag = 0
downsample_index += 1
if last_prune_flag:
# prune next block's filter in dim=0
layers[layer_index][block_index].conv2 = get_new_conv(layers[layer_index][block_index].conv2,
remove_channels, 0)
# prune next block's bn
layers[layer_index][block_index].bn2 = get_new_norm(layers[layer_index][block_index].bn2,
remove_channels)
# prune linear
if "downsample_3" in prune_layers:
net.module.fc = get_new_linear(net.module.fc, remove_channels)
# prune non-shortcut
else:
conv_index = 2
for layer_index in range(len(layers)):
for block_index in range(len(layers[layer_index])):
if "conv_%d" % conv_index in prune_layers:
# identify channels to remove
remove_channels = channels_index(layers[layer_index][block_index].conv1.weight.data,
prune_channels[arg_index], residue, independentflag)
print(prune_layers[arg_index], remove_channels)
# prune this layer's filter in dim=0
layers[layer_index][block_index].conv1 = get_new_conv(layers[layer_index][block_index].conv1,
remove_channels, 0)
# prune next layer's filter in dim=1
layers[layer_index][block_index].conv2, residue = get_new_conv(
layers[layer_index][block_index].conv2, remove_channels, 1)
residue = 0
# prune bn
layers[layer_index][block_index].bn1 = get_new_norm(layers[layer_index][block_index].bn1,
remove_channels)
arg_index += 1
conv_index += 2
net = net.cuda()
print(net)
return net
def channels_index(weight_matrix, prune_num, residue, independentflag):
abs_sum = torch.sum(torch.abs(weight_matrix.view(weight_matrix.size(0), -1)), dim=1)
if independentflag and residue is not None:
abs_sum = abs_sum + torch.sum(torch.abs(residue.view(residue.size(0), -1)), dim=1)
_, indices = torch.sort(abs_sum)
return indices[:prune_num].tolist()
def select_channels(weight_matrix, remove_channels, dim):
indices = torch.tensor(list(set(range(weight_matrix.shape[dim])) - set(remove_channels)))
new = torch.index_select(weight_matrix, dim, indices.cuda())
if dim == 1:
residue = torch.index_select(weight_matrix, dim, torch.tensor(remove_channels).cuda())
return new, residue
return new
def get_new_conv(old_conv, remove_channels, dim):
if dim == 0:
new_conv = nn.Conv2d(in_channels=old_conv.in_channels,
out_channels=old_conv.out_channels - len(remove_channels),
kernel_size=old_conv.kernel_size, stride=old_conv.stride, padding=old_conv.padding,
dilation=old_conv.dilation, bias=old_conv.bias is not None)
new_conv.weight.data = select_channels(old_conv.weight.data, remove_channels, dim)
if old_conv.bias is not None:
new_conv.bias.data = select_channels(old_conv.bias.data, remove_channels, dim)
return new_conv
else:
new_conv = nn.Conv2d(in_channels=old_conv.in_channels - len(remove_channels), out_channels=old_conv.out_channels,
kernel_size=old_conv.kernel_size, stride=old_conv.stride, padding=old_conv.padding,
dilation=old_conv.dilation, bias=old_conv.bias is not None)
new_conv.weight.data, residue = select_channels(old_conv.weight.data, remove_channels, dim)
if old_conv.bias is not None:
new_conv.bias.data = old_conv.bias.data
return new_conv, residue
def get_new_norm(old_norm, remove_channels):
new = torch.nn.BatchNorm2d(num_features=old_norm.num_features - len(remove_channels), eps=old_norm.eps,
momentum=old_norm.momentum, affine=old_norm.affine,
track_running_stats=old_norm.track_running_stats)
new.weight.data = select_channels(old_norm.weight.data, remove_channels, 0)
new.bias.data = select_channels(old_norm.bias.data, remove_channels, 0)
if old_norm.track_running_stats:
new.running_mean.data = select_channels(old_norm.running_mean.data, remove_channels, 0)
new.running_var.data = select_channels(old_norm.running_var.data, remove_channels, 0)
return new
def get_new_linear(old_linear, remove_channels):
new = torch.nn.Linear(in_features=old_linear.in_features - len(remove_channels),
out_features=old_linear.out_features, bias=old_linear.bias is not None)
new.weight.data, residue = select_channels(old_linear.weight.data, remove_channels, 1)
if old_linear.bias is not None:
new.bias.data = old_linear.bias.data
return new