File size: 7,786 Bytes
a361db3 | 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 | """
Benchmark script: Compare all approaches on test audio files.
Runs all three approaches on each input file, collects metrics,
and produces a CSV comparison report.
Usage:
uv run python scripts/benchmark.py --data-dir data --output-dir benchmark_results
"""
import argparse
import sys
import csv
import json
import logging
import time
from pathlib import Path
from datetime import datetime
from approaches import list_approaches, get_approach
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger("benchmark")
# Ensure project root is importable when script is run directly
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
class BenchmarkRunner:
"""Run benchmark across all approaches and audio files."""
def __init__(self, data_dir: str, output_dir: str):
self.data_dir = Path(data_dir)
self.output_dir = Path(output_dir)
self.output_dir.mkdir(parents=True, exist_ok=True)
# Results storage
self.results = []
def find_test_files(self) -> list:
"""Find all WAV files in data directory."""
wav_files = sorted(self.data_dir.glob("*.wav"))
if not wav_files:
log.warning(f"No WAV files found in {self.data_dir}")
return wav_files
def run_benchmark(self, whisper_model: str = "base"):
"""Run all approaches on all test files."""
test_files = self.find_test_files()
if not test_files:
log.error(f"No test files found in {self.data_dir}")
return
log.info("="*70)
log.info(f"BENCHMARK: {len(list_approaches())} approaches × {len(test_files)} files")
log.info("="*70)
for input_file in test_files:
log.info(f"\n{'='*70}")
log.info(f"File: {input_file.name}")
log.info(f"{'='*70}")
for approach_name in list_approaches():
log.info(f"\n Testing approach: {approach_name}")
log.info("-"*70)
try:
result = self._run_approach(
approach_name,
input_file,
whisper_model,
)
self.results.append(result)
except Exception as e:
log.error(f" FAILED: {e}")
result = {
"timestamp": datetime.now().isoformat(),
"input_file": input_file.name,
"approach": approach_name,
"status": "FAILED",
"error": str(e),
}
self.results.append(result)
log.info(f"\n{'='*70}")
log.info("BENCHMARK COMPLETE")
log.info(f"{'='*70}")
# Save results
self._save_results()
self._print_summary()
def _run_approach(self, approach_name: str, input_file: Path, whisper_model: str):
"""Run single approach on single file."""
# Create output directory for this run
output_subdir = self.output_dir / approach_name / input_file.stem
output_subdir.mkdir(parents=True, exist_ok=True)
# Initialize approach
approach_class = get_approach(approach_name)
approach = approach_class()
# Run
start_time = time.time()
pipeline_output = approach.run(
input_file=str(input_file),
output_dir=str(output_subdir),
whisper_model=whisper_model,
)
execution_time = time.time() - start_time
result = {
"timestamp": datetime.now().isoformat(),
"input_file": input_file.name,
"input_size_mb": input_file.stat().st_size / (1024*1024),
"approach": approach_name,
"status": "SUCCESS",
"duration_seconds": pipeline_output.duration_seconds,
"execution_time_seconds": execution_time,
"samples_per_second": (pipeline_output.duration_seconds / execution_time)
if execution_time > 0 else 0,
"n_speakers": pipeline_output.n_speakers,
"talker_of_interest": pipeline_output.talker_of_interest,
"separation_method": pipeline_output.separation_method,
"doa_method": pipeline_output.doa_method,
"gender_method": pipeline_output.gender_method,
"asr_model": pipeline_output.asr_model,
"output_dir": str(output_subdir),
}
# Log metrics
log.info(" Status: SUCCESS")
log.info(f" Execution time: {execution_time:.2f}s")
log.info(f" Speakers: {pipeline_output.n_speakers}")
log.info(f" ToI: Speaker {pipeline_output.talker_of_interest}")
log.info(f" Output: {output_subdir}")
return result
def _save_results(self):
"""Save results to CSV and JSON."""
# Save CSV
csv_path = self.output_dir / "benchmark_results.csv"
if self.results:
fieldnames = self.results[0].keys()
with open(csv_path, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(self.results)
log.info(f"\nSaved: {csv_path}")
# Save JSON
json_path = self.output_dir / "benchmark_results.json"
with open(json_path, 'w') as f:
json.dump(self.results, f, indent=2)
log.info(f"Saved: {json_path}")
def _print_summary(self):
"""Print summary statistics."""
if not self.results:
return
log.info("\n" + "="*70)
log.info("SUMMARY")
log.info("="*70)
# Group by approach
by_approach = {}
for result in self.results:
approach = result.get("approach")
if approach not in by_approach:
by_approach[approach] = []
by_approach[approach].append(result)
# Print stats per approach
for approach, runs in sorted(by_approach.items()):
successful = [r for r in runs if r.get("status") == "SUCCESS"]
failed = [r for r in runs if r.get("status") == "FAILED"]
log.info(f"\nApproach: {approach}")
log.info(f" Successful: {len(successful)}/{len(runs)}")
if successful:
avg_exec_time = sum(r["execution_time_seconds"] for r in successful) / len(successful)
avg_speedup = sum(r.get("samples_per_second", 0) for r in successful) / len(successful)
log.info(f" Avg execution time: {avg_exec_time:.2f}s")
log.info(f" Avg speedup (samples/s): {avg_speedup:.1f}x")
if failed:
log.info(f" Failed runs: {len(failed)}")
def main():
parser = argparse.ArgumentParser(description="Benchmark all approaches")
parser.add_argument("--data-dir", default="data", help="Directory with test WAV files")
parser.add_argument("--output-dir", default="benchmark_results", help="Output directory")
parser.add_argument("-w", "--whisper-model", default="base", help="Whisper model")
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
data_path = Path(args.data_dir)
if not data_path.exists():
log.error(f"Data directory not found: {data_path}")
return 1
runner = BenchmarkRunner(args.data_dir, args.output_dir)
runner.run_benchmark(args.whisper_model)
return 0
if __name__ == "__main__":
sys.exit(main())
|