File size: 2,380 Bytes
cadfcff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""How far each smaller version's outputs move from the full-precision export, frame by frame.

For every recording and version: mean and largest change in P(speech) and P(end of turn), and the
share of frames whose yes/no answer at 0.5 flips. This measures faithfulness to the original
model, not accuracy against labels.
"""

from __future__ import annotations

import argparse
import time
from pathlib import Path

import numpy as np
import soundfile as sf

import sava_onnx


def curves(model: Path, audio: np.ndarray) -> tuple[np.ndarray, np.ndarray, float]:
    session = sava_onnx.Session(str(model))
    start = time.perf_counter()
    speech, turn = session.run(audio)
    return speech, turn, 1000 * (time.perf_counter() - start) / session.frames


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument("--reference", type=Path, default=Path("build/sava.onnx"))
    parser.add_argument("--versions", type=Path, nargs="+", required=True)
    parser.add_argument("--audio", type=Path, nargs="+", required=True)
    args = parser.parse_args()

    print("| Recording | Version | MB | ms/frame | Speech: mean / max change | flips | Turn: mean / max change | flips |")
    print("|---|---|---|---|---|---|---|---|")
    for path in args.audio:
        audio, sample_rate = sf.read(path, dtype="float32")
        assert sample_rate == sava_onnx.SAMPLE_RATE and audio.ndim == 1, path
        ref_speech, ref_turn, ref_ms = curves(args.reference, audio)
        print(
            f"| {path.stem} | {args.reference.name} | {args.reference.stat().st_size / 1e6:.0f} | {ref_ms:.2f} | "
            f"speech in {100 * (ref_speech > 0.5).mean():.0f}% of {ref_speech.size} frames | | "
            f"turn ended in {100 * (ref_turn > 0.5).mean():.0f}% | |"
        )
        for version in args.versions:
            speech, turn, ms = curves(version, audio)
            cells = []
            for got, ref in ((speech, ref_speech), (turn, ref_turn)):
                change = np.abs(got - ref)
                flips = ((got > 0.5) != (ref > 0.5)).mean()
                cells += [f"{change.mean():.4f} / {change.max():.3f}", f"{100 * flips:.2f}%"]
            print(f"| {path.stem} | {version.name} | {version.stat().st_size / 1e6:.0f} | {ms:.2f} | " + " | ".join(cells) + " |")


if __name__ == "__main__":
    main()