2023-05-13 15:45:56 +00:00
|
|
|
import torch
|
2023-06-26 06:57:53 +00:00
|
|
|
|
|
|
|
from vencoder.encoder import SpeechEncoder
|
2023-05-13 15:45:56 +00:00
|
|
|
from vencoder.hubert import hubert_model
|
2023-06-21 18:04:03 +00:00
|
|
|
|
|
|
|
|
2023-05-14 06:39:07 +00:00
|
|
|
class HubertSoft(SpeechEncoder):
|
2023-06-21 18:04:03 +00:00
|
|
|
def __init__(self, vec_path="pretrain/hubert-soft-0d54a1f4.pt", device=None):
|
|
|
|
super().__init__()
|
2023-05-13 15:45:56 +00:00
|
|
|
print("load model(s) from {}".format(vec_path))
|
2023-05-14 14:53:07 +00:00
|
|
|
hubert_soft = hubert_model.hubert_soft(vec_path)
|
2023-05-13 15:45:56 +00:00
|
|
|
if device is None:
|
|
|
|
self.dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
else:
|
|
|
|
self.dev = torch.device(device)
|
|
|
|
self.hidden_dim = 256
|
|
|
|
self.model = hubert_soft.to(self.dev)
|
|
|
|
|
|
|
|
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-13 15:45:56 +00:00
|
|
|
assert feats.dim() == 1, feats.dim()
|
2023-06-01 18:44:18 +00:00
|
|
|
feats = feats[None,None,:]
|
|
|
|
with torch.no_grad():
|
|
|
|
with torch.inference_mode():
|
2023-06-21 18:04:03 +00:00
|
|
|
units = self.model.units(feats)
|
|
|
|
return units.transpose(1,2)
|