-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpruning.py
More file actions
400 lines (347 loc) · 14.9 KB
/
pruning.py
File metadata and controls
400 lines (347 loc) · 14.9 KB
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
396
397
398
399
400
import numpy as np
import pandas as pd
import csv
import time
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
from torch.utils.tensorboard import SummaryWriter
import argparse
import torchsummary
import torch_pruning as tp
import models.mobilenetv1
import models.mobilenetv2
import models.mobilenetv3
import models.vgg16
import models.efficientnet
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# Argument parser
parser = argparse.ArgumentParser(description='Pruning MobileNet V1, V2, and V3')
parser.add_argument('--model', type=str, default='mobilenetv1', help='mobilenetv1, mobilenetv2, or mobilenetv3, efficientnet')
parser.add_argument('--batch_size', type=int, default=128, help='Number of samples per mini-batch')
parser.add_argument('--finetune_epochs', type=int, default=10, help='Number of epochs to finetune')
parser.add_argument('--prune', type=float, default=0.2)
parser.add_argument('--layer', type=str, default="one", help="one, two, three and all")
parser.add_argument('--mode', type=int, default=1, help="pruning: 1, measurement: 2")
parser.add_argument('--seed', type=int, default=1, help="random seed")
parser.add_argument('--strategy', type=str, default="L1", help="L1, L2, and random")
args = parser.parse_args()
finetune_epochs = args.finetune_epochs
batch_size = args.batch_size
model_name = args.model
prune_val = args.prune
layer = args.layer
mode = args.mode
seed = args.seed
strategy_name = args.strategy
print('model: ', model_name, ' layer: ', layer, ' prune_val: ', prune_val, ' strategy: ', strategy_name)
# model name: mobilenetv1, mobilenetv2, mobilenetv3
# layer: all, one
# prune: 0.05 ~ 0.9
# finetune: 0 ~ 200
model_path = f"{model_name}/{layer}/{strategy_name}/prune_{prune_val}"
torch.manual_seed(seed)
# CIFAR10 Dataset (Images and Labels)
train_dataset = dsets.CIFAR10(root='data', train=True, transform=transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=(0.4914, 0.4822, 0.4465), std=(0.2023, 0.1994, 0.2010)),
]), download=True)
test_dataset = dsets.CIFAR10(root='data', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=(0.4914, 0.4822, 0.4465), std=(0.2023, 0.1994, 0.2010)),
]))
# Dataset Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
model_names = {
'mobilenetv1': models.mobilenetv1.MobileNet,
'mobilenetv2': models.mobilenetv2.MobileNetV2,
'mobilenetv3': models.mobilenetv3.MobileNetV3,
'vgg16': models.vgg16.VGG16,
'efficientnet': models.efficientnet.EfficientNet
}
def load_model(model, path=f"./checkpoints/{model_name}.pt", print_msg=True):
try:
model = torch.load(path, map_location=torch.device(device))
# if print_msg:
# print(f"[I] Model loaded from {path}")
# j = 0
# dct = {}
# import pickle
# for name, param in model.state_dict().items():
# if (("weight" in name) and ("conv2" in name)):
# dct[name] = param
# print('number: ', j)
# print(param)
# j += 1
# with open('torch_pruning.pickle', 'wb') as handle:
# pickle.dump(dct, handle, protocol=pickle.HIGHEST_PROTOCOL)
return model
except:
if print_msg:
print(f"[E] Model failed to be loaded from {path}")
def model_size(model, count_zeros=True):
total_params = 0
nonzero_params = 0
for tensor in model.parameters():
t = np.prod(tensor.shape)
nz = np.sum(tensor.detach().cpu().numpy() != 0.0)
total_params += t
nonzero_params += nz
if not count_zeros:
return int(nonzero_params)
else:
return int(total_params)
model = model_names.get(model_name, models.mobilenetv1.MobileNet)()
model = model.to(torch.device(device))
# torchsummary.summary(model, (3, 32, 32))
# Define your loss and optimizer
criterion = nn.CrossEntropyLoss() # Softmax is internally computed.
optimizer = torch.optim.Adam(model.parameters())
# Training loop
max_acc = 0
accuracy = pd.DataFrame(index=range(finetune_epochs + 1), columns={'Testing'})
def train(model, epoch):
# Training phase
train_correct = 0
train_total = 0
train_loss = 0
# Sets the model in training mode.
model = model.train()
for batch_idx, (images, labels) in enumerate(train_loader):
images = images.to(torch.device(device))
labels = labels.to(torch.device(device))
# Sets the gradients to zero
optimizer.zero_grad()
# The actual inference
outputs = model(images)
# Compute the loss between the predictions (outputs) and the ground-truth labels
loss = criterion(outputs, labels)
# Do backpropagation to update the parameters of your model
loss.backward()
# Performs a single optimization step (parameter update)
optimizer.step()
train_loss += loss.item()
# The outputs are one-hot labels, we need to find the actual predicted
# labels which have the highest output confidence
_, predicted = outputs.max(1)
train_total += labels.size(0)
train_correct += predicted.eq(labels).sum().item()
# if (batch_idx + 1) % 100 == 0:
# print('Epoch: [%d/%d], Step: [%d/%d], Loss: %.4f Acc: %.2f%%' % (epoch + 1, finetune_epochs, batch_idx + 1,
# len(train_dataset) // batch_size,
# train_loss / (batch_idx + 1),
# 100. * train_correct / train_total))
# print('Accuracy of the model on the 60000 train images: % f %%' % (100. * train_correct / train_total))
return loss
def test(model, epoch):
global max_acc
test_correct = 0
test_total = 0
test_loss = 0
# Sets the model in evaluation mode
model = model.eval()
# Disabling gradient calculation is useful for inference.
# It will reduce memory consumption for computations.
with torch.no_grad():
for batch_idx, (images, labels) in enumerate(test_loader):
images = images.to(torch.device(device))
labels = labels.to(torch.device(device))
# Perform the actual inference
outputs = model(images)
# Compute the loss
loss = criterion(outputs, labels)
test_loss += loss.item()
# The outputs are one-hot labels, we need to find the actual predicted
# labels which have the highest output confidence
_, predicted = torch.max(outputs.data, 1)
test_total += labels.size(0)
test_correct += predicted.eq(labels).sum().item()
acc = 100. * test_correct / test_total
accuracy.loc[epoch + 1, 'Testing'] = acc
# print('Test loss: %.4f Test accuracy: %.2f %%' % (test_loss / (batch_idx + 1), acc))
if 100. * test_correct / test_total > max_acc:
max_acc = 100. * test_correct / test_total
torch.save(model, f"{model_path}/finetune_{epoch + 1}.pt")
return acc
# load model
model = load_model(model)
test(model, 0)
# Pruning
model = model.to(torch.device('cpu'))
# 1. setup strategy
if strategy_name == 'L1':
strategy = tp.strategy.L1Strategy()
elif strategy_name == 'L2':
strategy = tp.strategy.L2Strategy()
else:
strategy = tp.strategy.RandomStrategy()
# 2. build layer dependency for model
DG = tp.DependencyGraph()
DG.build_dependency(model, example_inputs=torch.randn(1, 3, 32, 32))
def prune_conv(conv, amount):
# 3. get a pruning plan from the dependency graph.
pruning_index = strategy(conv.weight, amount=amount)
pruning_plan = DG.get_pruning_plan(conv, tp.prune_conv, pruning_index)
pruning_plan.exec()
def prune_bn(bn, amount):
pruning_index = strategy(bn.weight, amount=amount)
pruning_plan = DG.get_pruning_plan(bn, tp.prune_batchnorm, pruning_index)
pruning_plan.exec()
def prune_linear(linear, amount):
pruning_index = strategy(linear.weight, amount=amount)
pruning_plan = DG.get_pruning_plan(linear, tp.prune_linear, pruning_index)
pruning_plan.exec()
if model_name == 'mobilenetv1':
# first layer
if layer == 'all':
prune_conv(model.conv1, amount=prune_val)
prune_bn(model.bn1, amount=prune_val)
# i = 0
for m in model.modules():
if isinstance(m, models.mobilenetv1.Block):
# print("number: ", i)
prune_conv(m.conv1, amount=prune_val)
prune_bn(m.bn1, amount=prune_val)
prune_conv(m.conv2, amount=prune_val)
prune_bn(m.bn2, amount=prune_val)
# i += 1
elif layer == 'one':
i = 0
for m in model.modules():
if isinstance(m, models.mobilenetv1.Block):
# print("number: ", i)
prune_conv(m.conv2, amount=prune_val)
i += 1
elif model_name == 'mobilenetv2':
if layer == 'all':
prune_conv(model.conv1, amount=prune_val)
prune_bn(model.bn1, amount=prune_val)
for m in model.modules():
if isinstance(m, models.mobilenetv2.Block):
prune_conv(m.conv1, amount=prune_val)
prune_bn(m.bn1, amount=prune_val)
prune_conv(m.conv2, amount=prune_val)
prune_bn(m.bn2, amount=prune_val)
prune_conv(m.conv3, amount=prune_val)
prune_bn(m.bn3, amount=prune_val)
prune_conv(model.conv2, amount=prune_val)
prune_bn(model.bn2, amount=prune_val)
# prune_linear(model.linear, amount=prune_val)
elif layer == 'one':
for m in model.modules():
if isinstance(m, models.mobilenetv2.Block):
prune_conv(m.conv1, amount=prune_val)
elif layer == 'two':
for m in model.modules():
if isinstance(m, models.mobilenetv2.Block):
prune_conv(m.conv1, amount=prune_val)
prune_conv(m.conv3, amount=prune_val)
elif layer == 'three':
for m in model.modules():
if isinstance(m, models.mobilenetv2.Block):
prune_conv(m.conv3, amount=prune_val)
elif model_name == 'mobilenetv3': # mobilenetv3
if layer == 'all':
prune_conv(model.conv1, amount=prune_val)
prune_bn(model.bn1, amount=prune_val)
for m in model.modules():
if isinstance(m, models.mobilenetv3.Block):
prune_conv(m.conv1, amount=prune_val)
prune_bn(m.bn1, amount=prune_val)
prune_conv(m.conv2, amount=prune_val)
prune_bn(m.bn2, amount=prune_val)
prune_conv(m.conv3, amount=prune_val)
prune_bn(m.bn3, amount=prune_val)
prune_conv(model.conv2, amount=prune_val)
prune_bn(model.bn2, amount=prune_val)
prune_conv(model.conv3, amount=prune_val)
if layer == 'one':
for m in model.modules():
if isinstance(m, models.mobilenetv3.Block):
prune_conv(m.conv1, amount=prune_val)
elif layer == 'two':
for m in model.modules():
if isinstance(m, models.mobilenetv3.Block):
prune_conv(m.conv1, amount=prune_val)
prune_conv(m.conv3, amount=prune_val)
elif layer == 'three':
for m in model.modules():
if isinstance(m, models.mobilenetv3.Block):
prune_conv(m.conv3, amount=prune_val)
elif model_name == 'vgg16':
if layer == 'all': # Prune Conv, Linear1, and Linear2
for m in model.modules():
if isinstance(m, nn.Conv2d):
prune_conv(m, amount=prune_val)
prune_linear(model.linear1, amount=prune_val)
prune_linear(model.linear2, amount=prune_val)
elif layer == 'one': # Prune Conv
for m in model.modules():
if isinstance(m, nn.Conv2d):
prune_conv(m, amount=prune_val)
else: # EfficientNet-b0
if layer == 'all': # Conv1, bn1, Blocks(Conv1, bn1, Conv2,bn2, SE, Conv3, bn3), Conv2, bn2
prune_conv(model.conv1, amount=prune_val)
prune_bn(model.bn1, amount=prune_val)
for m in model.modules():
if isinstance(m, models.efficientnet.Block):
prune_conv(m.conv1, amount=prune_val)
prune_bn(m.bn1, amount=prune_val)
prune_conv(m.conv2, amount=prune_val)
prune_bn(m.bn2, amount=prune_val)
prune_linear(m.se_linear1, amount=prune_val)
prune_linear(m.se_linear2, amount=prune_val)
prune_conv(m.conv3, amount=prune_val)
prune_bn(m.bn3, amount=prune_val)
prune_conv(model.conv2, amount=prune_val)
prune_bn(model.bn2, amount=prune_val)
elif layer == 'one': # Blocks(Conv1)
for m in model.modules():
if isinstance(m, models.efficientnet.Block):
prune_conv(m.conv1, amount=prune_val)
elif layer == 'two': # Blocks(Conv1, Conv3)
for m in model.modules():
if isinstance(m, models.efficientnet.Block):
prune_conv(m.conv1, amount=prune_val)
prune_conv(m.conv3, amount=prune_val)
elif layer == 'three': # Blocks (Conv3)
for m in model.modules():
if isinstance(m, models.efficientnet.Block):
prune_conv(m.conv3, amount=prune_val)
max_acc = 0
model = model.to(torch.device(device))
print("model size: ", model_size(model))
# No fine-tuning after pruning
# torchsummary.summary(model, (3, 32, 32))
first_acc = test(model, 0)
accuracy.loc[0, 'Testing'] = first_acc
torch.save(model, f"{model_path}/finetune_0.pt")
# No fine-tuning accuracy
with open(f'{model_name}/{layer}/{strategy_name}/no_finetuning_accuracy.csv', 'a') as f:
writer = csv.writer(f)
data = [prune_val, first_acc]
writer.writerow(data)
# Number of parameter
with open(f'{model_name}/{layer}/{strategy_name}/parameter_num.csv', 'a') as f1:
writer1 = csv.writer(f1)
parameter_num = model_size(model)
data = [prune_val, parameter_num]
writer1.writerow(data)
# Fine-tuning
for fine_tune_epoch in range(finetune_epochs):
train(model, fine_tune_epoch)
test(model, fine_tune_epoch)
# Fine-tuning accuracy
accuracy.to_csv(f'{model_path}/accuracy.csv', index=False)
# Fine-tuning best accuracy
with open(f'{model_name}/{layer}/{strategy_name}/finetuning_best_accuracy.csv', 'a') as f2:
writer2 = csv.writer(f2)
data = [prune_val, max_acc]
writer2.writerow(data)
# random_input = torch.randn(1, 3, 32, 32).to(device)
# torch.onnx.export(model, random_input, f'checkpoints/{model_name}_{prune_val}_{finetune_epochs}.onnx', export_params=True, opset_version=10)