-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheeg_encoder.py
More file actions
329 lines (272 loc) · 10.2 KB
/
eeg_encoder.py
File metadata and controls
329 lines (272 loc) · 10.2 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
"""
Different EEG encoders for comparison
SA GA
shallownet, deepnet, eegnet, conformer, tsconv
"""
import os
import argparse
import math
import glob
import random
import itertools
import datetime
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch import Tensor
from torch.autograd import Variable
from torch_geometric.nn import GATConv
from torchsummary import summary
from einops import rearrange
from einops.layers.torch import Rearrange
class PatchEmbedding(nn.Module):
def __init__(self, emb_size=40):
# self.patch_size = patch_size
super().__init__()
self.tsconv = nn.Sequential(
nn.Conv2d(1, 40, (1, 25), (1, 1)),
nn.AvgPool2d((1, 51), (1, 5)),
nn.BatchNorm2d(40),
nn.ELU(),
nn.Conv2d(40, 40, (63, 1), (1, 1)),
nn.BatchNorm2d(40),
nn.ELU(),
nn.Dropout(0.5),
)
self.deepnet = nn.Sequential(
nn.Conv2d(1, 25, (1, 10), (1, 1)),
nn.Conv2d(25, 25, (63, 1), (1, 1)),
nn.BatchNorm2d(25),
nn.ELU(),
nn.MaxPool2d((1, 2), (1, 2)),
nn.Dropout(0.5),
nn.Conv2d(25, 50, (1, 10), (1, 1)),
nn.BatchNorm2d(50),
nn.ELU(),
nn.MaxPool2d((1, 2), (1, 2)),
nn.Dropout(0.5),
nn.Conv2d(50, 100, (1, 10), (1, 1)),
nn.BatchNorm2d(100),
nn.ELU(),
nn.MaxPool2d((1, 2), (1, 2)),
nn.Dropout(0.5),
nn.Conv2d(100, 200, (1, 10), (1, 1)),
nn.BatchNorm2d(200),
nn.ELU(),
nn.MaxPool2d((1, 2), (1, 2)),
nn.Dropout(0.5),
)
self.eegnet = nn.Sequential(
nn.Conv2d(1, 8, (1, 64), (1, 1)),
nn.BatchNorm2d(8),
nn.Conv2d(8, 16, (63, 1), (1, 1)),
nn.BatchNorm2d(16),
nn.ELU(),
nn.AvgPool2d((1, 2), (1, 2)),
nn.Dropout(0.5),
nn.Conv2d(16, 16, (1, 16), (1, 1)),
nn.BatchNorm2d(16),
nn.ELU(),
# nn.AvgPool2d((1, 2), (1, 2)),
nn.Dropout2d(0.5)
)
self.shallownet = nn.Sequential(
nn.Conv2d(1, 40, (1, 25), (1, 1)),
nn.Conv2d(40, 40, (63, 1), (1, 1)),
nn.BatchNorm2d(40),
nn.ELU(),
nn.AvgPool2d((1, 51), (1, 5)),
nn.Dropout(0.5),
)
self.projection = nn.Sequential(
nn.Conv2d(40, emb_size, (1, 1), stride=(1, 1)), # 5 is better than 1
Rearrange('b e (h) (w) -> b (h w) e'),
)
def forward(self, x: Tensor) -> Tensor:
x = self.tsconv(x)
return x
class MultiHeadAttention(nn.Module):
def __init__(self, emb_size, num_heads, dropout):
super().__init__()
self.emb_size = emb_size
self.num_heads = num_heads
self.keys = nn.Linear(emb_size, emb_size)
self.queries = nn.Linear(emb_size, emb_size)
self.values = nn.Linear(emb_size, emb_size)
self.att_drop = nn.Dropout(dropout)
self.projection = nn.Linear(emb_size, emb_size)
def forward(self, x: Tensor, mask: Tensor = None) -> Tensor:
queries = rearrange(self.queries(x), "b n (h d) -> b h n d", h=self.num_heads)
keys = rearrange(self.keys(x), "b n (h d) -> b h n d", h=self.num_heads)
values = rearrange(self.values(x), "b n (h d) -> b h n d", h=self.num_heads)
energy = torch.einsum('bhqd, bhkd -> bhqk', queries, keys) # batch, num_heads, query_len, key_len
if mask is not None:
fill_value = torch.finfo(torch.float32).min
energy.mask_fill(~mask, fill_value)
scaling = self.emb_size ** (1 / 2)
att = F.softmax(energy / scaling, dim=-1)
att = self.att_drop(att)
out = torch.einsum('bhal, bhlv -> bhav ', att, values)
out = rearrange(out, "b h n d -> b n (h d)")
out = self.projection(out)
return out
class ResidualAdd(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x, **kwargs):
res = x
x = self.fn(x, **kwargs)
x += res
return x
class FeedForwardBlock(nn.Sequential):
def __init__(self, emb_size, expansion, drop_p):
super().__init__(
nn.Linear(emb_size, expansion * emb_size),
nn.GELU(),
nn.Dropout(drop_p),
nn.Linear(expansion * emb_size, emb_size),
)
class GELU(nn.Module):
def forward(self, input: Tensor) -> Tensor:
return input*0.5*(1.0+torch.erf(input/math.sqrt(2.0)))
class TransformerEncoderBlock(nn.Sequential):
def __init__(self,
emb_size,
num_heads=10,
drop_p=0.5,
forward_expansion=4,
forward_drop_p=0.5):
super().__init__(
ResidualAdd(nn.Sequential(
nn.LayerNorm(emb_size),
MultiHeadAttention(emb_size, num_heads, drop_p),
nn.Dropout(drop_p)
)),
ResidualAdd(nn.Sequential(
nn.LayerNorm(emb_size),
FeedForwardBlock(
emb_size, expansion=forward_expansion, drop_p=forward_drop_p),
nn.Dropout(drop_p)
)
))
class TransformerEncoder(nn.Sequential):
def __init__(self, depth, emb_size):
super().__init__(*[TransformerEncoderBlock(emb_size) for _ in range(depth)])
class FlattenHead(nn.Sequential):
def __init__(self, emb_size, n_classes):
super().__init__()
def forward(self, x):
x = x.contiguous().view(x.size(0), -1)
return x
class ClassificationHead(nn.Sequential):
def __init__(self):
super().__init__()
self.fc = nn.Sequential(
nn.Linear(256, 40),
)
def forward(self, x):
cls_out = self.fc(x)
return x, cls_out
class channel_attention(nn.Module):
def __init__(self, sequence_num=250, inter=30):
super(channel_attention, self).__init__()
self.sequence_num = sequence_num
self.inter = inter
self.extract_sequence = int(self.sequence_num / self.inter) # You could choose to do that for less computation
self.query = nn.Sequential(
nn.Linear(63, 63),
nn.LayerNorm(63), # also may introduce improvement to a certain extent
nn.Dropout(0.3)
)
self.key = nn.Sequential(
nn.Linear(63, 63),
# nn.LeakyReLU(),
nn.LayerNorm(63),
nn.Dropout(0.3)
)
# self.value = self.key
self.projection = nn.Sequential(
nn.Linear(63, 63),
# nn.LeakyReLU(),
nn.LayerNorm(63),
nn.Dropout(0.3),
)
self.drop_out = nn.Dropout(0)
self.pooling = nn.AvgPool2d(kernel_size=(1, self.inter), stride=(1, self.inter))
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
def forward(self, x):
temp = rearrange(x, 'b o c s->b o s c')
temp_query = rearrange(self.query(temp), 'b o s c -> b o c s')
temp_key = rearrange(self.key(temp), 'b o s c -> b o c s')
channel_query = temp_query
channel_key = temp_key
scaling = self.extract_sequence ** (1 / 2)
channel_atten = torch.einsum('b o c s, b o m s -> b o c m', channel_query, channel_key) / scaling
channel_atten_score = F.softmax(channel_atten, dim=-1)
channel_atten_score = self.drop_out(channel_atten_score)
out = torch.einsum('b o c s, b o c m -> b o c s', x, channel_atten_score)
'''
projections after or before multiplying with attention score are almost the same.
'''
out = rearrange(out, 'b o c s -> b o s c')
out = self.projection(out)
out = rearrange(out, 'b o s c -> b o c s')
return out
class EEG_GAT(nn.Module):
def __init__(self, in_channels=250, out_channels=250):
super(EEG_GAT, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.conv1 = GATConv(in_channels=in_channels, out_channels=out_channels, heads=1)
self.num_channels = 63
# Create a list of tuples representing all possible edges between channels
self.edge_index_list = torch.Tensor([(i, j) for i in range(self.num_channels) for j in range(self.num_channels) if i != j]).cuda()
# Convert the list of tuples to a tensor
self.edge_index = torch.tensor(self.edge_index_list, dtype=torch.long).t().contiguous().cuda()
def forward(self, x):
batch_size, _, num_channels, num_features = x.size()
# Reshape x to (batch_size*num_channels, num_features) to pass through GATConv
x = x.view(batch_size*num_channels, num_features)
x = self.conv1(x, self.edge_index)
x = x.view(batch_size, num_channels, -1)
x = x.unsqueeze(1)
return x
class Enc_eeg(nn.Sequential):
def __init__(self, emb_size=40, depth=3, n_classes=4, **kwargs):
super().__init__(
PatchEmbedding(emb_size),
# TransformerEncoder(depth, emb_size), # for Transformer
FlattenHead(emb_size, n_classes)
)
class Proj_eeg(nn.Sequential):
def __init__(self, embedding_dim=1440, proj_dim=768, drop_proj=0.5):
super().__init__(
nn.Linear(embedding_dim, proj_dim),
ResidualAdd(nn.Sequential(
nn.GELU(),
nn.Linear(proj_dim, proj_dim),
nn.Dropout(drop_proj),
)),
nn.LayerNorm(proj_dim),
)
class Proj_img(nn.Sequential):
def __init__(self, embedding_dim=768, proj_dim=768, drop_proj=0.3):
super().__init__(
nn.Linear(embedding_dim, proj_dim),
ResidualAdd(nn.Sequential(
nn.GELU(),
nn.Linear(proj_dim, proj_dim),
nn.Dropout(drop_proj),
)),
nn.LayerNorm(proj_dim),
)
def forward(self, x):
return x