Views
No views yet
model_backbone.onnx and birdnet_backbone.onnx. Both models output a single tensor named embedding with shape (1, 1024).1import numpy as np
2import onnxruntime as ort
3from huggingface_hub import hf_hub_download
4
5# Download backbone
6path = hf_hub_download(
7 repo_id="biodiversica/BirdNET-onnx-backbone",
8 filename="model_backbone.onnx",
9)
10
11sess = ort.InferenceSession(path)
12
13# 3 s of audio at 48 kHz
14audio = np.zeros((1, 144000), dtype=np.float32)
15(embedding,) = sess.run(["embedding"], {"INPUT": audio})
16print(embedding.shape) # (1, 1024)birdnet_backbone.onnx the input key is "input" (lowercase):1path = hf_hub_download(
2 repo_id="biodiversica/BirdNET-onnx-backbone",
3 filename="birdnet_backbone.onnx",
4)
5sess = ort.InferenceSession(path)
6(embedding,) = sess.run(["embedding"], {"input": audio})
7print(embedding.shape) # (1, 1024)extract_backbone.py. The script will:model.onnx and birdnet.onnx from justinchuby/BirdNET-onnx.model/GLOBAL_AVG_POOL/Mean_reduced_0 node), renaming the output to embedding.model_backbone.onnx and birdnet_backbone.onnx.=== Downloading models ===
Downloaded model.onnx -> ...
Downloaded birdnet.onnx -> ...
Downloading BirdNET protobuf from Zenodo...
Extracted audio-model -> ...
=== Extracting backbones ===
Backbone saved -> model_backbone.onnx
inputs : ['INPUT']
outputs: ['embedding']
Backbone saved -> birdnet_backbone.onnx
inputs : ['input']
outputs: ['embedding']
=== Comparing embeddings against Zenodo TF SavedModel ===
PB embedding shape: (1, 1024)
model_backbone.onnx:
ONNX embedding shape: (1, 1024)
|diff| mean=1.230468e-06 max=9.298325e-06
Embeddings match PB reference with rtol=1e-03, atol=1e-03 PASSED
birdnet_backbone.onnx:
ONNX embedding shape: (1, 1024)
|diff| mean=6.440870e-05 max=5.004406e-04
Embeddings match PB reference with rtol=1e-03, atol=1e-03 PASSED_extract function in extract_backbone.py performs a backwards BFS from the
model/GLOBAL_AVG_POOL/Mean_reduced_0 output node (the global average pool), collecting
every node that contributes to that output and discarding everything downstream (the
classification dense layer). The output tensor is then renamed to embedding. It then
rebuilds a minimal ONNX graph containing only the retained nodes and their initializers.