Add BrainOmni weights converted from OpenTSLab/BrainOmni@9a4d3c70 (braindecode PR w41/brainomni-rehost @ d6d9804d)
Browse files- README.md +64 -0
- config.json +451 -0
- convert_brainomni_checkpoints.py +109 -0
- model.safetensors +3 -0
- pytorch_model.bin +3 -0
README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: braindecode
|
| 4 |
+
tags:
|
| 5 |
+
- braindecode
|
| 6 |
+
- pytorch
|
| 7 |
+
- safetensors
|
| 8 |
+
- eeg
|
| 9 |
+
- meg
|
| 10 |
+
- brainomni
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# brainomni-tiny-pretrained
|
| 14 |
+
|
| 15 |
+
Weights of BrainOmni tiny (lm_dim 256, 8 heads, 12 blocks) with its frozen tokenizer, for
|
| 16 |
+
[`braindecode.models.BrainOmni`](https://braindecode.org/stable/generated/braindecode.models.BrainOmni.html),
|
| 17 |
+
converted from the authors' release. The classification head is not pretrained (seeded random init); fine-tune or linear-probe before use.
|
| 18 |
+
|
| 19 |
+
```python
|
| 20 |
+
from braindecode.models import BrainOmni
|
| 21 |
+
model = BrainOmni.from_pretrained("braindecode/brainomni-tiny-pretrained", chs_info=raw.info["chs"], n_outputs=2)
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
`chs_info` must carry sensor positions (EEG) and coil orientations (MEG); the
|
| 25 |
+
`chs_info` in `config.json` (19 EEG channels, 10-20) is only a default. Input is
|
| 26 |
+
expected at 256 Hz, preprocessed as in the authors' code.
|
| 27 |
+
|
| 28 |
+
## Source and conversion
|
| 29 |
+
|
| 30 |
+
- Source: [OpenTSLab/BrainOmni](https://huggingface.co/OpenTSLab/BrainOmni) at
|
| 31 |
+
revision `9a4d3c70495370397ccfbfd6d2496f25647545a5`, file `tiny/BrainOmni.pt` (sha256 `62c67ba6a84ea0625e67a3b5e7463fe3930bfee88a612a225e9062a052542ffc`), MIT licence.
|
| 32 |
+
- `convert_brainomni_checkpoints.py` (in this repository) renames the keys to
|
| 33 |
+
braindecode's, drops the pretraining-only mask predictor, stores the RoPE
|
| 34 |
+
cache as `(cos, sin)` pairs with zero sine (the released cache holds cosines
|
| 35 |
+
only and the released code uses it as loaded) and writes `config.json`,
|
| 36 |
+
`model.safetensors` and `pytorch_model.bin` with `save_pretrained`.
|
| 37 |
+
- The converted model's outputs equal braindecode's loading of the original
|
| 38 |
+
file (max-abs difference 0.0, float32 and bfloat16).
|
| 39 |
+
- Requires a braindecode version newer than 1.8.1.
|
| 40 |
+
|
| 41 |
+
## Citation
|
| 42 |
+
|
| 43 |
+
```bibtex
|
| 44 |
+
@inproceedings{xiao2025brainomni,
|
| 45 |
+
title = {BrainOmni: A Brain Foundation Model for Unified EEG and MEG Signals},
|
| 46 |
+
author = {Xiao, Q. and Cui, Z. and Zhang, C. and Chen, S. and Wu, W. and
|
| 47 |
+
Thwaites, A. and Woolgar, A. and Zhou, B. and Zhang, C.},
|
| 48 |
+
booktitle = {Advances in Neural Information Processing Systems (NeurIPS)},
|
| 49 |
+
year = {2025},
|
| 50 |
+
note = {arXiv:2505.18185},
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
@article{aristimunha2025braindecode,
|
| 54 |
+
title = {Braindecode: a deep learning library for raw electrophysiological data},
|
| 55 |
+
author = {Aristimunha, Bruno and others},
|
| 56 |
+
journal = {Zenodo},
|
| 57 |
+
year = {2025},
|
| 58 |
+
doi = {10.5281/zenodo.17699192},
|
| 59 |
+
}
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## License
|
| 63 |
+
|
| 64 |
+
MIT, as the original BrainOmni release.
|
config.json
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ind": 2,
|
| 8 |
+
"coil_type": 1,
|
| 9 |
+
"unit": 107,
|
| 10 |
+
"cal": 1.0,
|
| 11 |
+
"range": 1.0,
|
| 12 |
+
"loc": [
|
| 13 |
+
-0.030902595782959074,
|
| 14 |
+
0.1145851808674079,
|
| 15 |
+
0.027866574329093563,
|
| 16 |
+
0.0,
|
| 17 |
+
0.0,
|
| 18 |
+
0.0,
|
| 19 |
+
NaN,
|
| 20 |
+
NaN,
|
| 21 |
+
NaN,
|
| 22 |
+
NaN,
|
| 23 |
+
NaN,
|
| 24 |
+
NaN
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"ch_name": "Fp2",
|
| 29 |
+
"kind": 2,
|
| 30 |
+
"coil_type": 1,
|
| 31 |
+
"unit": 107,
|
| 32 |
+
"cal": 1.0,
|
| 33 |
+
"range": 1.0,
|
| 34 |
+
"loc": [
|
| 35 |
+
0.028409487887091203,
|
| 36 |
+
0.11534630809630636,
|
| 37 |
+
0.02772125772253031,
|
| 38 |
+
0.0,
|
| 39 |
+
0.0,
|
| 40 |
+
0.0,
|
| 41 |
+
NaN,
|
| 42 |
+
NaN,
|
| 43 |
+
NaN,
|
| 44 |
+
NaN,
|
| 45 |
+
NaN,
|
| 46 |
+
NaN
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"ch_name": "F7",
|
| 51 |
+
"kind": 2,
|
| 52 |
+
"coil_type": 1,
|
| 53 |
+
"unit": 107,
|
| 54 |
+
"cal": 1.0,
|
| 55 |
+
"range": 1.0,
|
| 56 |
+
"loc": [
|
| 57 |
+
-0.07187663250970616,
|
| 58 |
+
0.07310353217275958,
|
| 59 |
+
0.02579045587891774,
|
| 60 |
+
0.0,
|
| 61 |
+
0.0,
|
| 62 |
+
0.0,
|
| 63 |
+
NaN,
|
| 64 |
+
NaN,
|
| 65 |
+
NaN,
|
| 66 |
+
NaN,
|
| 67 |
+
NaN,
|
| 68 |
+
NaN
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"ch_name": "F3",
|
| 73 |
+
"kind": 2,
|
| 74 |
+
"coil_type": 1,
|
| 75 |
+
"unit": 107,
|
| 76 |
+
"cal": 1.0,
|
| 77 |
+
"range": 1.0,
|
| 78 |
+
"loc": [
|
| 79 |
+
-0.05180904586237076,
|
| 80 |
+
0.0866879023191724,
|
| 81 |
+
0.07871409302883829,
|
| 82 |
+
0.0,
|
| 83 |
+
0.0,
|
| 84 |
+
0.0,
|
| 85 |
+
NaN,
|
| 86 |
+
NaN,
|
| 87 |
+
NaN,
|
| 88 |
+
NaN,
|
| 89 |
+
NaN,
|
| 90 |
+
NaN
|
| 91 |
+
]
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"ch_name": "Fz",
|
| 95 |
+
"kind": 2,
|
| 96 |
+
"coil_type": 1,
|
| 97 |
+
"unit": 107,
|
| 98 |
+
"cal": 1.0,
|
| 99 |
+
"range": 1.0,
|
| 100 |
+
"loc": [
|
| 101 |
+
-0.0012292786120998045,
|
| 102 |
+
0.09327445212894306,
|
| 103 |
+
0.10263929347682083,
|
| 104 |
+
0.0,
|
| 105 |
+
0.0,
|
| 106 |
+
0.0,
|
| 107 |
+
NaN,
|
| 108 |
+
NaN,
|
| 109 |
+
NaN,
|
| 110 |
+
NaN,
|
| 111 |
+
NaN,
|
| 112 |
+
NaN
|
| 113 |
+
]
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
"ch_name": "F4",
|
| 117 |
+
"kind": 2,
|
| 118 |
+
"coil_type": 1,
|
| 119 |
+
"unit": 107,
|
| 120 |
+
"cal": 1.0,
|
| 121 |
+
"range": 1.0,
|
| 122 |
+
"loc": [
|
| 123 |
+
0.05027427028109521,
|
| 124 |
+
0.087438385860065,
|
| 125 |
+
0.07727065434234637,
|
| 126 |
+
0.0,
|
| 127 |
+
0.0,
|
| 128 |
+
0.0,
|
| 129 |
+
NaN,
|
| 130 |
+
NaN,
|
| 131 |
+
NaN,
|
| 132 |
+
NaN,
|
| 133 |
+
NaN,
|
| 134 |
+
NaN
|
| 135 |
+
]
|
| 136 |
+
},
|
| 137 |
+
{
|
| 138 |
+
"ch_name": "F8",
|
| 139 |
+
"kind": 2,
|
| 140 |
+
"coil_type": 1,
|
| 141 |
+
"unit": 107,
|
| 142 |
+
"cal": 1.0,
|
| 143 |
+
"range": 1.0,
|
| 144 |
+
"loc": [
|
| 145 |
+
0.07143526180097565,
|
| 146 |
+
0.07450512184827826,
|
| 147 |
+
0.025101027393181548,
|
| 148 |
+
0.0,
|
| 149 |
+
0.0,
|
| 150 |
+
0.0,
|
| 151 |
+
NaN,
|
| 152 |
+
NaN,
|
| 153 |
+
NaN,
|
| 154 |
+
NaN,
|
| 155 |
+
NaN,
|
| 156 |
+
NaN
|
| 157 |
+
]
|
| 158 |
+
},
|
| 159 |
+
{
|
| 160 |
+
"ch_name": "T7",
|
| 161 |
+
"kind": 2,
|
| 162 |
+
"coil_type": 1,
|
| 163 |
+
"unit": 107,
|
| 164 |
+
"cal": 1.0,
|
| 165 |
+
"range": 1.0,
|
| 166 |
+
"loc": [
|
| 167 |
+
-0.08598208540859081,
|
| 168 |
+
0.014871643830998479,
|
| 169 |
+
0.03117337373904913,
|
| 170 |
+
0.0,
|
| 171 |
+
0.0,
|
| 172 |
+
0.0,
|
| 173 |
+
NaN,
|
| 174 |
+
NaN,
|
| 175 |
+
NaN,
|
| 176 |
+
NaN,
|
| 177 |
+
NaN,
|
| 178 |
+
NaN
|
| 179 |
+
]
|
| 180 |
+
},
|
| 181 |
+
{
|
| 182 |
+
"ch_name": "C3",
|
| 183 |
+
"kind": 2,
|
| 184 |
+
"coil_type": 1,
|
| 185 |
+
"unit": 107,
|
| 186 |
+
"cal": 1.0,
|
| 187 |
+
"range": 1.0,
|
| 188 |
+
"loc": [
|
| 189 |
+
-0.06714873038365092,
|
| 190 |
+
0.023358234962598565,
|
| 191 |
+
0.10451068106025274,
|
| 192 |
+
0.0,
|
| 193 |
+
0.0,
|
| 194 |
+
0.0,
|
| 195 |
+
NaN,
|
| 196 |
+
NaN,
|
| 197 |
+
NaN,
|
| 198 |
+
NaN,
|
| 199 |
+
NaN,
|
| 200 |
+
NaN
|
| 201 |
+
]
|
| 202 |
+
},
|
| 203 |
+
{
|
| 204 |
+
"ch_name": "Cz",
|
| 205 |
+
"kind": 2,
|
| 206 |
+
"coil_type": 1,
|
| 207 |
+
"unit": 107,
|
| 208 |
+
"cal": 1.0,
|
| 209 |
+
"range": 1.0,
|
| 210 |
+
"loc": [
|
| 211 |
+
-0.0013741408533525957,
|
| 212 |
+
0.027617093717945417,
|
| 213 |
+
0.1401994941744,
|
| 214 |
+
0.0,
|
| 215 |
+
0.0,
|
| 216 |
+
0.0,
|
| 217 |
+
NaN,
|
| 218 |
+
NaN,
|
| 219 |
+
NaN,
|
| 220 |
+
NaN,
|
| 221 |
+
NaN,
|
| 222 |
+
NaN
|
| 223 |
+
]
|
| 224 |
+
},
|
| 225 |
+
{
|
| 226 |
+
"ch_name": "C4",
|
| 227 |
+
"kind": 2,
|
| 228 |
+
"coil_type": 1,
|
| 229 |
+
"unit": 107,
|
| 230 |
+
"cal": 1.0,
|
| 231 |
+
"range": 1.0,
|
| 232 |
+
"loc": [
|
| 233 |
+
0.06532887326785189,
|
| 234 |
+
0.02357310071942504,
|
| 235 |
+
0.10369243420048097,
|
| 236 |
+
0.0,
|
| 237 |
+
0.0,
|
| 238 |
+
0.0,
|
| 239 |
+
NaN,
|
| 240 |
+
NaN,
|
| 241 |
+
NaN,
|
| 242 |
+
NaN,
|
| 243 |
+
NaN,
|
| 244 |
+
NaN
|
| 245 |
+
]
|
| 246 |
+
},
|
| 247 |
+
{
|
| 248 |
+
"ch_name": "T8",
|
| 249 |
+
"kind": 2,
|
| 250 |
+
"coil_type": 1,
|
| 251 |
+
"unit": 107,
|
| 252 |
+
"cal": 1.0,
|
| 253 |
+
"range": 1.0,
|
| 254 |
+
"loc": [
|
| 255 |
+
0.08326136240706139,
|
| 256 |
+
0.015258183861365685,
|
| 257 |
+
0.030972968524064372,
|
| 258 |
+
0.0,
|
| 259 |
+
0.0,
|
| 260 |
+
0.0,
|
| 261 |
+
NaN,
|
| 262 |
+
NaN,
|
| 263 |
+
NaN,
|
| 264 |
+
NaN,
|
| 265 |
+
NaN,
|
| 266 |
+
NaN
|
| 267 |
+
]
|
| 268 |
+
},
|
| 269 |
+
{
|
| 270 |
+
"ch_name": "P7",
|
| 271 |
+
"kind": 2,
|
| 272 |
+
"coil_type": 1,
|
| 273 |
+
"unit": 107,
|
| 274 |
+
"cal": 1.0,
|
| 275 |
+
"range": 1.0,
|
| 276 |
+
"loc": [
|
| 277 |
+
-0.07445797197401204,
|
| 278 |
+
-0.0421231600703902,
|
| 279 |
+
0.041273631387385656,
|
| 280 |
+
0.0,
|
| 281 |
+
0.0,
|
| 282 |
+
0.0,
|
| 283 |
+
NaN,
|
| 284 |
+
NaN,
|
| 285 |
+
NaN,
|
| 286 |
+
NaN,
|
| 287 |
+
NaN,
|
| 288 |
+
NaN
|
| 289 |
+
]
|
| 290 |
+
},
|
| 291 |
+
{
|
| 292 |
+
"ch_name": "P3",
|
| 293 |
+
"kind": 2,
|
| 294 |
+
"coil_type": 1,
|
| 295 |
+
"unit": 107,
|
| 296 |
+
"cal": 1.0,
|
| 297 |
+
"range": 1.0,
|
| 298 |
+
"loc": [
|
| 299 |
+
-0.05503823925315043,
|
| 300 |
+
-0.04421030104524479,
|
| 301 |
+
0.09990897692201317,
|
| 302 |
+
0.0,
|
| 303 |
+
0.0,
|
| 304 |
+
0.0,
|
| 305 |
+
NaN,
|
| 306 |
+
NaN,
|
| 307 |
+
NaN,
|
| 308 |
+
NaN,
|
| 309 |
+
NaN,
|
| 310 |
+
NaN
|
| 311 |
+
]
|
| 312 |
+
},
|
| 313 |
+
{
|
| 314 |
+
"ch_name": "Pz",
|
| 315 |
+
"kind": 2,
|
| 316 |
+
"coil_type": 1,
|
| 317 |
+
"unit": 107,
|
| 318 |
+
"cal": 1.0,
|
| 319 |
+
"range": 1.0,
|
| 320 |
+
"loc": [
|
| 321 |
+
-0.0017094524972804195,
|
| 322 |
+
-0.04521299319629392,
|
| 323 |
+
0.12667292418210063,
|
| 324 |
+
0.0,
|
| 325 |
+
0.0,
|
| 326 |
+
0.0,
|
| 327 |
+
NaN,
|
| 328 |
+
NaN,
|
| 329 |
+
NaN,
|
| 330 |
+
NaN,
|
| 331 |
+
NaN,
|
| 332 |
+
NaN
|
| 333 |
+
]
|
| 334 |
+
},
|
| 335 |
+
{
|
| 336 |
+
"ch_name": "P4",
|
| 337 |
+
"kind": 2,
|
| 338 |
+
"coil_type": 1,
|
| 339 |
+
"unit": 107,
|
| 340 |
+
"cal": 1.0,
|
| 341 |
+
"range": 1.0,
|
| 342 |
+
"loc": [
|
| 343 |
+
0.053636008012841616,
|
| 344 |
+
-0.044334525348021875,
|
| 345 |
+
0.10051602773215543,
|
| 346 |
+
0.0,
|
| 347 |
+
0.0,
|
| 348 |
+
0.0,
|
| 349 |
+
NaN,
|
| 350 |
+
NaN,
|
| 351 |
+
NaN,
|
| 352 |
+
NaN,
|
| 353 |
+
NaN,
|
| 354 |
+
NaN
|
| 355 |
+
]
|
| 356 |
+
},
|
| 357 |
+
{
|
| 358 |
+
"ch_name": "P8",
|
| 359 |
+
"kind": 2,
|
| 360 |
+
"coil_type": 1,
|
| 361 |
+
"unit": 107,
|
| 362 |
+
"cal": 1.0,
|
| 363 |
+
"range": 1.0,
|
| 364 |
+
"loc": [
|
| 365 |
+
0.07103246343848865,
|
| 366 |
+
-0.04225998064272267,
|
| 367 |
+
0.04119886278195534,
|
| 368 |
+
0.0,
|
| 369 |
+
0.0,
|
| 370 |
+
0.0,
|
| 371 |
+
NaN,
|
| 372 |
+
NaN,
|
| 373 |
+
NaN,
|
| 374 |
+
NaN,
|
| 375 |
+
NaN,
|
| 376 |
+
NaN
|
| 377 |
+
]
|
| 378 |
+
},
|
| 379 |
+
{
|
| 380 |
+
"ch_name": "O1",
|
| 381 |
+
"kind": 2,
|
| 382 |
+
"coil_type": 1,
|
| 383 |
+
"unit": 107,
|
| 384 |
+
"cal": 1.0,
|
| 385 |
+
"range": 1.0,
|
| 386 |
+
"loc": [
|
| 387 |
+
-0.0315735653251896,
|
| 388 |
+
-0.08056835005318111,
|
| 389 |
+
0.054789645624052044,
|
| 390 |
+
0.0,
|
| 391 |
+
0.0,
|
| 392 |
+
0.0,
|
| 393 |
+
NaN,
|
| 394 |
+
NaN,
|
| 395 |
+
NaN,
|
| 396 |
+
NaN,
|
| 397 |
+
NaN,
|
| 398 |
+
NaN
|
| 399 |
+
]
|
| 400 |
+
},
|
| 401 |
+
{
|
| 402 |
+
"ch_name": "O2",
|
| 403 |
+
"kind": 2,
|
| 404 |
+
"coil_type": 1,
|
| 405 |
+
"unit": 107,
|
| 406 |
+
"cal": 1.0,
|
| 407 |
+
"range": 1.0,
|
| 408 |
+
"loc": [
|
| 409 |
+
0.027683093109090593,
|
| 410 |
+
-0.08048884161275736,
|
| 411 |
+
0.05473408147779854,
|
| 412 |
+
0.0,
|
| 413 |
+
0.0,
|
| 414 |
+
0.0,
|
| 415 |
+
NaN,
|
| 416 |
+
NaN,
|
| 417 |
+
NaN,
|
| 418 |
+
NaN,
|
| 419 |
+
NaN,
|
| 420 |
+
NaN
|
| 421 |
+
]
|
| 422 |
+
}
|
| 423 |
+
],
|
| 424 |
+
"n_times": 512,
|
| 425 |
+
"input_window_seconds": null,
|
| 426 |
+
"sfreq": 256.0,
|
| 427 |
+
"emb_dim": 256,
|
| 428 |
+
"n_neuro": 16,
|
| 429 |
+
"window_length": 512,
|
| 430 |
+
"overlap_ratio": 0.25,
|
| 431 |
+
"n_filters": 32,
|
| 432 |
+
"ratios": [
|
| 433 |
+
8,
|
| 434 |
+
4,
|
| 435 |
+
2
|
| 436 |
+
],
|
| 437 |
+
"kernel_size": 5,
|
| 438 |
+
"last_kernel_size": 5,
|
| 439 |
+
"tokenizer_num_heads": 4,
|
| 440 |
+
"codebook_dim": 256,
|
| 441 |
+
"codebook_size": 512,
|
| 442 |
+
"num_quantizers": 4,
|
| 443 |
+
"rotation_trick": true,
|
| 444 |
+
"tokenizer_drop_prob": 0.0,
|
| 445 |
+
"lm_dim": 256,
|
| 446 |
+
"num_heads": 8,
|
| 447 |
+
"depth": 12,
|
| 448 |
+
"drop_prob": 0.1,
|
| 449 |
+
"activation": "torch.nn.modules.activation.SELU",
|
| 450 |
+
"braindecode_version": "1.8.1"
|
| 451 |
+
}
|
convert_brainomni_checkpoints.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Convert the released BrainOmni checkpoints to braindecode-native files.
|
| 2 |
+
|
| 3 |
+
Source: https://huggingface.co/OpenTSLab/BrainOmni at revision
|
| 4 |
+
9a4d3c70495370397ccfbfd6d2496f25647545a5 (MIT), files
|
| 5 |
+
``braintokenizer/BrainTokenizer.pt``, ``tiny/BrainOmni.pt``, ``base/BrainOmni.pt``
|
| 6 |
+
and their ``model_cfg.json``.
|
| 7 |
+
|
| 8 |
+
Usage::
|
| 9 |
+
|
| 10 |
+
python convert_brainomni_checkpoints.py OUT_DIR [SOURCE_DIR]
|
| 11 |
+
|
| 12 |
+
writes ``OUT_DIR/{braintokenizer,brainomni-tiny,brainomni-base}-pretrained``
|
| 13 |
+
with ``config.json``, ``model.safetensors`` and ``pytorch_model.bin``
|
| 14 |
+
(``save_pretrained``). Without ``SOURCE_DIR`` the files are downloaded.
|
| 15 |
+
|
| 16 |
+
Key changes: ``quantizer.rvq.`` -> ``quantizer.``, ``decoder.`` ->
|
| 17 |
+
``final_layer.``, the doubled ``conv``/``convtr`` wrappers and SEANet's
|
| 18 |
+
``.model`` are flattened, ``weight_g``/``weight_v`` -> weight-norm
|
| 19 |
+
parametrizations, the feed-forward linears are renumbered as
|
| 20 |
+
``FeedForwardBlock``. The pretraining-only ``mask_token`` and ``predict_head``
|
| 21 |
+
are dropped. The RoPE cache ``rotate`` is stored by the release as cosines only
|
| 22 |
+
(the released code copies it into a complex cache, so the sine is zero); it is
|
| 23 |
+
kept as ``(cos, 0)`` pairs. The BrainOmni classification head is not
|
| 24 |
+
pretrained: it is a seeded random ``nn.Linear`` default init.
|
| 25 |
+
The stored ``chs_info`` (19 EEG channels, 10-20) is only a default; pass
|
| 26 |
+
your own ``chs_info`` to ``from_pretrained``.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
import json
|
| 30 |
+
import sys
|
| 31 |
+
from pathlib import Path
|
| 32 |
+
|
| 33 |
+
import mne
|
| 34 |
+
import torch
|
| 35 |
+
|
| 36 |
+
from braindecode.models import BrainOmni, BrainTokenizer
|
| 37 |
+
|
| 38 |
+
REPO, REVISION = "OpenTSLab/BrainOmni", "9a4d3c70495370397ccfbfd6d2496f25647545a5"
|
| 39 |
+
RENAMES = {
|
| 40 |
+
"n_dim": "emb_dim",
|
| 41 |
+
"n_head": "tokenizer_num_heads",
|
| 42 |
+
"lm_head": "num_heads",
|
| 43 |
+
"lm_depth": "depth",
|
| 44 |
+
"lm_dropout": "drop_prob",
|
| 45 |
+
}
|
| 46 |
+
UNUSED = {"mask_ratio", "num_quantizers_used", "quantize_optimize_method"}
|
| 47 |
+
CH_NAMES = (
|
| 48 |
+
"Fp1 Fp2 F7 F3 Fz F4 F8 T7 C3 Cz C4 T8 P7 P3 Pz P4 P8 O1 O2".split()
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def fetch(source, path):
|
| 53 |
+
if source is not None:
|
| 54 |
+
return Path(source) / path
|
| 55 |
+
from huggingface_hub import hf_hub_download
|
| 56 |
+
|
| 57 |
+
return Path(hf_hub_download(REPO, path, revision=REVISION))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def rename(key):
|
| 61 |
+
key = key.replace("quantizer.rvq.", "quantizer.")
|
| 62 |
+
if key.startswith("decoder."):
|
| 63 |
+
key = "final_layer." + key.removeprefix("decoder.")
|
| 64 |
+
key = key.replace("tokenizer.decoder.", "tokenizer.final_layer.")
|
| 65 |
+
key = key.replace(".convtr.convtr.", ".convtr.").replace(".conv.conv.", ".conv.")
|
| 66 |
+
key = key.replace("seanet_encoder.model.", "seanet_encoder.")
|
| 67 |
+
key = key.replace("seanet_decoder.model.", "seanet_decoder.")
|
| 68 |
+
key = key.replace(".weight_g", ".parametrizations.weight.original0")
|
| 69 |
+
key = key.replace(".weight_v", ".parametrizations.weight.original1")
|
| 70 |
+
for name in ("ff", "aggregate_mlp"):
|
| 71 |
+
key = key.replace(f"{name}.layer.0.", f"{name}.0.")
|
| 72 |
+
key = key.replace(f"{name}.layer.2.", f"{name}.3.")
|
| 73 |
+
return key
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def convert(source, folder, cls, out, **kwargs):
|
| 77 |
+
config = json.loads(fetch(source, f"{folder}/model_cfg.json").read_text())
|
| 78 |
+
config = {RENAMES.get(k, k): v for k, v in config.items() if k not in UNUSED}
|
| 79 |
+
dropout = config.pop("dropout")
|
| 80 |
+
config["drop_prob" if cls is BrainTokenizer else "tokenizer_drop_prob"] = dropout
|
| 81 |
+
info = mne.create_info(CH_NAMES, 256.0, "eeg")
|
| 82 |
+
info.set_montage("standard_1020")
|
| 83 |
+
torch.manual_seed(0) # the BrainOmni head is a seeded random init
|
| 84 |
+
model = cls(chs_info=info["chs"], n_times=512, sfreq=256.0, **config, **kwargs)
|
| 85 |
+
|
| 86 |
+
official = torch.load(
|
| 87 |
+
fetch(source, f"{folder}/{cls.__name__}.pt"), weights_only=True
|
| 88 |
+
)
|
| 89 |
+
state = {}
|
| 90 |
+
for key, value in official.items():
|
| 91 |
+
if key == "mask_token" or key.startswith("predict_head."):
|
| 92 |
+
continue
|
| 93 |
+
if key.endswith("rope_embedding_layer.rotate"):
|
| 94 |
+
value = torch.stack((value, torch.zeros_like(value)), dim=-1)
|
| 95 |
+
state[rename(key)] = value
|
| 96 |
+
for key, value in model.state_dict().items():
|
| 97 |
+
if key.startswith("final_layer.") and cls is BrainOmni:
|
| 98 |
+
state[key] = value
|
| 99 |
+
model.load_state_dict(state, strict=True)
|
| 100 |
+
model.save_pretrained(out)
|
| 101 |
+
return model
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
out = Path(sys.argv[1])
|
| 106 |
+
source = sys.argv[2] if len(sys.argv) > 2 else None
|
| 107 |
+
convert(source, "braintokenizer", BrainTokenizer, out / "braintokenizer-pretrained")
|
| 108 |
+
for size in ("tiny", "base"):
|
| 109 |
+
convert(source, size, BrainOmni, out / f"brainomni-{size}-pretrained", n_outputs=2)
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d542be2616afebc8af8e1d46203da01ec3b693800c29a7036292a4d47e541b8c
|
| 3 |
+
size 61708712
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5a76fbd178ba5a477f40e272727dfecb62ac88e0060f9474a0d7ab1ca3ab64a4
|
| 3 |
+
size 61805015
|