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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
library_name: pytorch
|
| 5 |
+
tags:
|
| 6 |
+
- audio
|
| 7 |
+
- audio-classification
|
| 8 |
+
- drone-detection
|
| 9 |
+
- pytorch
|
| 10 |
+
- crnn
|
| 11 |
+
- real-time
|
| 12 |
+
- edge-deployment
|
| 13 |
+
license: mit
|
| 14 |
+
datasets:
|
| 15 |
+
- geronimobasso/drone-audio-detection-samples
|
| 16 |
+
model-index:
|
| 17 |
+
- name: DroneClassifier
|
| 18 |
+
results:
|
| 19 |
+
- task:
|
| 20 |
+
type: audio-classification
|
| 21 |
+
name: Audio Classification
|
| 22 |
+
dataset:
|
| 23 |
+
name: Drone Audio Detection Samples
|
| 24 |
+
type: geronimobasso/drone-audio-detection-samples
|
| 25 |
+
metrics:
|
| 26 |
+
- type: f1
|
| 27 |
+
value: 0.9996
|
| 28 |
+
name: F1 Score (clean test set, 27 050 samples)
|
| 29 |
+
- type: accuracy
|
| 30 |
+
value: 0.9993
|
| 31 |
+
name: Accuracy (clean test set)
|
| 32 |
+
- type: roc_auc
|
| 33 |
+
value: 1.0
|
| 34 |
+
name: ROC AUC (clean test set)
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
# DroneClassifier β Real-Time Drone Audio Detection
|
| 38 |
+
|
| 39 |
+
A lightweight **CRNN** (Convolutional Recurrent Neural Network) for binary drone audio detection.
|
| 40 |
+
Runs in real time on a Raspberry Pi CM4 and produces a detection decision every **500 ms**.
|
| 41 |
+
|
| 42 |
+
Trained on [geronimobasso/drone-audio-detection-samples](https://huggingface.co/datasets/geronimobasso/drone-audio-detection-samples) β 180 320 clips, 16 kHz mono.
|
| 43 |
+
|
| 44 |
+
## Model Variants
|
| 45 |
+
|
| 46 |
+
Three checkpoint variants are included:
|
| 47 |
+
|
| 48 |
+
| File | Augmentation | Recommended use |
|
| 49 |
+
|---|---|---|
|
| 50 |
+
| `drone_classifier_aug_mixed.pt` | Brown noise + real ESC-50 wind (50 / 50) | **General purpose (recommended)** |
|
| 51 |
+
| `drone_classifier_aug_pw10.pt` | Brown noise only | Best at extreme real-wind SNR (β5 dB) |
|
| 52 |
+
| `drone_classifier_baseline.pt` | None | Clean-audio reference baseline |
|
| 53 |
+
|
| 54 |
+
## Performance
|
| 55 |
+
|
| 56 |
+
### Clean test set (27 050 held-out samples)
|
| 57 |
+
|
| 58 |
+
| Variant | Accuracy | Precision | Recall | F1 | ROC-AUC |
|
| 59 |
+
|---|---|---|---|---|---|
|
| 60 |
+
| aug_mixed **(recommended)** | 0.9993 | 0.9998 | 0.9995 | **0.9996** | **1.000** |
|
| 61 |
+
| aug_pw10 | 0.9989 | 0.9996 | 0.9992 | 0.9994 | 0.9999 |
|
| 62 |
+
| baseline | 0.9980 | 1.0000 | 0.9978 | 0.9989 | 0.9997 |
|
| 63 |
+
|
| 64 |
+
### Noise robustness β real ESC-50 wind clips (aug_mixed, never seen during training)
|
| 65 |
+
|
| 66 |
+
| SNR | Recall | F1 | Notes |
|
| 67 |
+
|---|---|---|---|
|
| 68 |
+
| +20 dB | 1.000 | 1.000 | |
|
| 69 |
+
| +10 dB | 0.996 | 0.998 | |
|
| 70 |
+
| +5 dB | 0.996 | 0.998 | |
|
| 71 |
+
| 0 dB | 0.996 | 0.998 | |
|
| 72 |
+
| β5 dB | 0.860 | 0.925 | wind 1.8Γ louder than drone |
|
| 73 |
+
|
| 74 |
+
The baseline model collapses at 0 dB SNR (F1 = 0.353); aug_mixed stays at F1 β₯ 0.998 down to 0 dB.
|
| 75 |
+
|
| 76 |
+
Evaluation images are in the `eval/` folder of this repo.
|
| 77 |
+
|
| 78 |
+
## Usage
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
import torch
|
| 82 |
+
import torchaudio
|
| 83 |
+
import torchaudio.transforms as T
|
| 84 |
+
from huggingface_hub import hf_hub_download
|
| 85 |
+
|
| 86 |
+
# ββ 1. Download weights ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 87 |
+
ckpt_path = hf_hub_download(
|
| 88 |
+
repo_id="AntoineNaccache/drone-audio-detector",
|
| 89 |
+
filename="drone_classifier_aug_mixed.pt",
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# ββ 2. Define or import the model βββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
+
# Option A: download model.py from the repo and place it next to your script,
|
| 94 |
+
# then: from model import DroneClassifier, load_classifier
|
| 95 |
+
#
|
| 96 |
+
# Option B: inline definition (copy from model.py in this repo)
|
| 97 |
+
import torch.nn as nn
|
| 98 |
+
|
| 99 |
+
def _conv_block(in_ch, out_ch):
|
| 100 |
+
return nn.Sequential(
|
| 101 |
+
nn.Conv2d(in_ch, out_ch, 3, padding=1, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(True),
|
| 102 |
+
nn.Conv2d(out_ch, out_ch, 3, padding=1, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(True),
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
class DroneClassifier(nn.Module):
|
| 106 |
+
def __init__(self):
|
| 107 |
+
super().__init__()
|
| 108 |
+
self.enc1 = _conv_block(1, 32); self.pool1 = nn.MaxPool2d(2, 2)
|
| 109 |
+
self.enc2 = _conv_block(32, 64); self.pool2 = nn.MaxPool2d(2, 2)
|
| 110 |
+
self.enc3 = _conv_block(64, 128); self.pool3 = nn.MaxPool2d((2, 1), (2, 1))
|
| 111 |
+
self.gru = nn.GRU(1024, 128, num_layers=2, batch_first=True,
|
| 112 |
+
bidirectional=True, dropout=0.2)
|
| 113 |
+
self.head = nn.Sequential(nn.Linear(256, 64), nn.ReLU(True),
|
| 114 |
+
nn.Dropout(0.3), nn.Linear(64, 1))
|
| 115 |
+
def forward(self, x):
|
| 116 |
+
x = self.pool1(self.enc1(x))
|
| 117 |
+
x = self.pool2(self.enc2(x))
|
| 118 |
+
x = self.pool3(self.enc3(x))
|
| 119 |
+
B, C, F, T = x.shape
|
| 120 |
+
x, _ = self.gru(x.permute(0, 3, 1, 2).reshape(B, T, C * F))
|
| 121 |
+
return self.head(x.mean(1))
|
| 122 |
+
|
| 123 |
+
# ββ 3. Load checkpoint βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 124 |
+
model = DroneClassifier()
|
| 125 |
+
state = torch.load(ckpt_path, map_location="cpu", weights_only=False)
|
| 126 |
+
if "model_state_dict" in state:
|
| 127 |
+
state = state["model_state_dict"]
|
| 128 |
+
model.load_state_dict(state, strict=False)
|
| 129 |
+
model.eval()
|
| 130 |
+
|
| 131 |
+
# ββ 4. Prepare a 1-second audio chunk (16 kHz mono) βββββββββββββββββοΏ½οΏ½οΏ½βββββ
|
| 132 |
+
waveform, sr = torchaudio.load("drone.wav")
|
| 133 |
+
if sr != 16_000:
|
| 134 |
+
waveform = torchaudio.functional.resample(waveform, sr, 16_000)
|
| 135 |
+
waveform = waveform.mean(0, keepdim=True)[:, :16_000] # mono, 1 s
|
| 136 |
+
|
| 137 |
+
mel_transform = T.MelSpectrogram(
|
| 138 |
+
sample_rate=16_000, n_fft=512, hop_length=160,
|
| 139 |
+
n_mels=64, f_min=50, f_max=5_500,
|
| 140 |
+
)
|
| 141 |
+
log_mel = (T.AmplitudeToDB()(mel_transform(waveform)) + 40) / 40 # β [-1, 1]
|
| 142 |
+
x = log_mel.unsqueeze(0) # (1, 1, 64, T)
|
| 143 |
+
|
| 144 |
+
# ββ 5. Infer βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 145 |
+
with torch.no_grad():
|
| 146 |
+
prob = torch.sigmoid(model(x)).item()
|
| 147 |
+
|
| 148 |
+
print(f"Drone probability: {prob:.3f}")
|
| 149 |
+
print("DRONE DETECTED" if prob >= 0.5 else "No drone")
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
For streaming (live microphone β GPIO trigger) and full inference pipelines, see the
|
| 153 |
+
[source repository](https://github.com/AntoineNaccache/Drone_propellers_recognition).
|
| 154 |
+
|
| 155 |
+
## Architecture
|
| 156 |
+
|
| 157 |
+
```
|
| 158 |
+
Input: log-mel spectrogram (B, 1, 64, T) T β 101 frames for 1 second
|
| 159 |
+
β
|
| 160 |
+
SharedEncoder
|
| 161 |
+
ββ ConvBlock(1β32) MaxPool(2Γ2) β (B, 32, 32, T/2)
|
| 162 |
+
ββ ConvBlock(32β64) MaxPool(2Γ2) β (B, 64, 16, T/2)
|
| 163 |
+
ββ ConvBlock(64β128) MaxPool(2Γ1) β (B, 128, 8, T/2)
|
| 164 |
+
ββ reshape β (B, T/2, 1024)
|
| 165 |
+
BiGRU(1024 β 256, 2 layers, bidirectional, dropout=0.2)
|
| 166 |
+
β (B, T/2, 256)
|
| 167 |
+
β
|
| 168 |
+
ClassifierHead
|
| 169 |
+
GlobalAvgPool(time) β (B, 256)
|
| 170 |
+
FC(256β64) β ReLU β Dropout(0.3) β FC(64β1) β logit
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
| Component | Parameters | Share |
|
| 174 |
+
|---|---|---|
|
| 175 |
+
| SharedEncoder β CNN | 286 880 | 19.3% |
|
| 176 |
+
| SharedEncoder β BiGRU | 1 182 720 | 79.6% |
|
| 177 |
+
| ClassifierHead | 16 513 | 1.1% |
|
| 178 |
+
| **Total** | **1 486 113** | |
|
| 179 |
+
|
| 180 |
+
Checkpoint size: ~5.94 MB (FP32). Quantised INT8 ONNX: ~1.49 MB.
|
| 181 |
+
|
| 182 |
+
## Audio Front-End
|
| 183 |
+
|
| 184 |
+
| Parameter | Value |
|
| 185 |
+
|---|---|
|
| 186 |
+
| Sample rate | 16 kHz |
|
| 187 |
+
| FFT window | Hann, 512 samples (32 ms) |
|
| 188 |
+
| Hop length | 160 samples (10 ms) |
|
| 189 |
+
| Mel bins | 64 |
|
| 190 |
+
| Frequency range | 50 β 5 500 Hz |
|
| 191 |
+
| Normalisation | `(AmplitudeToDB + 40) / 40` |
|
| 192 |
+
| Chunk duration | 1 second |
|
| 193 |
+
| Detection cadence | 500 ms (50% overlap) |
|
| 194 |
+
|
| 195 |
+
## Training Details
|
| 196 |
+
|
| 197 |
+
- **Dataset:** 180 320 clips, 16 kHz mono, stratified 70 / 15 / 15 split
|
| 198 |
+
- **Loss:** `BCEWithLogitsLoss(pos_weight=0.102)` β compensates 10:1 drone-heavy imbalance
|
| 199 |
+
- **Optimiser:** Adam, lr = 1e-3, weight decay = 1e-4, 30 epochs
|
| 200 |
+
- **Schedule:** CosineAnnealingLR
|
| 201 |
+
- **Augmentation (aug_mixed):** Brown noise (1/fΒ²) or real ESC-50 wind clips, random SNR 0 β 20 dB, p = 0.5
|
| 202 |
+
|
| 203 |
+
## Limitations
|
| 204 |
+
|
| 205 |
+
- Evaluated only on the `geronimobasso/drone-audio-detection-samples` distribution
|
| 206 |
+
- Not validated against helicopters, fixed-wing aircraft, or other rotary-wing craft
|
| 207 |
+
- Recall drops to ~86 β 89% when wind is 1.8Γ louder than the drone (SNR < β5 dB)
|
| 208 |
+
- Designed for 16 kHz microphone input; other sample rates require resampling
|
| 209 |
+
|
| 210 |
+
## Citation
|
| 211 |
+
|
| 212 |
+
```bibtex
|
| 213 |
+
@misc{naccache2025droneclassifier,
|
| 214 |
+
author = {Antoine Naccache},
|
| 215 |
+
title = {DroneClassifier: Real-Time Drone Audio Detection with CRNN},
|
| 216 |
+
year = {2025},
|
| 217 |
+
publisher = {Hugging Face},
|
| 218 |
+
url = {https://huggingface.co/AntoineNaccache/drone-audio-detector}
|
| 219 |
+
}
|
| 220 |
+
```
|