File size: 4,197 Bytes
f730521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Two CPU-only readings of the held-out set that put the controller's numbers in context.

1. Pause handling by what came before the pause. About half of the data's pauses follow
   a complete sentence, which nobody could tell from a finished turn. "Cut off" pauses
   end on a function word (and, but, to, the, I ...): clearly unfinished.
2. Overlap handling against the plain rule the endpoint uses without a controller:
   240 ms of caller speech while the reply plays means STOP. That rule catches every
   interruption and also stops for every "mm-hm"; the controller is there to tell them apart.

  python -m tools.held_out_baselines --data data/out/dataset --dialogues data/out/dialogues.jsonl \
      --pred eval/results/controller/pred --out eval/results/controller/held_out_baselines.json
"""
import argparse
import json
from pathlib import Path

import numpy as np

from duplex.spec import CALLER, FRAME, START, STOP, read_jsonl, read_wav, write_json
from eval.eval_judge import cut_off, pause_prefixes


def rate(hits, total):
    return {"correct": int(hits), "total": int(total), "rate": hits / total if total else None}


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--data", required=True)
    ap.add_argument("--dialogues", required=True)
    ap.add_argument("--pred", required=True)
    ap.add_argument("--out", required=True)
    ap.add_argument("--vad-db", type=float, default=-45.0)
    args = ap.parse_args()

    data = Path(args.data)
    dialogues = {d["id"]: d for d in read_jsonl(args.dialogues)}
    ctl = {"cut_off": [0, 0], "other": [0, 0]}
    silence_rule = {"cut_off": [0, 0], "other": [0, 0]}
    vad_int, vad_bc = [0, 0], [0, 0]
    ctl_int, ctl_bc = [0, 0], [0, 0]
    for it in json.loads((data / "index_test.json").read_text()):
        events = json.loads((data / "meta" / f"{it['id']}.json").read_text())["events"]
        pred = np.load(Path(args.pred) / f"{it['id']}.npz")["pred"]
        pauses = [e for e in events if e["type"] == "pause"]
        prefixes = pause_prefixes(dialogues[it["id"]])
        assert len(prefixes) == len(pauses), it["id"]
        for e, before in zip(pauses, prefixes):
            if e["f1"] <= e["f0"]:
                continue
            group = "cut_off" if cut_off(before) else "other"
            ctl[group][0] += not (pred[e["f0"]: e["f1"]] == START).any()
            ctl[group][1] += 1
            silence_rule[group][0] += (e["end"] - e["start"]) / 16000 < 0.64      # a perfect voice detector with a 640 ms rule
            silence_rule[group][1] += 1

        wav, _ = read_wav(data / "audio" / f"{it['id']}.wav")
        x = wav[: it["n_frames"] * FRAME, CALLER].reshape(-1, FRAME)
        voiced = 20 * np.log10(np.sqrt(np.mean(x ** 2, axis=1)) + 1e-9) > args.vad_db
        run = np.zeros(len(voiced), dtype=np.int32)
        for i, v in enumerate(voiced):
            run[i] = (run[i - 1] + 1 if i else 1) if v else 0
        for e in events:
            if e["type"] not in ("interrupt", "backchannel") or e["f1"] <= e["f0"]:
                continue
            # the rule may fire up to two frames after the labelled window (it needs three voiced frames in a row)
            fired = bool((run[e["f0"]: e["f1"] + 2] >= 3).any())
            ctl_stop = bool((pred[e["f0"]: e["f1"]] == STOP).any())
            if e["type"] == "interrupt":
                vad_int[0] += fired; vad_int[1] += 1; ctl_int[0] += ctl_stop; ctl_int[1] += 1
            else:
                vad_bc[0] += not fired; vad_bc[1] += 1; ctl_bc[0] += not ctl_stop; ctl_bc[1] += 1

    result = {
        "pause_handling": {
            "controller": {g: rate(*v) for g, v in ctl.items()},
            "fixed_640ms_silence_rule_with_perfect_voice_detector": {g: rate(*v) for g, v in silence_rule.items()},
        },
        "overlaps": {
            "interruption_caught": {"controller": rate(*ctl_int), "plain_rule_240ms_of_caller_speech": rate(*vad_int)},
            "backchannel_not_interrupted": {"controller": rate(*ctl_bc), "plain_rule_240ms_of_caller_speech": rate(*vad_bc)},
        },
    }
    write_json(args.out, result)
    print(json.dumps(result, indent=1))


if __name__ == "__main__":
    main()