File size: 3,040 Bytes
d4101a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Score Lua Nova V1 eval WAVs with tts-eval judges (UTMOS22, SCOREQ, DNSMOS, Whisper WER/CER, ECAPA speaker sim).

Run with experiments/tts-eval/.venv python after sourcing tts-eval/scripts/env.ps1.
Speaker similarity uses a centroid built from the LuaNovaDataset test references (new voice),
not the tts-eval F1 anchor.
"""
from __future__ import annotations

import json
import sys
from pathlib import Path

import numpy as np

sys.path.insert(0, r"C:\dev\AjudaEstudos\experiments\tts-eval\src")
from tts_eval.classifiers import ClassifierSuite  # noqa: E402
from tts_eval.env_setup import load_audio_dual_rate, split_windows  # noqa: E402

EXP = Path(__file__).resolve().parents[1]
EVAL = EXP / "eval"
SUITE = Path(r"C:\dev\AjudaEstudos\experiments\tts-eval\suite\ptbr_eval_v1.json")


def texts() -> dict[str, dict[str, str]]:
    test = {}
    for line in (EXP / "data/annotations/test.jsonl").read_text(encoding="utf-8").splitlines():
        if line.strip():
            r = json.loads(line)
            test[r["id"]] = r["text"]
    suite = json.loads(SUITE.read_text(encoding="utf-8"))
    return {"test": test, "stress_ptbr": {r["id"]: r["text"] for r in suite["stress_ptbr"]}}


def main() -> None:
    clf = ClassifierSuite()
    txt = texts()
    ref_dir = EVAL / "reference" / "test"
    embs = [clf.spk.embed(load_audio_dual_rate(p)[2]) for p in sorted(ref_dir.glob("*.wav"))]
    centroid = np.mean(np.stack(embs), axis=0)

    results: dict = {}
    systems = ["reference"] + [d.name for d in sorted(EVAL.iterdir()) if d.is_dir() and d.name != "reference"]
    for system in systems:
        for split, tmap in txt.items():
            d = EVAL / system / split
            if not d.exists():
                continue
            rows = []
            for wid, text in tmap.items():
                p = d / f"{wid}.wav"
                if not p.exists():
                    continue
                _, _, w16 = load_audio_dual_rate(p)
                wins = split_windows(w16, win_sec=10.0, stride_sec=5.0)
                r = {"id": wid}
                r.update(clf.utmos.predict(wins))
                r.update(clf.scoreq.predict(wins))
                r.update(clf.dns.predict(w16))
                asr = clf.asr.predict(w16, text)
                r.update({"wer": asr["wer"], "cer": asr["cer"], "asr_text": asr["asr_text"]})
                r["speaker_sim"] = float(clf.spk.cosine_to_centroid(w16, centroid))
                rows.append(r)
            keys = ["utmos22_mean", "scoreq_mean", "dns_ovr", "wer", "cer", "speaker_sim"]
            summary = {k: round(float(np.mean([x[k] for x in rows])), 4) for k in keys}
            summary["n"] = len(rows)
            results[f"{system}/{split}"] = {"summary": summary, "items": rows}
            print(system, split, summary, flush=True)

    (EVAL / "scores.json").write_text(json.dumps(results, indent=2, ensure_ascii=False), encoding="utf-8")
    print(json.dumps({k: v["summary"] for k, v in results.items()}, indent=2))


if __name__ == "__main__":
    main()