so-vits-svc/vencoder/ContentVec768L9_Onnx.py

34 lines
1.2 KiB
Python
Raw Normal View History

2023-05-22 16:02:00 +00:00
import onnxruntime
import torch
2023-05-22 15:29:33 +00:00
2023-06-26 06:57:53 +00:00
from vencoder.encoder import SpeechEncoder
2023-06-21 18:04:03 +00:00
2023-05-22 16:02:00 +00:00
class ContentVec768L9_Onnx(SpeechEncoder):
def __init__(self,vec_path = "pretrain/vec-768-layer-9.onnx",device=None):
2023-06-21 18:04:03 +00:00
super().__init__()
2023-05-22 16:02:00 +00:00
print("load model(s) from {}".format(vec_path))
self.hidden_dim = 768
if device is None:
self.dev = torch.device("cpu")
else:
self.dev = torch.device(device)
2023-06-21 18:04:03 +00:00
if device == 'cuda' or device == torch.device("cuda"):
2023-05-22 16:02:00 +00:00
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
2023-06-21 18:04:03 +00:00
else:
providers = ['CPUExecutionProvider']
2023-05-22 16:02:00 +00:00
self.model = onnxruntime.InferenceSession(vec_path, providers=providers)
def encoder(self, wav):
feats = wav
if feats.dim() == 2: # double channels
2023-06-21 18:04:03 +00:00
feats = feats.mean(-1)
2023-05-22 16:02:00 +00:00
assert feats.dim() == 1, feats.dim()
feats = feats.view(1, -1)
2023-05-24 12:45:56 +00:00
feats = feats.unsqueeze(0).cpu().detach().numpy()
2023-05-22 16:02:00 +00:00
onnx_input = {self.model.get_inputs()[0].name: feats}
logits = self.model.run(None, onnx_input)
2023-06-21 18:04:03 +00:00
return torch.tensor(logits[0]).transpose(1, 2).to(self.dev)