Download code/plans/fa2/end_viz_data.py from thucdangvan020999/aurora-wake-model: direct link, hf CLI and curl.
- Browser
- Download file 4.6 kB
-
https://huggingface.co/thucdangvan020999/aurora-wake-model/resolve/main/code/plans/fa2/end_viz_data.py
- Command line
-
hf download hf://thucdangvan020999/aurora-wake-model/code/plans/fa2/end_viz_data.py
-
curl -L -o end_viz_data.py https://huggingface.co/thucdangvan020999/aurora-wake-model/resolve/main/code/plans/fa2/end_viz_data.py
4.6 kB
| #!/usr/bin/env python3 | |
| """Dump per-clip signals for the end-of-keyword visualization (same processing as end_rules.py). | |
| EH_V2=1 EH_V3=1 python end_viz_data.py WAKE_CKPT HEAD N T OUT.json""" | |
| import base64, io, json, os, sys | |
| import numpy as np, soundfile as sf, torch | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| os.environ["FAR_SET"] = "car"; _a = sys.argv; sys.argv = [_a[0], "x"]; import vf_far as FAR; sys.argv = _a | |
| import end_head as EH | |
| from end_head import audio, load_checkpoint, read_manifest, K, SR | |
| CK, HD, N, T, OUT = sys.argv[1], sys.argv[2], int(sys.argv[3]), float(sys.argv[4]), sys.argv[5] | |
| sys.argv = [sys.argv[0], "runs/en13_ft2_causal_mel80/best.pt", "stream", "/dev/null"]; sys.path.insert(0, K + "/plans_local"); import stream_eval as S | |
| torch.set_num_threads(8) | |
| model, fe, cfg, _, _ = load_checkpoint(CK, torch.device("cpu")); z = torch.load(HD); head = EH.mk_head(z["dim"]); head.load_state_dict(z["head"]); head.eval() | |
| rows = [json.loads(l) for l in open(K + "/data/aurora/manifest_en4_real85.jsonl")] | |
| clips = [r["audio"] for r in rows if r["label"] == "aurora" and r.get("split") == "dev" and "fish_s2" in r["audio"]] | |
| vs = [i.audio for i in read_manifest(K + "/data/aurora/manifest_hp6.jsonl") if i.split == "dev" and i.kind == "negative_speech"] | |
| WANT = {"silence": 7, "command": 7, "car_command": 6}; got = {k: 0 for k in WANT}; out = [] | |
| order = np.random.default_rng(7).permutation(len(clips)) | |
| for ci in order: | |
| if all(got[k] >= WANT[k] for k in WANT): break | |
| cond = [k for k in WANT if got[k] < WANT[k]][0] | |
| rng = np.random.default_rng(int(ci)) | |
| w0 = audio.load(clips[ci], SR); se = S.speech_end(w0, drop=40, floor=-60) | |
| if se is None: continue | |
| w = np.concatenate([np.zeros(int(0.6 * SR), np.float32), w0[:int(se * SR)], np.zeros(int(1.0 * SR), np.float32)]); kend = 0.6 + se | |
| pad = SR - int(0.6 * SR) # model warm-up: prepend silence not shown | |
| if cond != "silence": | |
| c = audio.load(vs[int(rng.integers(len(vs)))], SR, start=0.0, duration=0.9); g = int((kend + rng.uniform(0, 0.15)) * SR); c = c[:len(w) - g] | |
| ref = np.sqrt(np.mean(w0 ** 2) + 1e-10); rms = np.sqrt(np.mean(c ** 2) + 1e-10) | |
| if rms > 1e-4: w[g:g + len(c)] += c * ref / rms | |
| if cond == "car_command": w = FAR.far(w.astype(np.float32), int(ci)).astype(np.float32) | |
| w = (w + 1e-3 * rng.standard_normal(len(w))).astype(np.float32) | |
| x = np.concatenate([np.zeros(pad, np.float32) + 1e-3 * rng.standard_normal(pad).astype(np.float32), w]) | |
| with torch.no_grad(): | |
| m = fe(torch.from_numpy(x)[None]); m = m.unsqueeze(1) if m.dim() == 3 else m | |
| f = model.frame_features(m); L = model.classifier(f.unsqueeze(2)).squeeze(2)[0].numpy(); pe = torch.sigmoid(head(EH.feats(model, m)))[0].numpy() | |
| d = L[1] - L[0]; c_ = np.concatenate([[0.0], np.cumsum(d, dtype=np.float64)]); ks = np.arange(len(d)); lo = np.maximum(0, ks + 1 - N) | |
| t = 1 / (1 + np.exp(-(c_[ks + 1] - c_[lo]) / (ks + 1 - lo))); t[:146] = 0 | |
| h = np.flatnonzero(t >= T) | |
| if not len(h): continue | |
| off = pad / SR; fr = h[0]; fire = (fr + 1) * 0.01; capf = fr + 25 | |
| nf = len(x) // 160; E = 10 * np.log10(np.array([np.mean(x[i * 160:(i + 1) * 160] ** 2) for i in range(nf)]) + 1e-12) | |
| Es = np.convolve(E, np.ones(3) / 3, mode="full")[:nf] | |
| Pk = lambda j: E[max(0, fr - 50):j + 1].max(); kst = max(0, fr - 150); F = np.percentile(E[max(0, kst - 100):kst + 1], 10) | |
| rule = next(((j + 1) * 0.01 for j in range(fr, min(capf, nf - 1) + 1) if Es[j] < F + 0.2 * (Pk(j) - F)), fire + 0.25) | |
| g2 = np.flatnonzero(pe[fr:capf + 1] >= 0.4); hd = (fr + g2[0] + 1) * 0.01 if len(g2) else fire + 0.25 | |
| s0 = pad // 160 | |
| env = [[round(float(x[i * 160:(i + 1) * 160].min()), 4), round(float(x[i * 160:(i + 1) * 160].max()), 4)] for i in range(s0, nf)] | |
| b = io.BytesIO(); sf.write(b, x[pad:], SR, format="WAV", subtype="PCM_16") | |
| o = lambda v: round(v - off, 3) | |
| out.append({"id": len(out) + 1, "cond": cond, "file": os.path.basename(clips[ci]), "dur": round((len(x) - pad) / SR, 3), | |
| "env": env, "db": [round(float(v), 1) for v in E[s0:nf]], "wake": [round(float(v), 3) for v in t[s0:nf]], "pend": [round(float(v), 3) for v in pe[s0:nf]], | |
| "noise_floor_db": round(float(F), 1), "thr_wake": T, | |
| "m": {"kend": o(kend + off) if False else round(kend, 3), "wake": o(fire), "vivi": o(fire - 0.24), "hold": o(fire + 0.15), "rule": o(rule), "head": o(hd)}, | |
| "wav": base64.b64encode(b.getvalue()).decode()}) | |
| got[cond] += 1; print(cond, out[-1]["m"], flush=True) | |
| json.dump(out, open(OUT, "w")) | |