-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmelgan_convert.py
89 lines (77 loc) · 2.43 KB
/
melgan_convert.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import argparse
import glob
import logging
import os
import textwrap
import torch
# From melgan:
from model.generator import Generator
from torch.utils.mobile_optimizer import optimize_for_mobile
from utils.hparams import HParam, load_hparam_str
def main(args: argparse.Namespace):
logging.basicConfig(level=args.log_level)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
checkpoint = torch.load(
args.checkpoint_path,
map_location=device,
)
hp = load_hparam_str(checkpoint["hp_str"])
model = Generator(hp.audio.n_mel_channels).to(device)
model.load_state_dict(checkpoint["model_g"])
model.eval()
model.requires_grad = False
model.remove_weight_norm()
melpaths = list(glob.glob(os.path.join(args.input_folder, "*.mel")))
mel = torch.load(melpaths[0])
if len(mel.shape) == 2:
mel = mel.unsqueeze(0)
mel = mel.cpu()
scripted_model = torch.jit.script(model)
if args.for_mobile:
optimized_model = optimize_for_mobile(
scripted_model, preserved_methods=["inference"]
)
optimized_model._save_for_lite_interpreter(args.output_path)
else:
frozen_model = torch.jit.freeze(scripted_model)
# optimized_model = torch.jit.optimize_for_inference(frozen_model)
torch.jit.save(scripted_model, args.output_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=textwrap.dedent(
"""\
Convert a [PyTorch MelGAN model](https://github.com/seungwonpark/melgan)
to TorchScript.
"""
)
)
parser.add_argument(
"-p",
"--checkpoint_path",
type=str,
required=True,
help="path to checkpoint pt file for evaluation",
)
parser.add_argument(
"-o",
"--output_path",
type=str,
required=True,
help="path to output TorchScript pt file",
)
parser.add_argument(
"-i",
"--input_folder",
type=str,
help="directory of mel-spectrograms to invert into raw audio. ",
)
parser.add_argument(
"--for_mobile",
action="store_true",
help="Should the output be optimized for mobile? And saved for the 'lite' interpreter?",
)
parser.add_argument(
"--log-level", choices=("DEBUG", "INFO", "WARNING", "ERROR"), default="INFO"
)
args = parser.parse_args()
main(args)