|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from kokoro import phonemize, tokenize, length_to_mask |
|
import torch.nn.functional as F |
|
from models_scripting import build_model |
|
import torch |
|
from typing import Dict |
|
|
|
device = "cpu" |
|
|
|
model = build_model('kokoro-v0_19.pth', device) |
|
|
|
voicepack = torch.load('voices/af.pt', weights_only=True).to(device) |
|
|
|
speed = 1. |
|
|
|
text = "How could I know? It's an unanswerable question. Like asking an unborn child if they'll lead a good life. They haven't even been born." |
|
|
|
ps = phonemize(text, "a") |
|
tokens = tokenize(ps) |
|
|
|
tokens = torch.LongTensor([[0, *tokens, 0]]).to(device) |
|
|
|
class StyleTTS2(torch.nn.Module): |
|
def __init__(self, model, voicepack): |
|
super().__init__() |
|
|
|
self.bert = model.bert |
|
self.bert_encoder = model.bert_encoder |
|
self.predictor = model.predictor |
|
self.decoder = model.decoder |
|
self.text_encoder = model.text_encoder |
|
self.voicepack = voicepack |
|
|
|
def forward(self, tokens : torch.Tensor): |
|
speed = 1. |
|
|
|
device = tokens.device |
|
input_lengths = torch.LongTensor([tokens.shape[-1]]).to(device) |
|
|
|
text_mask = length_to_mask(input_lengths).to(device) |
|
bert_dur = self.bert(tokens) |
|
|
|
d_en = self.bert_encoder(bert_dur).transpose(-1, -2) |
|
|
|
ref_s = self.voicepack[tokens.shape[1]] |
|
s = ref_s[:, 128:] |
|
|
|
d = self.predictor.text_encoder.inference(d_en, s) |
|
x, _ = self.predictor.lstm(d) |
|
|
|
duration = self.predictor.duration_proj(x) |
|
duration = torch.sigmoid(duration).sum(axis=-1) / speed |
|
pred_dur = torch.round(duration).clamp(min=1).long() |
|
|
|
c_start = F.pad(pred_dur,(1,0), "constant").cumsum(dim=1)[0,0:-1] |
|
c_end = c_start + pred_dur[0,:] |
|
|
|
|
|
indices = torch.arange(0, pred_dur.sum().item()).long().to(device) |
|
|
|
pred_aln_trg_list=[] |
|
for cs, ce in zip(c_start, c_end): |
|
row = torch.where((indices>=cs) & (indices<ce), 1., 0.) |
|
pred_aln_trg_list.append(row) |
|
pred_aln_trg=torch.vstack(pred_aln_trg_list) |
|
|
|
en = d.transpose(-1, -2) @ pred_aln_trg.unsqueeze(0).to(device) |
|
|
|
F0_pred, N_pred = self.predictor.F0Ntrain(en, s) |
|
t_en = self.text_encoder.inference(tokens) |
|
asr = t_en @ pred_aln_trg.unsqueeze(0).to(device) |
|
return (asr, F0_pred, N_pred, ref_s[:, :128]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
style_model = StyleTTS2(model=model, voicepack=voicepack) |
|
style_model.eval() |
|
|
|
|
|
(asr, F0_pred, N_pred, ref_s) = style_model(tokens) |
|
print(asr.shape, F0_pred.shape, N_pred.shape, ref_s.shape) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dynamic_shapes = {"tokens":{ 1:"token_len"}} |
|
print(f"{tokens.shape=}") |
|
torch.onnx.export(model=style_model, args=( tokens, ), dynamic_axes=dynamic_shapes, input_names=["tokens"], f="style_model.onnx", |
|
output_names=["asr", "F0_pred", "N_pred", "ref_s"], opset_version=13, verbose=False, dynamo=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|