-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
228 lines (187 loc) · 8.02 KB
/
benchmark.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
import argparse
import json
import math
import tempfile
from collections import namedtuple
from concurrent.futures import ProcessPoolExecutor
import matplotlib.pyplot as plt
from pyannote.core import notebook
from pyannote.metrics.detection import (
DetectionAccuracy,
DetectionErrorRate,
)
from tqdm import tqdm
from dataset import *
from engine import *
from util import split_audio
RESULTS_FOLDER = os.path.join(os.path.dirname(__file__), "results")
class BenchmarkTypes(Enum):
ACCURACY = "ACCURACY"
CPU = "CPU"
def _engine_params_parser(in_args: argparse.Namespace) -> Dict[str, Any]:
kwargs_engine = dict()
engine = Engines(in_args.engine)
if engine is Engines.PICOVOICE_EAGLE:
if in_args.picovoice_access_key is None:
raise ValueError(f"Engine {in_args.engine} requires --picovoice-access-key")
kwargs_engine.update(access_key=in_args.picovoice_access_key)
elif engine in {Engines.PYANNOTE, Engines.WESPEAKER}:
if in_args.auth_token is None:
raise ValueError(f"Engine {in_args.engine} requires --auth-token")
kwargs_engine.update(auth_token=in_args.auth_token)
return kwargs_engine
def _process_accuracy(engine: Engine, dataset: Dataset, verbose: bool, plot: bool, name: str) -> None:
metric_da = DetectionAccuracy(skip_overlap=True)
metric_der = DetectionErrorRate(skip_overlap=True)
metrics = [metric_da, metric_der]
os.makedirs(os.path.join(RESULTS_FOLDER, str(dataset)), exist_ok=True)
try:
for index in tqdm(range(dataset.size)):
audio_path, audio_length, ground_truth = dataset.get(index)
if verbose:
print(f"Processing {audio_path}...")
with tempfile.TemporaryDirectory() as tmp_dir:
speakers = ground_truth.labels()
for speaker in speakers:
print("Speaker:", speaker)
try:
enroll_audio, test_audio, test_ground_truth = split_audio(
path=audio_path,
label=ground_truth,
speaker=speaker,
folder=tmp_dir)
except ValueError as e:
print(f"Error: {e}")
continue
try:
profile = engine.enrollment(enroll_audio)
hypothesis, _ = engine.recognition(test_audio, profile)
except Exception as e:
print(f"Error: {e}")
continue
if plot:
notebook.reset()
notebook.width = 10
notebook.crop = Segment(0, audio_length)
plt.rcParams["figure.figsize"] = (notebook.width, 3)
plt.subplot(211)
plt.title(os.path.basename(audio_path), loc="right")
notebook.plot_annotation(test_ground_truth, legend=True, time=True)
plt.gca().text(0.6, 0.15, "reference", fontsize=16)
plt.subplot(212)
notebook.plot_annotation(hypothesis, legend=True, time=True)
plt.gca().text(0.6, 0.15, "hypothesis", fontsize=16)
plt.show()
for metric in metrics:
res = metric(test_ground_truth, hypothesis, detailed=True)
if verbose:
print(f"{metric.name}: {res}")
except KeyboardInterrupt:
print("Stopping benchmark...")
results = dict()
for metric in metrics:
results[metric.name] = abs(metric)
results_path = os.path.join(RESULTS_FOLDER, str(dataset), f"{str(engine)}{name}.json")
with open(results_path, "w") as f:
json.dump(results, f, indent=2)
results_details_path = os.path.join(RESULTS_FOLDER, str(dataset), f"{str(engine)}{name}.log")
with open(results_details_path, "w") as f:
for metric in metrics:
f.write(f"{metric.name}:\n{str(metric)}")
f.write("\n")
WorkerResult = namedtuple(
'WorkerResult',
[
'total_audio_sec',
'process_time_sec',
])
def _process_worker(
engine_type: str,
engine_params: Dict[str, Any],
enroll_audio_path: str,
samples: Sequence[Tuple[str, float]]) -> WorkerResult:
engine = Engine.create(Engines(engine_type), **engine_params)
total_audio_sec = 0
process_time = 0
profile = engine.enrollment(enroll_audio_path)
for sample in samples:
print(sample)
audio_path, audio_length = sample
try:
_, elapsed_time = engine.recognition(audio_path, profile)
process_time += elapsed_time
total_audio_sec += audio_length
except Exception as e:
print(f"Error: {e}")
continue
engine.cleanup()
return WorkerResult(total_audio_sec, process_time)
def _process_cpu(
engine: str,
engine_params: Dict[str, Any],
dataset: Dataset,
num_samples: Optional[int] = None) -> None:
num_workers = min(os.cpu_count(), 4)
samples = list(dataset.samples[:])
if num_samples is not None:
samples = samples[:num_samples]
enroll_audio_path, _, _ = dataset.sample_info(samples[0])
chunk_size = math.floor(len(samples) / num_workers)
futures = []
with ProcessPoolExecutor(max_workers=num_workers) as executor:
for i in range(num_workers):
chunk = samples[i * chunk_size: (i + 1) * chunk_size]
chunk_samples = list()
for s in chunk:
audio_path, audio_length, _ = dataset.sample_info(s)
chunk_samples.append((audio_path, audio_length))
future = executor.submit(
_process_worker,
engine_type=engine,
engine_params=engine_params,
enroll_audio_path=enroll_audio_path,
samples=chunk_samples)
futures.append(future)
res = [f.result() for f in futures]
total_audio_time_sec = sum([r.total_audio_sec for r in res])
total_process_time_sec = sum([r.process_time_sec for r in res])
results_path = os.path.join(RESULTS_FOLDER, str(dataset), f"{str(engine)}_cpu.json")
results = {
"total_audio_time_sec": total_audio_time_sec,
"total_process_time_sec": total_process_time_sec,
"num_workers": num_workers,
}
with open(results_path, "w") as f:
json.dump(results, f, indent=2)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", choices=[ds.value for ds in Datasets], required=True)
parser.add_argument("--data-folder", required=True)
parser.add_argument("--label-folder", required=True)
parser.add_argument("--engine", choices=[en.value for en in Engines], required=True)
parser.add_argument("--verbose", action="store_true")
parser.add_argument("--plot", action="store_true")
parser.add_argument("--name", default='')
parser.add_argument("--type", choices=[bt.value for bt in BenchmarkTypes], required=True)
parser.add_argument("--picovoice-access-key")
parser.add_argument("--auth-token")
parser.add_argument("--num-samples", type=int)
args = parser.parse_args()
engine_args = _engine_params_parser(args)
dataset = Dataset.create(Datasets(args.dataset), data_folder=args.data_folder, label_folder=args.label_folder)
print(f"Dataset: {dataset}")
engine = Engine.create(Engines(args.engine), **engine_args)
print(f"Engine: {engine}")
if args.type == BenchmarkTypes.ACCURACY.value:
_process_accuracy(engine, dataset, verbose=args.verbose, plot=args.plot, name=args.name)
elif args.type == BenchmarkTypes.CPU.value:
_process_cpu(
engine=args.engine,
engine_params=engine_args,
dataset=dataset,
num_samples=args.num_samples)
if __name__ == "__main__":
main()
__all__ = [
"RESULTS_FOLDER",
]