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 
 
 
8c84bf1
 
 
 
 
 
ec1c5f9
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
---
license: cc-by-nd-4.0
language:
- en
library_name: pytorch
tags:
- eeg
- biosignal
- mamba
- state-space-model
- bidirectional
- foundation-model
- self-supervised
- masked-modeling
- efficient
- edge-ai
- neuroscience
datasets:
- TUEG
- TUAB
- TUAR
- TUSL
metrics:
- balanced_accuracy
- roc_auc
- pr_auc
thumbnail: https://raw.githubusercontent.com/pulp-bio/BioFoundation/refs/heads/main/docs/model/logo/FEMBA_logo.png
model-index:
  - name: FEMBA-Tiny
    results:
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Binary)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: binary
        metrics:
          - type: roc_auc
            value: 0.937
            name: AUROC
          - type: pr_auc
            value: 0.912
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multilabel)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multilabel
        metrics:
          - type: roc_auc
            value: 0.887
            name: AUROC
          - type: pr_auc
            value: 0.645
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multiclass-Multioutput)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multiclass-multioutput
        metrics:
          - type: roc_auc
            value: 0.893
            name: AUROC
          - type: pr_auc
            value: 0.504
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multiclass)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multiclass
        metrics:
          - type: roc_auc
            value: 0.918
            name: AUROC
          - type: pr_auc
            value: 0.518
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Slowing Classification
        dataset:
          type: TUSL
          name: TUH EEG Slowing Corpus (TUSL)
        metrics:
          - type: roc_auc
            value: 0.708
            name: AUROC
          - type: pr_auc
            value: 0.277
            name: AUC-PR
  - name: FEMBA-Base
    results:
      - task:
          type: time-series-classification
          name: EEG Abnormality Detection
        dataset:
          type: TUAB
          name: TUH EEG Abnormal Corpus (TUAB)
        metrics:
          - type: balanced_accuracy
            value: 81.05
            name: Balanced Accuracy (%)
          - type: roc_auc
            value: 0.8829
            name: AUROC
          - type: pr_auc
            value: 0.8894
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Binary)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: binary
        metrics:
          - type: roc_auc
            value: 0.949
            name: AUROC
          - type: pr_auc
            value: 0.932
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multilabel)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multilabel
        metrics:
          - type: roc_auc
            value: 0.909
            name: AUROC
          - type: pr_auc
            value: 0.634
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multiclass-Multioutput)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multiclass-multioutput
        metrics:
          - type: roc_auc
            value: 0.888
            name: AUROC
          - type: pr_auc
            value: 0.518
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multiclass)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multiclass
        metrics:
          - type: roc_auc
            value: 0.900
            name: AUROC
          - type: pr_auc
            value: 0.559
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Slowing Classification
        dataset:
          type: TUSL
          name: TUH EEG Slowing Corpus (TUSL)
        metrics:
          - type: roc_auc
            value: 0.731
            name: AUROC
          - type: pr_auc
            value: 0.289
            name: AUC-PR
  - name: FEMBA-Large
    results:
      - task:
          type: time-series-classification
          name: EEG Abnormality Detection
        dataset:
          type: TUAB
          name: TUH EEG Abnormal Corpus (TUAB)
        metrics:
          - type: balanced_accuracy
            value: 81.47
            name: Balanced Accuracy (%)
          - type: roc_auc
            value: 0.8856
            name: AUROC
          - type: pr_auc
            value: 0.8992
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Binary)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: binary
        metrics:
          - type: roc_auc
            value: 0.944
            name: AUROC
          - type: pr_auc
            value: 0.913
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multilabel)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multilabel
        metrics:
          - type: roc_auc
            value: 0.899
            name: AUROC
          - type: pr_auc
            value: 0.608
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multiclass-Multioutput)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multiclass-multioutput
        metrics:
          - type: roc_auc
            value: 0.878
            name: AUROC
          - type: pr_auc
            value: 0.516
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Artifact Detection (Multiclass)
        dataset:
          type: TUAR
          name: TUH EEG Artifact Corpus (TUAR)
          config: multiclass
        metrics:
          - type: roc_auc
            value: 0.915
            name: AUROC
          - type: pr_auc
            value: 0.521
            name: AUC-PR
      - task:
          type: time-series-classification
          name: EEG Slowing Classification
        dataset:
          type: TUSL
          name: TUH EEG Slowing Corpus (TUSL)
        metrics:
          - type: roc_auc
            value: 0.714
            name: AUROC
          - type: pr_auc
            value: 0.282
            name: AUC-PR
  - name: FEMBA-Huge
    results:
      - task:
          type: time-series-classification
          name: EEG Abnormality Detection
        dataset:
          type: TUAB
          name: TUH EEG Abnormal Corpus (TUAB)
        metrics:
          - type: balanced_accuracy
            value: 81.82
            name: Balanced Accuracy (%)
          - type: roc_auc
            value: 0.8921
            name: AUROC
          - type: pr_auc
            value: 0.9005
            name: AUC-PR
---

<div align="center">
  <img src="https://raw.githubusercontent.com/pulp-bio/BioFoundation/refs/heads/main/docs/model/logo/FEMBA_logo.png" alt="FEMBA Logo" width="800"/>
  <h1>FEMBA: Foundational Encoder Model with Bidirectional Mamba for EEG</h1>
</div>
<p align="center">
  <a href="https://github.com/pulp-bio/BioFoundation">
    <img src ="https://img.shields.io/github/stars/pulp-bio/BioFoundation?color=ccf" alt="Github">
  </a>
  <a href="https://creativecommons.org/licenses/by-nd/4.0/">
    <img src="https://img.shields.io/badge/License-CC_BY--ND_4.0-lightgrey.svg" alt="License">
  </a>
  <a href="https://arxiv.org/abs/2502.06438">
    <img src="https://img.shields.io/badge/arXiv-2502.06438-b31b1b.svg" alt="Paper">
  </a>
</p>

**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.

---

## 🔒 License & Usage Policy (Weights)

**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.*

### ✅ You may
- **Use** and **redistribute** the **unmodified** FEMBA weights (including in commercial settings) **with proper attribution** to the FEMBA authors.
- **Fine-tune / adapt** the weights **for your internal use** (research or production) **without redistributing** the modified weights.
- **Publish your code, configs, logs, and papers** describing experiments with FEMBA (please cite the paper).

### 🚫 You may not
- **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.
- **Imply endorsement** by the FEMBA authors for any derivative or evaluation without our written permission.
- **Use the FEMBA name** in a way that suggests your modified model is an official FEMBA release.

### 🤝 How to contribute improvements (PR-gated releases)
We welcome community improvements via a **pull-request (PR)** workflow. If you believe your improvements should become an **official FEMBA release**:
1. **Open a PR** in the [BioFoundation repository](https://github.com/pulp-bio/BioFoundation) describing the change (architecture/head/training recipe, datasets, preprocessing, compute).
2. Include **reproducibility artifacts**: configs, seeds, scripts, environment details, training/validation logs, and the **evaluation protocol** (e.g., TUAB/TUAR/TUSL) with exact splits.
3. Provide **comprehensive results** (AUROC/AUPR/BA, FLOPs, memory) vs. the baselines reported in the FEMBA paper.
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**.

> 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.

---

## 🔎 Model Summary

- **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.
- **Scaling:** Linear time & memory in sequence length (state-space model), enabling efficient long-context EEG modeling and on-device scenarios.
- **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.
- **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.
- **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.

---

## 🚀 Model Variants

| Variant | Parameters | (num_blocks, embed_dim) |
| :--- | :--- | :--- |
| **FEMBA-tiny** | 7.8M | (2, 35) |
| **FEMBA-base** | 47.7M | (12, 35) |
| **FEMBA-large** | 77.8M | (4, 79) |
| **FEMBA-huge** | 386M | (20, 79) |

*Hidden state size is 80 for all variants; blocks correspond to Bi-Mamba layers in the encoder.*

---

## 🧠 Intended Use & Limitations

**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.

**Out-of-scope / limitations.**
- **Not a medical device.** Outputs are research signals and **must not** be used for clinical decision-making without appropriate validation and regulatory processes.
- **Domain shift.** Performance can degrade across cohorts (e.g., neonatal vs. adult EEG) and label protocols; domain adaptation is encouraged.
- **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.

---

## 🏗️ Architecture & Training Details

**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.

**Self-supervised objective.** Randomly mask **60%** of patches; reconstruct masked content with a lightweight decoder using **Smooth L1** loss (computed on masked patches only).

**Encoder.** Stacked **Bidirectional Mamba** blocks (forward + backward over a reversed copy), merged and residually connected; hidden size fixed to 80.

**Fine-tuning heads.**
- *Linear classifier:* small MLP (≈0.5M params).
- *Mamba-enhanced classifier:* adds one Mamba block before the linear layer (up to ≈0.7M params).

**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).

---

## 📚 Training Data

- **Pre-training:** Temple University Hospital EEG (TUEG), ~21k hours, ~15k subjects; broad clinical coverage. Overlaps with TUAB/TUAR/TUSL removed to avoid leakage.
- **Downstream:**  
  - **TUAB** (abnormal vs normal; predefined split).  
  - **TUAR** (artifact detection, BC/MC/MMC/MCC protocols; randomized 80/10/10).  
  - **TUSL** (4-class slowing/seizure/complex/normal; randomized 80/10/10).

*See paper for dataset licenses and curation details; users are responsible for complying with source dataset terms.*

---

## 🔧 How to Use

FEMBA weights are organized by downstream task:

- **`TUAB/`** → base/large variants pre-trained on TUEG (excluding TUAB), for TUAB abnormal EEG.  
- **`TUAR/`** → tiny/base/large variants pre-trained on TUEG (excluding TUAR), for TUAR artifact detection.  
- **`TUSL/`** → variants pre-trained on TUEG (excluding TUSL), for TUSL slowing classification.

**Example:** fine-tune TUAR with the base checkpoint:

```
TUAR/base.safetensors
```

Open `run_train.py` from the [BioFoundation GitHub repository](https://github.com/pulp-bio/BioFoundation.git) and configure:

```python
# Set paths (example)
os.environ['DATA_PATH'] = "/path/to/dataset"
os.environ['CHECKPOINT_DIR'] = "/my_directory/TUAR/base.safetensors"
```

Then launch fine-tuning (Hydra):

```bash
python -u run_train.py +experiment=FEMBA_finetune
```

**Environment variables**  
- `DATA_PATH`: directory of the fine-tuning dataset.  
- `CHECKPOINT_DIR`: path to the chosen task-specific checkpoint.

---

## 📊 Results (Key Highlights)

**TUAB (Abnormal EEG Detection)**  
- **FEMBA-Huge:** **81.82%** balanced accuracy, **0.892** AUROC.

**TUAR (Artifact Detection)**  
- **Binary (BC):** **FEMBA-Base** AUROC **0.949**, AUPR **0.932**.

**TUSL (Slowing Classification, 4-class)**  
- **FEMBA-Base:** AUROC **0.731**.

> Full metrics, protocols, and comparisons—including MC/MMC on TUAR and multiple FEMBA sizes—are reported in the paper.

---

## ⚡ Efficiency

FEMBA provides strong accuracy with reduced compute/memory relative to Transformer baselines:

- **FEMBA-Huge (386M):** ~**58.7B FLOPs**, ~**30% less** memory than comparable Transformer baselines, with competitive TUAB accuracy.
- **FEMBA-Tiny (7.8M):** ~**1.31B FLOPs**—substantially fewer than large Transformer baselines—while achieving strong TUAR MCC performance.
- **FEMBA-Base (47.7M):** ~**7.52B FLOPs**, markedly lower than many attention-based baselines.

See the paper for details on measurement setup and baseline references.

---

## ✅ Responsible AI, Risks & Biases

- **Clinical safety:** This model is **not** a certified medical device and should **not** be used for diagnosis. Human oversight is required.  
- **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).  
- **Artifacts:** While artifact detection is strong, rare/complex artifacts may still be misclassified; use robust pre-processing and QC procedures.

---

## 🔗 Sources

- **Code:** https://github.com/pulp-bio/BioFoundation  
- **Paper:** FEMBA: Efficient and Scalable EEG Analysis with a Bidirectional Mamba Foundation Model (arXiv:2502.06438).

---

## 📜 Citation

If you use FEMBA in your research, please cite:

```bibtex
@misc{tegon2025fembaefficientscalableeeg,
      title={FEMBA: Efficient and Scalable EEG Analysis with a Bidirectional Mamba Foundation Model}, 
      author={Anna Tegon and Thorir Mar Ingolfsson and Xiaying Wang and Luca Benini and Yawei Li},
      year={2025},
      eprint={2502.06438},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2502.06438}
}
```

---

## 🛠️ Maintenance & Contact

- **Issues & support:** please open a GitHub issue in the BioFoundation repository.

---

## 🔗 Related Models

- **[LuMamba](https://huggingface.co/PulpBio/LuMamba)** — Direct successor. Combines FEMBA's bi-Mamba encoder with LUNA's channel-unification cross-attention for topology-invariant EEG modeling, plus LeJEPA pre-training.
- **[LUNA](https://huggingface.co/PulpBio/LUNA)** — Transformer-based topology-agnostic EEG foundation model (NeurIPS 2025). Companion architecture focused on channel-heterogeneity rather than sequence-length efficiency.
- **[TinyMyo](https://huggingface.co/PulpBio/TinyMyo)** — Tiny foundation model for flexible EMG signal processing at the edge.
---
## 🗒️ Changelog

- **v1.0:** Initial release of FEMBA model card with task-specific checkpoints and instructions.