kitzeslab / opensoundscape

Open source, scalable software for the analysis of bioacoustic recordings
http://opensoundscape.org
MIT License
139 stars 16 forks source link

OpenSoundscape

CI Status Documentation Status

OpenSoundscape (OPSO) is free and open source Python utility library analyzing bioacoustic data.

OpenSoundscape includes utilities which can be strung together to create data analysis pipelines, including functions to:

OpenSoundscape's documentation can be found on OpenSoundscape.org.

Show me the code!

For examples of how to use OpenSoundscape, see the Quick Start Guide below.

For full API documentation and tutorials on how to use OpenSoundscape to work with audio and spectrograms, train machine learning models, apply trained machine learning models to acoustic data, and detect periodic vocalizations using RIBBIT, see the documentation.

Contact & Citation

OpenSoundcape is developed and maintained by the Kitzes Lab at the University of Pittsburgh. It is currently in active development. If you find a bug, please submit an issue on the GitHub repository. If you have another question about OpenSoundscape, please use the (OpenSoundscape Discussions board)[https://github.com/kitzeslab/opensoundscape/discussions] or email Sam Lapp (sam.lapp at pitt.edu)

Suggested citation:

Lapp, Sam; Rhinehart, Tessa; Freeland-Haynes, Louis; 
Khilnani, Jatin; Syunkova, Alexandra; Kitzes, Justin. 
“OpenSoundscape: An Open-Source Bioacoustics Analysis Package for Python.” 
Methods in Ecology and Evolution 2023. https://doi.org/10.1111/2041-210X.14196.

Quick Start Guide

A guide to the most commonly used features of OpenSoundscape.

Installation

Details about installation are available on the OpenSoundscape documentation at OpenSoundscape.org. FAQs:

How do I install OpenSoundscape?

Will OpenSoundscape work on my machine?

Use Audio and Spectrogram classes to inspect audio data

from opensoundscape import Audio, Spectrogram

#load an audio file and trim out a 5 second clip
my_audio = Audio.from_file("/path/to/audio.wav")
clip_5s = my_audio.trim(0,5)

#create a spectrogram and plot it
my_spec = Spectrogram.from_audio(clip_5s)
my_spec.plot()

Load audio starting at a real-world timestamp

from datetime import datetime; import pytz

start_time = pytz.timezone('UTC').localize(datetime(2020,4,4,10,25))
audio_length = 5 #seconds  
path = '/path/to/audiomoth_file.WAV' #an AudioMoth recording

Audio.from_file(path, start_timestamp=start_time,duration=audio_length)

Load and use a model from the Bioacoustics Model Zoo

The Bioacoustics Model Zoo hosts models in a respository that can be accessed via torch.hub and are compatible with OpenSoundscape. Load up a model and apply it to your own audio right away:

from opensoundscape.ml import bioacoustics_model_zoo as bmz

#list available models
print(bmz.list_models())

#generate class predictions and embedding vectors with Perch
perch = bmz.load("Perch")
scores = perch.predict(files)
embeddings = perch.generate_embeddings(files)

#...or BirdNET
birdnet = bmz.load("BirdNET")
scores = birdnet.predict(files)
embeddings = birdnet.generate_embeddings(files)

Load a pre-trained CNN from a local file, and make predictions on long audio files

from opensoundscape import load_model

#get list of audio files
files = glob('./dir/*.WAV')

#generate predictions with a model
model = load_model('/path/to/saved.model')
scores = model.predict(files)

#scores is a dataframe with MultiIndex: file, start_time, end_time
#containing inference scores for each class and each audio window

Train a CNN using audio files and Raven annotations

from sklearn.model_selection import train_test_split
from opensoundscape import BoxedAnnotations, CNN

# assume we have a list of raven annotation files and corresponding audio files
# load the annotations into OpenSoundscape
all_annotations = BoxedAnnotations.from_raven_files(raven_file_paths,audio_file_paths)

# pick classes to train the model on. These should occur in the annotated data
class_list = ['IBWO','BLJA']

# create labels for fixed-duration (2 second) clips 
labels = all_annotations.clip_labels(
  cip_duration=2,
  clip_overlap=0,
  min_label_overlap=0.25,
  class_subset=class_list
)

# split the labels into training and validation sets
train_df, validation_df = train_test_split(labels, test_size=0.3)

# create a CNN and train on the labeled data
model = CNN(architecture='resnet18', sample_duration=2, classes=class_list)

# train the model to recognize the classes of interest in audio data
model.train(train_df, validation_df, epochs=20, num_workers=8, batch_size=256)

Train a custom classifier on BirdNET or Perch embeddings

from opensoundscape.ml import bioacoustics_model_zoo as bmz

# load a model from the model zoo
model = bmz.load('BirdNET') #or bmz.load('Perch')

# define classes for your custom classifier
model.change_classes(train_df.columns)

# fit the trainable PyTorch classifier on your labels
model.train(train_df,val_df,num_augmentation_variants=4,batch_size=64)

# run inference using your custom classifier on audio data
model.predict(audio_files)