Download test_pellav2.py from SpeechAntiSpoofingBenchmarks/Pellav2: direct link, hf CLI and curl.
- Browser
- Download file 4.17 kB
-
https://huggingface.co/SpeechAntiSpoofingBenchmarks/Pellav2/resolve/main/test_pellav2.py
- Command line
-
hf download hf://SpeechAntiSpoofingBenchmarks/Pellav2/test_pellav2.py
-
curl -L -o test_pellav2.py https://huggingface.co/SpeechAntiSpoofingBenchmarks/Pellav2/resolve/main/test_pellav2.py
4.17 kB
| import io | |
| import os | |
| import numpy as np | |
| import pytest | |
| import torch | |
| import _net | |
| import pellav2 as P | |
| N_PARAMS = 580_494_170 # every checkpoint tensor (776) is a Detector parameter | |
| N_PARAMS_FWD = 580_493_146 # minus backbone.masked_spec_embed (1024, SpecAugment token, unused at inference) | |
| # ---------------------------------------------------------------- preprocessing (CPU) ---- | |
| def test_centre_crop_long(): | |
| rng = np.random.default_rng(1) | |
| x = rng.standard_normal(100_001).astype(np.float32) * 0.1 | |
| out = _net.preprocess(x) | |
| assert out.shape == (64000,) and out.dtype == np.float32 | |
| # same as: high-pass whole clip, centre crop, then gain -- check the crop position via a gain-free comparison | |
| hp = _net.sosfiltfilt(_net._HP_SOS, x - x.mean()).astype(np.float32) | |
| off = (100_001 - 64000) // 2 | |
| seg = hp[off:off + 64000] | |
| g = out[1000:2000] / seg[1000:2000] | |
| assert np.allclose(g, g[0], rtol=1e-4) | |
| def test_short_zero_pad_and_fallback(): | |
| x = np.linspace(-0.2, 0.3, 3000, dtype=np.float32) # < 0.4 s -> RMS fallback loudness | |
| out = _net.preprocess(x) | |
| assert out.shape == (64000,) and not out[3000:].any() and out[:3000].any() | |
| assert np.array_equal(_net.preprocess(np.zeros(0, np.float32)), np.zeros(64000, np.float32)) | |
| def test_loudness_target(): | |
| rng = np.random.default_rng(2) | |
| x = rng.standard_normal(48000).astype(np.float32) * 0.01 | |
| y = _net.preprocess(x) | |
| assert abs(_net.integrated_loudness(y[:48000].astype(np.float64)) - _net.TARGET_LUFS) < 0.05 | |
| def test_bs1770_reference_tone(): | |
| t = np.arange(16000 * 5) / 16000 | |
| # pyloudnorm's cookbook K-weighting at 16 kHz: 0 dBFS 997 Hz mono sine -> -3.08 LUFS (BS.1770 48 kHz ref -3.01) | |
| assert abs(_net.integrated_loudness(np.sin(2 * np.pi * 997 * t)) - (-3.0832)) < 1e-3 | |
| def test_matches_pyloudnorm_if_installed(): | |
| pyln = pytest.importorskip("pyloudnorm") | |
| rng = np.random.default_rng(3) | |
| for n in (6400, 20000, 64000, 64000 + 77): | |
| x = rng.standard_normal(n) * np.linspace(0.001, 0.3, n) | |
| assert _net.integrated_loudness(x) == pytest.approx(pyln.Meter(16000).integrated_loudness(x), abs=1e-9) | |
| # ---------------------------------------------------------------- weights ---- | |
| def test_strict_load_all_tensors_used(): | |
| sd = P.load_state() | |
| net = _net.Detector() | |
| assert set(sd) == set(net.state_dict()) # every tensor consumed, nothing missing | |
| r = net.load_state_dict(sd, strict=True) | |
| assert not r.missing_keys and not r.unexpected_keys | |
| assert P.UNUSED_AT_INFERENCE <= set(sd) | |
| def model(): | |
| m = P.Pellav2(); m.load() | |
| yield m | |
| m.unload() | |
| def test_eval_mode_and_params(model): | |
| assert not any(mod.training for mod in model.net.modules()) | |
| n = sum(p.numel() for p in model.net.parameters()) | |
| assert n == N_PARAMS | |
| assert n - model.net.backbone.masked_spec_embed.numel() == N_PARAMS_FWD | |
| def test_batch_matches_single(model): | |
| rng = np.random.default_rng(0) | |
| a = [rng.standard_normal(n).astype(np.float32) * s for n, s in ((48000, 0.1), (20000, 0.5), (90000, 0.02), (3000, 0.3))] | |
| batched = model.score_batch(a, [16000] * 4) | |
| single = [model.score_batch([x], [16000])[0] for x in a] | |
| assert len(batched) == 4 and all(np.isfinite(batched)) | |
| assert np.allclose(batched, single, atol=1e-3) | |
| def test_deterministic(model): | |
| a = [np.random.randn(40000).astype(np.float32) * 0.1 for _ in range(3)] | |
| assert model.score_batch(a, [16000] * 3) == model.score_batch(a, [16000] * 3) | |
| def test_sign_bonafide_higher(model): | |
| import pyarrow.parquet as pq | |
| import soundfile as sf | |
| d = os.path.join(os.path.dirname(P.__file__), "..", "J-SPAW_LA", "data") | |
| t = pq.read_table(os.path.join(d, "test-00000-of-00001.parquet"), columns=["audio", "label"]).to_pylist() | |
| bona = [r for r in t if r["label"] == 0][:8] | |
| spoof = [r for r in t if r["label"] == 1][:8] | |
| dec = lambda rows: [sf.read(io.BytesIO(r["audio"]["bytes"]), dtype="float32")[0] for r in rows] | |
| sb = model.score_batch(dec(bona), [16000] * 8) | |
| ss = model.score_batch(dec(spoof), [16000] * 8) | |
| assert np.mean(sb) > np.mean(ss) | |