aboutsummaryrefslogtreecommitdiff
path: root/tools/make_golden.py
blob: 109af1d0f1e3bae6892437a158a05eda26e54a1b (plain) (blame)
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
91
92
93
"""Converts the captured references into flat binary files the C tests read.

Keeping the test fixtures as plain little-endian float32 means the C side needs
no parser and no dependency, and the conversion is the only place the two
representations have to agree.

    python tools/make_golden.py chunks golden.npz build/golden.bin
    python tools/make_golden.py slide audio.wav build/slide_golden.bin
"""

import argparse
import struct
import sys

import numpy as np

from model import NUM_CLASSES, NUM_FRAMES, NUM_SAMPLES, SAMPLE_RATE

MAGIC_CHUNKS = b"PSGG"
MAGIC_SLIDE = b"PSGS"


def chunks(args) -> int:
    """Per-window fixtures: waveform in, class probabilities out.

    The torch reference emits log probabilities. They are exponentiated here
    because that is what the engine returns, and because a difference of 2e-3
    in a log probability of -17 is a difference of 4e-8 in the probability
    itself: comparing in log space would set the tolerance by the least
    significant outputs rather than the ones that decide a frame.
    """
    data = np.load(args.npz)
    inputs = data["inputs"].astype(np.float32)
    probs = np.exp(data["outputs"].astype(np.float64)).astype(np.float32)
    n = inputs.shape[0]
    assert inputs.shape == (n, NUM_SAMPLES)
    assert probs.shape == (n, NUM_FRAMES, NUM_CLASSES)

    with open(args.out, "wb") as f:
        f.write(struct.pack("<4sIII", MAGIC_CHUNKS, n, NUM_SAMPLES, NUM_FRAMES))
        f.write(inputs.tobytes())
        f.write(np.ascontiguousarray(probs).tobytes())
    print(f"wrote {args.out}: {n} windows")
    return 0


def slide(args) -> int:
    """Whole-recording fixture: the aggregated tracks pyannote's Inference
    produces, next to the samples that produced them."""
    import soundfile as sf

    from pipeline import aggregate_tracks

    wav, rate = sf.read(args.audio, dtype="float32", always_2d=True)
    if rate != SAMPLE_RATE:
        raise SystemExit(f"{args.audio}: expected {SAMPLE_RATE} Hz, got {rate}")
    samples = np.ascontiguousarray(wav[: args.limit, 0])

    tracks = aggregate_tracks(samples, step=args.step)
    with open(args.out, "wb") as f:
        f.write(
            struct.pack(
                "<4sIId", MAGIC_SLIDE, len(samples), tracks.shape[0], args.step
            )
        )
        f.write(samples.tobytes())
        f.write(np.ascontiguousarray(tracks, dtype=np.float32).tobytes())
    print(f"wrote {args.out}: {len(samples)} samples, {tracks.shape[0]} frames")
    return 0


def main() -> int:
    ap = argparse.ArgumentParser()
    sub = ap.add_subparsers(dest="cmd", required=True)

    c = sub.add_parser("chunks")
    c.add_argument("npz")
    c.add_argument("out")
    c.set_defaults(func=chunks)

    s = sub.add_parser("slide")
    s.add_argument("audio")
    s.add_argument("out")
    s.add_argument("--step", type=float, default=1.0)
    s.add_argument("--limit", type=int, default=45 * SAMPLE_RATE)
    s.set_defaults(func=slide)

    args = ap.parse_args()
    return args.func(args)


if __name__ == "__main__":
    sys.exit(main())