-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathexport_torchscript.py
66 lines (53 loc) · 2.23 KB
/
export_torchscript.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
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import torch
import argparse
from scipy.io.wavfile import write
from utils import AttrDict
import numpy as np
from generator import FreGAN
import json
MAX_WAV_VALUE = 32768.0
def load_checkpoint(filepath, device):
assert os.path.isfile(filepath)
print("Loading '{}'".format(filepath))
checkpoint_dict = torch.load(filepath, map_location=device)
print("Complete.")
return checkpoint_dict
def main(args):
if args.config is not None:
with open(args.config) as f:
data = f.read()
global h
json_config = json.loads(data)
h = AttrDict(json_config)
torch.manual_seed(h.seed)
model = FreGAN(h).cuda()
state_dict_g = load_checkpoint(args.checkpoint_path, 'cuda')
model.load_state_dict(state_dict_g['generator'])
model.eval()
model.remove_weight_norm()
with torch.no_grad():
mel = torch.from_numpy(np.load(args.input))
if len(mel.shape) == 2:
mel = mel.unsqueeze(0)
mel = mel.cuda()
#zero = torch.full((1, 80, 10), -11.5129).to(mel.device)
#mel = torch.cat((mel, zero), dim=2)
hifigan_trace = torch.jit.trace(model, mel)
print(state_dict_g.keys())
hifigan_trace.save("{}/hifigan_{}.pt".format(args.out, args.name))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default=None, required=True,
help="yaml file for config. will use hp_str from checkpoint if not given.")
parser.add_argument('-p', '--checkpoint_path', type=str, required=True,
help="path of checkpoint pt file for evaluation")
parser.add_argument('-i', '--input', type=str, required=True,
help="directory of mel-spectrograms to invert into raw audio. ")
parser.add_argument('-o', '--out', type=str, required=True,
help="path of output pt file")
parser.add_argument('-n', '--name', type=str, required=True,
help="name of the output file")
args = parser.parse_args()
main(args)