Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli] rename cosine_distance as cosine_similarity and fix recognize func #224

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions wespeaker/cli/speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ def compute_similarity(self, audio_path1: str, audio_path2) -> float:
if e1 is None or e2 is None:
return 0.0
else:
return self.cosine_distance(e1, e2)
return self.cosine_similarity(e1, e2)

def cosine_distance(self, e1, e2):
return np.dot(e1, e2) / (norm(e1) * norm(e2))
def cosine_similarity(self, e1, e2):
cosine_score = np.dot(e1, e2) / (norm(e1) * norm(e2))
return (cosine_score + 1.0) / 2 # normalize: [-1, 1] => [0, 1]

def register(self, name: str, audio_path: str):
if name in self.table:
Expand All @@ -92,8 +93,8 @@ def recognize(self, audio_path: str):
best_score = 0.0
best_name = ''
for name, e in self.table.items():
score = self.cosine_distance(q, e)
if best_score < score:
score = self.cosine_similarity(q, e)
if best_score > score:
best_score = score
best_name = name
result = {}
Expand Down
Loading