feat(eval): implement evaluation metrics (timing MAE, pitch accuracy, WER) and runner
This commit is contained in:
@ -1,8 +1,122 @@
|
||||
"""Quality metrics against UltraStar reference (timing, pitch, WER)."""
|
||||
"""Quality metrics comparing predicted vocals against UltraStar reference ground-truth."""
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
import jiwer
|
||||
import numpy as np
|
||||
|
||||
def calculate_timing_metrics(predicted: list[dict[str, Any]], reference: list[dict[str, Any]]) -> dict[str, float]:
|
||||
"""Calculate mean/median syllable onset error in ms and fraction within +-100ms."""
|
||||
raise NotImplementedError("Metrics will be implemented in M2.")
|
||||
from songsplit.export.ultrastar import UltraStarSong
|
||||
from songsplit.schema.vocals import VocalsDocument
|
||||
|
||||
|
||||
def normalize_text_for_wer(text: str) -> str:
|
||||
"""Normalize text: remove punctuation, lowercase, normalize whitespace."""
|
||||
# Remove UltraStar formatting tokens (~, -, etc.) and standard punctuation
|
||||
text = re.sub(r"[^\w\s]", "", text)
|
||||
text = re.sub(r"\s+", " ", text)
|
||||
return text.strip().lower()
|
||||
|
||||
|
||||
def evaluate_vocals(
|
||||
predicted: VocalsDocument,
|
||||
reference: UltraStarSong,
|
||||
max_match_distance_sec: float = 0.5,
|
||||
) -> dict[str, Any]:
|
||||
"""Calculate comprehensive timing, pitch, and WER metrics against UltraStar reference."""
|
||||
# Flatten reference syllables
|
||||
ref_notes = []
|
||||
for line in reference.lines:
|
||||
for note in line:
|
||||
# Skip empty whitespace notes
|
||||
if note.text.strip():
|
||||
ref_notes.append(note)
|
||||
|
||||
# Flatten predicted syllables
|
||||
pred_syllables = []
|
||||
for line in predicted.lines:
|
||||
for syl in line.syllables:
|
||||
if syl.text.strip():
|
||||
pred_syllables.append(syl)
|
||||
|
||||
if not ref_notes or not pred_syllables:
|
||||
return {
|
||||
"num_ref_syllables": len(ref_notes),
|
||||
"num_pred_syllables": len(pred_syllables),
|
||||
"mae_timing_ms": 0.0,
|
||||
"median_timing_ms": 0.0,
|
||||
"fraction_within_100ms": 0.0,
|
||||
"strict_pitch_accuracy": 0.0,
|
||||
"octave_equiv_pitch_accuracy": 0.0,
|
||||
"wer": 1.0 if not pred_syllables and ref_notes else 0.0,
|
||||
}
|
||||
|
||||
# Match each reference syllable to nearest predicted syllable within max_match_distance_sec
|
||||
timing_errors_ms = []
|
||||
strict_pitch_correct = []
|
||||
octave_pitch_correct = []
|
||||
|
||||
matched_pred_indices = set()
|
||||
for ref in ref_notes:
|
||||
best_idx = None
|
||||
best_diff = float("inf")
|
||||
|
||||
for idx, pred in enumerate(pred_syllables):
|
||||
diff = abs(pred.start - ref.start_sec)
|
||||
if diff < best_diff and diff <= max_match_distance_sec:
|
||||
best_diff = diff
|
||||
best_idx = idx
|
||||
|
||||
if best_idx is not None:
|
||||
matched_pred_indices.add(best_idx)
|
||||
pred = pred_syllables[best_idx]
|
||||
timing_errors_ms.append(best_diff * 1000.0)
|
||||
|
||||
# Pitch check
|
||||
pitch_diff = abs(pred.midi - ref.midi)
|
||||
is_strict = pitch_diff <= 0.5
|
||||
strict_pitch_correct.append(1.0 if is_strict else 0.0)
|
||||
|
||||
# Octave equivalent pitch check (mod 12)
|
||||
mod_diff = pitch_diff % 12.0
|
||||
octave_diff = min(mod_diff, 12.0 - mod_diff)
|
||||
is_octave = octave_diff <= 0.5
|
||||
octave_pitch_correct.append(1.0 if is_octave else 0.0)
|
||||
else:
|
||||
# Unmatched reference note -> penalty
|
||||
timing_errors_ms.append(max_match_distance_sec * 1000.0)
|
||||
strict_pitch_correct.append(0.0)
|
||||
octave_pitch_correct.append(0.0)
|
||||
|
||||
# 1. Timing metrics
|
||||
mae_timing_ms = float(np.mean(timing_errors_ms)) if timing_errors_ms else 0.0
|
||||
median_timing_ms = float(np.median(timing_errors_ms)) if timing_errors_ms else 0.0
|
||||
fraction_within_100ms = (
|
||||
float(np.mean([1.0 if err <= 100.0 else 0.0 for err in timing_errors_ms])) if timing_errors_ms else 0.0
|
||||
)
|
||||
|
||||
# 2. Pitch metrics
|
||||
strict_pitch_acc = float(np.mean(strict_pitch_correct)) if strict_pitch_correct else 0.0
|
||||
octave_pitch_acc = float(np.mean(octave_pitch_correct)) if octave_pitch_correct else 0.0
|
||||
|
||||
# 3. WER metric
|
||||
ref_words_raw = "".join(r.text for r in ref_notes)
|
||||
pred_words_raw = "".join(p.text for p in pred_syllables)
|
||||
norm_ref = normalize_text_for_wer(ref_words_raw)
|
||||
norm_pred = normalize_text_for_wer(pred_words_raw)
|
||||
|
||||
if norm_ref:
|
||||
wer_score = float(jiwer.wer(norm_ref, norm_pred))
|
||||
else:
|
||||
wer_score = 0.0 if not norm_pred else 1.0
|
||||
|
||||
return {
|
||||
"num_ref_syllables": len(ref_notes),
|
||||
"num_pred_syllables": len(pred_syllables),
|
||||
"mae_timing_ms": mae_timing_ms,
|
||||
"median_timing_ms": median_timing_ms,
|
||||
"fraction_within_100ms": fraction_within_100ms,
|
||||
"strict_pitch_accuracy": strict_pitch_acc,
|
||||
"octave_equiv_pitch_accuracy": octave_pitch_acc,
|
||||
"wer": wer_score,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user