"""Shared schema + helpers for the unified drone-acoustic dataset.""" import hashlib, io import numpy as np CLASSES = ["drone_buzz", "vehicle_mechanical", "explosion", "ambient_outdoor", "unknown"] CLASS_ID = {c: i for i, c in enumerate(CLASSES)} SPLITS = ["train", "validation", "test"] SPLIT_ID = {s: i for i, s in enumerate(SPLITS)} TARGET_SR = 16000 WINDOW_SEC = 5.0 WINDOW_SAMPLES = TARGET_SR * int(WINDOW_SEC) # 80000 SOURCE_REPOS = { "dads": "geronimobasso/drone-audio-detection-samples", "droneaudioset": "ahlab-drone-project/DroneAudioSet", "itu-aris": "imm61/itu-aris-lab-acoustic-drone-dataset", "bucket-drone": "buckets/yaroslav2005/drone-datasets-bucket (drone/)", "didgeridoo": "buckets/yaroslav2005/drone-datasets-bucket (didgeridoo-earth-drone-v2/)", "drone-test2": "buckets/yaroslav2005/drone-datasets-bucket (drone_all_test_datasets_2/)", "dronedata": "buckets/yaroslav2005/drone-datasets-bucket (DroneData/data.zip)", "challenge5": "local: challenge-5-acoustic-visual-cuas", "zenodo-15190811": "zenodo:15190811 (Miesikowska UAV+speech, CC-BY-4.0)", "uavirbase": "zenodo:15391924 (UaVirBASE, CC-BY-4.0)", "shared": "dataverse.harvard.edu doi:10.7910/DVN/ROWODP (SHAReD smartphone high-explosive recordings, CC0) + INL extended", "esc50": "github.com/karolpiczak/ESC-50 (ESC-50, CC-BY-NC-3.0) — used by PMC11511011 (Sensors 24(21):6688)", "audioset": "research.google.com/audioset via HF agkphysics/AudioSet (CC-BY-4.0) — explosion & vehicle segments", } def assign_split(source: str, group_id: str) -> str: """Deterministic group-disjoint split for external sources: 90/5/5 by group hash.""" if source == "challenge5": raise ValueError("challenge5 uses its official split") h = int(hashlib.md5(f"{source}|{group_id}".encode()).hexdigest(), 16) % 100 return "train" if h < 90 else ("validation" if h < 95 else "test") def resample(x: np.ndarray, sr: int) -> np.ndarray: if sr == TARGET_SR: return x.astype(np.float32) from scipy.signal import resample_poly from math import gcd g = gcd(TARGET_SR, sr) return resample_poly(x.astype(np.float64), TARGET_SR // g, sr // g).astype(np.float32) def wav_bytes(x: np.ndarray, sr: int = TARGET_SR) -> bytes: import soundfile as sf buf = io.BytesIO() sf.write(buf, x, sr, format="WAV", subtype="PCM_16") return buf.getvalue() def read_audio_any(path_or_bytes): import soundfile as sf if isinstance(path_or_bytes, (bytes, bytearray)): return sf.read(io.BytesIO(path_or_bytes), dtype="float32") return sf.read(path_or_bytes, dtype="float32") # ---- log-mel spectrogram 224x224 grayscale PNG, params matched to the challenge pack ---- # Verified against dataset spectrograms/*.png (corr ~0.96, residual = PNG quantization): # librosa-style: hann window (periodic), n_fft=1024, hop=354, center=True reflect pad, # slaney mel scale with area norm, 224 mels, 0-8 kHz, power=2, 10*log10 dB with top_db=80, # per-image min-max, low frequency at the bottom, bilinear resize to 224x224, gray->RGB. _N_FFT = 1024 _HOP = 354 _N_MELS = 224 _FMAX = 8000.0 _TOP_DB = 80.0 def _mel_filterbank_slaney(sr, n_fft, n_mels, fmax): f_sp = 200.0 / 3.0 def hz2mel(f): f = np.asarray(f, dtype=np.float64) return np.where(f < 1000.0, f / f_sp, 15.0 + np.log(np.maximum(f, 1000.0) / 1000.0) / np.log(6.4) * 27.0) def mel2hz(m): m = np.asarray(m, dtype=np.float64) return np.where(m < 15.0, m * f_sp, 1000.0 * np.exp(np.log(6.4) / 27.0 * (m - 15.0))) mpts = np.linspace(hz2mel(0.0), hz2mel(fmax), n_mels + 2) freqs = mel2hz(mpts) fftfreqs = np.fft.rfftfreq(n_fft, 1.0 / sr) fb = np.zeros((n_mels, len(fftfreqs)), dtype=np.float64) for i in range(n_mels): l, c, r = freqs[i], freqs[i + 1], freqs[i + 2] fb[i] = np.maximum(0.0, np.minimum((fftfreqs - l) / max(c - l, 1e-9), (r - fftfreqs) / max(r - c, 1e-9))) fb *= (2.0 / (freqs[2:n_mels + 2] - freqs[:n_mels]))[:, None] # slaney area norm return fb _FB = _mel_filterbank_slaney(TARGET_SR, _N_FFT, _N_MELS, _FMAX) def _hann_periodic(n): return 0.5 - 0.5 * np.cos(2.0 * np.pi * np.arange(n) / n) _WIN = _hann_periodic(_N_FFT).astype(np.float32) def logmel_png(x: np.ndarray) -> bytes: """x: float32 mono at 16 kHz, any length (zero-padded to >=1 window) -> 224x224 RGB PNG bytes.""" from PIL import Image x = np.asarray(x, dtype=np.float32) if len(x) < WINDOW_SAMPLES: x = np.pad(x, (0, WINDOW_SAMPLES - len(x))) xp = np.pad(x, _N_FFT // 2, mode="reflect") # center=True frames = np.lib.stride_tricks.sliding_window_view(xp, _N_FFT)[::_HOP] spec = np.abs(np.fft.rfft(frames * _WIN, axis=1)) ** 2 # power spectrogram mel = spec @ _FB.T # (T, mels) db = 10.0 * np.log10(np.maximum(mel, 1e-10)).T # (mels, T) db = np.maximum(db, db.max() - _TOP_DB) db = db[::-1] # low freq at bottom lo, hi = db.min(), db.max() img = ((db - lo) / max(hi - lo, 1e-6) * 255.0).astype(np.uint8) im = Image.fromarray(img, mode="L").resize((224, 224), Image.BILINEAR).convert("RGB") buf = io.BytesIO() im.save(buf, format="PNG") return buf.getvalue() def make_windows(x: np.ndarray): """Yield (window_index, start_s, chunk) for non-overlapping 5 s windows. A trailing partial window is only emitted when it holds >0.25 s of audio, so a 5 s clip with a few extra samples still yields exactly one window. """ n = len(x) n_w = n // WINDOW_SAMPLES if n % WINDOW_SAMPLES > int(0.05 * WINDOW_SAMPLES): n_w += 1 n_w = max(1, n_w) for i in range(n_w): s0 = i * WINDOW_SAMPLES yield i, s0 / TARGET_SR, x[s0 : s0 + WINDOW_SAMPLES]