Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
142 lines (89 loc) · 2.61 KB
/
Copy pathtest.py
File metadata and controls
142 lines (89 loc) · 2.61 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
# test.py
import numpy as np
import torch
from tqdm import tqdm
# ---------------- Local Imports ---------------- #
from config import exp_cfg, SAVE_PATH
from utils.device import get_device
from utils.metrics import calculate_eer, compute_tDCF
from data.dataloader import create_dataloaders
from models.preprocess import PreEmphasis
# ============================================================
# Main
# ============================================================
def main():
# ---------------- Device ---------------- #
device = get_device()
# ---------------- Data ---------------- #
_, _, test_loader = create_dataloaders()
# ---------------- Pre-Emphasis ---------------- #
pre = PreEmphasis(
exp_cfg.PRE_EMPHASIS
).to(device)
# ---------------- Load Best Model ---------------- #
best_model = torch.load(
SAVE_PATH,
map_location=device,
weights_only=False,
)
best_model.eval()
# ---------------- t-DCF Parameters ---------------- #
Pfa_asv = 0.05
Pmiss_asv = 0.01
Pfa_spoof_asv = 0.05
cost_model = {
"Cmiss": 1,
"Cfa": 10,
"Cfa_spoof": 10,
"Ptar": 0.9801,
"Pnon": 0.0099,
"Pspoof": 0.01,
}
# ---------------- Testing ---------------- #
test_scores = []
test_labels = []
with torch.no_grad():
pbar = tqdm(
test_loader,
desc="Testing",
leave=True,
)
for wav, label in pbar:
wav = wav.to(device)
label = label.to(device)
wav = pre(wav)
pred = best_model(wav).squeeze(-1)
test_scores.extend(
pred.cpu().numpy()
)
test_labels.extend(
label.cpu().numpy()
)
# ---------------- Metrics ---------------- #
test_eer = calculate_eer(
test_labels,
test_scores,
)
bona_cm = np.array(test_scores)[
np.array(test_labels) == 1
]
spoof_cm = np.array(test_scores)[
np.array(test_labels) == 0
]
tDCF_curve, thr = compute_tDCF(
bona_cm,
spoof_cm,
Pfa_asv,
Pmiss_asv,
Pfa_spoof_asv,
cost_model,
)
min_tDCF = np.min(tDCF_curve)
# ---------------- Report ---------------- #
print(f"\n🎯 Final Test EER: {test_eer * 100:.2f}%")
print(f"📊 Final min-tDCF: {min_tDCF:.4f}")
# ============================================================
# Entry
# ============================================================
if __name__ == "__main__":
main()