Spaces:
Sleeping
Sleeping
Delete eegnet_model.py
Browse files- eegnet_model.py +0 -50
eegnet_model.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
| 1 |
-
import torch.nn as nn
|
| 2 |
-
import torch.nn.functional as F
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
class EEGNet(nn.Module):
|
| 6 |
-
def __init__(self, n_channels=21, n_samples=1250, num_classes=2, dropout_rate=0.5):
|
| 7 |
-
super(EEGNet, self).__init__()
|
| 8 |
-
|
| 9 |
-
# Temporal convolution: learn temporal filters across time dimension
|
| 10 |
-
self.firstconv = nn.Sequential(
|
| 11 |
-
nn.Conv2d(1, 8, kernel_size=(1, 64), padding=(0, 32), bias=False), # shape: (B, 8, C, T)
|
| 12 |
-
nn.BatchNorm2d(8)
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
# Depthwise spatial convolution: one spatial filter per temporal filter
|
| 16 |
-
self.depthwiseConv = nn.Sequential(
|
| 17 |
-
nn.Conv2d(8, 16, kernel_size=(n_channels, 1), groups=8, bias=False), # shape: (B, 16, 1, T)
|
| 18 |
-
nn.BatchNorm2d(16),
|
| 19 |
-
nn.ELU(),
|
| 20 |
-
nn.AvgPool2d(kernel_size=(1, 4)),
|
| 21 |
-
nn.Dropout(dropout_rate)
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
# Separable convolution: combines temporal filters again
|
| 25 |
-
self.separableConv = nn.Sequential(
|
| 26 |
-
nn.Conv2d(16, 16, kernel_size=(1, 16), padding=(0, 8), bias=False),
|
| 27 |
-
nn.BatchNorm2d(16),
|
| 28 |
-
nn.ELU(),
|
| 29 |
-
nn.AvgPool2d(kernel_size=(1, 8)),
|
| 30 |
-
nn.Dropout(dropout_rate)
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
# Dynamically compute the flattened feature size after conv layers
|
| 34 |
-
dummy_input = torch.zeros(1, 1, n_channels, n_samples)
|
| 35 |
-
with torch.no_grad():
|
| 36 |
-
x = self.firstconv(dummy_input)
|
| 37 |
-
x = self.depthwiseConv(x)
|
| 38 |
-
x = self.separableConv(x)
|
| 39 |
-
flattened_size = x.reshape(1, -1).shape[1] # dynamically computed
|
| 40 |
-
|
| 41 |
-
# Final classification layer
|
| 42 |
-
self.classifier = nn.Linear(flattened_size, num_classes)
|
| 43 |
-
|
| 44 |
-
def forward(self, x):
|
| 45 |
-
x = self.firstconv(x)
|
| 46 |
-
x = self.depthwiseConv(x)
|
| 47 |
-
x = self.separableConv(x)
|
| 48 |
-
x = x.reshape(x.size(0), -1) # flatten
|
| 49 |
-
x = self.classifier(x)
|
| 50 |
-
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|