Code Monkey home page Code Monkey logo

batdetect2's Introduction

BatDetect2

Code for detecting and classifying bat echolocation calls in high frequency audio recordings.

Getting started

Python Environment

We recommend using an isolated Python environment to avoid dependency issues. Choose one of the following options:

  • Install the Anaconda Python 3.10 distribution for your operating system from here. Create a new environment and activate it:
conda create -y --name batdetect2 python==3.10
conda activate batdetect2
  • If you already have Python installed (version >= 3.8,< 3.11) and prefer using virtual environments then:
python -m venv .venv
source .venv/bin/activate

Installing BatDetect2

You can use pip to install batdetect2:

pip install batdetect2

Alternatively, download this code from the repository (by clicking on the green button on top right) and unzip it. Once unzipped, run this from extracted folder.

pip install .

Make sure you have the environment activated before installing batdetect2.

Try the model

  1. You can try a demo of the model (for UK species) on huggingface.

  2. Alternatively, click here to run the model using Google Colab. You can also run this notebook locally.

Running the model on your own data

After following the above steps to install the code you can run the model on your own data.

Using the command line

You can run the model by opening the command line and typing:

batdetect2 detect AUDIO_DIR ANN_DIR DETECTION_THRESHOLD

e.g.

batdetect2 detect example_data/audio/ example_data/anns/ 0.3

AUDIO_DIR is the path on your computer to the audio wav files of interest. ANN_DIR is the path on your computer where the model predictions will be saved. The model will output both .csv and .json results for each audio file. DETECTION_THRESHOLD is a number between 0 and 1 specifying the cut-off threshold applied to the calls. A smaller number will result in more calls detected, but with the chance of introducing more mistakes.

There are also optional arguments, e.g. you can request that the model outputs features (i.e. estimated call parameters) such as duration, max_frequency, etc. by setting the flag --spec_features. These will be saved as *_spec_features.csv files: batdetect2 detect example_data/audio/ example_data/anns/ 0.3 --spec_features

You can also specify which model to use by setting the --model_path argument. If not specified, it will default to using a model trained on UK data e.g. batdetect2 detect example_data/audio/ example_data/anns/ 0.3 --model_path models/Net2DFast_UK_same.pth.tar

Using the Python API

If you prefer to process your data within a Python script then you can use the batdetect2 Python API.

from batdetect2 import api

AUDIO_FILE = "example_data/audio/20170701_213954-MYOMYS-LR_0_0.5.wav"

# Process a whole file
results = api.process_file(AUDIO_FILE)

# Or, load audio and compute spectrograms
audio = api.load_audio(AUDIO_FILE)
spec = api.generate_spectrogram(audio)

# And process the audio or the spectrogram with the model
detections, features, spec = api.process_audio(audio)
detections, features = api.process_spectrogram(spec)

# Do something else ...

You can integrate the detections or the extracted features to your custom analysis pipeline.

Training the model on your own data

Take a look at the steps outlined in finetuning readme here for a description of how to train your own model.

Data and annotations

The raw audio data and annotations used to train the models in the paper will be added soon. The audio interface used to annotate audio data for training and evaluation is available here.

Warning

The models developed and shared as part of this repository should be used with caution. While they have been evaluated on held out audio data, great care should be taken when using the model outputs for any form of biodiversity assessment. Your data may differ, and as a result it is very strongly recommended that you validate the model first using data with known species to ensure that the outputs can be trusted.

FAQ

For more information please consult our FAQ.

Reference

If you find our work useful in your research please consider citing our paper which you can find here:

@article{batdetect2_2022,
    title     = {Towards a General Approach for Bat Echolocation Detection and Classification},
    author    = {Mac Aodha, Oisin and  Mart\'{i}nez Balvanera, Santiago and  Damstra, Elise and  Cooke, Martyn and  Eichinski, Philip and  Browning, Ella and  Barataudm, Michel and  Boughey, Katherine and  Coles, Roger and  Giacomini, Giada and MacSwiney G., M. Cristina and  K. Obrist, Martin and Parsons, Stuart and  Sattler, Thomas and  Jones, Kate E.},
    journal   = {bioRxiv},
    year      = {2022}
}

Acknowledgements

Thanks to all the contributors who spent time collecting and annotating audio data.

TODOs

  • Release the code and pretrained model
  • Release the datasets and annotations used the experiments in the paper
  • Add the scripts used to generate the tables and figures from the paper

batdetect2's People

Contributors

arky avatar macaodha avatar mbsantiago avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

batdetect2's Issues

No file name in console output

(me again! I'm not sure if this comment should be in issues? Happy to change it if needed!)

With the first version of batdetect2 the output in command line was giving a serial number and the filename before the result of the processing of the file. Like this (2 results out of many files in a folder):

128 PaRecAR1292524_20230429_215723.wav
0 call(s) detected above the threshold.

129 PaRecAR1292524_20230429_202535.wav
6 call(s) detected above the threshold.
species name probablity present
Eptesicus serotinus 0.62
Nyctalus noctula 0.191
Nyctalus leisleri 0.142
Plecotus auritus 0.03
Plecotus austriacus 0.013

But now, since v1.0.x, there is not anymore the first line in the output, for example:

time_exp_fact 10
13 call(s) detected above the threshold.
species name probablity present
Pipistrellus pipistrellus 0.972
Nyctalus leisleri 0.017
Pipistrellus nathusii 0.007
time_exp_fact 10
18 call(s) detected above the threshold.
species name probablity present
Nyctalus leisleri 0.541
Eptesicus serotinus 0.226
Nyctalus noctula 0.225

Therefore one can not use the output without being able to connect the results with the file. Actually I collect the probability of the first species in my workflow (in a R script) from the console output to make a summary table.
I guess it's not a big deal to put back this line in the output?

Batdetect2 detects Bats when there are no calls and not even audio features

My files:
Audiomoths have the option to generate a .wav file every X seconds. That can lead (combined with a frequency trigger) to "empty" files. Those are normally around 50kB big, have a length around 0.1 seconds but no audio features (at least not visible in a spectogram viewer like Audacity), since the Audiomoth didn't record anything in that timeperiod.

What I would expect:
That batdetect2 detects 0 calls and classifies no species.

What is happening:
Even though they are featureless, batdetect2 detects two calls.
species, time, detection_prob, class_prob

species, time, detection_prob, class_prob
Myotis daubentonii, 0.0015, 0.12, 0.066
Myotis nattereri, 0.0825, 0.122, 0.106

Those results are consistent. I tested that with 25k different empty files.

When looking at the spectrogram on huggingface one vertical line is visible at 0.08s (that is detected as Mnat) while the Mdau bounding box is in the empty void. I don't know where that vertical line is coming from.

A subset of those empty files is in an otherwise empty repository.

I used batdetect2 version 1.0.8

typo in species name

In the batdetect2 version we are using, there is a typo for the Western barbastelle: it should read "Barbastella barbastellus" instead of "Barbastellus barbastellus".

Some questions about the output data

Guys,
I like a lot using batDetect2, it's open, fast and efficient :-). So I'm developing a shiny app (under R, I plan to upload it on my GitHub soon) that is calling batDetect2 through a convenient GUI. I'm a bit stuck when it come to understand some numbers from the output.

  1. "probability present" (console)
    I cannot understand how this value is calculated? I've been checking all the numbers from json files and csv table, as well as from the --spec_features table output. I also read -thoroughly enough I think- your paper "Towards a General Approach for Bat Echolocation Detection and Classification" without finding answer. Could you tell me how it is calculated or where I can find the info? In the following chunk of the output, the probability of 0.676 is retunrned for Nnoc, 0.297 for Nleii and 0.026 for Eser but I cannot relate these probabilities to the numbers in the csv files.
    PaRecAR863806_20230609_214859.wav_spec_features.csv
    PaRecAR863806_20230609_214859.wav.csv
...
4 /home/jf/tmp/TESTfakeAudiomothFolder/20010911_WTC/A/PaRecAR863806_20230609_214859.wav
10 call(s) detected above the threshold.
species name                  probablity present
Nyctalus noctula              0.676
Nyctalus leisleri             0.297
Eptesicus serotinus           0.026
...

Could you tell me how I can recalculate these numbers by myself or where to find this info?

  1. I'm just wondering what "_bb" stands for in the spec feature csv table? For instance, what is the difference between max_power_bb and max_power ?

Hope you can help me with these points.
Best,
JF Godeau

module not found when using jupyter

I hopefully have a simple issue. I activate batdect2 in the anaconda prompt. I then use jupyter notebook batdetect_test.ipynb to open my notebook. In the notebook I then run the code that was provided on GitHub: from batdetect2 import api
Then I get the below:

ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 1
----> 1 from batdetect2 import api

ModuleNotFoundError: No module named 'batdetect2'

I have installed the batdetect through using the conda create code provided on the GitHub. Anythoughts on why this is not working out for me?
I am quite new to Python so apologies if this is a rather silly question.

Release of the training data

Hi,
an open item on your TODO list is the release of the training data.
I would highly appreciate the release to train the model with additional species that we have here in Germany. The results with the already incorporated species is very good so far. It works better than anything else I tested before.

Cheers
Christian

Latest numpy results in error when detecting bat calls

I just noticed that when I installed your package using pip install batdetect2 as well as installing it from source (cloning and pip install . that the latest numpy version results in an error when I try to detect calls.

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

The suggested fix worked nicely. I was able to run batdetect2 after downgrading numpy. Perhaps you should specify "numpy<2" in your pyproject.toml which will force pip to install a Numpy version 1.x. as it might take time for other dependencies to support Numpy version 2.x

Output file explaination

Hi. Is there any documentation on the json output and what the fields actually mean. Most I can make a good guess but the actual probability seems to have 2 outputs class_prob and det_prob and I'm not sure why there are 2 and what the differences are. I'd also like to be sure on all of the fields. I've also noticed that the Probability Percent reported when it is run does not match the JSON or CSV contents. Is there is somewhere I can look add be grateful if I could be pointed in the right direction please? Cheers

IndexError: list index out of range

Hello,
I was using the batdetect2 api (the 1.0.4) on a raspberry pi with high frequence microphone.
It worked very well (thanks to your work) few hours, but I encountered this error

File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
  self.run()
File "/usr/lib/python3.9/threading.py", line 892, in run 
   self._target (self._args, **self._kwargs)
File "/usr/local/bin/server.py", line 178, in handle_client 
   results = api.process_file(file_path) ["pred_dict"]
File "/home/bird/BirdNET-Pi/scripts/batdetect2/api.py", line 262, in process_file 
   return du.process_file(
File "/home/bird/BirdNET-Pi/scripts/batdetect2/utils/detector_utils.py", line 785,  in process_file 
   predictions, spec_feats, cnn_feats, spec_slices = _merge_results( 
 File "/home/bird/BirdNET-Pi/scripts/batdetect2/utils/detector_utils.py", line 157, in _merge_results( 
    predictions_m=predictions[0]
IndexError: list index out of range

I only used this line
results = api.process_file(file_path) ["pred_dict"]
in my code

I have not dived in the concerned code yet so I don't have any other info but don't hesitate to ask.
Could you give it a look ?
I joind a picture of the error as the code above is the result of a image to text and may contains error.
Thanks in advance
1683751572362

Can't install packages via conda

Following the steps in the readme, when I get to:

conda install --file requirements.txt

I get:

(batdetect2) vscode ➜ /workspaces/bat-detector-msds (build-refactor) $ conda install --file requirements.txt   

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

  - torchaudio==0.13.0
  - scikit_learn==1.2.0
  - torch==1.13.0

Current channels:

  - https://repo.anaconda.com/pkgs/main/linux-aarch64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/linux-aarch64
  - https://repo.anaconda.com/pkgs/r/noarch
  - https://conda.anaconda.org/conda-forge/linux-aarch64
  - https://conda.anaconda.org/conda-forge/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

I was able to install with pip, but no conda. Seems some of these packages to not exist on Anaconda.

Cannot pass --time_expansion_factor argument

No success to pass the time expansion argument when I want to process audio files which are already 10x time expanded (although it works fine with the very first version).

When I run (with the environment correctly activated):

batdetect2 detect --time_expansion_factor 10 /home/jf/Music/Training_Sounds/tmpADD/ /home/jf/Music/Training_Sounds/tmpADD/anns102/ 0.3
or
batdetect2 detect /home/jf/Music/Training_Sounds/tmpADD/ /home/jf/Music/Training_Sounds/tmpADD/anns102/ 0.3 --time_expansion_factor 10

The processing runs well but the first lines of the console output are:

Saving results to: /home/jf/Music/Training_Sounds/tmpADD/anns102/
time_exp_fact 1
25 call(s) detected above the threshold.
species name probablity present
Myotis nattereri 0.456
Rhinolophus ferrumequinum 0.248
Pipistrellus pipistrellus 0.136
...

Which of course gives inconsistent results!
Line 2 tells "time_exp_fact 1" i.e. it considers the files as real time and not as 10x time expanded.

Am I missing something about using the flag?

Nyquist sampling rate fix problem

In the fix the code:
results = convert_results(
file_id=os.path.basename(audio_file),
time_exp=config.get("time_expansion", 1) or 1,
duration=audio_full.shape[0] / float(sampling_rate),
params=config,
predictions=predictions,
spec_feats=spec_feats,
cnn_feats=cnn_feats,
spec_slices=spec_slices,

    nyquist_freq=orig_samp_rate / 2,

may be incorrect.
The sample rate for nyquist checks should be the lower of the two "target_sample_rate"(256Khz) or "orig_samp_rate"

Pass time expansion factor in API

This question is pretty much the same as in issue #5 but for script integration to call the batdetect2 python API.
Can I pass a time expansion factor argument to process_file function?

Process a whole file

results = api.process_file(AUDIO_FILE)

My attempts did not work, however I can add time_exp_fact argument in

audio = api.load_audio(AUDIO_FILE, time_exp_fact = 10)

I believe it makes sense to add this argument in process_file() or should I work differently?
Thx!

Doesn't handle 0 length audio files gracfully

Some of the files my AudioMoth recorded were empty causing the output below.

 0 run/media/mikee/boot/20231007_234400.WAV
 /home/mikee/.local/lib/python3.11/site-packages/batdetect2/utils/audio_utils.py:147: UserWarning: PySoundFile failed.  Trying audioread instead.
 audio_raw, sampling_rate = librosa.load(
 /home/mikee/.local/lib/python3.11/site-packages/librosa/core/audio.py:183: FutureWarning:  librosa.core.audio.__audioread_load
 Deprecated as of librosa version 0.10.0.
 It will be removed in librosa version 1.0.
 y, sr_native = __audioread_load(path, offset, duration, dtype)

 Aborted!

I simply removed the empty files to fix the issue but maybe these should be handled gracefully within batdetect2?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.