-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
listen to @eonglints and add hubert with kmeans as an option
- Loading branch information
1 parent
ed313d3
commit a11722e
Showing
4 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from pathlib import Path | ||
|
||
import torch | ||
from torch import nn | ||
from einops import rearrange, pack, unpack | ||
|
||
import joblib | ||
import fairseq | ||
|
||
class HubertWithKmeans(nn.Module): | ||
def __init__( | ||
self, | ||
checkpoint_path, | ||
kmeans_path | ||
): | ||
super().__init__() | ||
model_path = Path(checkpoint_path) | ||
kmeans_path = Path(kmeans_path) | ||
|
||
assert model_path.exists(), f'path {checkpoint_path} does not exist' | ||
assert kmeans_path.exists(), f'path {kmeans_path} does not exist' | ||
|
||
checkpoint = torch.load(checkpoint_path) | ||
load_model_input = {checkpoint_path: checkpoint} | ||
model, *_ = fairseq.checkpoint_utils.load_model_ensemble_and_task(load_model_input) | ||
|
||
self.model = model[0] | ||
self.model.eval() | ||
|
||
kmeans = joblib.load(kmeans_path) | ||
self.kmeans = kmeans | ||
|
||
@property | ||
def groups(self): | ||
return 1 | ||
|
||
@property | ||
def codebook_size(self): | ||
return self.kmeans.n_clusters | ||
|
||
@torch.no_grad() | ||
def forward(self, wav_input, flatten = True): | ||
device = wav_input.device | ||
|
||
embed = self.model(wav_input, features_only = True) | ||
embed, packed_shape = pack([embed['x']], '* d') | ||
|
||
codebook_indices = self.kmeans.predict(embed.cpu().detach().numpy()) | ||
|
||
codebook_indices = torch.from_numpy(codebook_indices).to(device).long() | ||
|
||
if flatten: | ||
return codebook_indices | ||
|
||
codebook_indices, = unpack(codebook_indices, packed_shape, '*') | ||
return codebook_indices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a11722e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️