-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
319 lines (261 loc) · 10.4 KB
/
utils.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
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
import time
import torchvision.transforms as transforms
class AvgrageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.avg = 0
self.sum = 0
self.cnt = 0
def update(self, val, n=1):
self.sum += val * n
self.cnt += n
self.avg = self.sum / self.cnt
class Cutout(object):
"""Randomly mask out one or more patches from an image.
Args:
n_holes (int): Number of patches to cut out of each image.
length (int): The length (in pixels) of each square patch.
"""
def __init__(self, n_holes, length):
self.n_holes = n_holes
self.length = length
def __call__(self, img):
"""
Args:
img (Tensor): Tensor image of size (C, H, W).
Returns:
Tensor: Image with n_holes of dimension length x length cut out of it.
"""
h = img.size(1)
w = img.size(2)
mask = np.ones((h, w), np.float32)
for n in range(self.n_holes):
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
mask[y1: y2, x1: x2] = 0.
mask = torch.from_numpy(mask)
mask = mask.expand_as(img)
img = img * mask
return img
def accuracy(output, label, topk=(1,)):
maxk = max(topk)
batch_size = label.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(label.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].flatten().float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def save_checkpoint(state, iters, tag=''):
if not os.path.exists("./snapshots"):
os.makedirs("./snapshots")
filename = os.path.join("./snapshots/{}_ckpt_{:04}.pth.tar".format(tag, iters))
torch.save(state, filename)
def data_transforms(args):
if args.dataset == 'fmnist':
MEAN = [0.5]
STD = [0.5]
elif args.dataset == 'svhn':
MEAN = [0.5,0.5,0.5]
STD = [0.5,0.5,0.5]
elif args.dataset == 'cifar10':
MEAN = [0.4913, 0.4821, 0.4465]
STD = [0.2023, 0.1994, 0.2010]
elif args.dataset == 'cifar100':
MEAN = [0.5071, 0.4867, 0.4408]
STD = [0.2673, 0.2564, 0.2762]
elif args.dataset == 'tinyimagenet':
MEAN = [0.485, 0.456, 0.406]
STD = [0.229, 0.224, 0.225]
if (args.dataset== 'fmnist'):
train_transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.Resize(32),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
valid_transform = transforms.Compose([
transforms.Resize(32),
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
elif (args.dataset== 'svhn'):
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
elif (args.dataset== 'tinyimagenet'):
train_transform = transforms.Compose([
transforms.RandomCrop(64, padding=8),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
elif (args.dataset == 'cifar10'):
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
Cutout(n_holes=1, length=16),
transforms.Normalize(MEAN, STD)
])
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
else:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
Cutout(n_holes=1, length=16),
transforms.Normalize(MEAN, STD)
])
valid_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(MEAN, STD)
])
return train_transform, valid_transform
def random_choice(num_choice, layers):
return list(np.random.randint(num_choice, size=layers))
def print_nonzeros(model):
nonzero = total = 0
for name, p in model.named_parameters():
tensor = p.data.cpu().numpy()
nz_count = np.count_nonzero(tensor)
total_params = np.prod(tensor.shape)
nonzero += nz_count
total += total_params
print(f'{name:20} | nonzeros = {nz_count:7} / {total_params:7} ({100 * nz_count / total_params:6.2f}%) | total_pruned = {total_params - nz_count :7} | shape = {tensor.shape}')
print(f'alive: {nonzero}, pruned : {total - nonzero}, total: {total}, Compression rate : {total/nonzero:10.2f}x ({100 * (total-nonzero) / total:6.2f}% pruned)')
return (round((nonzero/total)*100,1))
def time_record(start):
end = time.time()
duration = end - start
hour = duration // 3600
minute = (duration - hour * 3600) // 60
second = duration - hour * 3600 - minute * 60
print('Elapsed time: hour: %d, minute: %d, second: %f' % (hour, minute, second))
# ANCHOR Libraries
import numpy as np
import torch
import os
import seaborn as sns
import matplotlib.pyplot as plt
import copy
# ANCHOR Print table of zeros and non-zeros count
def print_nonzeros(model):
nonzero = total = 0
for name, p in model.named_parameters():
tensor = p.data.cpu().numpy()
nz_count = np.count_nonzero(tensor)
total_params = np.prod(tensor.shape)
nonzero += nz_count
total += total_params
# print(
# f'{name:20} | nonzeros = {nz_count:7} / {total_params:7} ({100 * nz_count / total_params:6.2f}%) | total_pruned = {total_params - nz_count :7} | shape = {tensor.shape}')
print(
f'alive: {nonzero}, pruned : {total - nonzero}, total: {total}, Compression rate : {total / nonzero:10.2f}x ({100 * (total - nonzero) / total:6.2f}% pruned)')
return (round((nonzero / total) * 100, 1))
def print_nonzeros_all(model):
nonzero = total = 0
for name, p in model.named_parameters():
tensor = p.data.cpu().numpy()
nz_count = np.count_nonzero(tensor)
total_params = np.prod(tensor.shape)
nonzero += nz_count
total += total_params
print(
f'{name:20} | nonzeros = {nz_count:7} / {total_params:7} ({100 * nz_count / total_params:6.2f}%) | total_pruned = {total_params - nz_count :7} | shape = {tensor.shape}')
print(
f'alive: {nonzero}, pruned : {total - nonzero}, total: {total}, Compression rate : {total / nonzero:10.2f}x ({100 * (total - nonzero) / total:6.2f}% pruned)')
return (round((nonzero / total) * 100, 1))
def print_nonzeros_weight(model):
nonzero = total = 0
for name, p in model.named_parameters():
if "weight" in name:
tensor = p.data.cpu().numpy()
nz_count = np.count_nonzero(tensor)
total_params = np.prod(tensor.shape)
nonzero += nz_count
total += total_params
# print(
# f'{name:20} | nonzeros = {nz_count:7} / {total_params:7} ({100 * nz_count / total_params:6.2f}%) | total_pruned = {total_params - nz_count :7} | shape = {tensor.shape}')
print(
f'alive: {nonzero}, pruned : {total - nonzero}, total: {total}, Compression rate : {total / nonzero:10.2f}x ({100 * (total - nonzero) / total:6.2f}% pruned)')
return (round((nonzero / total) * 100, 1))
def original_initialization(mask_temp, initial_state_dict):
global model
step = 0
for name, param in model.named_parameters():
if "weight" in name:
weight_dev = param.device
param.data = torch.from_numpy(mask_temp[step] * initial_state_dict[name].cpu().numpy()).to(weight_dev)
step = step + 1
if "bias" in name:
param.data = initial_state_dict[name]
step = 0
# ANCHOR Checks of the directory exist and if not, creates a new directory
def checkdir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
# FIXME
def plot_train_test_stats(stats,
epoch_num,
key1='train',
key2='test',
key1_label=None,
key2_label=None,
xlabel=None,
ylabel=None,
title=None,
yscale=None,
ylim_bottom=None,
ylim_top=None,
savefig=None,
sns_style='darkgrid'
):
assert len(stats[key1]) == epoch_num, "len(stats['{}'])({}) != epoch_num({})".format(key1, len(stats[key1]),
epoch_num)
assert len(stats[key2]) == epoch_num, "len(stats['{}'])({}) != epoch_num({})".format(key2, len(stats[key2]),
epoch_num)
plt.clf()
sns.set_style(sns_style)
x_ticks = np.arange(epoch_num)
plt.plot(x_ticks, stats[key1], label=key1_label)
plt.plot(x_ticks, stats[key2], label=key2_label)
if xlabel is not None:
plt.xlabel(xlabel)
if ylabel is not None:
plt.ylabel(ylabel)
if title is not None:
plt.title(title)
if yscale is not None:
plt.yscale(yscale)
if ylim_bottom is not None:
plt.ylim(bottom=ylim_bottom)
if ylim_top is not None:
plt.ylim(top=ylim_top)
plt.legend(bbox_to_anchor=(1.04, 0.5), loc="center left", borderaxespad=0, fancybox=True)
if savefig is not None:
plt.savefig(savefig, bbox_inches='tight')
else:
plt.show()