forked from colstone/SOFA_AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
286 lines (243 loc) · 8.96 KB
/
evaluate.py
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
import json
import pathlib
import warnings
from typing import List, Set, Tuple, Dict
import click
import textgrid
import tqdm
class Interval:
def __init__(self, mark: str, start: float, end: float):
self.mark = mark
self.start = start
self.end = end
def __str__(self):
return f"{self.__class__.__name__}(mark={self.mark}, start={self.start}, end={self.end})"
class Boundary:
def __init__(self, mark: str, position: float):
self.mark = mark
self.position = position
def __str__(self):
return f"{self.__class__.__name__}(mark={self.mark}, position={self.position})"
def intervals_to_boundaries(intervals: List[Interval]) -> List[Boundary]:
"""
Convert intervals to boundaries.
"""
if len(intervals) == 0:
return []
boundaries = [Boundary(mark=None, position=0.)]
for interval in intervals:
if boundaries[-1].mark is None and boundaries[-1].position == interval.start:
boundaries[-1].mark = interval.mark
else:
boundaries.append(Boundary(mark=interval.mark, position=interval.start))
boundaries.append(Boundary(mark=None, position=interval.end))
return boundaries
def match_boundaries(boundaries1: List[Boundary], boundaries2: List[Boundary]) -> Set[Tuple[Boundary, Boundary]]:
"""
Match the beginnings and endings of non-space intervals between the given boundaries.
"""
mappings: Set[Tuple[Boundary, Boundary]] = set()
if len(boundaries1) == 0 or len(boundaries2) == 0:
return mappings
i = j = 0
num_iters = min(
len([b for b in boundaries1 if b.mark is not None]),
len([b for b in boundaries2 if b.mark is not None])
)
for _ in range(num_iters):
# find beginning boundaries
while boundaries1[i].mark is None:
i += 1
while boundaries2[j].mark is None:
j += 1
mappings.add((boundaries1[i], boundaries2[j]))
# find ending boundaries
i += 1
j += 1
if i < len(boundaries1) and j < len(boundaries2):
mappings.add((boundaries1[i], boundaries2[j]))
return mappings
class Metric:
"""
A torchmetrics.Metric-like class with similar methods but lowered computing overhead.
"""
def update(self, pred, target):
raise NotImplementedError()
def compute(self):
raise NotImplementedError()
def reset(self):
raise NotImplementedError()
class BoundaryEditDistance(Metric):
"""
The total moving distance from the predicted boundaries to the target boundaries.
"""
def __init__(self):
self.distance = 0.
def update(self, pred: List[Interval], target: List[Interval]):
assert len(pred) == len(target), (
f"Number of intervals should be equal in pred and target ({len(pred)} != {len(target)})."
)
if len(target) == 0:
return
# get boundaries of pred and target
p_boundaries = intervals_to_boundaries(pred)
t_boundaries = intervals_to_boundaries(target)
# find boundary mappings
mappings = match_boundaries(p_boundaries, t_boundaries)
# compute the distance
distance = sum(abs(b1.position - b2.position) for b1, b2 in mappings)
self.distance += distance
def compute(self):
return self.distance
def reset(self):
self.distance = 0.
class BoundaryEditRatio(Metric):
"""
The boundary edit distance divided by the total duration of target intervals.
"""
def __init__(self):
self.distance_metric = BoundaryEditDistance()
self.duration = 0.
def update(self, pred: List[Interval], target: List[Interval]):
self.distance_metric.update(pred=pred, target=target)
if len(target) > 0:
self.duration += target[-1].end
def compute(self):
return self.distance_metric.compute() / self.duration
def reset(self):
self.distance_metric.reset()
self.duration = 0.
class BoundaryErrorRate(Metric):
"""
The proportion of misplaced boundaries to all target boundaries under a given tolerance of distance.
"""
def __init__(self, tolerance=0.05):
self.tolerance = tolerance
self.errors = 0
self.total = 0
def update(self, pred: List[Interval], target: List[Interval]):
if len(target) == 0:
return
# get boundaries of pred and target
p_boundaries = intervals_to_boundaries(pred)
t_boundaries = intervals_to_boundaries(target)
# find boundary mappings
mappings = match_boundaries(p_boundaries, t_boundaries)
errors = sum(abs(b1.position - b2.position) > self.tolerance for b1, b2 in mappings)
self.errors += errors
self.total += len(t_boundaries)
def compute(self):
return self.errors / self.total
def reset(self):
self.errors = 0
self.total = 0
def parse_intervals_from_textgrid(file: pathlib.Path) -> List[Interval]:
"""
Parse the intervals from a Textgrid file and return interval tuples (mark, start, end).
"""
tg = textgrid.TextGrid()
tg.read(file, encoding='utf8')
tier: textgrid.IntervalTier = None
for t in tg.tiers:
if isinstance(t, textgrid.IntervalTier) and t.name == 'phones':
tier = t
break
assert tier is not None, f"There are no phones tier in file \"{file}\"."
return [Interval(mark=i.mark, start=i.minTime, end=i.maxTime) for i in tier]
@click.command(
help="Calculate metrics between the FA predictions and the targets (ground truth)."
)
@click.argument(
"pred",
type=click.Path(
exists=True,
file_okay=False,
dir_okay=True,
readable=True
),
metavar="PRED_DIR"
)
@click.argument(
"target",
type=click.Path(
exists=True,
file_okay=False,
dir_okay=True,
readable=True
),
metavar="TARGET_DIR"
)
@click.option(
"--recursive",
"-r",
is_flag=True,
help="Compare files in subdirectories recursively"
)
@click.option(
"--strict",
"-s",
is_flag=True,
help="Raise errors on mismatching phone sequences"
)
@click.option(
"--ignore",
type=str,
default="AP,SP,<AP>,<SP>,,pau,cl",
help="Ignored phone marks, split by commas",
show_default=True
)
def main(pred: str, target: str, recursive: bool, strict: bool, ignore: str):
pred_dir = pathlib.Path(pred)
target_dir = pathlib.Path(target)
if recursive:
iterable = pred_dir.rglob("*.TextGrid")
else:
iterable = pred_dir.glob("*.TextGrid")
ignored = ignore.split(",")
metrics: Dict[str, Metric] = {
"BoundaryEditRatio": BoundaryEditRatio(),
"BoundaryErrorRate10ms": BoundaryErrorRate(tolerance=0.01),
"BoundaryErrorRate20ms": BoundaryErrorRate(tolerance=0.02),
"BoundaryErrorRate50ms": BoundaryErrorRate(tolerance=0.05),
}
cnt = 0
for pred_file in tqdm.tqdm(iterable):
target_file = target_dir / pred_file.relative_to(pred_dir)
if not target_file.exists():
warnings.warn(
f"The prediction file \"{pred_file}\" has no matching target file, "
f"which should be \"{target_file}\".",
category=UserWarning
)
warnings.filterwarnings("default")
continue
pred_intervals = parse_intervals_from_textgrid(pred_file)
target_intervals = parse_intervals_from_textgrid(target_file)
pred_intervals = [p for p in pred_intervals if p.mark not in ignored]
target_intervals = [p for p in target_intervals if p.mark not in ignored]
pred_interval_marks = [p.mark for p in pred_intervals]
target_interval_marks = [p.mark for p in target_intervals]
if pred_interval_marks != target_interval_marks:
if strict:
raise RuntimeError(
f"Phone sequences from prediction file and target file are not identical: "
f"{pred_interval_marks} in {pred_file} compared to {target_interval_marks} in {target_file}."
)
else:
continue
for metric in metrics.values():
metric.update(pred_intervals, target_intervals)
cnt += 1
if cnt == 0:
raise RuntimeError(
"Unable to compare any files in the given directories. "
"Matching files should have same names and same relative paths, "
"containing the same phone sequences except for spaces."
)
result = {
key: metric.compute()
for key, metric in metrics.items()
}
print(json.dumps(result, indent=4, ensure_ascii=False))
if __name__ == "__main__":
main()