Created
April 27, 2021 18:05
-
-
Save pplantinga/8cbb5db9986c7eac6195789d0ee5d8ba to your computer and use it in GitHub Desktop.
Convert the names of model parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import argparse | |
import speechbrain as sb | |
from collections import OrderedDict | |
from hyperpyyaml import load_hyperpyyaml | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--hparams", required=True) | |
parser.add_argument("--hparams_key", required=True) | |
parser.add_argument("--old_ckpt", required=True) | |
parser.add_argument("--new_ckpt", required=True) | |
args = parser.parse_args() | |
with open(args.hparams) as f: | |
hparams = load_hyperpyyaml(f) | |
ckpt = torch.load(args.old_ckpt) | |
assert len(hparams[args.hparams_key].state_dict()) == len(ckpt) | |
new_state_dict = OrderedDict() | |
for old_key, new_key in zip(ckpt, hparams[args.hparams_key].state_dict()): | |
new_state_dict[new_key] = ckpt[old_key] | |
torch.save(new_state_dict, args.new_ckpt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment