-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.py
executable file
·124 lines (95 loc) · 3.54 KB
/
interface.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import sys
sys.path.insert(0,'./tacotron2')
import numpy as np
import torch
import argparse
from tacotron2.train import train
from tacotron2.hparams import create_hparams
from tacotron2.train import load_model
from tacotron2.text import text_to_sequence
'''
class AbstractClass(ABC):
@abstractmethod
def train_mel(self):
pass
@abstractmethod
def train_wav(self):
pass
@abstractmethod
def inference_mel(self):
pass
@abstractmethod
def inference_audio(self):
pass
@abstractmethod
def play_audio(self):
pass
'''
def train_mel(outdir,logdir,checkpoint):
hparams = create_hparams()
torch.backends.cudnn.enabled = hparams.cudnn_enabled
torch.backends.cudnn.benchmark = hparams.cudnn_benchmark
print("FP16 Run:", hparams.fp16_run)
print("Dynamic Loss Scaling:", hparams.dynamic_loss_scaling)
print("Distributed Run:", hparams.distributed_run)
print("cuDNN Enabled:", hparams.cudnn_enabled)
print("cuDNN Benchmark:", hparams.cudnn_benchmark)
train(outdir, logdir, checkpoint,
True, 1, 0, False, hparams)
def load_mel_model(checkpoint_path):
hparams = create_hparams()
hparams.sampling_rate = 22050
model = load_model(hparams)
model.load_state_dict(torch.load(checkpoint_path)['state_dict'])
_ = model.cuda().eval().half()
# hparams = create_hparams("distributed_run=False,mask_padding=False")
# hparams.sampling_rate = 22050
# hparams.filter_length = 1024
# hparams.hop_length = 256
# hparams.win_length = 1024
#
# model = load_model(hparams)
# try:
# model = model.module
# except:
# pass
#
# model.load_state_dict({k.replace('module.', ''): v for k, v in torch.load(checkpoint_path)['state_dict'].items()})
# _ = model.eval()
return model
def inference_mel(text,model):
""""
Performs conversion from text to mel spectogram
"""
sequence = np.array(text_to_sequence(text, ['english_cleaners']))[None, :]
sequence = torch.autograd.Variable(torch.from_numpy(sequence)).cuda().long()
mel_outputs, mel_outputs_postnet, _, alignments = model.inference(sequence)
mel = torch.autograd.Variable(mel_outputs_postnet)
mel = mel.reshape(80, mel.shape[2])
mel = mel.data
filename = "text_to_mel"
torch.save(mel, filename)
file = open(str(filename) + ".txt", 'w')
file.write(filename)
file.close()
return mel, file.name
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output_directory', type=str,
help='directory to save checkpoints')
parser.add_argument('-l', '--log_directory', type=str,
help='directory to save tensorboard logs')
parser.add_argument('-c', '--checkpoint_path', type=str, default=None,
required=False, help='checkpoint path')
parser.add_argument('--hparams', type=str,
required=False, help='comma separated name=value pairs')
args = parser.parse_args()
hparams = create_hparams(args.hparams)
torch.backends.cudnn.enabled = hparams.cudnn_enabled
torch.backends.cudnn.benchmark = hparams.cudnn_benchmark
print("FP16 Run:", hparams.fp16_run)
print("Dynamic Loss Scaling:", hparams.dynamic_loss_scaling)
print("Distributed Run:", hparams.distributed_run)
print("cuDNN Enabled:", hparams.cudnn_enabled)
print("cuDNN Benchmark:", hparams.cudnn_benchmark)
train_mel(args.output_directory, args.log_directory, args.checkpoint_path)