-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
162 lines (140 loc) · 5.5 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
import time
import torch
import numpy as np
import torch.nn as nn
import torchvision.transforms as transforms
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 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 hook_fn(layer_name, args):
def _all_zero_check(module, input, output):
p_spikes = input[0] #? The input is a tuple, second element is the device
hook_fn.weighted_layers[layer_name] = p_spikes.shape[1]*p_spikes.shape[2]*p_spikes.shape[3]
timesteps = args.timestep
b = int(p_spikes.shape[0]/timesteps)
for i in range(timesteps+1):
num_spikes = (p_spikes.view(timesteps,b,-1).sum(0)==i).sum()
total_elements = torch.numel(p_spikes.view(timesteps,b,-1).sum(0))
percent_spikes = num_spikes / total_elements
hook_fn.results[layer_name][i] += percent_spikes.item()
return _all_zero_check
def print_nonzeros(model):
nonzero = total = 0
for name, p in model.named_modules():
if isinstance(p, nn.Conv2d) or isinstance(p, nn.Linear):
tensor = p.weight.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))
# 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)