-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
94 lines (73 loc) · 3.15 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
import torch
import torch.nn as nn
from modules import *
def isActivation(name):
if 'relu' in name.lower() or 'qcfs' in name.lower():
return True
return False
def replace_MPLayer_by_neuron(model):
for name, module in model._modules.items():
if hasattr(module, "_modules"):
model._modules[name] = replace_MPLayer_by_neuron(module)
if module.__class__.__name__ == 'MPLayer':
model._modules[name] = IFNeuron(scale=module.v_threshold)
return model
def replace_activation_by_MPLayer(model, presim_len, sim_len, batchsize):
for name, module in model._modules.items():
if hasattr(module, "_modules"):
model._modules[name] = replace_activation_by_MPLayer(module, presim_len, sim_len, batchsize)
if isActivation(module.__class__.__name__.lower()):
model._modules[name] = MPLayer(v_threshold=module.up.item(), presim_len=presim_len, sim_len=sim_len, batchsize=batchsize)
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_activation_by_floor(model, t):
for name, module in model._modules.items():
if hasattr(module, "_modules"):
model._modules[name] = replace_activation_by_floor(module, t)
if isActivation(module.__class__.__name__.lower()):
model._modules[name] = QCFS(up=8., t=t)
return model
def reset_net(model):
for name, module in model._modules.items():
if hasattr(module, "_modules"):
reset_net(module)
if 'Neuron' in module.__class__.__name__:
module.reset()
return model
def calculate_MPLayer(model,ans):
for name, module in model._modules.items():
if hasattr(module, "_modules"):
calculate_MPLayer(module,ans)
if module.__class__.__name__ == 'MPLayer':
tot_input, tot_one, tot_two, tot_three, tot_zero = 0,0,0,0,0
tot_len = len(module.arr2) / 4.
for i in range(0,len(module.arr2),4):
tot_one += module.arr2[i+1]
tot_two += module.arr2[i+2]
tot_three += module.arr2[i+3]
tot_zero += module.arr2[i]
ans.append(tot_zero.item()/tot_len)
ans.append(tot_one.item()/tot_len)
ans.append(tot_two.item()/tot_len)
ans.append(tot_three.item()/tot_len)
module.arr = []
module.arr2 = []
return model
def set_MPLayer(model,snn):
for name, module in model._modules.items():
if hasattr(module, "_modules"):
set_MPLayer(module,snn)
if module.__class__.__name__ == 'MPLayer':
module.snn_mode = snn
return model
def error(info):
print(info)
exit(1)