metrics
birdnet_stm32.evaluation.metrics
¶
Evaluation metrics and per-file inference pipeline.
ModelRunner
¶
Bases: Protocol
Inference interface shared by Keras and TFLite runners.
Source code in birdnet_stm32/evaluation/metrics.py
class_average_precision(labels, scores)
¶
Exact AP in output order; classes without positives contribute zero.
Keeping the full class contract prevents a sparse validation subset from silently changing the macro metric. All-positive classes have AP one.
Source code in birdnet_stm32/evaluation/metrics.py
macro_cmap(labels, scores)
¶
Class-macro average precision, shared by training and evaluation.
make_chunks_for_file(path, cfg, frontend, mag_scale, n_fft, chunk_overlap)
¶
Build a list of model-ready inputs from one audio file by chunking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Audio file path. |
required |
cfg
|
dict
|
Training config dict (sample_rate, chunk_duration, num_mels, spec_width). |
required |
frontend
|
str
|
'librosa'|'hybrid'|'raw'. |
required |
mag_scale
|
str
|
Magnitude scaling for precomputed paths. |
required |
n_fft
|
int
|
FFT length. |
required |
chunk_overlap
|
float
|
Overlap fraction (seconds) between consecutive chunks. |
required |
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of per-chunk inputs ready for model.predict. |
Source code in birdnet_stm32/evaluation/metrics.py
evaluate(model_runner, files, classes, cfg, pooling='average', batch_size=64, overlap=0.0, mep_beta=10.0, measure_latency=False, profile_memory=False, num_workers=None)
¶
Run inference per chunk, pool to file-level, and compute metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_runner
|
ModelRunner
|
Runner exposing predict(x_batch). |
required |
files
|
list[str]
|
List of file paths to evaluate. |
required |
classes
|
list[str]
|
Ordered class names. |
required |
cfg
|
dict
|
Training config dict. |
required |
pooling
|
str
|
'avg'|'max'|'lme' pooling method. |
'average'
|
batch_size
|
int
|
Batch size for chunk inference. |
64
|
overlap
|
float
|
Overlap in seconds for chunking. |
0.0
|
mep_beta
|
float
|
Temperature for LME pooling. |
10.0
|
measure_latency
|
bool
|
If True, measure per-chunk inference latency. |
False
|
profile_memory
|
bool
|
If True, report peak RSS during inference. |
False
|
num_workers
|
int | None
|
Threads that decode and preprocess files ahead of inference (default: up to 8). |
None
|
Returns:
| Type | Description |
|---|---|
tuple[dict, list[dict], ndarray, ndarray]
|
Tuple of (metrics dict, per_file list, y_true array, y_scores array). |
Source code in birdnet_stm32/evaluation/metrics.py
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 | |
optimize_thresholds(y_true, y_scores, classes)
¶
Find per-class thresholds that maximize F1 using the precision-recall curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
ndarray
|
Binary ground-truth array of shape |
required |
y_scores
|
ndarray
|
Predicted scores of shape |
required |
classes
|
list[str]
|
Class name list matching the column order. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary mapping each class name to its optimal threshold. |
Source code in birdnet_stm32/evaluation/metrics.py
bootstrap_ap_ci(y_true, y_scores, classes, n_bootstrap=1000, confidence=0.95, seed=42)
¶
Compute per-class AP with bootstrap confidence intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
ndarray
|
Binary ground-truth array |
required |
y_scores
|
ndarray
|
Predicted scores |
required |
classes
|
list[str]
|
Class name list matching column order. |
required |
n_bootstrap
|
int
|
Number of bootstrap resamples. |
1000
|
confidence
|
float
|
Confidence level (e.g. 0.95 for 95% CI). |
0.95
|
seed
|
int
|
Random seed for reproducibility. |
42
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dicts, one per class: |
list[dict]
|
``{"class": str, "ap": float, "ci_lower": float, "ci_upper": float, |
list[dict]
|
"n_positive": int, "n_total": int}``. |
Source code in birdnet_stm32/evaluation/metrics.py
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 | |
compute_det_curve(y_true, y_scores)
¶
Compute Detection Error Tradeoff (DET) curve points.
The DET curve plots false rejection rate (FRR = 1 - recall) against false acceptance rate (FAR = FPR) across thresholds. Standard in bioacoustics evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
ndarray
|
Binary ground-truth (flattened or 2-D, treated as binary). |
required |
y_scores
|
ndarray
|
Predicted scores (same shape as y_true). |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
Tuple (far, frr, thresholds) — arrays of equal length. |