Install dependencies for kNN-VC inference
mastertorch v2.0 or greater, torchaudio, and numpy.repository·master·Indexed 19 days ago
https://github.com/bshall/knn-vcAn any-to-any voice conversion model that utilizes k-nearest neighbors regression to map source features to target features within a self-supervised WavLM feature space, followed by HiFiGAN vocoding. The library supports loading via torch.hub and provides tools for feature extraction, kNN matching, and training the HiFiGAN vocoder.
torch v2.0 or greater, torchaudio, and numpy.The inference process follows four steps: loading the model, computing features for the source and reference audio, performing kNN matching, and vocoding the result.
Requirements for Reference Audio:
ref_wav_paths can be any clean speech from the desired speaker.Output:
out_wav is a (T,) tensor representing the converted 16kHz output waveform.import torch, torchaudio
# 1. Load models
knn_vc = torch.hub.load('bshall/knn-vc', 'knn_vc', prematched=True, trust_repo=True, pretrained=True)
# 2. Compute features
src_wav_path = '<path to arbitrary 16kHz waveform>.wav'
ref_wav_paths = ['<path to arbitrary 16kHz waveform from target speaker>.wav', '<path to 2nd utterance from target speaker>.wav', ...]
query_seq = knn_vc.get_features(src_wav_path)
matching_set = knn_vc.get_matching_set(ref_wav_paths)
# 3. Perform kNN matching and vocoding
out_wav = knn_vc.match(query_seq, matching_set, topk=4)You can load the kNN-VC model directly via torch.hub without cloning the repository. The model consists of a WavLM encoder and a HiFiGAN vocoder.
Use the prematched argument to choose between the high-performance prematched vocoder or a regular HiFiGAN vocoder. Setting prematched=True uses the best model described in the paper.
import torch, torchaudio
# Load the model with the prematched vocoder (recommended)
knn_vc = torch.hub.load('bshall/knn-vc', 'knn_vc', prematched=True, trust_repo=True, pretrained=True)
# Alternatively, use the regular HiFiGAN vocoder
# knn_vc = torch.hub.load('bshall/knn-vc', 'knn_vc', prematched=False, trust_repo=True, pretrained=True)To train the HiFiGAN vocoder for WavLM features, use the adapted training script hifigan/train.py. This requires dependencies like librosa, tensorboard, matplotlib, fastprogress, and scipy.
python -m hifigan.train \
--audio_root_path /path/to/librispeech/root/ \
--feature_root_path /path/to/the/output/of/previous/step/ \
--input_training_file data_splits/wavlm-hifigan-train.csv \
--input_validation_file data_splits/wavlm-hifigan-valid.csv \
--checkpoint_path /path/where/you/want/to/save/checkpoint \
--fp16 False \
--config hifigan/config_v1_wavlm.json \
--stdout_interval 25 \
--training_epochs 1800 \
--fine_tuningYou can load the kNN-VC model directly using torch.hub.load. To use the pre-trained model, set pretrained=True. If you want to use a vocoder that was not trained using prematched data, set prematched=False. Ensure you specify the appropriate device (e.g., 'cuda').
import torch
knn_vc = torch.hub.load('bshall/knn-vc', 'knn_vc', prematched=True, trust_repo=True, pretrained=True, device='cuda')Voice conversion follows a three-step process:
get_features.get_matching_set.match.Requirements:
src_wav_path should be a string path to a single waveform.ref_wav_paths should be a list of string paths to reference waveforms from the target speaker.# 1. Extract features from source
query_seq = knn_vc.get_features(src_wav_path)
# 2. Create matching set from target references
matching_set = knn_vc.get_matching_set(ref_wav_paths)
# 3. Match and convert
out_wav = knn_vc.match(query_seq, matching_set, topk=4)Before training the HiFiGAN vocoder, you must precompute WavLM features for your dataset. Use the prematch_dataset.py script. You can use the --prematch flag to determine whether to use prematching when generating features.
# Example: Generate the dataset used to train the prematched HiFiGAN from the paper
python prematch_dataset.py \
--librispeech_path /path/to/librispeech/root \
--out_path /path/where/you/want/outputs/to/go \
--topk 4 \
--matching_layer 6 \
--synthesis_layer 6 \
--prematchThe output of knn_vc.match is a tensor representing the converted waveform. You can play it in a notebook using IPython.display.Audio or save it to a file using torchaudio.save.
# Play in notebook
import IPython.display as ipd
ipd.Audio(out_wav.numpy(), rate=16000)
# Save to file
import torchaudio
torchaudio.save('knnvc1_out.wav', out_wav[None], 16000)