File size: 2,349 Bytes
9f347a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""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"))