-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathslim_prune.py
165 lines (122 loc) · 6.45 KB
/
slim_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
from models import *
from utils.utils import *
import numpy as np
from copy import deepcopy
from test import test
from terminaltables import AsciiTable
import time
from utils.prune_utils import *
import argparse
import warnings
warnings.filterwarnings("ignore")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', type=str, default='cfg/dense_yolov3_4.cfg', help='cfg file path')
parser.add_argument('--data', type=str, default='data/visdrone.data', help='*.data file path')
parser.add_argument('--weights', type=str, default='weights/last.pt', help='sparse model weights')
parser.add_argument('--global_percent', type=float, default=0.5, help='global channel prune percent')
parser.add_argument('--layer_keep', type=float, default=0.01, help='channel keep percent per layer')
parser.add_argument('--img_size', type=int, default=800, help='inference size (pixels)')
opt = parser.parse_known_args()[0]
opt.cfg = check_file(opt.cfg) # check file
opt.data = check_file(opt.data)
img_size = opt.img_size
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Darknet(opt.cfg, (img_size, img_size)).to(device)
if opt.weights.endswith(".pt"):
model.load_state_dict(torch.load(opt.weights, map_location=device)['model'])
else:
_ = load_darknet_weights(model, opt.weights)
print('\nloaded weights from ',opt.weights)
eval_model = lambda model:test(model = model, cfg=opt.cfg, data=opt.data,batch_size=64, imgsz=img_size,is_training = False)
obtain_num_parameters = lambda model:sum([param.nelement() for param in model.parameters()])
print("\nlet's test the original model first:")
with torch.no_grad():
origin_model_metric = eval_model(model)
origin_nparameters = obtain_num_parameters(model)
CBL_idx, Conv_idx, prune_idx, _, _= parse_module_defs2(model.module_defs)
bn_weights = gather_bn_weights(model.module_list, prune_idx)
sorted_bn = torch.sort(bn_weights)[0]
sorted_bn, sorted_index = torch.sort(bn_weights)
thresh_index = int(len(bn_weights) * opt.global_percent)
thresh = sorted_bn[thresh_index].cuda()
print(f'Global Threshold should be less than {thresh:.4f}.')
def obtain_filters_mask(model, thre, CBL_idx, prune_idx):
pruned = 0
total = 0
num_filters = []
filters_mask = []
for idx in CBL_idx:
bn_module = model.module_list[idx][1]
if idx in prune_idx:
weight_copy = bn_module.weight.data.abs().clone()
channels = weight_copy.shape[0] #
min_channel_num = int(channels * opt.layer_keep) if int(channels * opt.layer_keep) > 0 else 1
mask = weight_copy.gt(thresh).float()
if int(torch.sum(mask)) < min_channel_num:
_, sorted_index_weights = torch.sort(weight_copy,descending=True)
mask[sorted_index_weights[:min_channel_num]]=1.
remain = int(mask.sum())
pruned = pruned + mask.shape[0] - remain
print(f'layer index: {idx:>3d} \t total channel: {mask.shape[0]:>4d} \t '
f'remaining channel: {remain:>4d}')
else:
mask = torch.ones(bn_module.weight.data.shape)
remain = mask.shape[0]
total += mask.shape[0]
num_filters.append(remain)
filters_mask.append(mask.clone())
prune_ratio = pruned / total
print(f'Prune channels: {pruned}\tPrune ratio: {prune_ratio:.3f}')
return num_filters, filters_mask
num_filters, filters_mask = obtain_filters_mask(model, thresh, CBL_idx, prune_idx)
CBLidx2mask = {idx: mask for idx, mask in zip(CBL_idx, filters_mask)}
CBLidx2filters = {idx: filters for idx, filters in zip(CBL_idx, num_filters)}
for i in model.module_defs:
if i['type'] == 'shortcut':
i['is_access'] = False
print('merge the mask of layers connected to shortcut!')
merge_mask(model, CBLidx2mask, CBLidx2filters)
for i in CBLidx2mask:
CBLidx2mask[i] = CBLidx2mask[i].clone().cpu().numpy()
pruned_model = prune_model_keep_size2(model, prune_idx, CBL_idx, CBLidx2mask)
for i in model.module_defs:
if i['type'] == 'shortcut':
i.pop('is_access')
compact_module_defs = deepcopy(model.module_defs)
for idx in CBL_idx:
assert compact_module_defs[idx]['type'] == 'convolutional'
compact_module_defs[idx]['filters'] = str(CBLidx2filters[idx])
compact_model = Darknet([model.hyperparams.copy()] + compact_module_defs, (img_size, img_size)).to(device)
compact_nparameters = obtain_num_parameters(compact_model)
init_weights_from_loose_model(compact_model, pruned_model, CBL_idx, Conv_idx, CBLidx2mask)
random_input = torch.rand((1, 3, img_size, img_size)).to(device)
def obtain_avg_forward_time(input, model, repeat=200):
model.eval()
start = time.time()
with torch.no_grad():
for i in range(repeat):
output = model(input)
avg_infer_time = (time.time() - start) / repeat
return avg_infer_time, output
print('testing inference time...')
pruned_forward_time, pruned_output = obtain_avg_forward_time(random_input, pruned_model)
compact_forward_time, compact_output = obtain_avg_forward_time(random_input, compact_model)
print('testing the final model...')
with torch.no_grad():
compact_model_metric = eval_model(compact_model)
metric_table = [
["Metric", "Before", "After"],
["mAP", f'{origin_model_metric[0][2]:.6f}', f'{compact_model_metric[0][2]:.6f}'],
["Parameters", f"{origin_nparameters}", f"{compact_nparameters}"],
["Inference", f'{pruned_forward_time:.4f}', f'{compact_forward_time:.4f}']
]
print(AsciiTable(metric_table).table)
pruned_cfg_name = opt.cfg.replace('/', f'/prune_{opt.global_percent}_keep_{opt.layer_keep}_')
pruned_cfg_file = write_cfg(pruned_cfg_name, [model.hyperparams.copy()] + compact_module_defs)
print(f'Config file has been saved: {pruned_cfg_file}')
compact_model_name = opt.weights.replace('/', f'/prune_{opt.global_percent}_keep_{opt.layer_keep}_')
if compact_model_name.endswith('.pt'):
compact_model_name = compact_model_name.replace('.pt', '.weights')
save_weights(compact_model, path=compact_model_name)
print(f'Compact model has been saved: {compact_model_name}')