joeweiss / birdnetlib

A python api for BirdNET-Lite and BirdNET-Analyzer
https://joeweiss.github.io/birdnetlib/
Apache License 2.0
41 stars 14 forks source link

[Question] How do I use `birdnetlib` with BirdNet's models and labels? #71

Closed bmitc closed 1 year ago

bmitc commented 1 year ago

I am trying to use birdnetlib as a library to identify birds instead of using the scripts included in BirdNet. I am having trouble pointing birdnetlib to the models and labels included in BirdNET.

Installation steps performed

I am on Ubuntu 22.04.

sudo apt-get update
sudo apt-get upgrade
<Python 3.10 was already installed>
pip3 install tensorflow
pip3 install librosa resampy
pip3 install birdnetlib
sudo apt-get install ffmpeg
git clone https://github.com/kahst/BirdNET-Analyzer.git

I tried following BirdNet's recommendation of installing tflite-runtime but got

>>> from birdnetlib.analyzer import Analyzer
Traceback (most recent call last):
  File "/home/<user>/.local/lib/python3.10/site-packages/birdnetlib/analyzer.py", line 8, in <module>
    import tflite_runtime.interpreter as tflite
ModuleNotFoundError: No module named 'tflite_runtime'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/<user>/.local/lib/python3.10/site-packages/birdnetlib/analyzer.py", line 10, in <module>
    from tensorflow import lite as tflite
ModuleNotFoundError: No module named 'tensorflow'

So this is why I just installed the other option of tensorflow.

Code

from birdnetlib import Recording
from birdnetlib.analyzer import Analyzer
from datetime import datetime

model="<path>/BirdNET-Analyzer/checkpoints/V2.4/BirdNET_GLOBAL_6K_V2.4_Model_FP32.tflite"
labels="<path>/BirdNET-Analyzer/checkpoints/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels.txt"

analyzer=Analyzer(
    classifier_labels_path=labels,
    classifier_model_path=model
)

recording = Recording(
    analyzer,
    "2023-07-03 19_27.wav",
    lat=42,
    lon=71,
    date=datetime(year=2023, month=7, day=4),
    min_conf = 0
)

But the analysis fails:

>>> recording.analyze()
read_audio_data
read_audio_data: complete, read  16 chunks.
analyze_recording 2023-07-03 19_27.wav
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/<user>/.local/lib/python3.10/site-packages/birdnetlib/main.py", line 61, in analyze
    self.analyzer.analyze_recording(self)
  File "/home/<user>/.local/lib/python3.10/site-packages/birdnetlib/analyzer.py", line 206, in analyze_recording
    pred = self.predict_with_custom_classifier(c)[0]
  File "/home/<user>/.local/lib/python3.10/site-packages/birdnetlib/analyzer.py", line 316, in predict_with_custom_classifier
    C_INTERPRETER.invoke()
  File "/home/<user>/.local/lib/python3.10/site-packages/tensorflow/lite/python/interpreter.py", line 917, in invoke
    self._interpreter.Invoke()
RuntimeError: tensorflow/lite/kernels/concatenation.cc:159 t->dims->data[d] != t0->dims->data[d] (1 != 0)Node number 92 (CONCATENATION) failed to prepare.

This worked fine with the built-in model and labels in birdnetlib using just analyzer=Analyzer(), but those are apparently not correct for my area as the results were not accurate.

How do I use birdnetlib with BirdNet's models and labels? Have I done something wrong here?

Thanks for any help! Excited to try and get this system going.

joeweiss commented 1 year ago

Hi @bmitc, and my apologies for the delay in getting back to you.

The explicit classifier_labels_path and classifier_model_path arguments refer to the classifiers that are trained with BirdNET, not for the BirdNET models themselves. The BirdNET models (ie "BirdNET_GLOBAL_6K_V2.4_Model_FP32") are installed along with birdnetlib and used by the Analyzer class.

If you update to 0.8.0 pip install birdnetlib==0.8.0, then you can use the BirdNET 2.4 "6K" model by using the Analyzer class as you've documented above.

Prior to 0.8.0 (June 2023), the library used the BirdNET 2.3 models.

If you're still seeing wildly inaccurate results, let me know and I'll take a second look.