Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -2,18 +2,86 @@
|
|
| 2 |
license: mit
|
| 3 |
pipeline_tag: feature-extraction
|
| 4 |
tags:
|
| 5 |
-
-
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
- graph-neural-network
|
| 10 |
-
-
|
| 11 |
-
-
|
| 12 |
-
- pytorch
|
| 13 |
-
- pytorch_model_hub_mixin
|
| 14 |
---
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
license: mit
|
| 3 |
pipeline_tag: feature-extraction
|
| 4 |
tags:
|
| 5 |
+
- eeg
|
| 6 |
+
- bci
|
| 7 |
+
- motor-imagery
|
| 8 |
+
- eegnet
|
| 9 |
+
- graph-neural-network
|
| 10 |
+
- feature-extraction
|
| 11 |
+
- pytorch
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# EEGNet-GNN feature extractor (Option C)
|
| 15 |
+
|
| 16 |
+
Standard **EEGNet** with one change: Layer 2's depthwise *spatial* conv is replaced by a
|
| 17 |
+
**graph convolution** over the electrode montage, so electrodes mix according to how close
|
| 18 |
+
they are on the scalp rather than as a flat, order-agnostic channel list.
|
| 19 |
+
|
| 20 |
+
```
|
| 21 |
+
Temporal conv -> GRAPH conv -> Separable conv -> Avg-pool + flatten -> flat vector
|
| 22 |
+
(Layer 1) (Layer 2) (Layer 3) (Layer 4) OUTPUT
|
| 23 |
+
IDENTICAL THE SWAP IDENTICAL IDENTICAL
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
**This model stops at Layer 4 and returns a flat feature vector — there is no classifier
|
| 27 |
+
head.** Attach your own head (a linear layer, an MLP, or a variational quantum circuit)
|
| 28 |
+
downstream. The output size is exposed as `model.flat_dim` (496 with the defaults).
|
| 29 |
+
|
| 30 |
+
## The swap
|
| 31 |
+
|
| 32 |
+
Standard EEGNet's Layer 2 learns, per output map, a single weighted sum over **all**
|
| 33 |
+
electrodes at once — topology is ignored. Here that becomes a graph convolution
|
| 34 |
+
`H = Â X W`, where `Â` is the symmetric-normalised adjacency of the electrode montage.
|
| 35 |
+
Each electrode aggregates only from its physical neighbours (e.g. `C3` from
|
| 36 |
+
`FC3, FC1, C5, C1, CP3, CP1`), then a learned per-node readout collapses the electrodes to
|
| 37 |
+
the same `(F2, 1, T)` shape the depthwise conv produced — so Layers 1/3/4 are unchanged.
|
| 38 |
+
A `spatial="depthwise"` flag restores the original EEGNet for a controlled comparison.
|
| 39 |
+
|
| 40 |
+
The adjacency is built from 2-D positions for the 22 channels of **BCI Competition IV-2a**
|
| 41 |
+
and travels with the checkpoint (a saved buffer), so `from_pretrained` restores the exact
|
| 42 |
+
graph you trained on.
|
| 43 |
+
|
| 44 |
+
## Usage
|
| 45 |
+
|
| 46 |
+
The model is a custom `PyTorchModelHubMixin` module, so you need its class definition
|
| 47 |
+
(`eegnet_gnn.py`, included in this repo) alongside the weights.
|
| 48 |
+
|
| 49 |
+
```python
|
| 50 |
+
from eegnet_gnn import EEGNetGNN
|
| 51 |
+
import torch
|
| 52 |
+
|
| 53 |
+
model = EEGNetGNN.from_pretrained("shemalfoy/eegnet-gnn-features").eval()
|
| 54 |
+
|
| 55 |
+
x = torch.randn(1, 1, 22, 1000) # (batch, 1, channels, time)
|
| 56 |
+
features = model(x) # (1, model.flat_dim) == (1, 496)
|
| 57 |
+
|
| 58 |
+
# attach your own classifier
|
| 59 |
+
head = torch.nn.Linear(model.flat_dim, 4)
|
| 60 |
+
logits = head(features)
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
Pull the class straight from the repo if you don't have the file locally:
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
import importlib.util
|
| 67 |
+
from huggingface_hub import hf_hub_download
|
| 68 |
+
path = hf_hub_download("shemalfoy/eegnet-gnn-features", "eegnet_gnn.py")
|
| 69 |
+
spec = importlib.util.spec_from_file_location("eegnet_gnn", path)
|
| 70 |
+
mod = importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)
|
| 71 |
+
model = mod.EEGNetGNN.from_pretrained("shemalfoy/eegnet-gnn-features")
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
## Inputs / outputs
|
| 75 |
+
|
| 76 |
+
- **Input:** `(batch, 1, 22, T)` float tensor — 22 EEG channels in BCI IV-2a order, `T` samples.
|
| 77 |
+
- **Output:** `(batch, flat_dim)` feature vector (`flat_dim = 496` with defaults).
|
| 78 |
+
|
| 79 |
+
## Dependencies
|
| 80 |
+
|
| 81 |
+
`torch`, `huggingface_hub`, `safetensors`. No quantum / PennyLane dependency.
|
| 82 |
+
|
| 83 |
+
## Notes and limitations
|
| 84 |
+
|
| 85 |
+
- A different electrode set requires rebuilding the adjacency (`build_adjacency(coords=...)`)
|
| 86 |
+
and retraining.
|
| 87 |
+
- Built on EEGNet (Lawhern et al., 2018) and graph convolution (Kipf & Welling, 2017).
|