-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_metrics.py
More file actions
executable file
·273 lines (210 loc) · 8.68 KB
/
Copy pathtest_metrics.py
File metadata and controls
executable file
·273 lines (210 loc) · 8.68 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
import math
import torch
import torch.nn
def test_metric_stats(device):
from speechbrain.nnet.losses import l1_loss
from speechbrain.utils.metric_stats import MetricStats
l1_stats = MetricStats(metric=l1_loss)
l1_stats.append(
ids=["utterance1", "utterance2"],
predictions=torch.tensor([[0.1, 0.2], [0.1, 0.2]], device=device),
targets=torch.tensor([[0.1, 0.3], [0.2, 0.3]], device=device),
length=torch.ones(2, device=device),
reduction="batch",
)
summary = l1_stats.summarize()
assert math.isclose(summary["average"], 0.075, rel_tol=1e-5)
assert math.isclose(summary["min_score"], 0.05, rel_tol=1e-5)
assert summary["min_id"] == "utterance1"
assert math.isclose(summary["max_score"], 0.1, rel_tol=1e-5)
assert summary["max_id"] == "utterance2"
def test_error_rate_stats(device):
from speechbrain.utils.metric_stats import ErrorRateStats
wer_stats = ErrorRateStats()
i2l = {1: "hello", 2: "world", 3: "the"}
def mapper(batch):
return [[i2l[int(x)] for x in seq] for seq in batch]
wer_stats.append(
ids=["utterance1", "utterance2"],
predict=[[3, 2, 1], [2, 3]],
target=torch.tensor([[3, 2, 0], [2, 1, 0]], device=device),
target_len=torch.tensor([0.67, 0.67], device=device),
ind2lab=mapper,
)
summary = wer_stats.summarize()
assert summary["WER"] == 50.0
assert summary["insertions"] == 1
assert summary["substitutions"] == 1
assert summary["deletions"] == 0
assert wer_stats.scores[0]["ref_tokens"] == ["the", "world"]
assert wer_stats.scores[0]["hyp_tokens"] == ["the", "world", "hello"]
def test_weighted_error_rate_stats():
from speechbrain.utils.metric_stats import (
ErrorRateStats,
WeightedErrorRateStats,
)
# simple example where a and a' substitution get matched as similar
def test_cost(edit, a, b):
if edit != "S":
return 1.0
a_syms = ["a", "a'"]
if a in a_syms and b in a_syms:
return 0.1 # high similarity
return 1.0 # low similarity
wer_stats = ErrorRateStats()
weighted_wer_stats = WeightedErrorRateStats(
wer_stats, cost_function=test_cost
)
predict = [["d", "b", "c"], ["a'", "b", "c"]]
refs = [["a", "b", "c"]] * 2
wer_stats.append(
ids=["utterance1", "utterance2"], predict=predict, target=refs
)
summary = weighted_wer_stats.summarize()
assert math.isclose(summary["weighted_wer"], 18.33333, abs_tol=1e-3)
assert math.isclose(summary["weighted_substitutions"], 1.0 + 0.1)
def test_synonym_dict_error_rate_stats():
from speechbrain.utils.dictionaries import SynonymDictionary
from speechbrain.utils.metric_stats import ErrorRateStats
syn_dict = SynonymDictionary()
syn_dict.add_synonym_set({"a", "a'"})
syn_dict.add_synonym_set({"b", "b'"}) # unused syn to check for correctness
wer_stats = ErrorRateStats(equality_comparator=syn_dict)
predict = [["a'", "b", "c", "e"]]
refs = [["a", "b", "c", "d"]]
wer_stats.append(ids=["utterance1"], predict=predict, target=refs)
summary = wer_stats.summarize()
assert math.isclose(summary["WER"], 25.0)
def test_embedding_error_rate_stats(device):
from speechbrain.utils.metric_stats import EmbeddingErrorRateSimilarity
def test_word_embedding(sentence):
if sentence == "a":
return torch.tensor([1.0, 0.0], device=device)
if sentence == "b":
return torch.tensor([0.0, 1.0], device=device)
if sentence == "c":
return torch.tensor([0.9, 0.1], device=device)
ember = EmbeddingErrorRateSimilarity(test_word_embedding, 1.0, 0.1, 0.4)
assert ember("S", "a", "b") == 1.0 # low similarity
assert ember("S", "a", "c") == 0.1 # high similarity
def test_binary_metrics(device):
from speechbrain.utils.metric_stats import BinaryMetricStats
binary_stats = BinaryMetricStats()
binary_stats.append(
ids=["utt1", "utt2", "utt3", "utt4", "utt5", "utt6"],
scores=torch.tensor([0.1, 0.4, 0.8, 0.2, 0.3, 0.6], device=device),
labels=torch.tensor([1, 0, 1, 0, 1, 0], device=device),
)
summary = binary_stats.summarize(threshold=0.5)
assert summary["TP"] == 1
assert summary["TN"] == 2
assert summary["FP"] == 1
assert summary["FN"] == 2
summary = binary_stats.summarize(threshold=None)
assert summary["threshold"] >= 0.3 and summary["threshold"] < 0.4
summary = binary_stats.summarize(threshold=None, max_samples=1)
assert summary["threshold"] >= 0.1 and summary["threshold"] < 0.2
def test_EER(device):
from speechbrain.utils.metric_stats import EER
positive_scores = torch.tensor([0.1, 0.2, 0.3], device=device)
negative_scores = torch.tensor([0.4, 0.5, 0.6], device=device)
eer, threshold = EER(positive_scores, negative_scores)
assert eer == 1.0
assert threshold > 0.3 and threshold < 0.4
positive_scores = torch.tensor([0.4, 0.5, 0.6], device=device)
negative_scores = torch.tensor([0.3, 0.2, 0.1], device=device)
eer, threshold = EER(positive_scores, negative_scores)
assert eer == 0
assert threshold > 0.3 and threshold < 0.4
cos = torch.nn.CosineSimilarity(dim=1, eps=1e-6)
input1 = torch.randn(1000, 64, device=device)
input2 = torch.randn(1000, 64, device=device)
positive_scores = cos(input1, input2)
input1 = torch.randn(1000, 64, device=device)
input2 = torch.randn(1000, 64, device=device)
negative_scores = cos(input1, input2)
eer, threshold = EER(positive_scores, negative_scores)
correct = (positive_scores > threshold).nonzero(as_tuple=False).size(0) + (
negative_scores < threshold
).nonzero(as_tuple=False).size(0)
assert correct > 900 and correct < 1100
def test_minDCF(device):
from speechbrain.utils.metric_stats import minDCF
positive_scores = torch.tensor([0.1, 0.2, 0.3], device=device)
negative_scores = torch.tensor([0.4, 0.5, 0.6], device=device)
min_dcf, threshold = minDCF(positive_scores, negative_scores)
assert (0.01 - min_dcf) < 1e-4
assert threshold >= 0.6
positive_scores = torch.tensor([0.4, 0.5, 0.6], device=device)
negative_scores = torch.tensor([0.1, 0.2, 0.3], device=device)
min_dcf, threshold = minDCF(positive_scores, negative_scores)
assert min_dcf == 0
assert threshold > 0.3 and threshold < 0.4
def test_classification_stats():
import pytest
from speechbrain.utils.metric_stats import ClassificationStats
stats = ClassificationStats()
stats.append(ids=["1", "2"], predictions=["B", "A"], targets=["B", "A"])
stats.append(ids=["3", "4"], predictions=["A", "B"], targets=["B", "C"])
summary = stats.summarize()
assert pytest.approx(summary["accuracy"], 0.01) == 0.5
classwise_accuracy = summary["classwise_accuracy"]
assert pytest.approx(classwise_accuracy["A"]) == 1.0
assert pytest.approx(classwise_accuracy["B"]) == 0.5
assert pytest.approx(classwise_accuracy["C"]) == 0.0
def test_categorized_classification_stats():
import pytest
from speechbrain.utils.metric_stats import ClassificationStats
stats = ClassificationStats()
stats.append(
ids=["1", "2"],
predictions=["B", "A"],
targets=["B", "A"],
categories=["C1", "C2"],
)
stats.append(
ids=["3", "4"],
predictions=["A", "B"],
targets=["B", "C"],
categories=["C2", "C1"],
)
stats.append(
ids=["5", "6"],
predictions=["A", "C"],
targets=["B", "C"],
categories=["C2", "C1"],
)
summary = stats.summarize()
assert pytest.approx(summary["accuracy"], 0.01) == 0.5
classwise_accuracy = summary["classwise_accuracy"]
assert pytest.approx(classwise_accuracy["C1", "B"]) == 1.0
assert pytest.approx(classwise_accuracy["C1", "C"]) == 0.5
assert pytest.approx(classwise_accuracy["C2", "A"]) == 1.0
assert pytest.approx(classwise_accuracy["C2", "B"]) == 0.0
def test_classification_stats_report():
from io import StringIO
from speechbrain.utils.metric_stats import ClassificationStats
stats = ClassificationStats()
stats.append(ids=["1", "2"], predictions=["B", "A"], targets=["B", "A"])
stats.append(ids=["3", "4"], predictions=["A", "B"], targets=["B", "C"])
report_file = StringIO()
stats.write_stats(report_file)
report_file.seek(0)
report = report_file.read()
ref_report = """Overall Accuracy: 50%
Class-Wise Accuracy
-------------------
A: 1 / 1 (100.00%)
B: 1 / 2 (50.00%)
C: 0 / 1 (0.00%)
Confusion
---------
Target: A
-> A: 1 / 1 (100.00%)
Target: B
-> A: 1 / 2 (50.00%)
-> B: 1 / 2 (50.00%)
Target: C
-> B: 1 / 1 (100.00%)
"""
assert report == ref_report