Download source_parity.py from SpeechAntiSpoofingBenchmarks/Forensics-XLSR-Wild: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/SpeechAntiSpoofingBenchmarks/Forensics-XLSR-Wild/resolve/main/source_parity.py
- Command line
-
hf download hf://SpeechAntiSpoofingBenchmarks/Forensics-XLSR-Wild/source_parity.py
-
curl -L -o source_parity.py https://huggingface.co/SpeechAntiSpoofingBenchmarks/Forensics-XLSR-Wild/resolve/main/source_parity.py
1.86 kB
| # Usage: CUDA_VISIBLE_DEVICES=2 python source_parity.py <dir of 16 kHz wav clips> | |
| # Compare the author's model.py + inference.py (from_pretrained base, strict=False) to our wrapper. | |
| import os, sys, glob, importlib.util, numpy as np, torch, soundfile as sf | |
| os.environ["HF_HUB_OFFLINE"] = "1" | |
| assert "4070" in torch.cuda.get_device_name() | |
| SRC = "/home/kirill/speech-spoof-bench/benchmarks/_ckpt_dl/eliya__forensics_0.3B_xlsr_wild_deepfake_classifier" | |
| MD = "/home/kirill/speech-spoof-bench/benchmarks/Forensics-XLSR-Wild" | |
| def imp(name, path): | |
| spec = importlib.util.spec_from_file_location(name, path); m = importlib.util.module_from_spec(spec) | |
| sys.modules[name] = m; spec.loader.exec_module(m); return m | |
| srcmodel = imp("model", f"{SRC}/model.py") | |
| srcinf = imp("inference", f"{SRC}/inference.py") | |
| from safetensors.torch import load_file | |
| ref = srcmodel.DeepfakeDetector().cuda().eval() | |
| r = ref.load_state_dict(load_file(f"{SRC}/checkpoint_epoch_4.safetensors"), strict=False) | |
| print("source strict=False:", "missing", r.missing_keys, "unexpected", r.unexpected_keys) | |
| sys.path.insert(0, MD) | |
| import forensics_xlsr_wild as F | |
| m = F.ForensicsXLSRWild(); m.load() | |
| print("params", sum(p.numel() for p in m.net.parameters()), "buffers+params", sum(t.numel() for t in m.net.state_dict().values())) | |
| files = sorted(glob.glob(sys.argv[1] + "/**/*.wav", recursive=True))[:12] + sorted(glob.glob(sys.argv[1] + "/**/*.flac", recursive=True))[:12] | |
| print("n files", len(files)) | |
| a, b = [], [] | |
| for f in files: | |
| x = srcinf.load_audio(f) | |
| a.append(ref(x.unsqueeze(0).cuda()).float().item()) | |
| w, sr = sf.read(f, dtype="float32"); assert sr == 16000, sr | |
| if w.ndim > 1: w = w.mean(1) | |
| b.append(m.score_batch([w], [16000])[0]) | |
| a, b = np.array(a), np.array(b) | |
| print("src", np.round(a, 4)); print("ours", np.round(b, 4)); print("max abs diff", np.abs(a - b).max()) | |