The birdnetR package lets you use the
birdnet Python package from R. This guide shows how to load
models, identify species in audio files, predict species for a location,
extract embeddings, and run custom classifiers.
birdnetR is one step in the BirdNET tool chain:
- (Optional) Train a custom classifier with BirdNET-Analyzer.
- Identify species in your recordings with birdnetR.
- Filter, visualize and validate the detections with birdnetTools.
Installation
Install the released version from CRAN:
install.packages("birdnetR")
or install the development version from GitHub with:
pak::pak("birdnet-team/birdnetR")Note
You don’t need to install Python yourself.
On first use, birdnetR sets up Python and the birdnet
package for you. The first setup takes a few minutes. TensorFlow and
PyTorch are downloaded only when you first load a model that needs them.
See Installation and setup for
details.
Usage
Load a BirdNET model
To begin, load a model. On first use, the model files are downloaded; after that they are read from disk.
# BirdNET+ V3.0 acoustic model (the default)
model <- birdnet_load()
#> BirdNET acoustic 3.0 · onnx · fp32 · en_us · 11,725 classesEvery load prints one line: the model, its backend, precision,
language and number of classes. Use
suppressMessages(birdnet_load()) to load quietly.
print(model) shows the same line again.
BirdNET+ V3.0 recognizes about 11,700 classes, including many non-bird animals such as frogs, insects and mammals. The exact number depends on the model release.
Identify species in an audio file
birdnet_predict() runs the model on one or more audio
files. It returns a prediction object that you convert to a data frame
with as.data.frame(). Each row is one detection in one
segment of the recording (3 seconds by default).
# Path to an example audio file (replace with your own file path)
audio_path <- system.file("extdata", "soundscape.mp3", package = "birdnetR")
# Predict species in the audio file
predictions <- birdnet_predict(model, audio_path, min_confidence = 0.3)
# Convert to a data frame
df <- as.data.frame(predictions)
head(df)
# Example output (the input column holds the full file path):
#> input start_time end_time species_name confidence
#> 1 soundscape.mp3 0 3 Poecile atricapillus_Black-capped Chickadee 0.9206197
#> 2 soundscape.mp3 0 3 Haemorhous mexicanus_House Finch 0.3681544
#> 3 soundscape.mp3 3 6 Poecile atricapillus_Black-capped Chickadee 0.7824634
#> 4 soundscape.mp3 6 9 Haemorhous mexicanus_House Finch 0.5423213
#> 5 soundscape.mp3 6 9 Junco hyemalis_Dark-eyed Junco 0.5064150
#> 6 soundscape.mp3 9 12 Haemorhous mexicanus_House Finch 0.9012285The columns are:
-
input: the audio file, -
start_time,end_time: the segment, in seconds from the start of the file, -
species_name: scientific and common name, separated by an underscore, -
confidence: the score (see?birdnet_predict.birdnet_model_acousticfor how to read it).
If several species score above min_confidence in the
same segment, you get one row per species (at most top_k,
default 5; top_k = NULL returns all of them).
Process many files
Pass a vector of paths to analyze several files in one call:
files <- list.files("recordings", pattern = "\\.wav$", full.names = TRUE)
predictions <- birdnet_predict(model, files)birdnet_predict() already uses several CPU cores. Set
n_workers to change how many. Don’t run birdnetR inside
parallel or future workers; see Parallel
processing.
For large datasets, keep the predictions in Python and write them to
disk with birdnet_write() instead of converting them with
as.data.frame(). One way to do this is to loop over chunks
of your data, such as one folder per site or one recording day, and
write one output file per chunk:
# Folders of audio recordings, e.g. one folder per site
audio_folders <- list.dirs("recordings", recursive = FALSE)
dir.create("results", showWarnings = FALSE)
for (folder in audio_folders) {
files <- list.files(folder, pattern = "\\.wav$", full.names = TRUE)
predictions <- birdnet_predict(model, files)
out_file <- file.path("results", paste0(basename(folder), ".csv"))
birdnet_write(predictions, out_file)
}This keeps memory use constant however large your dataset is, because each iteration replaces the previous prediction object.
birdnet_write() chooses the format from the file
extension:
| Format | Best for | Notes |
|---|---|---|
| CSV | birdnetTools, quick inspection, other software | Human-readable; largest file size |
| Parquet | Large datasets analyzed in R or Python | Columnar; much smaller than CSV |
| NPZ | NumPy-based pipelines | Compact; requires Python to read |
Parquet is not supported for geo predictions; use CSV or NPZ instead.
birdnet_write() also writes embeddings from
birdnet_encode().
In CSV files, start_time and end_time are
text in the form "HH:MM:SS.cc" (hours, minutes, seconds and
hundredths), e.g. "00:00:03.00". In the data frame from
as.data.frame() they are numbers of seconds.
birdnet_write() writes Parquet files directly from
Python and does not need the arrow R package. To read them
back into R (e.g. with arrow::read_parquet()), install
arrow with install.packages("arrow"). On
Linux, this can mean compiling it, which takes several minutes.
Using a custom species list
Often you only need some of the species the model knows. Pass a custom species list to limit the output to those species.
Class labels consist of the scientific name and the common name, separated by an underscore, like this:
"Agelaius phoeniceus_Red-winged Blackbird"
"Cyanocitta cristata_Blue Jay"Get the full species list of a loaded model with
birdnet_list_species():
species <- birdnet_list_species(model)
head(species)Every name must be in the model’s species list, otherwise
birdnet_predict() stops with an error. Names can differ
between model versions. For example, Cooper’s Hawk is
Accipiter cooperii in BirdNET V2.4 and
Astur cooperii in V3.0. Always take the names from the
model you use.
To limit predictions to specific species, pass a character vector to
the species_list argument:
predictions <- birdnet_predict(model, audio_path,
species_list = c("Cyanocitta cristata_Blue Jay", "Junco hyemalis_Dark-eyed Junco"),
min_confidence = 0.3
)
as.data.frame(predictions)BirdNET V2.4 and Perch v2
BirdNET V2.4 is the model that birdnetR 0.x used. It has 6,522 classes and runs on the default installation:
model_24 <- birdnet_load(version = "2.4")
#> BirdNET acoustic 2.4 · tf/litert · fp32 · en_us · 6,522 classesPerch v2 is an acoustic model by Google with 14,795 classes (birds and other animals). It analyzes 5-second segments.
perch <- birdnet_load_perch()
#> Perch v2 · pb · CPU · 14,795 classes
predictions <- birdnet_predict(perch, audio_path)Perch needs TensorFlow. birdnetR installs it (about 600 MB) the first time you load Perch. Perch does not run on Intel Macs.
Perch scores are raw model outputs (logits), not values between 0 and
1. They are not comparable with BirdNET scores, and the default
min_confidence = 0.1 has a different meaning for them. Use
apply_sigmoid = TRUE or apply_softmax = TRUE
in birdnet_predict() if you need scores between 0 and
1.
Perch uses scientific names only (for example
"Poecile atricapillus"), so
birdnet_load_perch() has no language
argument.
Predict species occurrence with the geo model
BirdNET includes a geographic model that predicts which species are likely to occur at a location and week of the year. It returns a prediction object with species names and scores.
BirdNET’s year has 48 weeks: four per month, so week 1 is the first week of January and week 48 the last week of December.
# Load the geo model
geo_model <- birdnet_load(type = "geo")
# Predict species occurrence in Ithaca, NY in week 4 (the end of January)
predictions <- birdnet_predict(geo_model, latitude = 42.5, longitude = -76.45, week = 4L)
# Convert to a data frame and sort by score
df <- as.data.frame(predictions)
head(df[order(-df$confidence), ])
# Example output:
#> species_name confidence
#> Spinus tristis_American Goldfinch 0.9998151
#> Junco hyemalis_Dark-eyed Junco 0.9997241
#> Dryobates pubescens_Downy Woodpecker 0.9997090
#> Zenaida macroura_Mourning Dove 0.9996312With week = NULL (the default), the model predicts for
the whole year. year_round_aggregation then decides how the
weekly scores are combined: "max" (the default) or
"average".
predictions <- birdnet_predict(geo_model, latitude = 42.5, longitude = -76.45)You can use the geo prediction as a species list for the acoustic
model. Every name in species_list must be known to the
acoustic model, so take the names from the acoustic model’s own list.
The two models can name the same species differently, so match the names
by scientific name, and by common name for the rest. Use geo and
acoustic models of the same version:
geo_species <- as.data.frame(predictions)$species_name
acoustic_species <- birdnet_list_species(model)
# Names are "<scientific name>_<common name>"
scientific <- function(x) sub("_.*", "", x)
common <- function(x) sub("^[^_]*_", "", x)
keep <- scientific(acoustic_species) %in% scientific(geo_species) |
common(acoustic_species) %in% common(geo_species)
predictions <- birdnet_predict(model, audio_path, species_list = acoustic_species[keep])Translating common species names
Common names are available in several languages. The two model versions support different languages:
birdnet_list_languages() # BirdNET+ V3.0
birdnet_list_languages("2.4") # BirdNET V2.4To get common names in your preferred language, load the model with
the language argument:
model_fr <- birdnet_load(language = "fr")
# Species list in French
head(birdnet_list_species(model_fr))Extract embeddings
Embeddings are the feature vectors a model computes for each audio segment before it scores the species. You can use them for clustering, for similarity search, or to train your own classifier.
embeddings <- birdnet_encode(model, audio_path)
df <- as.data.frame(embeddings)The data frame has the columns input,
start_time, end_time and
embedding. embedding is a list column with one
numeric vector per segment (1,280 values for BirdNET+ V3.0, 1,024 for
V2.4). To get a matrix with one row per segment:
birdnet_write(embeddings, "embeddings.parquet") saves
them to a file.
Custom classifiers
You can train your own classifier with BirdNET-Analyzer on top of BirdNET V2.4 or V3.0. BirdNET-Analyzer exports a TFLite model file and a label file with the species list.
birdnet_inspect_custom() describes a TFLite classifier
without loading it:
info <- birdnet_inspect_custom("/path/to/CustomClassifier.tflite")
str(info)
#> List of 6
#> $ version : chr "2.4"
#> $ mode : chr "replace"
#> $ n_species : int 345
#> $ applies_sigmoid : logi FALSE
#> $ supports_encoding: logi TRUE
#> $ input_samples : int 144000-
version: the base model,"2.4"or"3.0". -
mode:"replace"(only your classes) or"append"(your classes added to the base model’s). -
n_species: the number of classes. -
applies_sigmoid: whether the model applies the sigmoid itself. -
supports_encoding: whetherbirdnet_encode()works with it. -
input_samples: the number of audio samples per segment (NULLif any length is accepted).
To load it, give the paths to the model file and the label file. birdnetR reads the base version from the file:
custom_model <- birdnet_load_custom(
model = "/path/to/CustomClassifier.tflite",
species_list = "/path/to/CustomClassifier_Labels.txt"
)
predictions <- birdnet_predict(custom_model, audio_path)TFLite classifiers built on V2.4 run on the default installation. For
a Raven export (a folder) or another Protobuf classifier, set
backend = "pb" and also give the base version,
because birdnetR can’t read it from the files. This installs TensorFlow
on first use:
raven_model <- birdnet_load_custom(
model = "/path/to/raven_export",
species_list = "/path/to/raven_labels.csv",
version = "2.4",
backend = "pb",
is_raven = TRUE
)Raven exports can’t be used with birdnet_encode().
Choosing a model
| Model | Load with |
|---|---|
| BirdNET+ V3.0 acoustic (default) | birdnet_load() |
| BirdNET+ V3.0 geo | birdnet_load(type = "geo") |
| BirdNET V2.4 acoustic | birdnet_load(version = "2.4") |
| BirdNET V2.4 geo | birdnet_load(type = "geo", version = "2.4") |
| Perch v2 (Google) | birdnet_load_perch() |
| Custom classifier | birdnet_load_custom() |
Each model comes in several variants (backends and precisions). The
Models and backends article explains these
variants, birdnet_list_models() and the licenses of the
models.
Next steps
To filter, visualize, or validate your predictions, use the birdnetTools package. It filters detections by species, confidence or date, draws heatmaps, and has an interactive app for reviewing detections and setting a confidence threshold per species.
More articles:
- Installation and setup: extra downloads, your own Python environment, working offline.
- Models and backends: model variants, backends, precisions and licenses.
- GPU: running models on a GPU.
- Troubleshoot: reporting a problem and common issues.
