-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
466 lines (382 loc) · 16.8 KB
/
Copy pathclassifier.py
File metadata and controls
466 lines (382 loc) · 16.8 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
"""
Classifier module voor mammal-watcher.
"""
from __future__ import annotations
import csv
from abc import ABC, abstractmethod
from pathlib import Path
import numpy as np
class BaseClassifier(ABC):
"""Abstracte basisklasse voor alle zoogdier-classifiers.
Elke concrete implementatie moet ``classify`` implementeren en een
dict teruggeven dat voldoet aan het payload-schema van mammal-watcher.
"""
@abstractmethod
def classify(self, audio: np.ndarray, sr: int) -> dict:
"""Classificeer een audio-fragment.
Parameters
----------
audio:
Mono audio-samples als numpy array (float32 of float64).
sr:
Sample-rate in Hz (bijv. 48000).
Returns
-------
dict met de sleutels:
species_scientific, species_nl, species_en, confidence, tier,
model_version
"""
class MammalCNNClassifier(BaseClassifier):
"""Classifier op basis van het lokaal getrainde MammalCNN model (mammal_cnn.pt)."""
MODEL_VERSION = "mammal-cnn-1.0"
TARGET_SR = 16000
CLIP_SECONDS = 10
MEL_PARAMS = {"sample_rate": 16000, "n_mels": 64, "n_fft": 1024, "hop_length": 512}
def __init__(
self,
model_path: str = "models/mammal_cnn.pt",
min_confidence: float = 0.1,
species_csv_path: str = "species_mammals_nl.csv",
) -> None:
import torch
import torchaudio
self.min_confidence = float(min_confidence)
self._torch = torch
self._torchaudio = torchaudio
self._species_lookup = self._load_species_lookup(species_csv_path)
checkpoint = torch.load(model_path, map_location="cpu", weights_only=False)
class_mapping = checkpoint["class_mapping"]
self._idx_to_class: dict[int, str] = {int(idx): str(slug) for idx, slug in class_mapping.items()}
self._mel_params: dict[str, int] = {
**self.MEL_PARAMS,
**checkpoint.get("mel_params", self.MEL_PARAMS),
}
num_classes = len(self._idx_to_class)
self._model = self._build_model(num_classes)
self._model.load_state_dict(checkpoint["model_state_dict"])
self._model.eval()
self._mel_transform = torchaudio.transforms.MelSpectrogram(
sample_rate=self._mel_params["sample_rate"],
n_mels=self._mel_params["n_mels"],
n_fft=self._mel_params["n_fft"],
hop_length=self._mel_params["hop_length"],
)
self._to_db = torchaudio.transforms.AmplitudeToDB(stype="power")
@staticmethod
def _build_model(num_classes: int):
import torch.nn as nn
class MammalCNN(nn.Module):
def __init__(self, n_classes: int) -> None:
super().__init__()
self.features = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=3, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(16, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(64, 96, kernel_size=3, padding=1),
nn.BatchNorm2d(96),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d((1, 1)),
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Dropout(p=0.4),
nn.Linear(96, n_classes),
)
def forward(self, x):
x = self.features(x)
return self.classifier(x)
return MammalCNN(num_classes)
@staticmethod
def _slug_to_scientific(slug: str) -> str:
scientific = slug.strip().replace("_", " ").lower()
if not scientific:
return "Unknown species"
return scientific.capitalize()
@staticmethod
def _load_species_lookup(path: str) -> dict[str, dict[str, str]]:
csv_path = Path(path)
if not csv_path.exists():
return {}
lookup: dict[str, dict[str, str]] = {}
with open(csv_path, newline="", encoding="utf-8") as fh:
for row in csv.DictReader(fh):
key = row.get("scientific_name", "").strip().lower()
if key:
lookup[key] = row
return lookup
def _preprocess_audio(self, audio: np.ndarray, sr: int):
samples = np.asarray(audio, dtype=np.float32).reshape(-1)
if samples.size == 0 or sr <= 0:
return None
peak = float(np.max(np.abs(samples)))
if peak > 0.0:
samples = samples / peak
waveform = self._torch.from_numpy(samples)
if sr != self.TARGET_SR:
waveform = self._torchaudio.functional.resample(
waveform,
orig_freq=sr,
new_freq=self.TARGET_SR,
)
expected_samples = self.TARGET_SR * self.CLIP_SECONDS
if waveform.shape[0] > expected_samples:
waveform = waveform[:expected_samples]
elif waveform.shape[0] < expected_samples:
waveform = self._torch.nn.functional.pad(waveform, (0, expected_samples - waveform.shape[0]))
return waveform.unsqueeze(0)
def _resolve_species_meta(self, slug: str) -> tuple[str, str, str, int]:
scientific = self._slug_to_scientific(slug)
row = self._species_lookup.get(scientific.lower(), {})
nl_name = str(row.get("nl_name", "")).strip() or slug.replace("_", " ")
en_name = str(row.get("en_name", "")).strip() or scientific
try:
tier = int(row.get("tier", 3))
except (TypeError, ValueError):
tier = 3
return scientific, nl_name, en_name, tier
def classify(self, audio: np.ndarray, sr: int) -> dict | None:
waveform = self._preprocess_audio(audio, sr)
if waveform is None:
return None
mel = self._mel_transform(waveform)
mel_db = self._to_db(mel).to(dtype=self._torch.float32)
model_input = mel_db.unsqueeze(0)
with self._torch.no_grad():
logits = self._model(model_input)
probabilities = self._torch.softmax(logits, dim=1).squeeze(0)
best_score, best_idx = self._torch.max(probabilities, dim=0)
confidence = float(best_score.item())
if confidence < self.min_confidence:
return None
class_idx = int(best_idx.item())
slug = self._idx_to_class.get(class_idx, "unknown_species")
scientific, nl_name, en_name, tier = self._resolve_species_meta(slug)
return {
"species_scientific": scientific,
"species_nl": nl_name,
"species_en": en_name,
"confidence": round(float(np.clip(confidence, 0.0, 1.0)), 4),
"tier": int(tier),
"model_version": self.MODEL_VERSION,
}
class BirdNetMLPClassifier(BaseClassifier):
"""Classifier op basis van BirdNET embeddings + kleine PyTorch MLP (mammal_mlp.pt)."""
MODEL_VERSION = "birdnet-mlp-1.0"
TARGET_SR = 16000
EMBEDDING_DIM = 1024
def __init__(
self,
model_path: str = "models/mammal_mlp.pt",
min_confidence: float = 0.1,
species_csv_path: str = "species_mammals_nl.csv",
) -> None:
import torch
self.min_confidence = float(min_confidence)
self._torch = torch
self._species_lookup = MammalCNNClassifier._load_species_lookup(species_csv_path)
checkpoint = torch.load(model_path, map_location="cpu", weights_only=False)
class_mapping = checkpoint["class_mapping"]
self._idx_to_class: dict[int, str] = {int(idx): str(slug) for idx, slug in class_mapping.items()}
self._input_dim: int = int(checkpoint.get("input_dim", self.EMBEDDING_DIM))
num_classes = len(self._idx_to_class)
self._model = self._build_model(self._input_dim, num_classes)
self._model.load_state_dict(checkpoint["model_state_dict"])
self._model.eval()
self._extract_fn = self._load_extractor()
@staticmethod
def _build_model(input_dim: int, num_classes: int):
import torch.nn as nn
class MammalMLP(nn.Module):
def __init__(self, in_dim: int, n_classes: int) -> None:
super().__init__()
self.net = nn.Sequential(
nn.Linear(in_dim, 512),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Linear(256, n_classes),
)
def forward(self, x):
return self.net(x)
return MammalMLP(input_dim, num_classes)
def _load_extractor(self):
"""Laad BirdNET feature extractor via birdnetlib."""
try:
from birdnetlib import Recording
from birdnetlib.analyzer import Analyzer
analyzer = Analyzer()
def extract_birdnet(audio: np.ndarray, sr: int) -> np.ndarray:
import os
import tempfile
import soundfile as sf
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
tmp_path = tmp.name
samples = audio.astype(np.float32)
sf.write(tmp_path, samples, sr)
try:
recording = Recording(analyzer, tmp_path, lat=52.0, lon=5.0, min_conf=0.0)
recording.analyze()
if recording.embeddings is not None and len(recording.embeddings) > 0:
emb = np.mean(np.array(recording.embeddings, dtype=np.float32), axis=0)
else:
emb = np.zeros(self.EMBEDDING_DIM, dtype=np.float32)
finally:
try:
os.unlink(tmp_path)
except OSError:
pass
return emb.reshape(self.EMBEDDING_DIM).astype(np.float32)
return extract_birdnet
except ImportError as exc:
raise RuntimeError(
"BirdNetMLPClassifier vereist birdnetlib met TensorFlow Lite ondersteuning "
"(installeer tensorflow-cpu)."
) from exc
def _resolve_species_meta(self, slug: str) -> tuple[str, str, str, int]:
scientific = MammalCNNClassifier._slug_to_scientific(slug)
row = self._species_lookup.get(scientific.lower(), {})
nl_name = str(row.get("nl_name", "")).strip() or slug.replace("_", " ")
en_name = str(row.get("en_name", "")).strip() or scientific
try:
tier = int(row.get("tier", 3))
except (TypeError, ValueError):
tier = 3
return scientific, nl_name, en_name, tier
def classify(self, audio: np.ndarray, sr: int) -> dict | None:
samples = np.asarray(audio, dtype=np.float32).reshape(-1)
if samples.size == 0 or sr <= 0:
return None
peak = float(np.max(np.abs(samples)))
if peak > 0.0:
samples = samples / peak
try:
embedding = self._extract_fn(samples, sr)
except Exception: # noqa: BLE001
return None
tensor = self._torch.from_numpy(embedding).unsqueeze(0)
with self._torch.no_grad():
logits = self._model(tensor)
probabilities = self._torch.softmax(logits, dim=1).squeeze(0)
best_score, best_idx = self._torch.max(probabilities, dim=0)
confidence = float(best_score.item())
if confidence < self.min_confidence:
return None
class_idx = int(best_idx.item())
slug = self._idx_to_class.get(class_idx, "unknown_species")
# Background/ruis → geen detectie rapporteren
if slug == "background":
return None
scientific, nl_name, en_name, tier = self._resolve_species_meta(slug)
return {
"species_scientific": scientific,
"species_nl": nl_name,
"species_en": en_name,
"confidence": round(float(np.clip(confidence, 0.0, 1.0)), 4),
"tier": int(tier),
"model_version": self.MODEL_VERSION,
}
class YAMNetMLPClassifier(BaseClassifier):
"""Classifier op basis van YAMNet embeddings + kleine PyTorch MLP (mammal_mlp.pt).
YAMNet (Google, AudioSet) is general audio — in tegenstelling tot BirdNET
onderscheidt het zoogdieren, vogels, mens en omgevingsgeluid, wat een
betrouwbare background-klasse mogelijk maakt.
"""
MODEL_VERSION = "yamnet-mlp-1.0"
TARGET_SR = 16000
EMBEDDING_DIM = 1024
YAMNET_MODEL_URL = "https://tfhub.dev/google/yamnet/1"
def __init__(
self,
model_path: str = "models/mammal_mlp.pt",
min_confidence: float = 0.1,
species_csv_path: str = "species_mammals_nl.csv",
) -> None:
import torch
self.min_confidence = float(min_confidence)
self._torch = torch
self._species_lookup = MammalCNNClassifier._load_species_lookup(species_csv_path)
checkpoint = torch.load(model_path, map_location="cpu", weights_only=False)
class_mapping = checkpoint["class_mapping"]
self._idx_to_class: dict[int, str] = {int(idx): str(slug) for idx, slug in class_mapping.items()}
self._input_dim: int = int(checkpoint.get("input_dim", self.EMBEDDING_DIM))
num_classes = len(self._idx_to_class)
self._model = BirdNetMLPClassifier._build_model(self._input_dim, num_classes)
self._model.load_state_dict(checkpoint["model_state_dict"])
self._model.eval()
self._yamnet = self._load_yamnet()
def _load_yamnet(self):
try:
import tensorflow_hub as hub
except ImportError as exc:
raise RuntimeError(
"YAMNetMLPClassifier vereist tensorflow-hub. "
"Installeer via: pip install tensorflow-hub"
) from exc
return hub.load(self.YAMNET_MODEL_URL)
def _extract_embedding(self, audio: np.ndarray, sr: int) -> np.ndarray:
import tensorflow as tf
samples = audio.reshape(-1).astype(np.float32)
if sr != self.TARGET_SR:
try:
import librosa
samples = librosa.resample(samples, orig_sr=sr, target_sr=self.TARGET_SR)
except Exception: # noqa: BLE001
return np.zeros(self.EMBEDDING_DIM, dtype=np.float32)
peak = float(np.max(np.abs(samples)))
if peak > 0.0:
samples = samples / peak
waveform = tf.constant(samples, dtype=tf.float32)
_, embeddings, _ = self._yamnet(waveform)
return np.asarray(embeddings).mean(axis=0).astype(np.float32)
def _resolve_species_meta(self, slug: str) -> tuple[str, str, str, int]:
scientific = MammalCNNClassifier._slug_to_scientific(slug)
row = self._species_lookup.get(scientific.lower(), {})
nl_name = str(row.get("nl_name", "")).strip() or slug.replace("_", " ")
en_name = str(row.get("en_name", "")).strip() or scientific
try:
tier = int(row.get("tier", 3))
except (TypeError, ValueError):
tier = 3
return scientific, nl_name, en_name, tier
def classify(self, audio: np.ndarray, sr: int) -> dict | None:
samples = np.asarray(audio, dtype=np.float32).reshape(-1)
if samples.size == 0 or sr <= 0:
return None
try:
embedding = self._extract_embedding(samples, sr)
except Exception: # noqa: BLE001
return None
tensor = self._torch.from_numpy(embedding).unsqueeze(0)
with self._torch.no_grad():
logits = self._model(tensor)
probabilities = self._torch.softmax(logits, dim=1).squeeze(0)
best_score, best_idx = self._torch.max(probabilities, dim=0)
confidence = float(best_score.item())
if confidence < self.min_confidence:
return None
class_idx = int(best_idx.item())
slug = self._idx_to_class.get(class_idx, "unknown_species")
if slug in ("background", "unknown_species"):
return None
scientific, nl_name, en_name, tier = self._resolve_species_meta(slug)
return {
"species_scientific": scientific,
"species_nl": nl_name,
"species_en": en_name,
"confidence": round(float(np.clip(confidence, 0.0, 1.0)), 4),
"tier": int(tier),
"model_version": self.MODEL_VERSION,
}