-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEEGNET_model.py
More file actions
executable file
·249 lines (215 loc) · 7.51 KB
/
EEGNET_model.py
File metadata and controls
executable file
·249 lines (215 loc) · 7.51 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
import torch
import torch.nn as nn
import pytorch_lightning as pl
from torcheval.metrics.functional import (
multiclass_accuracy,
multiclass_f1_score
)
from torch import optim
from typing import Tuple, Optional, Dict, Iterable, Literal
#--------------------------------------------------------------------------
class EEGNetModel(pl.LightningModule):
def __init__(
self,
input_size: Tuple[int, int],
num_classes: Optional[int] = 3,
num_out_channels: Optional[int] = 16,
temporal_kernel_size: Optional[int] = 64,
spatial_kernel_size: Optional[int] = 7,
separable_kernel_size: Optional[int] = 16,
pooling_size: Optional[Tuple[int, int]] = (2, 5),
dropout_prob: Optional[float] = 0.5,
hidden_size: Optional[int] = 128,
):
super().__init__()
self.temporal_conv = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=num_out_channels,
kernel_size=[1, temporal_kernel_size],
stride=1,
bias=True,
padding="same"
),
nn.BatchNorm2d(num_features=num_out_channels),
)
self.spatial_conv = nn.Sequential(
nn.Conv2d(
in_channels=num_out_channels,
out_channels=num_out_channels * 2,
kernel_size=[spatial_kernel_size, 1],
stride=1,
bias=True,
padding="valid"
),
nn.BatchNorm2d(num_features=num_out_channels * 2),
nn.ReLU(True),
)
self.avg_pool = nn.AvgPool2d(
kernel_size=pooling_size,
stride=pooling_size,
padding=0
)
self.seperable_conv = nn.Sequential(
nn.Conv2d(
in_channels=num_out_channels * 2,
out_channels=num_out_channels * 2,
kernel_size=[1, separable_kernel_size],
padding="same",
bias=True
),
nn.Conv2d(
in_channels=num_out_channels * 2,
out_channels=num_out_channels * 2,
kernel_size=[1, 1],
padding="same",
bias=True
),
nn.BatchNorm2d(num_features=num_out_channels * 2),
nn.ReLU(True),
)
self.dropout = nn.Dropout(dropout_prob)
self.flatten = nn.Flatten()
flatten_size = int(
num_out_channels * 2 * (input_size[1] - spatial_kernel_size + 1) *\
input_size[2] / pooling_size[0]**2 / pooling_size[1]**2
)
self.fc1 = nn.Sequential(
nn.Linear(in_features=flatten_size, out_features=hidden_size),
nn.BatchNorm1d(num_features=hidden_size),
nn.ReLU(True)
)
self.fc2 = nn.Linear(in_features=hidden_size, out_features=num_classes)
def forward(self, x):
x = self.temporal_conv(x)
x = self.spatial_conv(x)
x = self.avg_pool(x)
x = self.dropout(x)
x = self.seperable_conv(x)
x = self.avg_pool(x)
x = self.dropout(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.dropout(x)
x = self.fc2(x)
return x
class EEGNet(pl.LightningModule):
def __init__(
self,
model_parameters: Dict,
lr: Optional[float] = 1e-4,
betas: Optional[Iterable[float]] = [0.9, 0.99],
weight_decay: Optional[float] = 1e-6,
epochs: Optional[int] = 100,
):
'''
'''
super().__init__()
self.save_hyperparameters()
self.loss_fun = nn.CrossEntropyLoss()
self.model = EEGNetModel(**model_parameters)
self.lr = lr
self.betas = betas
self.weight_decay = weight_decay
self.epochs = epochs
def forward(
self,
input: Tuple[torch.Tensor, int]
):
'''
Parameters:
-----------
input: Tuple[torch.Tensor, int]
Tensor of size (B, 1, M, W), plus an integer label.
Returns:
--------
x: (torch.Tensor)
Tensor of size (B, 3, M)
'''
# extract input (x point cloud, y label)
x, y = input
x = self.model(x)
return x
def training_step(
self,
train_batch: Tuple[torch.Tensor, torch.Tensor],
batch_idx: int
):
'''
Parameters:
-----------
train_batch: (Tuple[torch.Tensor, torch.Tensor])
Tensor of size (B, 1, M, W) (the EEG signal) and tensor of size (B) (the labels).
'''
# extract input (x signal, y label)
x, y = train_batch
# network output
out = self.model(x)
# compute loss & log it
loss = self.loss_fun(out, y)
# compute metrics & log them
accuracy = multiclass_accuracy(input=out, target=y)
f1_score = multiclass_f1_score(input=out, target=y, num_classes=out.shape[-1])
# log loss and metrics
self.log_dict(
{'train_loss': loss, "train_accuracy": accuracy, "train_f1_score": f1_score},
on_step=True,
on_epoch=False,
prog_bar=True,
logger=True
)
return loss
def validation_step(
self,
val_batch: Tuple[torch.Tensor, torch.Tensor],
batch_idx: int
):
'''
Parameters:
-----------
val_batch: (Tuple[torch.Tensor, torch.Tensor])
Tensor of size (B, 1, M, W) (the EEG signal) and tensor of size (B) (the labels).
'''
# extract input (x signal, y label)
x, y = val_batch
# network output
out = self.model(x)
# compute loss
loss = self.loss_fun(out, y)
# compute metrics & log them
accuracy = multiclass_accuracy(input=out, target=y)
f1_score = multiclass_f1_score(input=out, target=y, num_classes=out.shape[-1])
# log loss and metrics
self.log_dict(
{"val_loss": loss, "val_accuracy": accuracy, "val_f1_score": f1_score},
on_step=False,
on_epoch=True,
prog_bar=True,
logger=True
)
return loss
def test_step(
self,
test_batch: Tuple[torch.Tensor, torch.Tensor],
batch_idx: int
):
'''
Parameters:
-----------
test_batch: (Tuple[torch.Tensor, torch.Tensor])
Tensor of size (B, 1, M, W) (the EEG signal) and tensor of size (B) (the labels).
'''
# extract input (x signal, y label)
x, y = test_batch
# network output
out = self.model(x)
return self.loss_fun(out, y)
def configure_optimizers(self):
optimizer = optim.Adam(
self.parameters(), lr=self.lr, betas=self.betas, weight_decay=self.weight_decay
)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
optimizer=optimizer, mode='min', factor=0.5, patience=15, min_lr=1e-7
)
return [optimizer], [{"scheduler": scheduler,"monitor": "val_loss", "interval": "epoch", "frequency": 1}]
#----------------------------------------------------------------------------------------------------------------