Spaces:
Sleeping
Sleeping
Update ONNXVITS_infer.py
Browse files- ONNXVITS_infer.py +61 -0
ONNXVITS_infer.py
CHANGED
@@ -91,3 +91,64 @@ class SynthesizerTrn(models.SynthesizerTrn):
|
|
91 |
o = torch.from_numpy(o[0])
|
92 |
|
93 |
return o, attn, y_mask, (z, z_p, m_p, logs_p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
o = torch.from_numpy(o[0])
|
92 |
|
93 |
return o, attn, y_mask, (z, z_p, m_p, logs_p)
|
94 |
+
|
95 |
+
def predict_duration(self, x, x_lengths, sid=None, noise_scale=1, length_scale=1, noise_scale_w=1., max_len=None,
|
96 |
+
emotion_embedding=None):
|
97 |
+
from ONNXVITS_utils import runonnx
|
98 |
+
|
99 |
+
#x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths)
|
100 |
+
x, m_p, logs_p, x_mask = runonnx("ONNX_net/enc_p.onnx", x=x.numpy(), x_lengths=x_lengths.numpy())
|
101 |
+
x = torch.from_numpy(x)
|
102 |
+
m_p = torch.from_numpy(m_p)
|
103 |
+
logs_p = torch.from_numpy(logs_p)
|
104 |
+
x_mask = torch.from_numpy(x_mask)
|
105 |
+
|
106 |
+
if self.n_speakers > 0:
|
107 |
+
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
108 |
+
else:
|
109 |
+
g = None
|
110 |
+
|
111 |
+
#logw = self.dp(x, x_mask, g=g, reverse=True, noise_scale=noise_scale_w)
|
112 |
+
logw = runonnx("ONNX_net/dp.onnx", x=x.numpy(), x_mask=x_mask.numpy(), g=g.numpy())
|
113 |
+
logw = torch.from_numpy(logw[0])
|
114 |
+
|
115 |
+
w = torch.exp(logw) * x_mask * length_scale
|
116 |
+
w_ceil = torch.ceil(w)
|
117 |
+
return list(w_ceil.squeeze())
|
118 |
+
|
119 |
+
def infer_with_duration(self, x, x_lengths, w_ceil, sid=None, noise_scale=1, length_scale=1, noise_scale_w=1., max_len=None,
|
120 |
+
emotion_embedding=None):
|
121 |
+
from ONNXVITS_utils import runonnx
|
122 |
+
|
123 |
+
#x, m_p, logs_p, x_mask = self.enc_p(x, x_lengths)
|
124 |
+
x, m_p, logs_p, x_mask = runonnx("ONNX_net/enc_p.onnx", x=x.numpy(), x_lengths=x_lengths.numpy())
|
125 |
+
x = torch.from_numpy(x)
|
126 |
+
m_p = torch.from_numpy(m_p)
|
127 |
+
logs_p = torch.from_numpy(logs_p)
|
128 |
+
x_mask = torch.from_numpy(x_mask)
|
129 |
+
|
130 |
+
if self.n_speakers > 0:
|
131 |
+
g = self.emb_g(sid).unsqueeze(-1) # [b, h, 1]
|
132 |
+
else:
|
133 |
+
g = None
|
134 |
+
assert len(w_ceil) == x.shape[2]
|
135 |
+
w_ceil = torch.FloatTensor(w_ceil).reshape(1, 1, -1)
|
136 |
+
y_lengths = torch.clamp_min(torch.sum(w_ceil, [1, 2]), 1).long()
|
137 |
+
y_mask = torch.unsqueeze(commons.sequence_mask(y_lengths, None), 1).to(x_mask.dtype)
|
138 |
+
attn_mask = torch.unsqueeze(x_mask, 2) * torch.unsqueeze(y_mask, -1)
|
139 |
+
attn = commons.generate_path(w_ceil, attn_mask)
|
140 |
+
|
141 |
+
m_p = torch.matmul(attn.squeeze(1), m_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
142 |
+
logs_p = torch.matmul(attn.squeeze(1), logs_p.transpose(1, 2)).transpose(1, 2) # [b, t', t], [b, t, d] -> [b, d, t']
|
143 |
+
|
144 |
+
z_p = m_p + torch.randn_like(m_p) * torch.exp(logs_p) * noise_scale
|
145 |
+
|
146 |
+
#z = self.flow(z_p, y_mask, g=g, reverse=True)
|
147 |
+
z = runonnx("ONNX_net/flow.onnx", z_p=z_p.numpy(), y_mask=y_mask.numpy(), g=g.numpy())
|
148 |
+
z = torch.from_numpy(z[0])
|
149 |
+
|
150 |
+
#o = self.dec((z * y_mask)[:,:,:max_len], g=g)
|
151 |
+
o = runonnx("ONNX_net/dec.onnx", z_in=(z * y_mask)[:,:,:max_len].numpy(), g=g.numpy())
|
152 |
+
o = torch.from_numpy(o[0])
|
153 |
+
|
154 |
+
return o, attn, y_mask, (z, z_p, m_p, logs_p)
|