-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
315 lines (245 loc) · 9.42 KB
/
Copy pathevaluate.py
File metadata and controls
315 lines (245 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""
Evaluation module for speaker verification using EER (Equal Error Rate).
This module provides proper validation for speaker recognition models
by computing similarity-based metrics instead of classification loss.
"""
import torch
import torch.nn.functional as F
import numpy as np
from pathlib import Path
from typing import List, Tuple, Dict
import random
from tqdm import tqdm
import torchaudio
def generate_verification_pairs(
data_dir: str,
num_pairs: int = 1000,
positive_ratio: float = 0.5,
seed: int = 42
) -> List[Tuple[str, str, int]]:
"""
Generate verification pairs from a speaker dataset.
Args:
data_dir: Root directory with speaker subdirectories
num_pairs: Number of pairs to generate
positive_ratio: Ratio of positive (same speaker) pairs
seed: Random seed
Returns:
List of (file1_path, file2_path, label) tuples
label: 1 = same speaker, 0 = different speakers
"""
random.seed(seed)
np.random.seed(seed)
data_dir = Path(data_dir)
# Build speaker -> files mapping
speaker_files = {}
for speaker_dir in sorted(data_dir.iterdir()):
if not speaker_dir.is_dir():
continue
files = list(speaker_dir.rglob('*.wav')) + \
list(speaker_dir.rglob('*.m4a')) + \
list(speaker_dir.rglob('*.flac'))
if len(files) >= 2: # Need at least 2 files for pairs
speaker_files[speaker_dir.name] = files
speakers = list(speaker_files.keys())
num_positive = int(num_pairs * positive_ratio)
num_negative = num_pairs - num_positive
pairs = []
# Generate positive pairs (same speaker)
for _ in range(num_positive):
speaker = random.choice(speakers)
if len(speaker_files[speaker]) < 2:
continue
file1, file2 = random.sample(speaker_files[speaker], 2)
pairs.append((str(file1), str(file2), 1))
# Generate negative pairs (different speakers)
for _ in range(num_negative):
if len(speakers) < 2:
break
speaker1, speaker2 = random.sample(speakers, 2)
file1 = random.choice(speaker_files[speaker1])
file2 = random.choice(speaker_files[speaker2])
pairs.append((str(file1), str(file2), 0))
return pairs
def load_official_voxceleb_pairs(pairs_file: str, data_root: str) -> List[Tuple[str, str, int]]:
"""
Load official VoxCeleb verification pairs from veri_test2.txt format.
Format:
1 id10270/x6uYqmx31kE/00001.wav id10270/8jEAjG6SegY/00008.wav
0 id10309/0cYFdtyWVds/00001.wav id10296/q-8fGPszYYI/00001.wav
Args:
pairs_file: Path to pairs file (e.g., veri_test2.txt)
data_root: Root directory containing audio files
Returns:
List of (file1_path, file2_path, label) tuples
"""
pairs = []
data_root = Path(data_root)
with open(pairs_file, 'r') as f:
for line in f:
parts = line.strip().split()
if len(parts) != 3:
continue
label, path1, path2 = parts
full_path1 = data_root / path1
full_path2 = data_root / path2
if full_path1.exists() and full_path2.exists():
pairs.append((str(full_path1), str(full_path2), int(label)))
return pairs
def compute_eer(
model: torch.nn.Module,
pairs: List[Tuple[str, str, int]],
device: str = 'cuda',
batch_size: int = 1,
target_dim: int = None
) -> Dict[str, float]:
"""
Compute Equal Error Rate (EER) for speaker verification.
Args:
model: Speaker recognition model
pairs: List of (file1, file2, label) tuples
device: Device to run on
batch_size: Batch size for inference
target_dim: Target MRL dimension (None = use full embedding)
Returns:
Dictionary with 'eer', 'threshold', 'accuracy' metrics
"""
model.eval()
similarities = []
labels = []
with torch.no_grad():
for file1, file2, label in tqdm(pairs, desc="Computing EER"):
# Load audio files
try:
wav1, sr1 = torchaudio.load(file1)
wav2, sr2 = torchaudio.load(file2)
# Resample if needed
if sr1 != 16000:
wav1 = torchaudio.functional.resample(wav1, sr1, 16000)
if sr2 != 16000:
wav2 = torchaudio.functional.resample(wav2, sr2, 16000)
# Convert to mono
if wav1.shape[0] > 1:
wav1 = wav1.mean(dim=0, keepdim=True)
if wav2.shape[0] > 1:
wav2 = wav2.mean(dim=0, keepdim=True)
# Move to device
wav1 = wav1.unsqueeze(0).to(device) # [1, 1, T]
wav2 = wav2.unsqueeze(0).to(device)
# Extract embeddings
if target_dim is not None:
emb1 = model(wav1, target_dim=target_dim)
emb2 = model(wav2, target_dim=target_dim)
else:
emb1 = model(wav1)
emb2 = model(wav2)
# Normalize embeddings
emb1 = F.normalize(emb1, p=2, dim=1)
emb2 = F.normalize(emb2, p=2, dim=1)
# Compute cosine similarity
similarity = F.cosine_similarity(emb1, emb2).item()
similarities.append(similarity)
labels.append(label)
except Exception as e:
# Skip problematic files
continue
# Convert to numpy
similarities = np.array(similarities)
labels = np.array(labels)
# Compute EER
eer, threshold = calculate_eer(similarities, labels)
# Compute accuracy at EER threshold
predictions = (similarities >= threshold).astype(int)
accuracy = (predictions == labels).mean()
return {
'eer': eer,
'threshold': threshold,
'accuracy': accuracy,
'num_pairs': len(labels)
}
def calculate_eer(scores: np.ndarray, labels: np.ndarray) -> Tuple[float, float]:
"""
Calculate Equal Error Rate (EER) from similarity scores and labels.
Args:
scores: Similarity scores (higher = more similar)
labels: Ground truth labels (1 = same speaker, 0 = different)
Returns:
eer: Equal Error Rate (as fraction, not percentage)
threshold: Threshold at EER
"""
# Sort by scores
sorted_indices = np.argsort(scores)
sorted_scores = scores[sorted_indices]
sorted_labels = labels[sorted_indices]
# Compute FAR and FRR at each threshold
num_positive = np.sum(labels == 1)
num_negative = np.sum(labels == 0)
# FRR: False Rejection Rate (missed positive matches)
# FAR: False Acceptance Rate (false positive matches)
far = []
frr = []
thresholds = []
for threshold in np.linspace(scores.min(), scores.max(), 1000):
# Predictions at this threshold
predictions = (scores >= threshold).astype(int)
# False rejections: label=1 but pred=0
false_rejections = np.sum((labels == 1) & (predictions == 0))
frr_value = false_rejections / num_positive if num_positive > 0 else 0
# False acceptances: label=0 but pred=1
false_acceptances = np.sum((labels == 0) & (predictions == 1))
far_value = false_acceptances / num_negative if num_negative > 0 else 0
far.append(far_value)
frr.append(frr_value)
thresholds.append(threshold)
far = np.array(far)
frr = np.array(frr)
thresholds = np.array(thresholds)
# Find EER (where FAR = FRR)
eer_idx = np.argmin(np.abs(far - frr))
eer = (far[eer_idx] + frr[eer_idx]) / 2
eer_threshold = thresholds[eer_idx]
return eer, eer_threshold
def evaluate_mrl_all_dims(
model: torch.nn.Module,
pairs: List[Tuple[str, str, int]],
mrl_dims: List[int],
device: str = 'cuda'
) -> Dict[int, Dict[str, float]]:
"""
Evaluate EER for all MRL dimensions.
Args:
model: MRL-enabled speaker recognition model
pairs: Verification pairs
mrl_dims: List of MRL dimensions to evaluate
device: Device to run on
Returns:
Dictionary mapping dimension -> metrics
{64: {'eer': 0.012, 'threshold': 0.55, ...}, ...}
"""
results = {}
for dim in mrl_dims:
print(f"\nEvaluating {dim}D embeddings...")
metrics = compute_eer(model, pairs, device=device, target_dim=dim)
results[dim] = metrics
print(f" EER: {metrics['eer']*100:.3f}%")
print(f" Threshold: {metrics['threshold']:.4f}")
print(f" Accuracy: {metrics['accuracy']*100:.2f}%")
return results
if __name__ == "__main__":
# Test EER computation
import sys
if len(sys.argv) < 2:
print("Usage: python evaluate.py <test_data_dir>")
print("Example: python evaluate.py /home/tikim/dataset/voxceleb/test/wav")
sys.exit(1)
test_dir = sys.argv[1]
print(f"Generating verification pairs from {test_dir}")
pairs = generate_verification_pairs(test_dir, num_pairs=500)
print(f"Generated {len(pairs)} pairs")
# Print sample pairs
print("\nSample pairs:")
for i, (f1, f2, label) in enumerate(pairs[:3]):
print(f"{i+1}. {'SAME' if label else 'DIFF'}: {Path(f1).parent.name} vs {Path(f2).parent.name}")
# Test with dummy model (for development)
print("\n⚠️ This is a test run with random embeddings")
print("For real evaluation, use with trained model")