-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
144 lines (119 loc) · 4.85 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from spikingjelly import cext
from spikingjelly.cext import neuron as cext_neuron
from spikingjelly.clock_driven import neuron, functional, surrogate, layer
class ANN(nn.Module):
# in_c : input channels, nf : number of filters, ks : kernel size
def __init__(self, in_c = 1, nf = [16,32], ks=[5,5], dropout=0.5):
super().__init__()
self.convLayer1 = nn.Sequential(
nn.Conv2d(in_c, nf[0], kernel_size=ks[0], bias=False),
nn.BatchNorm2d(nf[0]),
nn.ReLU(),
nn.MaxPool2d(2,2)
)
self.dim1 = (28 - ks[0] + 1) // 2
self.convLayer2 = nn.Sequential(
nn.Conv2d(nf[0],nf[1], kernel_size=ks[1], bias=False),
nn.BatchNorm2d(nf[1]),
nn.ReLU(),
nn.MaxPool2d(2,2)
)
self.dim2 = (self.dim1 - ks[1] + 1) // 2
self.fc = nn.Sequential(
nn.Flatten(),
nn.Dropout(dropout),
nn.Linear(nf[1]*self.dim2**2, 10, bias=False),
nn.Softmax(dim=1)
)
def forward(self, x):
x = self.convLayer1(x)
x = self.convLayer2(x)
return self.fc(x)
class SNN(nn.Module):
def __init__(self, in_c = 1,nf = [16,32], ks=[5,5], T = 10, v_threshold = 1.0, v_reset = 0.0, dropout=0.5, use_softmax = False, tau=2.0, lif=False):
super().__init__()
self.T = T
self.use_softmax = use_softmax
self.static_conv = nn.Sequential(
nn.Conv2d(in_c, nf[0], kernel_size=ks[0], bias=False),
nn.BatchNorm2d(nf[0])
)
self.dim1 = (28 - ks[0] + 1) // 2
self.conv = nn.Sequential(
neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan(), detach_reset=True),
nn.MaxPool2d(2, 2),
nn.Conv2d(nf[0], nf[1], kernel_size=ks[1], bias=False),
nn.BatchNorm2d(nf[1]),
neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan(), detach_reset=True),
nn.MaxPool2d(2, 2)
)
if lif:
self.conv[0] = neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan())
self.conv[4] = neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan())
self.dim2 = (self.dim1 - ks[1] + 1) // 2
self.fc = nn.Sequential(
nn.Flatten(),
layer.Dropout(dropout),
nn.Linear(nf[1]*self.dim2**2, 10, bias=False),
neuron.IFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan(), detach_reset=True)
)
if lif:
self.fc[-1] = neuron.LIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function=surrogate.ATan())
if self.use_softmax:
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
x = self.static_conv(x)
x2 = self.conv(x).unsqueeze(0)
out_spikes_counter = self.fc(x2[-1])
for t in range(1, self.T):
x2 = torch.cat([x2, self.conv(x).unsqueeze(0)])
out_spikes_counter += self.fc(x2[-1])
if self.use_softmax:
return self.softmax(out_spikes_counter)
return out_spikes_counter / self.T, x2
class SNN_cuda(nn.Module):
def __init__(self, in_c = 1,nf = [16,32], ks=[5,5], T = 10, v_threshold = 1.0, v_reset = 0.0, dropout=0.5, use_softmax = False, tau=2.0, lif=False):
super().__init__()
self.T = T
self.use_softmax = use_softmax
self.static_conv = nn.Sequential(
nn.Conv2d(in_c, nf[0], kernel_size=ks[0], bias=False),
nn.BatchNorm2d(nf[0])
)
self.dim1 = (28 - ks[0] + 1) // 2
self.conv = nn.Sequential(
cext_neuron.MultiStepIFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function='ATan', alpha=2.0, detach_reset=True),
layer.SeqToANNContainer(
nn.MaxPool2d(2, 2),
nn.Conv2d(nf[0], nf[1], kernel_size=ks[1], bias=False),
nn.BatchNorm2d(nf[1])
),
cext_neuron.MultiStepIFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function='ATan', alpha=2.0, detach_reset=True),
)
if lif:
self.conv[0] = cext_neuron.MultiStepLIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function='ATan', alpha=2.0)
self.conv[2] = cext_neuron.MultiStepLIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function='ATan', alpha=2.0)
self.dim2 = (self.dim1 - ks[1] + 1) // 2
self.fc = nn.Sequential(
layer.SeqToANNContainer(
nn.MaxPool2d(2, 2),
nn.Flatten(),
),
layer.MultiStepDropout(dropout),
layer.SeqToANNContainer(nn.Linear(nf[1]*self.dim2**2, 10, bias=False)),
cext_neuron.MultiStepIFNode(v_threshold=v_threshold, v_reset=v_reset, surrogate_function='ATan', alpha=2.0, detach_reset=True)
)
if lif:
self.fc[-1] = cext_neuron.MultiStepLIFNode(tau=tau, v_threshold=v_threshold, v_reset=v_reset, surrogate_function='ATan', alpha=2.0)
if self.use_softmax:
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
x_seq = self.static_conv(x).unsqueeze(0).repeat(self.T, 1, 1, 1, 1)
# [N, C, H, W] -> [1, N, C, H, W] -> [T, N, C, H, W]
out_spikes_counter = self.fc(self.conv(x_seq)).sum(0)
if self.use_softmax:
return self.softmax(out_spikes_counter)
return out_spikes_counter / self.T