Encoder of Dcode-AI/SAMBA-100s@3a235a136b3c1c19e23249ff7b4f48cdf93e8912, converted to braindecode's SAMBA parameter names
Browse filesFiles changed (4)
- README.mdAdded+257
- config.jsonAdded+2183
- convert.pyAdded+101
- model.safetensorsAdded+3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257---license: otherlibrary_name: braindecodebase_model: Dcode-AI/SAMBA-100stags:- braindecode- pytorch- safetensors- eeg- samba- mamba- foundation-model---
# 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 acoordinate-driven spatial embedding and models time with differential Mamba-2 blocks. The authorspretrained it by masked reconstruction with a U-shaped encoder-decoder, on100 s windows (12,800 samples at 128 Hz) from the Temple University Hospital AbnormalEEG 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`.
```pythonfrom 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` arethe mean and the standard deviation over time of the encoder output, `(batch, 2 * 128)`; the first128 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, evalmode, the same seeded input. The encoder output, the pooled features and the outputs with a sharedhead differ by 0.0 (max abs) in float32 and in float64, for the 94 target electrodes at12,800 samples (100 s) and for 19 electrodes of the 10-20 system at 131 samples. A change of 1e-3in 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; theweights 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 thatconfig, with the 94 target electrodes as the default ``chs_info`` and the windowof the pretraining; renames the keys with the prefix pairs below, drops thereconstruction 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 notpretrained, so ``from_pretrained`` builds it for the caller's ``n_outputs``."""
import jsonimport osimport sys
import numpy as npfrom 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 releaseN_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 SAMBADECODER = ( "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>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183{ "n_outputs": 2, "n_chans": null, "chs_info": [ { "ch_name": "Fp1", "kind": "eeg", "loc": [ -0.0294367, 0.08391710000000001, -0.006990000000000001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "Fpz", "kind": "eeg", "loc": [ 0.0001123, 0.088247, -0.001713, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "Fp2", "kind": "eeg", "loc": [ 0.0298723, 0.0848959, -0.00708, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF9", "kind": "eeg", "loc": [ -0.04897079999999999, 0.0640872, -0.047683, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF7", "kind": "eeg", "loc": [ -0.0548397, 0.0685722, -0.01059, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF5", "kind": "eeg", "loc": [ -0.0454307, 0.0728622, 0.005978, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF3", "kind": "eeg", "loc": [ -0.0337007, 0.0768371, 0.021227, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF1", "kind": "eeg", "loc": [ -0.0184717, 0.0799041, 0.032752, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AFz", "kind": "eeg", "loc": [ 0.0002313, 0.080771, 0.035417, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF2", "kind": "eeg", "loc": [ 0.0198203, 0.08030190000000001, 0.032764, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF4", "kind": "eeg", "loc": [ 0.0357123, 0.0777259, 0.021956, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF6", "kind": "eeg", "loc": [ 0.0465843, 0.0738078, 0.006033999999999999, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF8", "kind": "eeg", "loc": [ 0.0557433, 0.0696568, -0.010755, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "AF10", "kind": "eeg", "loc": [ 0.0504352, 0.0638698, -0.04800500000000001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F9", "kind": "eeg", "loc": [ -0.0701019, 0.0416523, -0.049952, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F7", "kind": "eeg", "loc": [ -0.0702629, 0.0424743, -0.01142, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F5", "kind": "eeg", "loc": [ -0.0644658, 0.0480353, 0.016921, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F3", "kind": "eeg", "loc": [ -0.0502438, 0.0531112, 0.042192, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F1", "kind": "eeg", "loc": [ -0.0274958, 0.0569311, 0.060342, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "Fz", "kind": "eeg", "loc": [ 0.0003122, 0.058512, 0.06646200000000001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F2", "kind": "eeg", "loc": [ 0.0295142, 0.0576019, 0.05954, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F4", "kind": "eeg", "loc": [ 0.0518362, 0.0543048, 0.040814, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F6", "kind": "eeg", "loc": [ 0.0679142, 0.0498297, 0.016367, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F8", "kind": "eeg", "loc": [ 0.0730431, 0.0444217, -0.012, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "F10", "kind": "eeg", "loc": [ 0.07211409999999999, 0.0420667, -0.050452, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FT9", "kind": "eeg", "loc": [ -0.08407590000000001, 0.0145673, -0.050429, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FT7", "kind": "eeg", "loc": [ -0.080775, 0.0141203, -0.011135, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FC5", "kind": "eeg", "loc": [ -0.0772149, 0.0186433, 0.02446, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FC3", "kind": "eeg", "loc": [ -0.0601819, 0.0227162, 0.055544, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FC1", "kind": "eeg", "loc": [ -0.0340619, 0.0260111, 0.07998699999999999, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FCz", "kind": "eeg", "loc": [ 0.0003761, 0.02739, 0.08866800000000001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FC2", "kind": "eeg", "loc": [ 0.03478410000000001, 0.0264379, 0.078808, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FC4", "kind": "eeg", "loc": [ 0.0622931, 0.0237228, 0.05563000000000001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FC6", "kind": "eeg", "loc": [ 0.0795341, 0.0199357, 0.024438, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FT8", "kind": "eeg", "loc": [ 0.0818151, 0.0154167, -0.01133, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "FT10", "kind": "eeg", "loc": [ 0.0841131, 0.0143647, -0.050538, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T9", "kind": "eeg", "loc": [ -0.0858941, -0.0158287, -0.048283, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T7", "kind": "eeg", "loc": [ -0.0841611, -0.0160187, -0.009346, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "C5", "kind": "eeg", "loc": [ -0.08028010000000001, -0.0137597, 0.02916, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "C3", "kind": "eeg", "loc": [ -0.06535809999999999, -0.0116317, 0.064358, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "C1", "kind": "eeg", "loc": [ -0.036158, -0.0099839, 0.089752, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "Cz", "kind": "eeg", "loc": [ 0.0004009, -0.009167, 0.100244, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "C2", "kind": "eeg", "loc": [ 0.037672, -0.0096241, 0.088412, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "C4", "kind": "eeg", "loc": [ 0.06711790000000001, -0.0109003, 0.06358, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "C6", "kind": "eeg", "loc": [ 0.0834559, -0.0127763, 0.029208, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T8", "kind": "eeg", "loc": [ 0.0850799, -0.0150203, -0.00949, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T10", "kind": "eeg", "loc": [ 0.0855599, -0.0163613, -0.048271, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "TP9", "kind": "eeg", "loc": [ -0.0856192, -0.0465147, -0.045707, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "TP7", "kind": "eeg", "loc": [ -0.08483020000000001, -0.04602170000000001, -0.007056, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "CP5", "kind": "eeg", "loc": [ -0.0795922, -0.0465507, 0.030949, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "CP3", "kind": "eeg", "loc": [ -0.0635562, -0.0470088, 0.065624, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "CP1", "kind": "eeg", "loc": [ -0.0355131, -0.0472919, 0.091315, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "CPz", "kind": "eeg", "loc": [ 0.0003858, -0.047318, 0.099432, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "CP2", "kind": "eeg", "loc": [ 0.0383838, -0.0470731, 0.090695, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "CP4", "kind": "eeg", "loc": [ 0.0666118, -0.0466372, 0.06558, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "CP6", "kind": "eeg", "loc": [ 0.0833218, -0.0461013, 0.031206, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "TP8", "kind": "eeg", "loc": [ 0.0855488, -0.0455453, -0.00713, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "TP10", "kind": "eeg", "loc": [ 0.0861618, -0.0470353, -0.045869, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P9", "kind": "eeg", "loc": [ -0.0730093, -0.07376569999999999, -0.040998, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P7", "kind": "eeg", "loc": [ -0.0724343, -0.0734527, -0.002487, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P5", "kind": "eeg", "loc": [ -0.06727230000000001, -0.0762907, 0.028382, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P3", "kind": "eeg", "loc": [ -0.0530073, -0.0787878, 0.05594, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P1", "kind": "eeg", "loc": [ -0.0286203, -0.0805249, 0.075436, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "Pz", "kind": "eeg", "loc": [ 0.0003247, -0.08111499999999999, 0.082615, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P2", "kind": "eeg", "loc": [ 0.0319197, -0.08048709999999999, 0.07671599999999999, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P4", "kind": "eeg", "loc": [ 0.0556667, -0.0785602, 0.056561, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P6", "kind": "eeg", "loc": [ 0.0678877, -0.07590430000000001, 0.028091, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P8", "kind": "eeg", "loc": [ 0.0730557, -0.07306829999999999, -0.00254, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "P10", "kind": "eeg", "loc": [ 0.0738947, -0.07439029999999999, -0.04122, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO9", "kind": "eeg", "loc": [ -0.05491040000000001, -0.0980448, -0.035465, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO7", "kind": "eeg", "loc": [ -0.0548404, -0.0975279, 0.002792, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO5", "kind": "eeg", "loc": [ -0.0484244, -0.0993408, 0.021599, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO3", "kind": "eeg", "loc": [ -0.0365114, -0.1008529, 0.037167, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO1", "kind": "eeg", "loc": [ -0.0189724, -0.101768, 0.046536, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "POz", "kind": "eeg", "loc": [ 0.0002156, -0.102178, 0.050608, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO2", "kind": "eeg", "loc": [ 0.0198776, -0.101793, 0.046393, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO4", "kind": "eeg", "loc": [ 0.0367816, -0.1008491, 0.036397, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO6", "kind": "eeg", "loc": [ 0.0498196, -0.0994461, 0.021727, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO8", "kind": "eeg", "loc": [ 0.0556666, -0.0976251, 0.00273, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "PO10", "kind": "eeg", "loc": [ 0.0549876, -0.0980911, -0.035541, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "O1", "kind": "eeg", "loc": [ -0.0294134, -0.112449, 0.008839, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "Oz", "kind": "eeg", "loc": [ 0.0001076, -0.114892, 0.014657, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "O2", "kind": "eeg", "loc": [ 0.0298426, -0.112156, 0.0088, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "O9", "kind": "eeg", "loc": [ -0.0298184, -0.11457, -0.029216, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "Iz", "kind": "eeg", "loc": [ 4.499999999999999e-06, -0.118565, -0.023078, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "O10", "kind": "eeg", "loc": [ 0.0297416, -0.11426, -0.029256, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T3", "kind": "eeg", "loc": [ -0.0841611, -0.0160187, -0.009346, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T5", "kind": "eeg", "loc": [ -0.0724343, -0.0734527, -0.002487, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T4", "kind": "eeg", "loc": [ 0.0850799, -0.0150203, -0.00949, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "T6", "kind": "eeg", "loc": [ 0.0730557, -0.07306829999999999, -0.00254, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "M1", "kind": "eeg", "loc": [ -0.0860761, -0.0449897, -0.067986, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "M2", "kind": "eeg", "loc": [ 0.08579389999999999, -0.0450093, -0.06803100000000001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "A1", "kind": "eeg", "loc": [ -0.0860761, -0.0249897, -0.067986, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] }, { "ch_name": "A2", "kind": "eeg", "loc": [ 0.08579389999999999, -0.0250093, -0.06803100000000001, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] } ], "n_times": 12800, "input_window_seconds": null, "sfreq": 128.0, "target_positions": [ [ -0.02943670004606247, 0.08391710370779037, -0.006990000139921904 ], [ 0.00011229999654460698, 0.08824700117111206, -0.0017130000051110983 ], [ 0.02987230010330677, 0.08489590138196945, -0.007079999893903732 ], [ -0.04897079989314079, 0.06408719718456268, -0.047683000564575195 ], [ -0.05483970046043396, 0.06857220083475113, -0.010590000078082085 ], [ -0.04543070122599602, 0.07286220043897629, 0.00597799988463521 ], [ -0.03370070084929466, 0.07683710008859634, 0.021227000281214714 ], [ -0.018471699208021164, 0.07990410178899765, 0.03275199979543686 ], [ 0.00023130000045057386, 0.08077099919319153, 0.03541700169444084 ], [ 0.019820300862193108, 0.08030190318822861, 0.032763998955488205 ], [ 0.03571230173110962, 0.07772590219974518, 0.021956000477075577 ], [ 0.04658430069684982, 0.07380779832601547, 0.0060339998453855515 ], [ 0.055743299424648285, 0.06965679675340652, -0.010754999704658985 ], [ 0.050435200333595276, 0.06386979669332504, -0.04800499975681305 ], [ -0.07010190188884735, 0.04165229946374893, -0.04995200037956238 ], [ -0.07026290148496628, 0.04247429966926575, -0.011420000344514847 ], [ -0.06446579843759537, 0.04803530126810074, 0.01692100055515766 ], [ -0.0502437986433506, 0.05311119928956032, 0.0421920008957386 ], [ -0.027495799586176872, 0.05693110078573227, 0.06034199893474579 ], [ 0.0003121999907307327, 0.05851199850440025, 0.06646200269460678 ], [ 0.029514199122786522, 0.05760189890861511, 0.059539999812841415 ], [ 0.051836200058460236, 0.05430480092763901, 0.040814001113176346 ], [ 0.0679142028093338, 0.04982969909906387, 0.016366999596357346 ], [ 0.0730431005358696, 0.04442169889807701, -0.012000000104308128 ], [ 0.07211410254240036, 0.042066700756549835, -0.05045200139284134 ], [ -0.08407589793205261, 0.014567299745976925, -0.050429001450538635 ], [ -0.08077500015497208, 0.01412030030041933, -0.011134999804198742 ], [ -0.07721489667892456, 0.018643299117684364, 0.024460000917315483 ], [ -0.06018190085887909, 0.022716199979186058, 0.05554400011897087 ], [ -0.03406190127134323, 0.02601110003888607, 0.07998699694871902 ], [ 0.0003761000116355717, 0.027389999479055405, 0.08866800367832184 ], [ 0.03478410094976425, 0.026437899097800255, 0.0788080021739006 ], [ 0.062293101102113724, 0.023722799494862556, 0.055629998445510864 ], [ 0.07953409850597382, 0.019935699179768562, 0.024437999352812767 ], [ 0.08181510120630264, 0.015416700392961502, -0.01133000012487173 ], [ 0.08411309868097305, 0.014364699833095074, -0.05053799971938133 ], [ -0.08589410036802292, -0.015828700736165047, -0.04828299954533577 ], [ -0.08416110277175903, -0.01601869985461235, -0.009345999918878078 ], [ -0.08028010278940201, -0.013759699650108814, 0.029160000383853912 ], [ -0.06535810232162476, -0.011631700210273266, 0.06435800343751907 ], [ -0.0361579991877079, -0.009983900003135204, 0.08975200355052948 ], [ 0.0004008999967481941, -0.009166999720036983, 0.10024400055408478 ], [ 0.037671998143196106, -0.009624100290238857, 0.08841200172901154 ], [ 0.06711789965629578, -0.010900299996137619, 0.0635799989104271 ], [ 0.0834558978676796, -0.012776300311088562, 0.029208000749349594 ], [ 0.08507990092039108, -0.015020299702882767, -0.00949000008404255 ], [ 0.0855598971247673, -0.0163612999022007, -0.048271000385284424 ], [ -0.08561919629573822, -0.04651470109820366, -0.04570699855685234 ], [ -0.08483020216226578, -0.04602169990539551, -0.007056000176817179 ], [ -0.07959219813346863, -0.0465506985783577, 0.030949000269174576 ], [ -0.06355620175600052, -0.04700880125164986, 0.06562399864196777 ], [ -0.035513099282979965, -0.04729190096259117, 0.09131500124931335 ], [ 0.00038579999818466604, -0.047318000346422195, 0.09943199902772903 ], [ 0.03838380053639412, -0.04707309976220131, 0.09069500118494034 ], [ 0.06661179661750793, -0.04663719981908798, 0.06558000296354294 ], [ 0.08332180231809616, -0.046101298183202744, 0.031206000596284866 ], [ 0.08554880321025848, -0.0455452986061573, -0.0071299998089671135 ], [ 0.08616179972887039, -0.047035299241542816, -0.045869000256061554 ], [ -0.07300929725170135, -0.07376570254564285, -0.04099800065159798 ], [ -0.07243429869413376, -0.07345270365476608, -0.0024870000779628754 ], [ -0.06727229803800583, -0.07629069685935974, 0.02838199958205223 ], [ -0.053007300943136215, -0.07878780364990234, 0.05593999847769737 ], [ -0.028620300814509392, -0.08052489906549454, 0.07543600350618362 ], [ 0.00032469999860040843, -0.0811149999499321, 0.08261500298976898 ], [ 0.0319196991622448, -0.08048710227012634, 0.07671599835157394 ], [ 0.05566670000553131, -0.07856020331382751, 0.056561000645160675 ], [ 0.06788770109415054, -0.07590430229902267, 0.028091000393033028 ], [ 0.073055699467659, -0.0730682983994484, -0.0025400000158697367 ], [ 0.07389470189809799, -0.07439029961824417, -0.04121999815106392 ], [ -0.05491039901971817, -0.09804479777812958, -0.035464998334646225 ], [ -0.05484040081501007, -0.0975278988480568, 0.0027920000720769167 ], [ -0.04842440038919449, -0.09934079647064209, 0.021599000319838524 ], [ -0.03651139885187149, -0.10085289925336838, 0.03716700151562691 ], [ -0.018972400575876236, -0.10176800191402435, 0.046535998582839966 ], [ 0.00021559999731834978, -0.10217799991369247, 0.05060800164937973 ], [ 0.01987759955227375, -0.10179299861192703, 0.04639299958944321 ], [ 0.03678160160779953, -0.10084909945726395, 0.036396998912096024 ], [ 0.04981959983706474, -0.09944610297679901, 0.021726999431848526 ], [ 0.05566659942269325, -0.09762509912252426, 0.002730000065639615 ], [ 0.054987598210573196, -0.09809110313653946, -0.035541001707315445 ], [ -0.02941340021789074, -0.11244899779558182, 0.008839000016450882 ], [ 0.00010759999713627622, -0.11489199846982956, 0.014657000079751015 ], [ 0.029842600226402283, -0.11215600371360779, 0.008799999952316284 ], [ -0.029818400740623474, -0.1145699992775917, -0.02921600081026554 ], [ 4.500000159168849e-06, -0.11856500059366226, -0.02307800017297268 ], [ 0.029741600155830383, -0.1142600029706955, -0.029255999252200127 ], [ -0.08416110277175903, -0.01601869985461235, -0.009345999918878078 ], [ -0.07243429869413376, -0.07345270365476608, -0.0024870000779628754 ], [ 0.08507990092039108, -0.015020299702882767, -0.00949000008404255 ], [ 0.073055699467659, -0.0730682983994484, -0.0025400000158697367 ], [ -0.08607610315084457, -0.044989701360464096, -0.06798599660396576 ], [ 0.08579389750957489, -0.045009300112724304, -0.06803099811077118 ], [ -0.08607610315084457, -0.024989699944853783, -0.06798599660396576 ], [ 0.08579389750957489, -0.02500930055975914, -0.06803099811077118 ] ], "base_channels": 64, "bottleneck_channels": 128, "d_state": 16, "d_conv": 2, "expand": 8, "head_dim": 16, "cdse_hidden_dim": 32, "channel_strategy": "native", "channel_strategy_kwargs": null, "braindecode_version": "2.1.0dev0"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101"""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 thatconfig, with the 94 target electrodes as the default ``chs_info`` and the windowof the pretraining; renames the keys with the prefix pairs below, drops thereconstruction 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 notpretrained, so ``from_pretrained`` builds it for the caller's ``n_outputs``."""
import jsonimport osimport sys
import numpy as npfrom 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 releaseN_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 SAMBADECODER = ( "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"})123version https://git-lfs.github.com/spec/v1oid sha256:abc0c1c19b4f139c4cc0a978223b7b328a645327a138d2c67a0b2fe021b577b9size 1586652README.mdAdded+257
config.jsonAdded+2183
convert.pyAdded+101
model.safetensorsAdded+3