File size: 2,235 Bytes
d79e350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Shared AST (Audio Spectrogram Transformer) encoder — used IDENTICALLY in training
and in the Hugging Face Space so an uploaded clip is embedded exactly as the
training clips were.

We take a pretrained AST (MIT/ast-finetuned-audioset-10-10-0.4593), a state-of-the-art
audio transformer, and use it as a frozen feature extractor: each ~2-minute hive
recording is summarised by the MEAN of the AST pooled embeddings over N evenly
spaced 10.24 s windows -> a single 768-d audio vector.
"""
import numpy as np

AST_MODEL = "MIT/ast-finetuned-audioset-10-10-0.4593"
SR = 16000
WIN_SAMPLES = int(10.24 * SR)   # 163840 -> AST's native 1024 frames
N_WINDOWS = 4
EMB_DIM = 768


def load_ast(device=None):
    import torch
    from transformers import ASTFeatureExtractor, ASTModel
    if device is None:
        device = "mps" if torch.backends.mps.is_available() else (
            "cuda" if torch.cuda.is_available() else "cpu")
    fe = ASTFeatureExtractor.from_pretrained(AST_MODEL)
    model = ASTModel.from_pretrained(AST_MODEL).to(device).eval()
    return fe, model, device


def _windows(y):
    """Return a list of fixed-length windows covering the clip."""
    n = len(y)
    if n <= WIN_SAMPLES:
        return [np.pad(y, (0, WIN_SAMPLES - n))]
    centers = np.linspace(WIN_SAMPLES // 2, n - WIN_SAMPLES // 2, N_WINDOWS)
    segs = []
    for c in centers:
        s = int(c - WIN_SAMPLES // 2)
        segs.append(y[s:s + WIN_SAMPLES])
    return segs


def encode_waveform(y, sr, fe, model, device):
    """waveform -> 768-d AST embedding (mean over windows)."""
    import torch
    import librosa
    if sr != SR:
        y = librosa.resample(np.asarray(y, dtype=np.float32), orig_sr=sr, target_sr=SR)
    y = np.asarray(y, dtype=np.float32)
    if y.ndim > 1:
        y = y.mean(axis=0)
    segs = _windows(y)
    with torch.no_grad():
        inp = fe(segs, sampling_rate=SR, return_tensors="pt")
        out = model(inp["input_values"].to(device))
        emb = out.pooler_output.mean(dim=0).cpu().numpy()   # (768,)
    return emb.astype(np.float32)


def encode_file(path, fe, model, device):
    import librosa
    y, sr = librosa.load(path, sr=SR, mono=True)
    return encode_waveform(y, sr, fe, model, device)