birdnet-onnx
A Rust library for running inference on BirdNET and Perch ONNX models with CUDA GPU support.
Features
- Support for BirdNET v2.4, v3.0, and Perch v2 models
- Automatic model type detection from ONNX tensor shapes
- Thread-safe classifier with builder pattern
- Top-K predictions with configurable confidence threshold
- Batch inference for GPU efficiency
- CLI tool for WAV file analysis
Supported Models
| Model | Sample Rate | Segment | Embeddings |
|---|---|---|---|
| BirdNET v2.4 | 48 kHz | 3.0s | No |
| BirdNET v3.0 | 32 kHz | 5.0s | 1024-dim |
| Perch v2 | 32 kHz | 5.0s | Variable |
Installation
Add to your Cargo.toml:
[]
= "1.0"
Library Usage
use ;
With CUDA GPU
use ;
let classifier = builder
.model_path
.labels_path
.execution_provider
.build?;
Batch Inference
let segments: = chunk_audio_file;
let refs: = segments.iter.map.collect;
let results = classifier.predict_batch?;
Execution Provider Query
Query which execution provider was requested:
let classifier = builder
.model_path
.labels_path
.with_cuda
.build?;
// Returns the requested provider (not necessarily the active one)
println!;
Note: This returns the requested execution provider. If the requested provider is unavailable, ONNX Runtime silently falls back to CPU. To verify which provider is actually running:
- Enable verbose logging:
export ORT_LOG_LEVEL=Verbose - Check log output for "Using [provider]" messages
CLI Usage
A basic CLI tool is included for quick testing of the library. It is not intended for production analysis tasks.
Build:
Analyze a WAV file:
With options:
Example output:
Analyzing: recording.wav (3m 21s, 48000 Hz)
Model: BirdNET v2.4 (3.0s segments, 1.5s overlap)
00:00.0 Eurasian Pygmy-Owl (92.4%)
00:01.5 Eurasian Pygmy-Owl (97.8%)
00:03.0 Eurasian Pygmy-Owl (98.5%)
...
134 segments analyzed in 1.2s
Range Filter (Meta Model)
Filter species predictions by location and date using BirdNET's meta model:
use RangeFilter;
// Load the meta model
let range_filter = builder
.model_path
.labels
.threshold
.build?;
// Get species likely at location/date
// Helsinki, Finland on June 15th
let scores = range_filter.predict?;
println!;
for score in scores.iter.take
The meta model uses BirdNET's 48-week calendar (4 weeks per month).
Using RangeFilter for Location-Based Filtering
use ;
// Build classifier
let classifier = builder
.model_path
.labels_path
.build?;
// Build range filter using classifier labels
let range_filter = builder
.model_path
.from_classifier_labels
.threshold
.build?;
// Get predictions
let result = classifier.predict?;
// Filter by location (Helsinki, June 15th)
let location_scores = range_filter.predict?;
let filtered = range_filter.filter_predictions;
for pred in filtered
Batch processing multiple files:
// Calculate location scores once
let location_scores = range_filter.predict?;
// Process multiple audio segments from same location
let mut predictions_batch = Vecnew;
for segment in audio_segments
// Filter all predictions at once
let filtered_batch = range_filter.filter_batch_predictions;
Development
Requires Task runner:
Acknowledgments
This library provides Rust bindings for running inference on models from these projects:
- BirdNET-Analyzer - Bird sound identification by the K. Lisa Yang Center for Conservation Bioacoustics at the Cornell Lab of Ornithology and Chemnitz University of Technology
- Perch - Bioacoustics research by Google Research
Built with:
- ONNX Runtime - Cross-platform inference engine by Microsoft
- ort - Rust bindings for ONNX Runtime
License
MIT