|
Download README.md from braindecode/samba-100s-pretrained: direct link, hf CLI and curl.
- Browser
- Download file 11 kB
-
https://huggingface.co/braindecode/samba-100s-pretrained/resolve/main/README.md
- Command line
-
hf download hf://braindecode/samba-100s-pretrained/README.md
-
curl -L -o README.md https://huggingface.co/braindecode/samba-100s-pretrained/resolve/main/README.md
11 kB
| license: other | |
| library_name: braindecode | |
| base_model: Dcode-AI/SAMBA-100s | |
| tags: | |
| - braindecode | |
| - pytorch | |
| - safetensors | |
| - eeg | |
| - samba | |
| - mamba | |
| - foundation-model | |
| datasets: | |
| - TUAB | |
| # SAMBA-100s (pretrained encoder) | |
| Weights of SAMBA-100s, a checkpoint of SAMBA (Hong, Mackellar and Ghane, NeurReps 2026), for | |
| [`braindecode.models.SAMBA`](https://braindecode.org/stable/generated/braindecode.models.SAMBA.html) | |
| (braindecode 2.1): an EEG foundation model that reads electrode positions through a | |
| coordinate-driven spatial embedding and models time with differential Mamba-2 blocks. The authors | |
| pretrained it by masked reconstruction with a U-shaped encoder-decoder, on | |
| 100 s windows (12,800 samples at 128 Hz) from the Temple University Hospital Abnormal | |
| EEG Corpus (TUAB), with 16 bipolar channels. | |
| These files are the encoder of | |
| [`Dcode-AI/SAMBA-100s`](https://huggingface.co/Dcode-AI/SAMBA-100s) at revision | |
| `3a235a136b3c1c19e23249ff7b4f48cdf93e8912`, | |
| converted to braindecode's parameter names by `convert.py`. | |
| ```python | |
| from braindecode.models import SAMBA | |
| # The 94 target electrodes of the weights are the default montage ("Intended input" | |
| # gives another one) | |
| model = SAMBA.from_pretrained("braindecode/samba-100s-pretrained", n_outputs=2).eval() | |
| # x: (batch, 94, n_times) -> features: (batch, 256) | |
| features = model(x, return_features=True)["features"] | |
| ``` | |
| The files hold no classification head: it is not pretrained, so `from_pretrained` builds a new one | |
| (PyTorch's default init) for your `n_outputs`. Fine-tune or linear-probe before use. `features` are | |
| the mean and the standard deviation over time of the encoder output, `(batch, 2 * 128)`; the first | |
| 128 are the authors' mean-pooled features. | |
| ## Intended input | |
| - EEG at 128 Hz, each channel of each window divided by the 95th percentile of its absolute value, | |
| as in the authors' pretraining. `SAMBA` does not normalise. | |
| - Electrode positions in `chs_info[i]["loc"][:3]`, in metres, in the frame of the `colin27_1005` | |
| template of MNE (`standard_1005` in older versions of MNE), which is the frame of the weights. | |
| `raw.set_montage` writes head-frame positions instead, a few centimetres away (up to 5.7 cm for | |
| these electrodes), so build `chs_info` from the template. The default `chs_info` of `config.json` | |
| is the 94 target electrodes of the weights (`Fp1`, `Fpz`, ..., `A2`); any other montage with | |
| positions is mapped onto them: | |
| ```python | |
| import mne | |
| import numpy as np | |
| template = mne.channels.make_standard_montage("colin27_1005").get_positions()["ch_pos"] | |
| names = ["Fp1", "Fp2", "C3", "C4", "O1", "O2"] | |
| chs_info = [{"ch_name": n, "kind": "eeg", "loc": np.r_[template[n], np.zeros(9)]} for n in names] | |
| model = SAMBA.from_pretrained( | |
| "braindecode/samba-100s-pretrained", chs_info=chs_info, n_outputs=2, n_times=512 | |
| ) | |
| ``` | |
| - Pretrained on windows of 12,800 samples (100 s); any window of at least 8 samples is read (the | |
| encoder keeps `n_times // 8` steps). | |
| - The pretraining data was band-passed at 0.5-45 Hz. It is TUAB; the authors note that | |
| their pathology task "shares the TUAB data domain with pretraining". | |
| ## Changes from the original release | |
| `convert.py` renames the keys, drops the reconstruction decoder and writes the files with | |
| `save_pretrained`. No value is changed. | |
| - **Keys renamed.** The authors' network is `SAMBA.encoder`, so all 178 encoder keys gain that | |
| prefix; 173 of them also change a module name (plain prefix replacement, no pattern): | |
| | original prefix | braindecode (under `encoder.`) | keys | | |
| |---|---|---| | |
| | `cdse.net.` | `cdse.score.` | 4 | | |
| | `cdse.target_positions` | `cdse.target_positions` | 1 | | |
| | `input_embedding.0.` | `input_embedding.` | 8 | | |
| | `encoder1.0.` | `encoder1_linear.` | 2 | | |
| | `encoder1.1.mixer.` | `encoder1_mamba.` | 8 | | |
| | `encoder2.`, `encoder3.` | `encoder2.`, `encoder3.` | 2 + 2 | | |
| | `bottleneck.0.` | `bottleneck_in.` | 2 | | |
| | `bottleneck.1.` | `bottleneck_mamba.` | 147 | | |
| | `bottleneck.2.` | `bottleneck_out.` | 2 | | |
| - **Decoder dropped.** The checkpoint also holds the reconstruction decoder, which the | |
| encoder-only `SAMBA` does not have: 24 of 202 tensors are left out | |
| (key counts per prefix): | |
| `decoder1.` 2, `decoder2.` 2, `decoder2_mixer.` 8, `decoder3.` 2, `decoder3_mixer.` 8, | |
| `output_embedding.` 2 (this checkpoint has no `spatial_decoder.`). | |
| - **Values unchanged.** Each of the 178 tensors (float32) is bitwise equal to its original tensor. | |
| - **Config.** `config.json` is braindecode's, written by `save_pretrained`: the sizes are the | |
| authors', their `target_channel_names` and `target_positions` give the default `chs_info` and | |
| `target_positions`, and their `sampling_rate` and the window of the pretraining give `sfreq` and | |
| `n_times`. Their decoder fields (`reconstruction_mode`, `reconstruction_channels`) are not kept. | |
| ## Verification | |
| `SAMBA.from_pretrained` on these files against the port of braindecode#1689 (commit `e108b378`), | |
| whose `load_pretrained_encoder` reads the authors' original `model.safetensors` unchanged; CPU, eval | |
| mode, the same seeded input. The encoder output, the pooled features and the outputs with a shared | |
| head differ by 0.0 (max abs) in float32 and in float64, for the 94 target electrodes at | |
| 12,800 samples (100 s) and for 19 electrodes of the 10-20 system at 131 samples. A change of 1e-3 | |
| in one value of one block shows as a difference of 1e-5 or more, so the comparison would see it. | |
| ## Source and license | |
| - Paper: J. Hong, G. Mackellar, S. Ghane. *SAMBA: Toward an EEG Foundation Model via Differential | |
| Mamba and Coordinate-Driven Spatial Embedding.* NeurReps 2026. | |
| [arXiv:2511.18571](https://arxiv.org/abs/2511.18571) | |
| - Code: [Jiazhen-Hong/SAMBA](https://github.com/Jiazhen-Hong/SAMBA) | |
| - Weights: [Dcode-AI/SAMBA-100s](https://huggingface.co/Dcode-AI/SAMBA-100s) at | |
| revision `3a235a136b3c1c19e23249ff7b4f48cdf93e8912`: | |
| `model.safetensors` (sha256 `e0d5fb8df308c2c11438cb1199ff1c372ed77f7c868acea036c5e7c2216ce6d4`), | |
| `config.json` (sha256 `04394bbda6c22ce018de91fffa54f4f0501cef0a6f5ac6c8d22963514f77c7d5`). | |
| The authors retrained SAMBA-2s and SAMBA-100s after the checkpoints of the paper were lost, and | |
| evaluate them separately from the paper's SAMBA-E and SAMBA-T. | |
| Neither the authors' Hugging Face repository nor their GitHub repository has a licence file; the | |
| weights are by the SAMBA authors. Please cite: | |
| ```bibtex | |
| @inproceedings{hong2026samba, | |
| title = {{SAMBA}: Toward an {EEG} Foundation Model via Differential Mamba and Coordinate-Driven Spatial Embedding}, | |
| author = {Hong, Jiazhen and Mackellar, Geoffrey and Ghane, Soheila}, | |
| booktitle = {NeurReps 2026: Symmetry and Geometry in Neural Representations (Proceedings Track)}, | |
| year = {2026} | |
| } | |
| ``` | |
| ## Conversion | |
| `convert.py` (in this repository, copied below) was run with braindecode at `4696bd5b`: | |
| ``` | |
| python convert.py <directory of Dcode-AI/SAMBA-100s> <out_dir> | |
| ``` | |
| | file | sha256 | | |
| |---|---| | |
| | `model.safetensors` | `abc0c1c19b4f139c4cc0a978223b7b328a645327a138d2c67a0b2fe021b577b9` | | |
| | `config.json` | `653e823dd0b69a06aa2aab2c41a58f09bc594dad485c886766c02700de55725c` | | |
| | `convert.py` | `1169c4ade79e832124f216f1863bc88fec44fe19fc442cee92df6741494188e8` | | |
| <details> | |
| <summary><code>convert.py</code></summary> | |
| ```python | |
| """Convert ``Dcode-AI/SAMBA-2s`` or ``Dcode-AI/SAMBA-100s`` to braindecode's ``SAMBA``. | |
| usage: python convert.py <src_dir> <out_dir> | |
| ``src_dir`` holds ``config.json`` and ``model.safetensors`` of the authors' | |
| release (SAMBA-2s at e553287, SAMBA-100s at 3a235a1). Builds ``SAMBA`` from that | |
| config, with the 94 target electrodes as the default ``chs_info`` and the window | |
| of the pretraining; renames the keys with the prefix pairs below, drops the | |
| reconstruction decoder, loads the rest into ``SAMBA.encoder`` with ``strict=True`` | |
| and writes ``config.json`` and ``model.safetensors`` with ``SAMBA.save_pretrained``. | |
| No value is changed. The release holds the encoder only: the head is not | |
| pretrained, so ``from_pretrained`` builds it for the caller's ``n_outputs``. | |
| """ | |
| import json | |
| import os | |
| import sys | |
| import numpy as np | |
| from safetensors.torch import load_file, save_file | |
| from braindecode.models import SAMBA | |
| # Samples of one pretraining window at 128 Hz (2 s and 100 s), from the README of | |
| # each release | |
| N_TIMES = {"SAMBA-2s": 256, "SAMBA-100s": 12800} | |
| # (authors' prefix, braindecode's prefix) of the encoder keys that change name. | |
| # The others (cdse.target_positions, encoder2.*, encoder3.*) keep theirs. | |
| RENAMES = ( | |
| ("cdse.net.", "cdse.score."), | |
| ("input_embedding.0.", "input_embedding."), | |
| ("encoder1.0.", "encoder1_linear."), | |
| ("encoder1.1.mixer.", "encoder1_mamba."), | |
| ("bottleneck.0.", "bottleneck_in."), | |
| ("bottleneck.1.", "bottleneck_mamba."), | |
| ("bottleneck.2.", "bottleneck_out."), | |
| ) | |
| # The reconstruction decoder of the pretraining, not part of SAMBA | |
| DECODER = ( | |
| "decoder1.", | |
| "decoder2.", | |
| "decoder2_mixer.", | |
| "decoder3.", | |
| "decoder3_mixer.", | |
| "output_embedding.", | |
| "spatial_decoder.", | |
| ) | |
| def rename(key): | |
| for old, new in RENAMES: | |
| if key.startswith(old): | |
| return new + key[len(old) :] | |
| return key | |
| if __name__ == "__main__": | |
| src_dir, out_dir = sys.argv[1:] | |
| with open(os.path.join(src_dir, "config.json")) as f: | |
| config = json.load(f) | |
| assert config["model_type"] == "samba" and config["mixer_backend"] == "mamba2" | |
| source = load_file(os.path.join(src_dir, "model.safetensors")) | |
| encoder = {rename(k): v for k, v in source.items() if not k.startswith(DECODER)} | |
| # Default montage: the target electrodes, in metres and in the frame of the | |
| # standard_1005 template of MNE, which is the frame of the weights | |
| chs_info = [ | |
| {"ch_name": name, "kind": "eeg", "loc": np.r_[position, np.zeros(9)]} | |
| for name, position in zip( | |
| config["target_channel_names"], config["target_positions"] | |
| ) | |
| ] | |
| model = SAMBA( | |
| n_outputs=2, | |
| chs_info=chs_info, | |
| n_times=N_TIMES[config["checkpoint_variant"]], | |
| sfreq=config["sampling_rate"], | |
| target_positions=config["target_positions"], | |
| base_channels=config["base_channels"], | |
| bottleneck_channels=config["bottleneck_channels"], | |
| d_state=config["d_state"], | |
| d_conv=config["d_conv"], | |
| expand=config["expand"], | |
| head_dim=config["head_dim"], | |
| cdse_hidden_dim=config["cdse_hidden_dim"], | |
| ) | |
| # Every key of the encoder present, none left over | |
| model.encoder.load_state_dict(encoder, strict=True) | |
| model.save_pretrained(out_dir) | |
| # The safetensors file is the release: no pickle next to it | |
| pickle = os.path.join(out_dir, "pytorch_model.bin") | |
| if os.path.exists(pickle): | |
| os.remove(pickle) | |
| # The encoder only: the head is not pretrained | |
| weights = { | |
| k: v for k, v in model.state_dict().items() if not k.startswith("final_layer.") | |
| } | |
| save_file(weights, os.path.join(out_dir, "model.safetensors"), metadata={"format": "pt"}) | |
| ``` | |
| </details> | |