tinymyo-db5 / convert.py
bruAristimunha's picture
TinyMyo converted from PulpBio/TinyMyo at bc14906
9f347a1 verified
Raw History Blame Contribute Delete
2.35 kB
"""Convert the TinyMyo releases (PulpBio/TinyMyo, revision bc14906) to braindecode's ``TinyMyo``.
usage: python convert.py <file.safetensors> <out_dir> <n_chans> <n_outputs|encoder>
Strips the ``model.`` prefix, drops the pretraining-only mask token,
reconstruction decoder and DB8 regressor, reshapes the patch projection
``Conv2d(1, D, (1, patch_size))`` to ``Linear(patch_size, D)``, renames the
classifier to ``final_layer`` and writes ``config.json`` and
``model.safetensors`` with ``TinyMyo.save_pretrained`` for 1000-sample windows.
The pretrained encoder (``encoder``) ships without a head: the head's input
width depends on the number of channels, so ``from_pretrained`` builds it for
the caller's ``n_chans`` and ``n_outputs``.
"""
import os
import sys
import torch
from safetensors.torch import load_file, save_file
from braindecode.models import TinyMyo
RENAMES = [
("patch_embedding.patch_embed.", "patch_embed."),
("model_head.classifier.", "final_layer."),
]
DROPPED = ("mask_token", "model_head.decoder_pred.", "model_head.regressor.")
def convert_state_dict(state_dict):
out = {}
for key, value in state_dict.items():
key = key.removeprefix("model.")
if key.startswith(DROPPED):
continue
for old, new in RENAMES:
key = key.replace(old, new)
if key == "patch_embed.proj.weight":
value = value.flatten(1) # Conv2d(1, D, (1, patch_size)) -> Linear
out[key] = value
return out
if __name__ == "__main__":
src, out_dir, n_chans, n_outputs = sys.argv[1:]
encoder_only = n_outputs == "encoder"
torch.manual_seed(0)
model = TinyMyo(
n_chans=int(n_chans), n_outputs=2 if encoder_only else int(n_outputs), n_times=1000
)
missing, unexpected = model.load_state_dict(
convert_state_dict(load_file(src)), strict=False
)
assert not unexpected, unexpected
allowed = ("final_layer.",) if encoder_only else ()
assert all(key.startswith(allowed) for key in missing) if allowed else not missing, missing
model.save_pretrained(out_dir)
os.remove(os.path.join(out_dir, "pytorch_model.bin"))
if encoder_only:
encoder = {k: v for k, v in model.state_dict().items() if not k.startswith("final_layer.")}
save_file(encoder, os.path.join(out_dir, "model.safetensors"))