File size: 3,301 Bytes
a1fe2e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Wake delay on held-out clean Fish clips (same clips/padding/noise as delay_compare.py fish) for a stage-1 model
alone (thresholds) and for cascades stage1 -> verifier (D=0, fused rule T1,a,L). Also matched vs baseline (delay_fish_all.json).
    python casc_delay.py S1_CKPT VER_CKPT OUT.json "thr,thr" "T1:a:L T1:a:L" [workers]"""
import json, os, sys
from multiprocessing import get_context
from pathlib import Path
import numpy as np
K = "/home/user/Downloads/vita-kws"
S1, VER, OUT, THRS, RULES = sys.argv[1:6]; WORK = int(sys.argv[6]) if len(sys.argv) > 6 else 4
THRS = [float(t) for t in THRS.split(",")]; RULES = [tuple(float(v) for v in r.split(":")) for r in RULES.split()]
sys.argv = [sys.argv[0], "runs/en13_ft2_causal_mel80/best.pt", "stream", "/dev/null"]
sys.path.insert(0, K + "/plans_local"); sys.path.insert(0, K + "/plans_local/fa2")
import stream_eval as S  # noqa
import cascade as C  # noqa
from bench_stream import StreamScorer  # noqa
SC = None


def init():
    global SC
    SC = StreamScorer(S1); C.init(VER)


def lg(p):
    p = np.clip(p, 1e-6, 1 - 1e-6); return np.log(p / (1 - p))


def job(a):
    i, path = a
    w = S.audio.load(path, 16000)
    se = S.speech_end(w, drop=40, floor=-60); se = None if se is None else se + 1.0
    w = np.concatenate([np.zeros(16000, np.float32), w, np.zeros(16000, np.float32)])
    x = (w + 1e-3 * S.noise(len(w), np.random.default_rng(i))).astype(np.float32)
    if len(x) < 24000: x = np.concatenate([np.zeros(24000 - len(x), np.float32), x])
    from bench_stream import B
    L = SC.frame_logits(B.logmel_numpy(x, SC.feat)); d = L[1] - L[0]
    t, e = C.track110(d); res = {}
    for thr in THRS:
        h = np.flatnonzero(t >= thr); res[f"s1@{thr}"] = None if not len(h) or se is None else float(e[h[0]] / 16000 - se)
    s1, ends = C.cands(d)
    s2 = C.verify(x, ends)[:, 0] if len(ends) else np.zeros(0)
    for T1, A, Lm in RULES:
        ok = (s1 >= T1) & ((1 - A) * lg(s1) + A * lg(s2) >= Lm) if len(ends) else np.zeros(0, bool)
        h = np.flatnonzero(ok); res[f"casc T1={T1} a={A} L={Lm}"] = None if not len(h) or se is None else float(ends[h[0]] / 16000 - se)
    return Path(path).name, res


def main():
    rows = [json.loads(l) for l in open(K + "/data/aurora/manifest_en4_real85.jsonl")]
    jobs = [(i, r["audio"]) for i, r in enumerate(rows) if r["label"] == "aurora" and r.get("split") == "dev" and "fish_s2" in r["audio"]]
    with get_context("fork").Pool(WORK, initializer=init) as P:
        R = dict(P.imap(job, jobs, chunksize=4))
    comp = json.load(open(K + "/runs/delay_fish_all.json"))["per_clip"]
    names = list(next(iter(R.values())))
    for n in names:
        d = np.array([r[n] for r in R.values() if r[n] is not None]) * 1e3
        k = [c for c in R if R[c][n] is not None and comp.get(c, {}).get("baseline_3.8.6") is not None]
        a = np.array([comp[c]["baseline_3.8.6"] for c in k]) * 1e3; b = np.array([R[c][n] for c in k]) * 1e3
        print(f"{n:34s} fired {len(d)}/{len(R)} p50 {np.median(d):5.0f} p90 {np.percentile(d, 90):5.0f} | same {len(k)} clips: ours {np.median(b):5.0f}/{np.percentile(b, 90):5.0f}  baseline {np.median(a):5.0f}/{np.percentile(a, 90):5.0f}", flush=True)
    json.dump(R, open(OUT, "w"))


if __name__ == "__main__":
    main()