bruAristimunha commited on
Commit
f51061d
·
verified ·
1 Parent(s): 9af838a

GeoNIRS-Large encoder converted from GeoNIRS/GeoNIRS@81f8995

Browse files
Files changed (4) hide show
  1. README.md +65 -0
  2. config.json +34 -0
  3. convert.py +57 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ library_name: braindecode
4
+ tags:
5
+ - braindecode
6
+ - pytorch
7
+ - safetensors
8
+ - fnirs
9
+ - geonirs
10
+ - foundation-model
11
+ ---
12
+
13
+ # GeoNIRS-Large (pretrained encoder)
14
+
15
+ Weights of GeoNIRS-Large, the geometry-aware fNIRS foundation model of Li et al. (2026), for
16
+ [`braindecode.models.GeoNIRS`](https://braindecode.org/stable/generated/braindecode.models.GeoNIRS.html).
17
+ They are the encoder of `checkpoint/geonirs_large.pt` of
18
+ [GeoNIRS/GeoNIRS](https://github.com/GeoNIRS/GeoNIRS/tree/81f8995e5c3aa6b70e55812b194fc04e4e7ea5b7/checkpoint)
19
+ at commit `81f8995`, pretrained on 22 datasets (827.04 h), converted by `convert.py`.
20
+
21
+ ```python
22
+ from braindecode.models import GeoNIRS
23
+
24
+ model = GeoNIRS.from_pretrained(
25
+ "braindecode/geonirs-large-pretrained",
26
+ chs_info=raw.info["chs"],
27
+ n_outputs=2,
28
+ n_times=60,
29
+ )
30
+ features = model(x, return_features=True)["features"]
31
+ ```
32
+
33
+ `config.json` holds the sizes of GeoNIRS-Large (`embed_dim=384`, `depth=6`, `num_heads=12`,
34
+ `feature_dim=128`, `projection_dims=(384, 192, 96, 128)`). The classification head
35
+ (`final_layer`) is not part of the release: it reads `2 * n_latent_channels * n_patches *
36
+ feature_dim` features, so `from_pretrained` builds it for your `n_times` and `n_outputs`.
37
+ Fine-tune or linear-probe before use.
38
+
39
+ ## Intended input
40
+
41
+ The inputs of the GeoNIRS paper (Appendix B.1): HbO and HbR in µmol/L resampled to 5 Hz, each
42
+ chromophore z-scored per recording over all its valid channels and time points, in windows of at
43
+ most 150 samples (30 s, the 15 rows of the temporal position embedding). `chs_info` gives each
44
+ HbO/HbR pair the positions of its source and detector (`loc[3:9]`, where MNE stores them); a NaN
45
+ marks a missing sample.
46
+
47
+ ## Changes from the original release
48
+
49
+ `convert.py` unwraps the `{"model", "config"}` payload, builds the model from its `config`, drops
50
+ the two pretraining decoders (`hbo_to_hbr_decoder`, `hbr_to_hbo_decoder`) and writes the encoder
51
+ with `save_pretrained`. No value is changed: the encoder gives the features of the original file
52
+ bit for bit on CPU in float32.
53
+
54
+ ## License and citation
55
+
56
+ The source repository has no licence file; the weights are by the GeoNIRS authors. Please cite:
57
+
58
+ ```bibtex
59
+ @article{li2026geonirs,
60
+ author = {Li, Zongsheng and Zhu, Shengyuan and Zhang, Zimo and Li, Hanyao and Wang, Shengming and Liu, Quanying},
61
+ title = {{GeoNIRS}: Geometry-Aware Pretraining for Heterogeneous {fNIRS} Representation Learning},
62
+ journal = {arXiv preprint arXiv:2610.17845},
63
+ year = {2026},
64
+ }
65
+ ```
config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "n_outputs": 2,
3
+ "n_chans": 2,
4
+ "chs_info": null,
5
+ "n_times": 150,
6
+ "input_window_seconds": null,
7
+ "sfreq": 5.0,
8
+ "patch_size": 10,
9
+ "max_patches": 15,
10
+ "on_non_divisible": "error",
11
+ "embed_dim": 384,
12
+ "num_heads": 12,
13
+ "depth": 6,
14
+ "mlp_ratio": 4.0,
15
+ "geometry_hidden_dim": 384,
16
+ "geometry_heads": 12,
17
+ "n_latent_channels": 16,
18
+ "compressor_heads": 12,
19
+ "feature_dim": 128,
20
+ "projection_dims": [
21
+ 384,
22
+ 192,
23
+ 96,
24
+ 128
25
+ ],
26
+ "use_channel_geometry": true,
27
+ "pool": "flatten",
28
+ "channel_order": null,
29
+ "drop_prob": 0.1,
30
+ "head_drop_prob": 0.1,
31
+ "minimum_patch_valid_fraction": 0.8,
32
+ "activation": "torch.nn.modules.activation.GELU",
33
+ "braindecode_version": "2.1.0dev0"
34
+ }
convert.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Convert ``checkpoint/geonirs_{base,large}.pt`` (GeoNIRS/GeoNIRS@81f8995) to braindecode's ``GeoNIRS``.
2
+
3
+ usage: python convert.py <geonirs_base.pt or geonirs_large.pt> <out_dir>
4
+
5
+ Unwraps the ``{"model", "config"}`` payload, builds the model from its
6
+ ``config`` for the 30 s pretraining windows (150 samples at 5 Hz), drops the
7
+ two pretraining decoders (``hbo_to_hbr_decoder``, ``hbr_to_hbo_decoder``) and
8
+ writes ``config.json`` and ``model.safetensors`` with
9
+ ``GeoNIRS.save_pretrained``. The release holds the encoder only: the head
10
+ reads ``2 * n_latent_channels * n_patches * feature_dim`` features, so
11
+ ``from_pretrained`` builds it for the caller's ``n_times`` and ``n_outputs``.
12
+ """
13
+
14
+ import os
15
+ import sys
16
+
17
+ import torch
18
+ from safetensors.torch import save_file
19
+
20
+ from braindecode.models import GeoNIRS
21
+
22
+ DECODERS = ("hbo_to_hbr_decoder.", "hbr_to_hbo_decoder.")
23
+
24
+ if __name__ == "__main__":
25
+ src, out_dir = sys.argv[1:]
26
+ payload = torch.load(src, map_location="cpu", weights_only=True)
27
+ data, config = payload["config"]["data"], payload["config"]["model"]
28
+ # braindecode's GeoNIRS has a 6-D geometry and one geometry layer
29
+ assert config["geometry_dim"] == 6 and config["geometry_spatial_layers"] == 1
30
+ encoder = {k: v for k, v in payload["model"].items() if not k.startswith(DECODERS)}
31
+ torch.manual_seed(0)
32
+ model = GeoNIRS(
33
+ n_chans=2,
34
+ n_outputs=2,
35
+ n_times=round(data["window_seconds"] * data["sampling_rate_hz"]),
36
+ sfreq=data["sampling_rate_hz"],
37
+ patch_size=data["patch_samples"],
38
+ minimum_patch_valid_fraction=data["minimum_patch_valid_fraction"],
39
+ embed_dim=config["embedding_dim"],
40
+ depth=config["temporal_layers"],
41
+ num_heads=config["num_heads"],
42
+ mlp_ratio=config["mlp_ratio"],
43
+ drop_prob=config["dropout"],
44
+ geometry_hidden_dim=config["geometry_hidden_dim"],
45
+ geometry_heads=config["geometry_heads"],
46
+ n_latent_channels=config["num_latent_sources"],
47
+ compressor_heads=config["spatial_heads"],
48
+ feature_dim=config["output_feature_dim"],
49
+ projection_dims=tuple(config["projection_dimensions"]),
50
+ )
51
+ missing, unexpected = model.load_state_dict(encoder, strict=False)
52
+ assert not unexpected, unexpected
53
+ assert all(key.startswith("final_layer.") for key in missing), missing
54
+ model.save_pretrained(out_dir)
55
+ os.remove(os.path.join(out_dir, "pytorch_model.bin"))
56
+ weights = {k: v for k, v in model.state_dict().items() if not k.startswith("final_layer.")}
57
+ save_file(weights, os.path.join(out_dir, "model.safetensors"), metadata={"format": "pt"})
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1ae128345816a6f72ac529ee8981260c63778e65d53f680240fffdc26ef45681
3
+ size 97607816