-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpruning_pt.py
More file actions
309 lines (269 loc) · 11.1 KB
/
pruning_pt.py
File metadata and controls
309 lines (269 loc) · 11.1 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
import csv
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import matplotlib.pyplot as plt
from torch.utils.tensorboard import SummaryWriter
import torchsummary
import argparse
from mobilenet_rm_filt_pt import MobileNet, remove_channel
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# Argument parser
parser = argparse.ArgumentParser(description='Pruning MobileNet V1, V2, and V3')
parser.add_argument('--batch_size', type=int, default=128, help='Number of samples per mini-batch')
parser.add_argument('--finetune_epochs', type=int, default=0, help='Number of epochs to finetune')
parser.add_argument('--model', type=str, default='mobilenetv1', help='mobilenetv1, mobilenetv2, or mobilenetv3')
parser.add_argument('--prune', type=float, default=0.0)
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
pruning_method = "chn_prune"
# pruning_method = "mag_prune"
fine_tune = True
print('model: ', model_name, ' layer: ', layer, ' prune_val: ', prune_val, ' strategy: ', strategy_name)
model_path = f"{model_name}/code/prune_{prune_val}"
# 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)
def load_model(model, path="./checkpoints/mobilenetv1.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('code_pruning.pickle', 'wb') as handle:
# pickle.dump(dct, handle, protocol=pickle.HIGHEST_PROTOCOL)
return model
except BaseException as err:
print(f"Unexpected {err=}, {type(err)=}")
# 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 = MobileNet()
model.load_state_dict(torch.load('./checkpoints/mobilenetv1.pt').state_dict())
model = model.to(torch.device('cuda'))
criterion = nn.CrossEntropyLoss() # Softmax is internally computed.
optimizer = torch.optim.Adam(model.parameters())
iteration = 0
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
# if load_my_model:
# load_model(model)
# test(0, mode='Non-pruned model', value='True')
# else:
# for epoch in range(num_epochs):
# train(epoch)
# test(epoch, mode='test', value='True')
# exit(0)
# model = load_model(model)
test(model, 0)
def prune_model_thres(model, threshold):
mask_dict = {}
for name, param in model.state_dict().items():
if ("weight" in name):
param[param.abs() < threshold] = 0
mask_dict[name] = torch.where(torch.abs(param) > 0, 1, 0)
model.mask_dict = mask_dict
def channel_fraction_pruning(model, fraction=0.2):
mask_dict = {}
number = 0
for name, param in model.state_dict().items():
if (("weight" in name) and ("conv2" in name)):
# print('number: ', number)
# print(param)
score_list = torch.sum(torch.abs(param), dim=(1, 2, 3)).to('cpu')
# print(score_list)
removed_idx = []
threshold = np.percentile(np.abs(score_list), fraction * 100)
# print('threshold: ', threshold)
for i, score in enumerate(score_list):
# print('i: ', i, 'score: ', score)
if score < threshold:
removed_idx.append(i)
param[removed_idx, :, :, :] = 0
mask_dict[name] = torch.where(torch.abs(param) > 0, 1, 0)
# print(len(removed_idx))
# print(removed_idx)
number += 1
model.mask_dict = mask_dict
# print(mask_dict)
res1 = []
num_params = []
# for prune_thres in np.arange(0, 1, 0.1):
# print('Before pruning:', model_size(model))
# if pruning_method == 'mag_prune':
# prune_model_thres(model, prune_thres)
# else:
# channel_fraction_pruning(model, prune_thres)
# model._apply_mask()
# model = remove_channel(model)
# print('After pruning:', model_size(model))
# model = model.to(device)
# # if fine_tune:
# # for fine_tune_epoch in range(1):
# # train(fine_tune_epoch)
# # train(fine_tune_epoch)
# acc = test(1, "thres", prune_thres)
# res1.append([prune_thres, acc])
# num_params.append(model_size(model, prune_thres == 0))
# print('Before pruning:', model_size(model))
# print(model.layers[11].conv1.weight)
if pruning_method == 'mag_prune':
prune_model_thres(model, prune_val)
else:
channel_fraction_pruning(model, prune_val)
model._apply_mask()
model = remove_channel(model)
# print('After pruning:', model_size(model))
print(model.layers[11].conv1.weight)
model = model.to(device)
# torchsummary.summary(model, (3, 32, 32))
max_acc = 0
print("model size: ", model_size(model))
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}/code/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}/code/parameter_num.csv', 'a') as f1:
writer1 = csv.writer(f1)
parameter_num = model_size(model)
data = [prune_val, parameter_num]
writer1.writerow(data)
for fine_tune_epoch in range(finetune_epochs):
train(model, fine_tune_epoch)
test(model, fine_tune_epoch)
accuracy.to_csv(f'{model_path}/accuracy.csv', index=False)
# Fine-tuning best accuracy
with open(f'{model_name}/code/finetuning_best_accuracy.csv', 'a') as f2:
writer2 = csv.writer(f2)
data = [prune_val, max_acc]
writer2.writerow(data)
# plt.figure()
# plt.plot(num_params, res1[:, 1])
# plt.title('{}: vs #Params'.format(pruning_method))
# plt.xlabel('Number of parameters')
# plt.ylabel('Test accuracy')
# plt.savefig('{}_param_fine_tune_{}.png'.format(pruning_method, fine_tune))
# plt.close()
#
# plt.figure()
# plt.plot(np.arange(0, 1, 0.02), res1[:, 1])
# plt.title('{}: Accuracy vs Threshold'.format(pruning_method))
# plt.xlabel('Pruning Threshold')
# plt.ylabel('Test accuracy')
# plt.savefig('{}_thresh_fine_tune_{}.png'.format(pruning_method, fine_tune))
# plt.close()