-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6a42a1
commit 2ab6d51
Showing
3 changed files
with
27 additions
and
3 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Copyright (c) 2023 Binbin Zhang ([email protected]) | ||
# Shuai Wang ([email protected]) | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
|
@@ -40,14 +41,23 @@ def __init__(self, model_dir: str): | |
self.vad_model = vad.OnnxWrapper() | ||
self.table = {} | ||
self.resample_rate = 16000 | ||
self.apply_vad = True | ||
self.apply_vad = False | ||
self.device = torch.device('cpu') | ||
|
||
def set_resample_rate(self, resample_rate: int): | ||
self.resample_rate = resample_rate | ||
|
||
def set_vad(self, apply_vad: bool): | ||
self.apply_vad = apply_vad | ||
|
||
def set_gpu(self, device_id: int): | ||
if device_id >= 0: | ||
device = 'cuda:{}'.format(device_id) | ||
else: | ||
device = 'cpu' | ||
self.device = torch.device(device) | ||
self.model = self.model.to(self.device) | ||
|
||
def extract_embedding(self, audio_path: str): | ||
pcm, sample_rate = torchaudio.load(audio_path, normalize=False) | ||
if self.apply_vad: | ||
|
@@ -71,10 +81,11 @@ def extract_embedding(self, audio_path: str): | |
sample_frequency=16000) | ||
feats = feats - torch.mean(feats, 0) # CMN | ||
feats = feats.unsqueeze(0) | ||
feats = feats.to(self.device) | ||
self.model.eval() | ||
with torch.no_grad(): | ||
_, outputs = self.model(feats) | ||
embedding = outputs[0] | ||
embedding = outputs[0].to(torch.device('cpu')) | ||
return embedding | ||
|
||
def compute_similarity(self, audio_path1: str, audio_path2: str) -> float: | ||
|
@@ -150,6 +161,11 @@ def get_args(): | |
parser.add_argument('--vad', | ||
action='store_true', | ||
help='whether to do VAD or not') | ||
parser.add_argument('-g', | ||
'--gpu', | ||
type=int, | ||
default=-1, | ||
help='which gpu to use (number <0 means using cpu)') | ||
parser.add_argument('--output_file', | ||
help='output file to save speaker embedding') | ||
args = parser.parse_args() | ||
|
@@ -161,6 +177,7 @@ def main(): | |
model = load_model(args.language) | ||
model.set_resample_rate(args.resample_rate) | ||
model.set_vad(args.vad) | ||
model.set_gpu(args.gpu) | ||
if args.task == 'embedding': | ||
embedding = model.extract_embedding(args.audio_file) | ||
if embedding is not None: | ||
|