-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
1001 lines (817 loc) · 43.6 KB
/
model.py
File metadata and controls
1001 lines (817 loc) · 43.6 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
import torch
import torch.nn as nn
import torchvision
import math
import snntorch as snn
from snntorch import utils
from snntorch import surrogate
def get_SpikeSEE_autoencoder(cfg):
# init encoder and decoder
encoder = SpikeSEE_Encoder(in_channels=cfg['in_channels'],
n_electrodes=cfg['n_electrodes'],
out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
decoder = SpikeSEE_Decoder(out_channels=cfg['out_channels'],
out_activation=cfg['decoder_out_activation']).to(cfg['device'])
# If output steps are specified, add safety layer at the end of the encoder model
if cfg['output_steps'] != 'None':
assert cfg['encoder_out_activation'] == 'sigmoid'
encoder.output_scaling = 1.0
encoder = torch.nn.Sequential(encoder,
SafetyLayer(n_steps=10,
order=2,
out_scaling=cfg['output_scaling'])).to(cfg['device'])
return encoder, decoder
def get_MVH_autoencoder(cfg):
# init encoder and decoder
encoder = SpikeNN_Encoder(in_channels=cfg['in_channels'],
n_electrodes=cfg['n_electrodes'],
out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
decoder = SpikeNN_Decoder(out_channels=cfg['out_channels'],
n_electrodes=cfg['n_electrodes'],
out_activation=cfg['decoder_out_activation']).to(cfg['device'])
# If output steps are specified, add safety layer at the end of the encoder model
if cfg['output_steps'] != 'None':
assert cfg['encoder_out_activation'] == 'sigmoid'
encoder.output_scaling = 1.0
encoder = torch.nn.Sequential(encoder,
SafetyLayer(n_steps=10,
order=2,
out_scaling=cfg['output_scaling'])).to(cfg['device'])
return encoder, decoder
def get_vanilla_autoencoder(cfg):
spike_grad = surrogate.atan(alpha=2.0)
# init encoder and decoder
encoder = Vanilla_SNN_Encoder(spike_grad=spike_grad,
in_channels=cfg['in_channels'],
n_electrodes=cfg['n_electrodes'],
out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
decoder = Vanilla_SNN_Decoder(spike_grad=spike_grad,
in_channels=cfg['in_channels'],
out_channels=cfg['out_channels'],
n_electrodes=cfg['n_electrodes'],
out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
return encoder, decoder
def get_vanilla_autoencoder_extended(cfg):
spike_grad = surrogate.atan(alpha=2.0)
# init encoder and decoder
encoder = Vanilla_SNN_Encoder_STIM(spike_grad=spike_grad,
in_channels=cfg['in_channels'],
n_electrodes=cfg['n_electrodes'],
out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
decoder = E2E_Decoder(# spike_grad=spike_grad,
in_channels=cfg['in_channels'],
out_channels=cfg['out_channels'],
# n_electrodes=cfg['n_electrodes'],
# out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
return encoder, decoder
def get_vanilla_autoencoder_extended_test(cfg):
spike_grad = surrogate.atan(alpha=2.0)
# init encoder and decoder
encoder = Vanilla_SNN_Encoder_STIM_SPLIT2(spike_grad=spike_grad,
in_channels=cfg['in_channels'],
n_electrodes=cfg['n_electrodes'],
out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
decoder = E2E_Decoder(# spike_grad=spike_grad,
in_channels=cfg['in_channels'],
out_channels=cfg['out_channels'],
# n_electrodes=cfg['n_electrodes'],
# out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
return encoder, decoder
def get_e2e_autoencoder(cfg):
# initialize encoder and decoder
encoder = E2E_Encoder(in_channels=cfg['in_channels'],
n_electrodes=cfg['n_electrodes'],
out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
decoder = E2E_Decoder(out_channels=cfg['out_channels'],
out_activation=cfg['decoder_out_activation']).to(cfg['device'])
# If output steps are specified, add safety layer at the end of the encoder model
if cfg['output_steps'] != 'None':
assert cfg['encoder_out_activation'] == 'sigmoid'
encoder.output_scaling = 1.0
encoder = torch.nn.Sequential(encoder,
SafetyLayer(n_steps=10,
order=2,
out_scaling=cfg['output_scaling'])).to(cfg['device'])
return encoder, decoder
def get_Zhao_autoencoder(cfg):
encoder = ZhaoEncoder(in_channels=cfg['in_channels'], n_electrodes=cfg['n_electrodes']).to(cfg['device'])
decoder = ZhaoDecoder(out_channels=cfg['out_channels'], out_activation=cfg['decoder_out_activation']).to(cfg['device'])
return encoder, decoder
def get_beta_autoencoder(cfg):
encoder = SpikeNN_Encoder_Pehuen(in_channels=cfg['in_channels'], n_electrodes=cfg['n_electrodes']).to(cfg['device'])
decoder = E2E_Decoder( # spike_grad=spike_grad,
in_channels=cfg['in_channels'],
out_channels=cfg['out_channels'],
# n_electrodes=cfg['n_electrodes'],
# out_scaling=cfg['output_scaling'],
out_activation=cfg['encoder_out_activation']).to(cfg['device'])
return encoder, decoder
def convlayer(n_input, n_output, k_size=3, stride=1, padding=1, resample_out=None):
layer = [
nn.Conv2d(n_input, n_output, kernel_size=k_size, stride=stride, padding=padding, bias=False),
nn.BatchNorm2d(n_output),
nn.LeakyReLU(inplace=True),
resample_out]
if resample_out is None:
layer.pop()
return layer
def convlayer3d(n_input, n_output, k_size=3, stride=1, padding=1, resample_out=None):
layer = [
nn.Conv3d(n_input, n_output, kernel_size=k_size, stride=stride, padding=padding, bias=False),
nn.BatchNorm3d(n_output),
nn.LeakyReLU(inplace=True),
resample_out]
if resample_out is None:
layer.pop()
return layer
def deconvlayer3d(n_input, n_output, k_size=2, stride=2, padding=0, dilation=1, resample_out=None):
layer = [
nn.ConvTranspose3d(n_input, n_output, kernel_size=k_size, stride=stride, padding=padding, dilation=dilation, bias=False),
nn.BatchNorm3d(n_output),
nn.LeakyReLU(inplace=True),
resample_out]
if resample_out is None:
layer.pop()
return layer
class ResidualBlock(nn.Module):
def __init__(self, n_channels, stride=1, resample_out=None):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(n_channels, n_channels,kernel_size=3, stride=1,padding=1)
self.bn1 = nn.BatchNorm2d(n_channels)
self.relu = nn.LeakyReLU(inplace=True)
self.conv2 = nn.Conv2d(n_channels, n_channels,kernel_size=3, stride=1,padding=1)
self.bn2 = nn.BatchNorm2d(n_channels)
self.resample_out = resample_out
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += residual
out = self.relu(out)
if self.resample_out:
out = self.resample_out(out)
return out
class SafetyLayer(torch.nn.Module):
def __init__(self, n_steps=5, order=1, out_scaling=120e-6):
super(SafetyLayer, self).__init__()
self.n_steps = n_steps
self.order = order
self.output_scaling = out_scaling
def stairs(self, x):
"""Assumes input x in range [0,1]. Returns quantized output over range [0,1] with n quantization levels"""
return torch.round((self.n_steps-1)*x)/(self.n_steps-1)
def softstairs(self, x):
"""Assumes input x in range [0,1]. Returns sin(x) + x (soft staircase), scaled to range [0,1].
param n: number of phases (soft quantization levels)
param order: number of recursion levels (determining the steepnes of the soft quantization)"""
return (torch.sin(((self.n_steps - 1) * x - 0.5) * 2 * math.pi) +
(self.n_steps - 1) * x * 2 * math.pi) / ((self.n_steps - 1) * 2 * math.pi)
def forward(self, x):
out = self.softstairs(x) + self.stairs(x).detach() - self.softstairs(x).detach()
return (out * self.output_scaling).clamp(1e-32,None)
class VGGFeatureExtractor():
def __init__(self,layer_names=['1','3','6','8'], layer_depth=9 ,device='cpu'):
# Load the VGG16 model
model = torchvision.models.vgg16(weights=torchvision.models.VGG16_Weights.IMAGENET1K_V1)
self.feature_extractor = torch.nn.Sequential(*[*model.features][:layer_depth]).to(device)
# Register a forward hook for each layer of interest
self.layers = {name: layer for name, layer in self.feature_extractor.named_children() if name in layer_names}
self.outputs = dict()
for name, layer in self.layers.items():
layer.__name__ = name
layer.register_forward_hook(self.store_output)
def store_output(self, layer, input, output):
self.outputs[layer.__name__] = output
def __call__(self, x):
# If grayscale, convert to RGB
if x.shape[1] == 1:
x = x.repeat(1,3,1,1)
# Forward pass
self.feature_extractor(x)
activations = list(self.outputs.values())
return activations
class E2E_Encoder(nn.Module):
"""
Simple non-generic encoder class that receives 128x128 input and outputs 32x32 feature map as stimulation protocol
"""
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu'):
super(E2E_Encoder, self).__init__()
self.output_scaling = out_scaling
self.out_activation = {'tanh': nn.Tanh(), ## NOTE: simulator expects only positive stimulation values
'sigmoid': nn.Sigmoid(),
'relu': nn.ReLU(),
'softmax':nn.Softmax(dim=1)}[out_activation]
# Model
self.model = nn.Sequential(*convlayer(in_channels,8,3,1,1),
*convlayer(8,16,3,1,1,resample_out=nn.MaxPool2d(2)),
*convlayer(16,32,3,1,1,resample_out=nn.MaxPool2d(2)),
ResidualBlock(32, resample_out=None),
ResidualBlock(32, resample_out=None),
ResidualBlock(32, resample_out=None),
ResidualBlock(32, resample_out=None),
*convlayer(32,16,3,1,1),
nn.Conv2d(16,1,3,1,1),
nn.Flatten(),
nn.Linear(1024,n_electrodes),
self.out_activation)
def forward(self, x):
self.out = self.model(x)
stimulation = self.out*self.output_scaling #scaling improves numerical stability
return stimulation
class E2E_Decoder(nn.Module):
"""
Simple non-generic phosphene decoder.
in: (256x256) SVP representation
out: (128x128) Reconstruction
"""
def __init__(self, in_channels=1, out_channels=1, out_activation='sigmoid'):
super(E2E_Decoder, self).__init__()
# Activation of output layer
self.out_activation = {'tanh': nn.Tanh(),
'sigmoid': nn.Sigmoid(),
'relu': nn.LeakyReLU(),
'softmax':nn.Softmax(dim=1)}[out_activation]
# Model
self.model = nn.Sequential(*convlayer(in_channels,16,3,1,1),
*convlayer(16,32,3,1,1),
*convlayer(32,64,3,2,1),
ResidualBlock(64),
ResidualBlock(64),
ResidualBlock(64),
ResidualBlock(64),
*convlayer(64,32,3,1,1),
nn.Conv2d(32,out_channels,3,1,1),
self.out_activation)
def forward(self, x):
return self.model(x)
class ZhaoEncoder(nn.Module):
def __init__(self, in_channels=3,n_electrodes=638, out_channels=1):
super(ZhaoEncoder, self).__init__()
self.model = nn.Sequential(
*convlayer3d(in_channels,32,3,1,1, resample_out=nn.MaxPool3d(2,(1,2,2),padding=(1,0,0),dilation=(2,1,1))),
*convlayer3d(32,48,3,1,1, resample_out=nn.MaxPool3d(2,(1,2,2),padding=(1,0,0),dilation=(2,1,1))),
*convlayer3d(48,64,3,1,1),
*convlayer3d(64,1,3,1,1),
nn.Flatten(start_dim=3),
nn.Linear(1024,n_electrodes),
nn.ReLU()
)
def forward(self, x):
self.out = self.model(x)
self.out = self.out.squeeze(dim=1)
self.out = self.out*1e-4
return self.out
class ZhaoDecoder(nn.Module):
def __init__(self, in_channels=1, out_channels=1, out_activation='sigmoid'):
super(ZhaoDecoder, self).__init__()
# Activation of output layer
self.out_activation = {'tanh': nn.Tanh(),
'sigmoid': nn.Sigmoid(),
'relu': nn.LeakyReLU(),
'softmax':nn.Softmax(dim=1)}[out_activation]
self.model = nn.Sequential(
*convlayer3d(in_channels,16,3,1,1),
*convlayer3d(16,32,3,1,1),
*convlayer3d(32,64,3,(1,2,2),1),
*convlayer3d(64,32,3,1,1),
nn.Conv3d(32,out_channels,3,1,1),
self.out_activation
)
def forward(self, x):
self.out = self.model(x)
return self.out
class SpikeSEE_ResidualBlock(nn.Module):
def __init__(self, n_channels, stride=1, resample_out=None):
super(SpikeSEE_ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(n_channels, n_channels,kernel_size=3, stride=1,padding=1)
self.bn1 = nn.BatchNorm2d(n_channels)
self.relu = nn.LeakyReLU(inplace=True)
self.conv2 = nn.Conv2d(n_channels, n_channels,kernel_size=3, stride=1,padding=1)
self.bn2 = nn.BatchNorm2d(n_channels)
self.resample_out = resample_out
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.resample_out:
out = self.resample_out(out)
return out
class SpikeSEE_Encoder(nn.Module):
"""
Simple non-generic encoder class that receives 128x128 input and outputs 32x32 feature map as stimulation protocol
"""
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu'):
super(SpikeSEE_Encoder, self).__init__()
self.output_scaling = out_scaling
self.out_activation = {'tanh': nn.Tanh(), ## NOTE: simulator expects only positive stimulation values
'sigmoid': nn.Sigmoid(),
'relu': nn.ReLU(),
'softmax': nn.Softmax(dim=1)}[out_activation]
self.conv1 = nn.Conv2d(in_channels, 16, kernel_size=25, stride=1, padding=12, bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.relu1 = nn.LeakyReLU()
self.conv2 = nn.Conv2d(16, 4, kernel_size=25, stride=1, padding=12, bias=False)
self.bn2 = nn.BatchNorm2d(4)
self.relu2 = nn.LeakyReLU()
self.conv3 = nn.Conv2d(4, 2, kernel_size=25, stride=1, padding=12, bias=False)
self.bn3 = nn.BatchNorm2d(2)
self.relu3 = nn.LeakyReLU()
self.res1 = SpikeSEE_ResidualBlock(in_channels)
self.res2 = SpikeSEE_ResidualBlock(16, resample_out=nn.Conv2d(16, 2, kernel_size=1, stride=1, bias=False))
self.lin1 = nn.Linear(32768, n_electrodes)
def forward(self, x):
res1 = self.res1(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.relu1(out)
res2 = self.res2(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu2(out)
out = self.conv3(out)
out = self.bn3(out)
out = self.relu3(out)
out += res1
out += res2
out = torch.flatten(out, start_dim=1)
out = self.lin1(out)
self.out = self.out_activation(out)
stimulation = self.out * self.output_scaling # scaling improves numerical stability
# print("POST STACKING")
# print(stimulation.size())
return stimulation
class SpikeSEE_Decoder(nn.Module):
"""
Simple non-generic phosphene decoder.
"""
def __init__(self, in_channels=1, out_channels=1, out_activation='sigmoid'):
super(SpikeSEE_Decoder, self).__init__()
# Activation of output layer
self.out_activation = {'tanh': nn.Tanh(),
'sigmoid': nn.Sigmoid(),
'relu': nn.LeakyReLU(),
'softmax':nn.Softmax(dim=1)}[out_activation]
# Model
self.model = nn.Sequential(*convlayer(in_channels,16,3,1,1),
*convlayer(16,32,3,1,1),
*convlayer(32,64,3,2,1),
ResidualBlock(64),
ResidualBlock(64),
ResidualBlock(64),
ResidualBlock(64),
*convlayer(64,32,3,1,1),
nn.Conv2d(32,out_channels,3,1,1),
self.out_activation)
def forward(self, x):
return self.model(x)
# Define Network
class SpikeNN_Encoder(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1):
# latent_dim = n_electrodes
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
self.out_activation = {'tanh': nn.Tanh(), ## NOTE: simulator expects only positive stimulation values
'sigmoid': nn.Sigmoid(),
'relu': nn.ReLU(),
'softmax': nn.Softmax(dim=1)}[out_activation]
self.conv1 = nn.Conv2d(in_channels, 16, kernel_size=25, stride=1, padding=12, bias=False)
self.bn1 = nn.BatchNorm2d(16)
# self.relu1 = nn.LeakyReLU()
self.lif1 = snn.Leaky(beta=beta, threshold=thresh)
self.conv2 = nn.Conv2d(16, 4, kernel_size=25, stride=1, padding=12, bias=False)
self.bn2 = nn.BatchNorm2d(4)
# self.relu2 = nn.LeakyReLU()
self.lif2 = snn.Leaky(beta=beta, threshold=thresh)
self.conv3 = nn.Conv2d(4, 2, kernel_size=25, stride=1, padding=12, bias=False)
self.bn3 = nn.BatchNorm2d(2)
self.relu3 = nn.LeakyReLU()
self.lif3 = snn.Leaky(beta=beta, threshold=thresh)
self.res1 = SpikeSEE_ResidualBlock(in_channels)
self.res2 = SpikeSEE_ResidualBlock(16, resample_out=nn.Conv2d(16, 2, kernel_size=1, stride=1, bias=False))
self.lin1 = nn.Linear(32768, n_electrodes)
self.lif4 = snn.Leaky(beta=beta, output=True, threshold=thresh)
self.latentToConv = nn.Sequential(nn.Linear(n_electrodes, 128 * 4 * 4),
snn.Leaky(beta=beta, init_hidden=True, output=True, threshold=thresh))
def encode(self, x, mem):
out = self.conv1(x)
out = self.bn1(out)
out, mem[0] = self.lif1(out, mem[0])
out = self.conv2(out)
out = self.bn2(out)
out, mem[1] = self.lif2(out, mem[1])
out = self.conv3(out)
out = self.bn3(out)
out, mem[2] = self.lif3(out, mem[2])
# LAST OUT
out = torch.flatten(out, start_dim=1)
out = self.lin1(out)
out, mem[3] = self.lif4(out, mem[3])
return out, mem[3]
def forward(self, x):
# encode
spk_mem = []
spk_rec = []
# init mem
mem = [self.lif1.init_leaky(),
self.lif2.init_leaky(),
self.lif3.init_leaky(),
self.lif4.init_leaky()
]
for step in range(self.num_steps): # for t in time
spk_x,mem_x = self.encode(x, mem)
spk_rec.append(spk_x)
spk_mem.append(mem_x)
spk_rec = torch.stack(spk_rec, dim=0)
spk_mem = torch.stack(spk_mem, dim=0)
return spk_rec, spk_mem
class SpikeNN_Decoder(nn.Module):
"""
Simple non-generic phosphene decoder.
"""
def __init__(self, in_channels=1, out_channels=1, n_electrodes=638, out_activation='sigmoid', num_steps=25, beta=.95, thresh=1):
super(SpikeNN_Decoder, self).__init__()
self.num_steps = num_steps
# Activation of output layer
self.out_activation = {'tanh': nn.Tanh(),
'sigmoid': nn.Sigmoid(),
'relu': nn.LeakyReLU(),
'softmax': nn.Softmax(dim=1)}[out_activation]
self.decoder = nn.Sequential(# nn.Unflatten(1, (128, 4, 4)), # Unflatten data from 1 dim to tensor of 128 x 4 x 4
snn.Leaky(beta=beta, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(in_channels, 64, 3, padding=1, stride=(2, 2), output_padding=1),
nn.BatchNorm2d(64),
snn.Leaky(beta=beta, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(64, 32, 3, padding=1, stride=(2, 2), output_padding=1),
nn.BatchNorm2d(32),
snn.Leaky(beta=beta, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(32, 1, 3, padding=1, stride=(2, 2), output_padding=1),
snn.Leaky(beta=beta, init_hidden=True, output=True,
threshold=20000) # make large so membrane can be trained
)
self.conv1 = nn.Conv2d(in_channels, 16, kernel_size=25, stride=1, padding=12, bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.lif1 = snn.Leaky(beta=beta, threshold=thresh)
self.conv2 = nn.Conv2d(16, 4, kernel_size=25, stride=1, padding=12, bias=False)
self.bn2 = nn.BatchNorm2d(4)
self.lif2 = snn.Leaky(beta=beta, threshold=thresh)
self.conv3 = nn.Conv2d(4, 2, kernel_size=25, stride=1, padding=12, bias=False)
self.bn3 = nn.BatchNorm2d(2)
self.lif3 = snn.Leaky(beta=beta, output=True, threshold=20000)
def decoder(self, x, mem):
out = self.conv1(x)
out = self.bn1(out)
out, mem[0] = self.lif1(out, mem[0])
out = self.conv2(out)
out = self.bn2(out)
out, mem[1] = self.lif2(out, mem[1])
out = self.conv3(out)
out, mem[2] = self.lif3(out, mem[2])
return out, mem[2]
def forward(self, spk_rec):
mem = [self.lif1.init_leaky(),
self.lif2.init_leaky(),
self.lif3.init_leaky()
]
spk_mem2 = [];
spk_rec2 = [];
for step in range(self.num_steps):
x_recon, x_mem_recon = self.decoder(spk_rec[step, ...], mem)
spk_rec2.append(x_recon)
spk_mem2.append(x_mem_recon)
spk_rec2 = torch.stack(spk_rec2, dim=4)
spk_mem2 = torch.stack(spk_mem2, dim=4)
out = spk_mem2[:, :, :, :, -1]
return out
class SpikeNN_Encoder_Pehuen(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu',
num_steps=25, beta=.95, thresh=1, spike_grad=None):
super().__init__()
self.num_timesteps = num_steps
# Define the spiking layers as per the image architecture
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) # Corresponds to layer 3 in the architecture
self.conv2 = nn.Conv2d(in_channels, 32, kernel_size=7, stride=1, padding=0)
self.lif1 = snn.Leaky(beta=beta, threshold=thresh)
self.pool2 = nn.MaxPool2d(kernel_size=1, stride=1) # Corresponds to layer 5 in the architecture
self.conv3 = nn.Conv2d(32, 64, kernel_size=7, stride=1, padding=0)
self.lif2 = snn.Leaky(beta=beta, spike_grad=spike_grad, threshold=thresh)
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2) # Corresponds to layer 5 in the architecture
self.conv5 = nn.Conv2d(64, 64, kernel_size=7, stride=1, padding=0)
self.lif5 = snn.Leaky(beta=beta, spike_grad=spike_grad, threshold=thresh)
self.pool4 = nn.MaxPool2d(kernel_size=1, stride=1) # Corresponds to layer 7 in the architecture
self.conv7 = nn.Conv2d(64, 128, kernel_size=7, stride=1, padding=0)
self.lif7 = snn.Leaky(beta=beta, spike_grad=spike_grad, threshold=thresh)
self.pool5 = nn.MaxPool2d(kernel_size=1, stride=1) # Corresponds to layer 7 in the architecture
# Flatten layer to prepare for the linear layer
self.spikes_to_stim = nn.Sequential(nn.Flatten(start_dim=1),
nn.Linear(627200, n_electrodes), # 4900, 25088, 627200
nn.ReLU())
def encode(self, x, mem):
out = [None,
None,
None,
None,
None
]
temp_out = self.pool1(x) # Max pooling does not have state
out[0], mem[0] = self.lif1(self.conv2(temp_out), mem[0])
temp_out = self.pool2(out[0]) # Max pooling does not have state
out[1], mem[1] = self.lif2(self.conv3(temp_out), mem[1])
temp_out = self.pool3(out[1])
out[2], mem[2] = self.lif5(self.conv5(temp_out), mem[2])
temp_out = self.pool4(out[2])
out[3], mem[3] = self.lif7(self.conv7(temp_out), mem[3])
out[4] = self.pool5(out[3]) # TAKE CARE: THIS IS THE OUTPUT OF POOLING; NOT LIF
return out, mem
def forward(self, x):
# Initialize the list of membrane potentials for each LIF layer
mem = [self.lif1.init_leaky(), self.lif2.init_leaky(), self.lif5.init_leaky(),
self.lif7.init_leaky()]
# Lists to collect outputs and spikes across all timesteps
all_out = []
all_mem = []
last_out = []
for timestep in range(self.num_timesteps):
spikes, mem = self.encode(x, mem)
all_out.append(spikes)
all_mem.extend(spikes)
last_out.append(spikes[-1])
# Stack outputs and spikes from all timesteps
end_spikes = torch.stack(last_out, dim=1)
out = self.spikes_to_stim(end_spikes)
return out
class Spike_Decoder_STIM(nn.Module):
def __init__(self, in_channels=1, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1, spike_grad=None):
# latent_dim = n_electrodes
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
self.out_activation = {'tanh': nn.Tanh(), ## NOTE: simulator expects only positive stimulation values
'sigmoid': nn.Sigmoid(),
'relu': nn.ReLU(),
'softmax': nn.Softmax(dim=1)}[out_activation]
# From latent back to tensor for convolution
self.latentToConv = nn.Sequential(nn.Linear(n_electrodes, 128 * 16 * 16),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, output=True,
threshold=thresh)) # Decoder
self.decoder = nn.Sequential(
# First, downscale from 256x256 to 128x128
nn.Conv2d(in_channels, 128, 3, stride=2, padding=1),
nn.BatchNorm2d(128),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
# Now use ConvTranspose2d layers to process further
nn.ConvTranspose2d(128, 64, 3, padding=1, stride=1), # Size remains 128x128
nn.BatchNorm2d(64),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(64, 32, 3, padding=1, stride=1), # Size remains 128x128
nn.BatchNorm2d(32),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(32, out_channels, 3, padding=1, stride=1), # Size remains 128x128
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, output=True, threshold=20000)
)
def forward(self, spk_rec):
utils.reset(self.decoder)
utils.reset(self.latentToConv)
# decode
spk_mem2 = []
spk_rec2 = []
decoded_x = []
for step in range(self.num_steps): # for t in time
x_recon, x_mem_recon = self.decode(spk_rec[..., step])
spk_rec2.append(x_recon)
spk_mem2.append(x_mem_recon)
spk_rec2 = torch.stack(spk_rec2, dim=4)
spk_mem2 = torch.stack(spk_mem2, dim=4)
out = spk_mem2[:, :, :, :, -1] # return the membrane potential of the output neuron at t = -1 (last t)
return out
def decode(self, x):
# spk_x, mem_x = self.latentToConv(x) # convert latent dimension back to total size of features in encoder final layer
spk_x2, mem_x2 = self.decoder(x)
return spk_x2, mem_x2
class Vanilla_SNN_Encoder(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1, spike_grad=None):
# latent_dim = n_electrodes
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
self.out_activation = {'tanh': nn.Tanh(), ## NOTE: simulator expects only positive stimulation values
'sigmoid': nn.Sigmoid(),
'relu': nn.ReLU(),
'softmax': nn.Softmax(dim=1)}[out_activation]
self.encoder = nn.Sequential(nn.Conv2d(1, 32, 3, padding=1, stride=2),
nn.BatchNorm2d(32),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.Conv2d(32, 64, 3, padding=1, stride=2),
nn.BatchNorm2d(64),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.Conv2d(64, 128, 3, padding=1, stride=2),
nn.BatchNorm2d(128),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.Flatten(start_dim=1, end_dim=3),
nn.Linear(32768, n_electrodes),
# this needs to be the final layer output size (channels * pixels * pixels)
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, output=True,
threshold=thresh)
)
self.output_mapping = nn.Sequential(nn.Flatten(start_dim=1),
nn.Linear(n_electrodes * self.num_steps, n_electrodes),
nn.ReLU())
def forward(self, x):
utils.reset(self.encoder) # need to reset the hidden states of LIF
# encode
spk_mem = []
spk_rec = []
for step in range(self.num_steps): # for t in time
spk_x, mem_x = self.encode(x) # Output spike trains and neuron membrane states
spk_rec.append(spk_x)
spk_mem.append(mem_x)
spk_rec = torch.stack(spk_rec, dim=2)
spk_mem = torch.stack(spk_mem, dim=2)
# out = self.output_mapping(spk_rec)
return spk_rec
def encode(self, x):
spk_latent_x, mem_latent_x = self.encoder(x)
return spk_latent_x, mem_latent_x
class Vanilla_SNN_Encoder_STIM(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1, spike_grad=None):
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
# THIS ONE IS GOOD - it also worked with an initial out channel of 32 and 16 (each decreasing half each layer)
self.encoder = nn.Sequential(nn.Conv2d(in_channels, 128, 25, padding=12, stride=2),
nn.BatchNorm2d(128),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(128, 64, 25, padding=12, stride=2),
nn.BatchNorm2d(64),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(64, 32, 25, padding=12, stride=2),
nn.BatchNorm2d(32),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(32, 16, 25, padding=12, stride=2),
nn.BatchNorm2d(16),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Flatten(start_dim=1),
nn.Linear(1024, n_electrodes),
nn.Softmax()
)
self.output_mapping = nn.Sequential(nn.Flatten(start_dim=1),
nn.Linear(n_electrodes * self.num_steps, n_electrodes),
nn.ReLU())
def forward(self, x):
utils.reset(self.encoder) # need to reset the hidden states of LIF
# encode
stim = None
for step in range(self.num_steps):
stim = self.encoder(x)
return stim
class Vanilla_SNN_Encoder_STIM_SPLIT(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1, spike_grad=None):
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
# THIS ONE IS GOOD - it also worked with an initial out channel of 32 and 16 (each decreasing half each layer)
self.encoder = nn.Sequential(nn.Conv2d(in_channels, 128, 25, padding=12, stride=2),
nn.BatchNorm2d(128),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(128, 64, 25, padding=12, stride=2),
nn.BatchNorm2d(64),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(64, 32, 25, padding=12, stride=2),
nn.BatchNorm2d(32),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(32, 16, 25, padding=12, stride=2),
nn.BatchNorm2d(16),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh)
)
# Look at spikies here
# Shapre [1024, 25]
# Visualize as rasterplot
self.mapper = nn.Sequential(nn.Flatten(start_dim=1),
nn.Linear(1024, n_electrodes),
nn.Softmax()
)
def forward(self, x):
utils.reset(self.encoder) # need to reset the hidden states of LIF
utils.reset(self.mapper)
# encode
stim = None
for step in range(self.num_steps):
stim = self.encoder(x)
stim = self.mapper(stim)
return stim
def encode_only(self, x):
return self.encoder(x)
class Vanilla_SNN_Encoder_STIM_SPLIT2(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1, spike_grad=None):
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
# THIS ONE IS GOOD - it also worked with an initial out channel of 32 and 16 (each decreasing half each layer)
self.encoder = nn.Sequential(nn.Conv2d(in_channels, 256, 25, padding=12, stride=2),
nn.BatchNorm2d(256),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(256, 64, 25, padding=12, stride=2),
nn.BatchNorm2d(64),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh),
nn.Conv2d(64, 16, 25, padding=12, stride=2),
nn.BatchNorm2d(16),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True,
threshold=thresh)
)
# Look at spikies here
# Shapre [1024, 25]
# Visualize as rasterplot
self.mapper = nn.Sequential(nn.Flatten(start_dim=1),
nn.Linear(4096, n_electrodes),
nn.Softmax()
)
def forward(self, x):
utils.reset(self.encoder) # need to reset the hidden states of LIF
utils.reset(self.mapper)
# encode
stim = None
for step in range(self.num_steps):
stim = self.encoder(x)
stim = self.mapper(stim)
return stim
def encode_only(self, x):
return self.encoder(x)
class Vanilla_SNN_Encoder_STIM_LIN_SPLIT(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1, spike_grad=None):
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
self.mapper = nn.Sequential(nn.Flatten(start_dim=1),
nn.Linear(1024, n_electrodes),
nn.Softmax()
)
def forward(self, x):
utils.reset(self.mapper) # need to reset the hidden states of LIF
return self.mapper(x)
class Vanilla_SNN_Decoder(nn.Module):
def __init__(self, in_channels=3, out_channels=1, n_electrodes=638, out_scaling=1e-4, out_activation='relu', num_steps=25, beta=.95, thresh=1, spike_grad=None):
super().__init__()
self.output_scaling = out_scaling
self.num_steps = num_steps
self.out_activation = {'tanh': nn.Tanh(),
'sigmoid': nn.Sigmoid(),
'relu': nn.ReLU(),
'softmax': nn.Softmax(dim=1)}[out_activation]
# From latent back to tensor for convolution
self.latentToConv = nn.Sequential(nn.Linear(n_electrodes, 128 * 16 * 16),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, output=True,
threshold=thresh))
self.decoder = nn.Sequential(nn.Unflatten(1, (128, 16, 16)),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(128, 64, 3, padding=1, stride=(2, 2), output_padding=1),
nn.BatchNorm2d(64),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(64, 32, 3, padding=1, stride=(2, 2), output_padding=1),
nn.BatchNorm2d(32),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, threshold=thresh),
nn.ConvTranspose2d(32, out_channels, 3, padding=1, stride=(2, 2), output_padding=1),
snn.Leaky(beta=beta, spike_grad=spike_grad, init_hidden=True, output=True,
threshold=20000)
)
def forward(self, spk_rec):
utils.reset(self.decoder)
utils.reset(self.latentToConv)
# decode
spk_mem2 = []
spk_rec2 = []
for step in range(self.num_steps):
x_recon, x_mem_recon = self.decode(spk_rec[..., step])
spk_rec2.append(x_recon)
spk_mem2.append(x_mem_recon)
spk_rec2 = torch.stack(spk_rec2, dim=4)
spk_mem2 = torch.stack(spk_mem2, dim=4)
out = spk_mem2[:, :, :, :, -1]
return out
def decode(self, x):
spk_x, mem_x = self.latentToConv(x)
spk_x2, mem_x2 = self.decoder(spk_x)
return spk_x2, mem_x2