-
Notifications
You must be signed in to change notification settings - Fork 167
/
vits_infer_onnx_stream.py
228 lines (187 loc) · 6.55 KB
/
vits_infer_onnx_stream.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
# Copyright 2023 Xiaomi Corp. (authors: Fangjun Kuang)
import onnxruntime
import soundfile
import torch
import os
import torch
import argparse
import datetime
import numpy
from text import cleaned_text_to_sequence
from vits_pinyin import VITS_PinYin
def display(sess):
for i in sess.get_inputs():
print(i)
print("-" * 10)
for o in sess.get_outputs():
print(o)
class OnnxModel_Encoder:
def __init__(
self,
model: str,
):
session_opts = onnxruntime.SessionOptions()
session_opts.inter_op_num_threads = 1
session_opts.intra_op_num_threads = 4
self.session_opts = session_opts
self.model = onnxruntime.InferenceSession(
model,
sess_options=self.session_opts,
)
display(self.model)
meta = self.model.get_modelmeta().custom_metadata_map
self.add_blank = int(meta["add_blank"])
self.sample_rate = int(meta["sample_rate"])
print(meta)
def __call__(self, x: torch.Tensor):
"""
Args:
x:
A int64 tensor of shape (L,)
"""
x = x.unsqueeze(0)
x_length = torch.tensor([x.shape[1]], dtype=torch.int64)
noise_scale = torch.tensor([1], dtype=torch.float32)
length_scale = torch.tensor([1], dtype=torch.float32)
z_p, y_mask = self.model.run(
[
self.model.get_outputs()[0].name,
self.model.get_outputs()[1].name,
],
{
self.model.get_inputs()[0].name: x.numpy(),
self.model.get_inputs()[1].name: x_length.numpy(),
self.model.get_inputs()[2].name: noise_scale.numpy(),
self.model.get_inputs()[3].name: length_scale.numpy(),
},
)
return z_p, y_mask
class OnnxModel_Decoder:
def __init__(
self,
model: str,
):
session_opts = onnxruntime.SessionOptions()
session_opts.inter_op_num_threads = 1
session_opts.intra_op_num_threads = 4
self.session_opts = session_opts
self.model = onnxruntime.InferenceSession(
model,
sess_options=self.session_opts,
)
display(self.model)
meta = self.model.get_modelmeta().custom_metadata_map
self.hop_length = int(meta["hop_length"])
print(meta)
def __call__(self, z_p, y_mask):
y = self.model.run(
[
self.model.get_outputs()[0].name,
],
{
self.model.get_inputs()[0].name: z_p,
self.model.get_inputs()[1].name: y_mask,
},
)[0]
return y
def main_debug():
parser = argparse.ArgumentParser(
description='Inference code for bert vits models')
parser.add_argument('--encoder', type=str, required=True)
parser.add_argument('--decoder', type=str, required=True)
args = parser.parse_args()
print("Onnx model path:", args.encoder)
print("Onnx model path:", args.decoder)
encoder = OnnxModel_Encoder(args.encoder)
decoder = OnnxModel_Decoder(args.decoder)
tts_front = VITS_PinYin(None, None, hasBert=False)
os.makedirs("./vits_infer_out/", exist_ok=True)
n = 0
fo = open("vits_infer_item.txt", "r+", encoding='utf-8')
while (True):
try:
item = fo.readline().strip()
except Exception as e:
print('nothing of except:', e)
break
if (item == None or item == ""):
break
n = n + 1
print(n)
print(datetime.datetime.now())
phonemes, _ = tts_front.chinese_to_phonemes(item)
input_ids = cleaned_text_to_sequence(phonemes)
x = torch.tensor(input_ids, dtype=torch.int64)
z_p, y_mask = encoder(x)
y = decoder(z_p, y_mask)
print(datetime.datetime.now())
soundfile.write(
f"./vits_infer_out/onnx_stream_{n}.wav", y, encoder.sample_rate)
fo.close()
def main():
parser = argparse.ArgumentParser(
description='Inference code for bert vits models')
parser.add_argument('--encoder', type=str, required=True)
parser.add_argument('--decoder', type=str, required=True)
args = parser.parse_args()
print("Onnx model path:", args.encoder)
print("Onnx model path:", args.decoder)
encoder = OnnxModel_Encoder(args.encoder)
decoder = OnnxModel_Decoder(args.decoder)
tts_front = VITS_PinYin(None, None, hasBert=False)
os.makedirs("./vits_infer_out/", exist_ok=True)
n = 0
fo = open("vits_infer_item.txt", "r+", encoding='utf-8')
while (True):
try:
item = fo.readline().strip()
except Exception as e:
print('nothing of except:', e)
break
if (item == None or item == ""):
break
n = n + 1
print(n)
print(datetime.datetime.now())
phonemes, _ = tts_front.chinese_to_phonemes(item)
input_ids = cleaned_text_to_sequence(phonemes)
x = torch.tensor(input_ids, dtype=torch.int64)
z_p, y_mask = encoder(x)
print(datetime.datetime.now())
len_z = z_p.shape[2]
print('frame size is: ', len_z)
print('hop_length is: ', decoder.hop_length)
# can not change these parameters
hop_length = decoder.hop_length
hop_frame = 12
hop_sample = hop_frame * hop_length
stream_chunk = 50
stream_index = 0
stream_out_wav = []
while (stream_index < len_z):
if (stream_index == 0): # start frame
cut_s = stream_index
cut_s_wav = 0
else:
cut_s = stream_index - hop_frame
cut_s_wav = hop_sample
if (stream_index + stream_chunk > len_z - hop_frame): # end frame
cut_e = stream_index + stream_chunk
cut_e_wav = -1
else:
cut_e = stream_index + stream_chunk + hop_frame
cut_e_wav = -1 * hop_sample
z_chunk = z_p[:, :, cut_s:cut_e]
m_chunk = y_mask[:, :, cut_s:cut_e]
o_chunk = decoder(z_chunk, m_chunk)
o_chunk = o_chunk[cut_s_wav:cut_e_wav]
stream_out_wav.extend(o_chunk)
stream_index = stream_index + stream_chunk
print(datetime.datetime.now())
stream_out_wav = numpy.asarray(stream_out_wav)
soundfile.write(
f"./vits_infer_out/onnx_stream_{n}.wav", stream_out_wav, encoder.sample_rate)
fo.close()
if __name__ == "__main__":
main()