-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkgta.py
74 lines (62 loc) · 2.06 KB
/
mkgta.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
import os
import torch
import argparse
import numpy as np
import matplotlib.pylab as plt
from text import text_to_sequence
from model.model import Tacotron2
from hparams import hparams as hps
from utils.util import mode, to_var, to_arr
from utils.audio import load_wav, save_wav, melspectrogram
def files_to_list(fdir = 'data'):
f_list = []
with open(os.path.join(fdir, 'metadata.csv'), encoding = 'utf-8') as f:
for line in f:
parts = line.strip().split('|')
wav_path = os.path.join(fdir, 'wavs', '%s.wav' % parts[0])
f_list.append([wav_path, parts[1]])
return f_list
def load_model(ckpt_pth):
ckpt_dict = torch.load(ckpt_pth)
model = Tacotron2()
model.load_state_dict(ckpt_dict['model'])
model = mode(model, True).eval()
model.decoder.train()
model.postnet.train()
return model
def infer(wav_path, text, model):
sequence = text_to_sequence(text, hps.text_cleaners)
sequence = to_var(torch.IntTensor(sequence)[None, :]).long()
mel = melspectrogram(load_wav(wav_path))
mel_in = to_var(torch.Tensor([mel]))
r = mel_in.shape[2]%hps.n_frames_per_step
if r != 0:
mel_in = mel_in[:, :, :-r]
sequence = torch.cat([sequence, sequence], 0)
mel_in = torch.cat([mel_in, mel_in], 0)
_, mel_outputs_postnet, _, _ = model.teacher_infer(sequence, mel_in)
ret = mel
if r != 0:
ret[:, :-r] = to_arr(mel_outputs_postnet[0])
else:
ret = to_arr(mel_outputs_postnet[0])
return ret
def save_mel(res, pth, name):
out = os.path.join(pth, name)
np.save(out, res.T)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--ckpt_pth', type = str, default = '',
required = True, help = 'path to load checkpoints')
parser.add_argument('-n', '--npy_pth', type = str, default = 'dump',
help = 'path to save mels')
args = parser.parse_args()
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
model = load_model(args.ckpt_pth)
flist = files_to_list()
for x in flist:
ret = infer(x[0], x[1], model)
name = x[0].split('/')[-1].split('.wav')[0]
if args.npy_pth != '':
save_mel(ret, args.npy_pth, name)