so-vits-svc/vencoder/HubertSoft_Onnx.py

28 lines
1.2 KiB
Python
Raw Normal View History

2023-05-22 16:03:08 +00:00
from vencoder.encoder import SpeechEncoder
import onnxruntime
import torch
2023-05-24 12:16:58 +00:00
class HubertSoft_Onnx(SpeechEncoder):
2023-05-22 16:03:08 +00:00
def __init__(self,vec_path = "pretrain/hubert-soft.onnx",device=None):
print("load model(s) from {}".format(vec_path))
self.hidden_dim = 256
if device is None:
self.dev = torch.device("cpu")
else:
self.dev = torch.device(device)
if device == 'cpu' or device == torch.device("cpu") or device is None:
providers = ['CPUExecutionProvider']
elif device == 'cuda' or device == torch.device("cuda"):
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
self.model = onnxruntime.InferenceSession(vec_path, providers=providers)
def encoder(self, wav):
feats = wav
if feats.dim() == 2: # double channels
feats = feats.mean(-1)
assert feats.dim() == 1, feats.dim()
feats = feats.view(1, -1)
2023-05-24 12:45:16 +00:00
feats = feats.unsqueeze(0).cpu().detach().numpy()
2023-05-22 16:03:08 +00:00
onnx_input = {self.model.get_inputs()[0].name: feats}
logits = self.model.run(None, onnx_input)
2023-05-24 12:45:16 +00:00
return torch.tensor(logits[0]).transpose(1, 2).to(self.dev)