Download convert.py from braindecode/jungmae-pretrained: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/braindecode/jungmae-pretrained/resolve/main/convert.py
- Command line
-
hf download hf://braindecode/jungmae-pretrained/convert.py
-
curl -L -o convert.py https://huggingface.co/braindecode/jungmae-pretrained/resolve/main/convert.py
1.56 kB
| """Convert ``checkpoint/jung_mae.pt`` (GeoNIRS/GeoNIRS@81f8995) to braindecode's ``JungMAE``. | |
| usage: python convert.py <jung_mae.pt> <out_dir> | |
| Unwraps the ``{"model", "model_config", ...}`` payload (its configuration equals | |
| the class defaults), drops the pretraining decoder (input projection, mask token, | |
| layers, prediction layer) and writes ``config.json`` and ``model.safetensors`` | |
| with ``JungMAE.save_pretrained`` for the paper's 40 channels of 60 samples at | |
| 5 Hz. The release holds the encoder only: the head reads ``C * P * D`` features, | |
| so ``from_pretrained`` builds it for the caller's montage and ``n_outputs``. | |
| """ | |
| import os | |
| import sys | |
| import torch | |
| from safetensors.torch import save_file | |
| from braindecode.models import JungMAE | |
| DECODER = ("decoder_embed.", "mask_token", "decoder.", "prediction.") | |
| if __name__ == "__main__": | |
| src, out_dir = sys.argv[1:] | |
| payload = torch.load(src, map_location="cpu", weights_only=True) | |
| encoder = {k: v for k, v in payload["model"].items() if not k.startswith(DECODER)} | |
| torch.manual_seed(0) | |
| model = JungMAE(n_chans=40, n_outputs=2, n_times=60, sfreq=5.0) | |
| missing, unexpected = model.load_state_dict(encoder, strict=False) | |
| assert not unexpected, unexpected | |
| assert all(key.startswith("final_layer.") for key in missing), missing | |
| model.save_pretrained(out_dir) | |
| os.remove(os.path.join(out_dir, "pytorch_model.bin")) | |
| 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")) | |