Official PyTorch implementation of the Bachelor's Final Year Project: "Research on Motor Imagery EEG Classification Based on Shared-Sparse MoE Transformer".
Decoding Motor Imagery Electroencephalogram (MI-EEG) signals in Brain-Computer Interfaces (BCIs) is highly challenging due to low signal-to-noise ratios, limited training data, and high inter-subject variability.
This repository introduces a Shared-Sparse Mixture of Experts (MoE) Transformer architecture to decouple universal neural patterns from subject-specific idiosyncrasies.
- Frontend: Replaces deep, redundant CNNs with Filter Bank Common Spatial Patterns (FBCSP) followed by a lightweight Linear Projection.
- Encoder: Employs a Shared-Sparse MoE routing mechanism where a "shared expert" captures cross-subject universal patterns, and "sparse experts" adapt to individual variations.
- Result: Achieves an average accuracy of 83.91% on the BCIC IV 2a dataset, outperforming classic baselines like EEGNet and DeepConvNet while reducing model parameters by 58.5%.
- FBCSP-Linear Frontend: Explicit spatial-spectral prior extraction eliminating the need for computationally heavy convolutional layers.
- Dynamic Noisy Top-K Routing: A MoE mechanism with 1 shared expert and 4 sparse experts (Top-2 routing) to prevent expert collapse and balance training loads.
- Divide and Recombine (S&R) Augmentation: A novel temporal augmentation strategy that synthesizes cross-trial sequences to prevent overfitting on small datasets.
- High Interpretability: Includes spatial-spectral attention saliency mapping to visualize physiological decoupling (Mu and Beta bands).
├── checkpoints_final/ # Pre-trained and best model weights
├── process/ # Core execution scripts
│ ├── run_master_pipeline.py # One-click script to run all training & evaluation
│ ├── data_process_fbcsp_all.py # EEG preprocessing & FBCSP feature extraction
│ ├── dataset_loader.py # PyTorch Dataset with S&R augmentation
│ ├── train_experiment_20runs.py # Main MoE model training (20 independent runs)
│ ├── train_baseline_dl_20runs.py # Baseline training (EEGNet, DeepConvNet)
│ ├── eval_*.py # Evaluation scripts (XAI, Engineering metrics)
│ └── plot_figure_*.py # Visualization scripts for thesis figures
└── structure/ # Neural Network Architectures
├── model/
│ ├── model_moe.py # Core MoE Transformer implementation
│ ├── model_fbcsp_no_cnn.py # Proposed Linear+MoE architecture
│ ├── baseline_dl_models.py # EEGNet & DeepConvNet baselines
│ └── ablation_*.py # Ablation study architectures
└── cnn.py # CNN frontend implementations
- Clone this repository:
git clone https://github.com/wushi2333/fyp2026.git
cd fyp2026- Install dependencies:
pip install torch torchvision torchaudio mne scikit-learn pandas matplotlib seaborn thop
⚠️ Important Note: Before running the scripts, please update theCONFIGdictionaries inside the Python files to match your local absolute paths for datasets and output directories (e.g.,data_root,save_dir).
This project evaluates the model primarily on the BCIC IV 2a dataset, but the preprocessing script also supports OpenBMI and PhysioNet. Please download the raw data from their official sources:
- BCIC IV 2a: BNCI Horizon 2020 (Dataset 2a) (Download the
.gdffiles) - OpenBMI: GigaDB (Lee 2019)
- PhysioNet: EEG Motor Movement/Imagery Dataset
After downloading, organize your raw data and update the bcic_dir, openbmi_root, and physionet_root paths in process/data_process_fbcsp_all.py. Then run the preprocessing and FBCSP extraction script:
python process/data_process_fbcsp_all.pyTo reproduce the main results (Baseline vs. Proposed Model) with 20 independent runs and statistical t-tests, simply run:
python process/run_master_pipeline.pyYou can also run specific parts of the project individually:
- Train MoE Model:
python process/train_experiment_20runs.py - Train Baselines:
python process/train_baseline_dl_20runs.py - Ablation Studies:
python process/train_ablation_experiments.py
Generate the figures used in the thesis (Topoplots, S&R Illustration, Accuracy Bar Charts, t-SNE, Saliency Maps):
python process/plot_figure_1_topoplots.py
python process/eval_xai_analysis.py
python process/eval_engineering_metrics.py20 independent runs per subject. Paired t-test vs. the proposed model.
| Architecture | Parameters ↓ | FLOPs | Avg Accuracy (%) | p-value |
|---|---|---|---|---|
| DeepConvNet [3] | — | — | 80.27 ± 12.76 | < 0.0001 |
| EEGNet [17] | 87.99 K | 4.64 M | 81.13 ± 13.65 | 0.0010 |
| Linear + MoE Transformer (Ours) | 2.69 M | 1.37 G | 83.91 ± 10.18 | — |
All variants share identical FBCSP spatial-spectral priors. "Best Acc" reflects the peak performance ceiling per subject; "20-Run Avg" measures optimisation stability.
| Architecture | Best Acc (%) | 20-Run Avg Acc (%) | Avg Kappa | Avg F1 |
|---|---|---|---|---|
| No Transformer | 79.31 | 69.85 | 0.5862 | 0.7876 |
| Standard Transformer (FFN, no MoE) | 89.27 | 84.50 | 0.7847 | 0.8919 |
| Linear + MoE Transformer (Ours) | 90.04 | 83.81 | 0.6722 | 0.8351 |
💡 The Standard Transformer edges ahead on average accuracy (84.50% vs. 83.81%), but the MoE achieves the highest peak accuracy (90.04%) and lifts performance for subjects with atypical spatial-spectral distributions (e.g. A04: 86.21% → 89.66%, A09: 89.66% → 93.10% vs. the Standard Transformer). The routing mechanism decouples cross-subject universal patterns from individual idiosyncrasies, raising the performance ceiling.
By decoupling physiological markers, the MoE architecture provides transparent routing. Spatial-spectral saliency maps reveal that the model correctly focuses on the Mu band (8-12 Hz) and Beta band (13-30 Hz), dynamically adjusting to subjects with atypical frequency distributions (e.g., Subject A04).
This code is the result of a Final Year Project at the Glasgow College Hainan, UESTC. If you find this code or architecture useful in your research, please consider citing this repository.
This project is licensed under the MIT License — see the LICENSE file for details.
Copyright © 2026 Xia Zihang

