RawNetLite / rawnetlite.py
korallll's picture
Wrapper code (speech-spoof-bench)
7fbbfd9 verified
Raw History Blame Contribute Delete
1.9 kB
from __future__ import annotations
import os
import numpy as np
import torch
from speech_spoof_bench.model import AntiSpoofingModel
from _net import TARGET_SAMPLES, RawNetLite as RawNetLiteNet, prep_np
_DIR = os.path.dirname(os.path.abspath(__file__))
# github.com/adipiz99/RawNetLite @ fcb0fe8d0f40146d110e322c00a0723ba27d5520, models/triple_cross_domain_focal_rawnet_lite.pt
# sha256 6154d91f34f0a49d58e30bac602915f6b6853419419804be63b5274603891a9b -- plain state_dict (61 tensors), MIT
_CKPT = os.path.join(_DIR, "triple_cross_domain_focal_rawnet_lite.pt")
def load_state(path=_CKPT):
return torch.load(path, map_location="cpu", weights_only=True)
def build_net(path=_CKPT):
net = RawNetLiteNet()
r = net.load_state_dict(load_state(path), strict=True) # all 61 tensors used, 0 missing / 0 unexpected
assert not r.missing_keys and not r.unexpected_keys
return net
class RawNetLite(AntiSpoofingModel):
name = "RawNetLite"
expected_sample_rate = 16000
batch_size = 1 # sweep 2026-11-09 (48000-sample input, RTX 5060 Ti idx 3): 238 utt/s @1, ~172-183 @8-192 (4070 not swept)
def load(self) -> None:
self.device = "cuda" if torch.cuda.is_available() else "cpu"
torch.backends.cudnn.allow_tf32 = False
torch.backends.cuda.matmul.allow_tf32 = False
self.net = build_net().to(self.device).eval()
@torch.no_grad()
def score_batch(self, audios, srs):
x = torch.from_numpy(np.stack([prep_np(a, TARGET_SAMPLES) for a in audios]))[:, None, :].to(self.device)
logit_fake = self.net.forward_logit(x)[:, 0].float()
# source: sigmoid(logit) = P(fake) (label 1 = fake); -logit is the same ranking without sigmoid saturation
return (-logit_fake).cpu().tolist()
def unload(self) -> None:
self.net = None
if torch.cuda.is_available():
torch.cuda.empty_cache()