-
Notifications
You must be signed in to change notification settings - Fork 6
/
modules.py
296 lines (264 loc) · 11.2 KB
/
modules.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
import torch
import torch.nn as nn
from torch.nn.parameter import Parameter
from torch.nn import functional as F
import spikingjelly.clock_driven.neuron as neuron
import spikingjelly.clock_driven.ann2snn.modules as spike_module
class SpikingNorm(nn.Module):
def __init__(self, momentum=0.1, scale=True, sigmoid=True ,eps=1e-6):
super(SpikingNorm, self).__init__()
self.sigmoid = sigmoid
self.eps = eps
self.lock_max = False
if scale:
self.scale = Parameter(torch.Tensor([1.0]))
else:
self.register_buffer('scale', torch.ones(1))
if self.sigmoid:
self.scale.data *= 10.0
self.momentum = momentum
self.register_buffer('running_max', torch.ones(1))
def calc_scale(self):
if self.sigmoid:
scale = torch.sigmoid(self.scale)
else:
scale = torch.abs(self.scale)
return scale
def calc_v_th(self):
return self.running_max * self.calc_scale() + self.eps
def forward(self, x):
if self.training and (not self.lock_max):
self.running_max = (1 - self.momentum) * self.running_max + self.momentum * torch.max(F.relu(x)).item()
x = torch.clamp( F.relu(x) / self.calc_v_th(), min=0.0, max=1.0)
# x = F.relu(x)
return x
def extra_repr(self):
if self.sigmoid:
return 'v_th={}, scale={}, running_max={}'.format(
self.calc_v_th(), torch.sigmoid(self.scale.data), self.running_max.data
)
else:
return 'v_th={}, scale={}, running_max={}'.format(
self.calc_v_th(), torch.abs(self.scale.data), self.running_max.data
)
def extract_running_max(self):
x = self.running_max.data
self.running_max.data = self.running_max.data / x
return x
def extract_scale(self):
x = self.scale.data
self.running_max.data = self.running_max.data * x
self.scale.data = torch.Tensor([1.0]).to(self.scale.device)
return x
class SpikingNormInv(nn.Module):
def __init__(self, momentum=0.1, scale=True, sigmoid=True ,eps=1e-6):
super(SpikingNormInv, self).__init__()
self.sigmoid = sigmoid
self.eps = eps
self.lock_max = False
if scale:
self.scale = Parameter(torch.Tensor([1.0]))
else:
self.register_buffer('scale', torch.ones(1))
if self.sigmoid:
self.scale.data *= 10.0
self.momentum = momentum
self.register_buffer('running_max', torch.ones(1))
def calc_scale(self):
if self.sigmoid:
scale = torch.sigmoid(self.scale)
else:
scale = torch.abs(self.scale)
return scale
def calc_v_th(self):
return (self.running_max + self.eps) / self.calc_scale()
def forward(self, x):
if self.training and (not self.lock_max):
self.running_max = (1 - self.momentum) * self.running_max + self.momentum * torch.max(F.relu(x)).item()
x = torch.clamp( F.relu(x) / self.calc_v_th(), min=0.0, max=1.0)
# x = F.relu(x)
return x
def extra_repr(self):
if self.sigmoid:
return 'v_th={}, scale={}, running_max={}'.format(
self.calc_v_th(), torch.sigmoid(self.scale.data), self.running_max.data
)
else:
return 'v_th={}, scale={}, running_max={}'.format(
self.calc_v_th(), torch.abs(self.scale.data), self.running_max.data
)
def extract_running_max(self):
x = self.running_max.data
self.running_max.data = self.running_max.data / x
return x
def extract_scale(self):
x = self.scale.data
self.running_max.data = self.running_max.data / x
self.scale.data = torch.Tensor([1.0]).to(self.scale.device)
return x
def replace_relu_by_spikingnorm_inv(model,scale=True,share_scale=None):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_relu_by_spikingnorm_inv(module,scale,share_scale)
if 'relu' in module.__class__.__name__.lower():
if not scale:
model._modules[name] = SpikingNormInv(scale=False)
else:
if share_scale is None:
model._modules[name] = SpikingNormInv()
else:
model._modules[name] = SpikingNormInv()
model._modules[name].scale = share_scale
return model
def replace_relu_by_spikingnorm(model,scale=True,share_scale=None):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_relu_by_spikingnorm(module,scale,share_scale)
if 'relu' in module.__class__.__name__.lower():
if not scale:
model._modules[name] = SpikingNorm(scale=False)
else:
if share_scale is None:
model._modules[name] = SpikingNorm()
else:
model._modules[name] = SpikingNorm()
model._modules[name].scale = share_scale
return model
def replace_maxpool2d_by_avgpool2d(model):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_maxpool2d_by_avgpool2d(module)
if module.__class__.__name__ == 'MaxPool2d':
model._modules[name] = nn.AvgPool2d(kernel_size=module.kernel_size,
stride=module.stride,
padding=module.padding)
return model
def replace_maxpool2d_by_spikemaxpool(model):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_maxpool2d_by_spikemaxpool(module)
if module.__class__.__name__ == 'MaxPool2d':
model._modules[name] = spike_module.MaxPool2d(kernel_size=module.kernel_size,
stride=module.stride,
padding=module.padding)
return model
class ClippedReLU(nn.Module):
def __init__(self):
super(ClippedReLU, self).__init__()
def forward(self, x):
return torch.clamp(F.relu(x), min=0.0, max=1.0)
def replace_relu_by_clippedrelu(model):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_relu_by_clippedrelu(module)
if module.__class__.__name__ == 'ReLU':
model._modules[name] = ClippedReLU()
return model
def remove_batchnorm(model):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = remove_batchnorm(module)
if "BatchNorm" in module.__class__.__name__:
model._modules[name] = nn.Sequential()
return model
def replace_spikingnorm_by_ifnode(model):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_spikingnorm_by_ifnode(module)
if module.__class__.__name__ == "SpikingNorm":
model._modules[name] = neuron.IFNode(v_threshold=module.calc_v_th().data,v_reset=None)
return model
def replace_relu_by_ifnode(model):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_relu_by_ifnode(module)
if module.__class__.__name__ == "ReLU":
model._modules[name] = neuron.IFNode(v_reset=None)
return model
def replace_ifnode_by_relu(model):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_ifnode_by_relu(module)
if module.__class__.__name__ == "IFNode":
model._modules[name] = nn.ReLU()
return model
# def replace_spikingnorm_by_scaledifnode(model):
# for name, module in model._modules.items():
# if hasattr(module,"_modules"):
# model._modules[name] = replace_spikingnorm_by_scaledifnode(module)
# if module.__class__.__name__ == "SpikingNorm":
# model._modules[name] = neuron.ScaledIFNode(v_threshold=module.calc_v_th().data*0.75,emit=module.calc_v_th().data,v_reset=None)
# return model
def replace_ifnode_by_spikingnorm(model,sigmoid=False):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_ifnode_by_spikingnorm(module,sigmoid)
if module.__class__.__name__ == "IFNode":
model._modules[name] = SpikingNorm(sigmoid=sigmoid)
return model
def remove_runningmax_from_spikingnorm(model):
last_param_module = None
for name, module in model.named_modules():
if isinstance(module,(nn.Linear,nn.Conv2d)):
last_param_module = module
if isinstance(module,SpikingNorm):
if last_param_module is not None:
if hasattr(last_param_module, 'weight'):
running_max = module.extract_running_max()
last_param_module.weight.data = last_param_module.weight.data / running_max
if hasattr(last_param_module,'bias') and last_param_module.bias is not None:
last_param_module.bias.data = last_param_module.bias.data / running_max
last_param_module = None
return model
def replace_spikingnorm_by_relu(model, scale=1.0):
for name, module in model._modules.items():
if hasattr(module,"_modules"):
model._modules[name] = replace_spikingnorm_by_relu(module, scale)
if module.__class__.__name__ == "SpikingNorm":
model._modules[name] = nn.ReLU()
return model
if __name__ == "__main__":
pass
# print(torch.sigmoid(torch.FloatTensor([5])))
# exit(-1)
# # pass
# # x = torch.tensor([[0.2,0.3],[0.4,0.5]])
# # scale = 0.2
# #
# # def layerwise_loss(a):
# # #print(torch.pow(torch.norm(a, 2), 2))
# # k = torch.sum(a) / (torch.pow(torch.norm(a, 2), 2))
# # return k
# # print(layerwise_loss(torch.clamp(x/scale,0,1)))
# # print(torch.clamp(x/scale,0,1))
#
# import spikingjelly.clock_driven.neuron as neuron
# import spikingjelly.clock_driven.functional as functional
# import numpy as np
# import matplotlib.pyplot as plt
#
# n = neuron.ScaledIFNode(v_threshold=5.0,v_reset=None)
#
# y = []
# a = []
# h = np.linspace(-1, 10, 100)
# for i in h:
# x = torch.FloatTensor([i])
# functional.reset_net(n)
# sum = 0
# for t in range(100):
# s = n(x)
# # print(s)
# sum += s
# sum /= 100
# # print(sum)
# a.append(x.item())
# y.append(sum.item())
# plt.plot(a,y)
# plt.show()
#
#
# h2 = torch.from_numpy(h)
# x = 5.0 * torch.clamp(h2 / 5.0,0,1)
# plt.plot(a,x)
# plt.show()