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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-nd-4.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
<div align="center">
|
| 8 |
+
<img src="https://raw.githubusercontent.com/pulp-bio/BioFoundation/refs/heads/main/docs/model/logo/FEMBA_logo.png" alt="FEMBA Logo" width="800"/>
|
| 9 |
+
<h1>FEMBA: Foundational Encoder Model with Bidirectional Mamba for EEG</h1>
|
| 10 |
+
</div>
|
| 11 |
+
<p align="center">
|
| 12 |
+
<a href="https://github.com/pulp-bio/BioFoundation">
|
| 13 |
+
<img src ="https://img.shields.io/github/stars/pulp-bio/BioFoundation?color=ccf" alt="Github">
|
| 14 |
+
</a>
|
| 15 |
+
<a href="https://creativecommons.org/licenses/by-nd/4.0/">
|
| 16 |
+
<img src="https://img.shields.io/badge/License-CC_BY--ND_4.0-lightgrey.svg" alt="License">
|
| 17 |
+
</a>
|
| 18 |
+
<a href="https://arxiv.org/abs/2502.06438">
|
| 19 |
+
<img src="https://img.shields.io/badge/arXiv-2502.06438-b31b1b.svg" alt="Paper">
|
| 20 |
+
</a>
|
| 21 |
+
</p>
|
| 22 |
+
|
| 23 |
+
**FEMBA** is a powerful and efficient foundation model for **EEG signal analysis**, built upon a **bidirectional Mamba** state-space architecture. It supports **self-supervised pre-training** via masked reconstruction and **end-to-end supervised fine-tuning** for multiple downstream tasks (abnormal EEG, artifact detection, slowing classification). By using linear-time state-space modeling instead of quadratic attention, FEMBA scales to long EEG sequences and constrained hardware while remaining performant.
|
| 24 |
+
|
| 25 |
+
---
|
| 26 |
+
|
| 27 |
+
## 🔒 License & Usage Policy (Weights)
|
| 28 |
+
|
| 29 |
+
**Weights license:** The released model weights are licensed under **Creative Commons Attribution–NoDerivatives 4.0 (CC BY-ND 4.0)**. This section summarizes the practical implications for users. *This is not legal advice; please read the full license text.*
|
| 30 |
+
|
| 31 |
+
### ✅ You may
|
| 32 |
+
- **Use** and **redistribute** the **unmodified** FEMBA weights (including in commercial settings) **with proper attribution** to the FEMBA authors.
|
| 33 |
+
- **Fine-tune / adapt** the weights **for your internal use** (research or production) **without redistributing** the modified weights.
|
| 34 |
+
- **Publish your code, configs, logs, and papers** describing experiments with FEMBA (please cite the paper).
|
| 35 |
+
|
| 36 |
+
### 🚫 You may not
|
| 37 |
+
- **Share, host, or redistribute any modified weights** (including LoRA/adapter/delta checkpoints or pruned/quantized variants). Any parameter set that encodes an adaptation is considered a derivative and cannot be shared under CC BY-ND 4.0.
|
| 38 |
+
- **Imply endorsement** by the FEMBA authors for any derivative or evaluation without our written permission.
|
| 39 |
+
- **Use the FEMBA name** in a way that suggests your modified model is an official FEMBA release.
|
| 40 |
+
|
| 41 |
+
### 🤝 How to contribute improvements (PR-gated releases)
|
| 42 |
+
We welcome community improvements via a **pull-request (PR)** workflow. If you believe your improvements should become an **official FEMBA release**:
|
| 43 |
+
1. **Open a PR** in the [BioFoundation repository](https://github.com/pulp-bio/BioFoundation) describing the change (architecture/head/training recipe, datasets, preprocessing, compute).
|
| 44 |
+
2. Include **reproducibility artifacts**: configs, seeds, scripts, environment details, training/validation logs, and the **evaluation protocol** (e.g., TUAB/TUAR/TUSL) with exact splits.
|
| 45 |
+
3. Provide **comprehensive results** (AUROC/AUPR/BA, FLOPs, memory) vs. the baselines reported in the FEMBA paper.
|
| 46 |
+
4. After **maintainer review**, approved changes will be **retrained/validated** and, if accepted, **released by the maintainers** as a new **official FEMBA** checkpoint under **CC BY-ND 4.0**.
|
| 47 |
+
|
| 48 |
+
> Rationale: CC BY-ND protects users from fragmented, lower-quality “FEMBA variants,” while still enabling internal fine-tuning and a path for the community to upstream improvements through review.
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## 🔎 Model Summary
|
| 53 |
+
|
| 54 |
+
- **Architecture:** Bidirectional Mamba encoder with a 2D-conv tokenizer (patching over channels × time), random masking (60%) for SSL, and either a lightweight linear head or a Mamba-enhanced head for downstream tasks. Hidden state size is fixed at 80 across variants.
|
| 55 |
+
- **Scaling:** Linear time & memory in sequence length (state-space model), enabling efficient long-context EEG modeling and on-device scenarios.
|
| 56 |
+
- **Pre-training data:** >21,000 hours of unlabeled clinical EEG from Temple University Hospital (TUEG). Subjects overlapping with TUAB/TUAR/TUSL are removed to prevent leakage.
|
| 57 |
+
- **Downstream tasks:** TUAB abnormal/normal (binary), TUAR artifact detection (BC/MC/MMC/MCC), TUSL slowing (4-class). TUAB uses its predefined split; TUAR/TUSL use 80/10/10 splits.
|
| 58 |
+
- **Optimization (typical):** Pre-training with Smooth L1 masked-patch reconstruction; fine-tuning with Adam (LR 1e-4), cosine decay, early stopping; layer-wise LR decay factor 0.75.
|
| 59 |
+
|
| 60 |
+
---
|
| 61 |
+
|
| 62 |
+
## 🚀 Model Variants
|
| 63 |
+
|
| 64 |
+
| Variant | Parameters | (num_blocks, embed_dim) |
|
| 65 |
+
| :--- | :--- | :--- |
|
| 66 |
+
| **FEMBA-tiny** | 7.8M | (2, 35) |
|
| 67 |
+
| **FEMBA-base** | 47.7M | (12, 35) |
|
| 68 |
+
| **FEMBA-large** | 77.8M | (4, 79) |
|
| 69 |
+
| **FEMBA-huge** | 386M | (20, 79) |
|
| 70 |
+
|
| 71 |
+
*Hidden state size is 80 for all variants; blocks correspond to Bi-Mamba layers in the encoder.*
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
## 🧠 Intended Use & Limitations
|
| 76 |
+
|
| 77 |
+
**Intended use.** Research on EEG representation learning and downstream classification (e.g., abnormal EEG detection, artifact detection, slowing classification). FEMBA is particularly useful when long sequences or limited compute/memory preclude quadratic-cost attention.
|
| 78 |
+
|
| 79 |
+
**Out-of-scope / limitations.**
|
| 80 |
+
- **Not a medical device.** Outputs are research signals and **must not** be used for clinical decision-making without appropriate validation and regulatory processes.
|
| 81 |
+
- **Domain shift.** Performance can degrade across cohorts (e.g., neonatal vs. adult EEG) and label protocols; domain adaptation is encouraged.
|
| 82 |
+
- **Class imbalance.** On some tasks (e.g., TUSL), AUROC may be strong while AUPR can trail attention baselines, highlighting sensitivity to class imbalance and protocol specifics.
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
## 🏗️ Architecture & Training Details
|
| 87 |
+
|
| 88 |
+
**Tokenizer & patches.** Raw EEG (C×T) is quartile-normalized per channel (IQR scaling) and tokenized with a 2D convolution over channel×time patches (e.g., 4×32) with learnable positional embeddings.
|
| 89 |
+
|
| 90 |
+
**Self-supervised objective.** Randomly mask **60%** of patches; reconstruct masked content with a lightweight decoder using **Smooth L1** loss (computed on masked patches only).
|
| 91 |
+
|
| 92 |
+
**Encoder.** Stacked **Bidirectional Mamba** blocks (forward + backward over a reversed copy), merged and residually connected; hidden size fixed to 80.
|
| 93 |
+
|
| 94 |
+
**Fine-tuning heads.**
|
| 95 |
+
- *Linear classifier:* small MLP (≈0.5M params).
|
| 96 |
+
- *Mamba-enhanced classifier:* adds one Mamba block before the linear layer (up to ≈0.7M params).
|
| 97 |
+
|
| 98 |
+
**Optimization notes.** Layer-wise LR decay (0.75); fine-tuning with Adam (initial LR 1e-4), cosine decay, early stopping; end-to-end updates (encoder + head).
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## 📚 Training Data
|
| 103 |
+
|
| 104 |
+
- **Pre-training:** Temple University Hospital EEG (TUEG), ~21k hours, ~15k subjects; broad clinical coverage. Overlaps with TUAB/TUAR/TUSL removed to avoid leakage.
|
| 105 |
+
- **Downstream:**
|
| 106 |
+
- **TUAB** (abnormal vs normal; predefined split).
|
| 107 |
+
- **TUAR** (artifact detection, BC/MC/MMC/MCC protocols; randomized 80/10/10).
|
| 108 |
+
- **TUSL** (4-class slowing/seizure/complex/normal; randomized 80/10/10).
|
| 109 |
+
|
| 110 |
+
*See paper for dataset licenses and curation details; users are responsible for complying with source dataset terms.*
|
| 111 |
+
|
| 112 |
+
---
|
| 113 |
+
|
| 114 |
+
## 🔧 How to Use
|
| 115 |
+
|
| 116 |
+
FEMBA weights are organized by downstream task:
|
| 117 |
+
|
| 118 |
+
- **`TUAB/`** → base/large variants pre-trained on TUEG (excluding TUAB), for TUAB abnormal EEG.
|
| 119 |
+
- **`TUAR/`** → tiny/base/large variants pre-trained on TUEG (excluding TUAR), for TUAR artifact detection.
|
| 120 |
+
- **`TUSL/`** → variants pre-trained on TUEG (excluding TUSL), for TUSL slowing classification.
|
| 121 |
+
|
| 122 |
+
**Example:** fine-tune TUAR with the base checkpoint:
|
| 123 |
+
|
| 124 |
+
```
|
| 125 |
+
TUAR/base.safetensors
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
Open `run_train.py` from the [BioFoundation GitHub repository](https://github.com/pulp-bio/BioFoundation.git) and configure:
|
| 129 |
+
|
| 130 |
+
```python
|
| 131 |
+
# Set paths (example)
|
| 132 |
+
os.environ['DATA_PATH'] = "/path/to/dataset"
|
| 133 |
+
os.environ['CHECKPOINT_DIR'] = "/my_directory/TUAR/base.safetensors"
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
Then launch fine-tuning (Hydra):
|
| 137 |
+
|
| 138 |
+
```bash
|
| 139 |
+
python -u run_train.py +experiment=FEMBA_finetune
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
**Environment variables**
|
| 143 |
+
- `DATA_PATH`: directory of the fine-tuning dataset.
|
| 144 |
+
- `CHECKPOINT_DIR`: path to the chosen task-specific checkpoint.
|
| 145 |
+
|
| 146 |
+
---
|
| 147 |
+
|
| 148 |
+
## 📊 Results (Key Highlights)
|
| 149 |
+
|
| 150 |
+
**TUAB (Abnormal EEG Detection)**
|
| 151 |
+
- **FEMBA-Huge:** **81.82%** balanced accuracy, **0.892** AUROC.
|
| 152 |
+
|
| 153 |
+
**TUAR (Artifact Detection)**
|
| 154 |
+
- **Binary (BC):** **FEMBA-Base** AUROC **0.949**, AUPR **0.932**.
|
| 155 |
+
|
| 156 |
+
**TUSL (Slowing Classification, 4-class)**
|
| 157 |
+
- **FEMBA-Base:** AUROC **0.731**.
|
| 158 |
+
|
| 159 |
+
> Full metrics, protocols, and comparisons—including MC/MMC on TUAR and multiple FEMBA sizes—are reported in the paper.
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## ⚡ Efficiency
|
| 164 |
+
|
| 165 |
+
FEMBA provides strong accuracy with reduced compute/memory relative to Transformer baselines:
|
| 166 |
+
|
| 167 |
+
- **FEMBA-Huge (386M):** ~**58.7B FLOPs**, ~**30% less** memory than comparable Transformer baselines, with competitive TUAB accuracy.
|
| 168 |
+
- **FEMBA-Tiny (7.8M):** ~**1.31B FLOPs**—substantially fewer than large Transformer baselines—while achieving strong TUAR MCC performance.
|
| 169 |
+
- **FEMBA-Base (47.7M):** ~**7.52B FLOPs**, markedly lower than many attention-based baselines.
|
| 170 |
+
|
| 171 |
+
See the paper for details on measurement setup and baseline references.
|
| 172 |
+
|
| 173 |
+
---
|
| 174 |
+
|
| 175 |
+
## ✅ Responsible AI, Risks & Biases
|
| 176 |
+
|
| 177 |
+
- **Clinical safety:** This model is **not** a certified medical device and should **not** be used for diagnosis. Human oversight is required.
|
| 178 |
+
- **Bias & drift:** Clinical EEG cohorts vary by device, montage, age, and pathology. Expect distribution shift and validate locally; consider domain adaptation (e.g., neonatal vs adult).
|
| 179 |
+
- **Artifacts:** While artifact detection is strong, rare/complex artifacts may still be misclassified; use robust pre-processing and QC procedures.
|
| 180 |
+
|
| 181 |
+
---
|
| 182 |
+
|
| 183 |
+
## 🔗 Sources
|
| 184 |
+
|
| 185 |
+
- **Code:** https://github.com/pulp-bio/BioFoundation
|
| 186 |
+
- **Paper:** FEMBA: Efficient and Scalable EEG Analysis with a Bidirectional Mamba Foundation Model (arXiv:2502.06438).
|
| 187 |
+
|
| 188 |
+
---
|
| 189 |
+
|
| 190 |
+
## 📜 Citation
|
| 191 |
+
|
| 192 |
+
If you use FEMBA in your research, please cite:
|
| 193 |
+
|
| 194 |
+
```bibtex
|
| 195 |
+
@misc{tegon2025fembaefficientscalableeeg,
|
| 196 |
+
title={FEMBA: Efficient and Scalable EEG Analysis with a Bidirectional Mamba Foundation Model},
|
| 197 |
+
author={Anna Tegon and Thorir Mar Ingolfsson and Xiaying Wang and Luca Benini and Yawei Li},
|
| 198 |
+
year={2025},
|
| 199 |
+
eprint={2502.06438},
|
| 200 |
+
archivePrefix={arXiv},
|
| 201 |
+
primaryClass={cs.LG},
|
| 202 |
+
url={https://arxiv.org/abs/2502.06438}
|
| 203 |
+
}
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
---
|
| 207 |
+
|
| 208 |
+
## 🛠️ Maintenance & Contact
|
| 209 |
+
|
| 210 |
+
- **Issues & support:** please open a GitHub issue in the BioFoundation repository.
|
| 211 |
+
|
| 212 |
+
---
|
| 213 |
+
|
| 214 |
+
## 🗒️ Changelog
|
| 215 |
+
|
| 216 |
+
- **v1.0:** Initial release of FEMBA model card with task-specific checkpoints and instructions.
|