runners
birdnet_stm32.models.runners
¶
Inference runners for Keras and TFLite models.
Provides a uniform predict(x_batch) interface for both model formats, enabling the evaluation pipeline to be agnostic to the model type.
KerasRunner
¶
Thin wrapper for a Keras model to standardize batch prediction.
Source code in birdnet_stm32/models/runners.py
__init__(model)
¶
Initialize with a loaded Keras model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
Loaded Keras model (compiled=False is fine). |
required |
Source code in birdnet_stm32/models/runners.py
predict(x_batch)
¶
Run a forward pass on a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_batch
|
ndarray
|
Input batch in the model's expected shape and dtype. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Model outputs [B, C] as float32. |
Source code in birdnet_stm32/models/runners.py
TFLiteRunner
¶
TFLite model runner (XNNPACK, or the reference kernels where XNNPACK refuses the graph).
Source code in birdnet_stm32/models/runners.py
__init__(model_path, num_threads=None)
¶
Initialize with a TFLite model file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str
|
Path to a .tflite model file. |
required |
num_threads
|
int | None
|
Interpreter threads (default: up to 8). |
None
|
Source code in birdnet_stm32/models/runners.py
predict(x_batch)
¶
Run a forward pass on a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_batch
|
ndarray
|
Input batch in the model's expected shape and dtype. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Model outputs [B, C] as float32. |
Source code in birdnet_stm32/models/runners.py
ChainedTFLiteRunner
¶
Run a split backbone and classifier head as one model.
Conversion can emit the classifier head as its own artifact so it can be updated over a narrowband link without reflashing the backbone. Evaluation and deployment then need the two halves to behave like the model they came from, which is what this runner provides.
Source code in birdnet_stm32/models/runners.py
__init__(backbone_path, classifier_path)
¶
Initialize with the two halves of a split model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone_path
|
str
|
Path to the .tflite backbone (audio -> embeddings). |
required |
classifier_path
|
str
|
Path to the .tflite head (embeddings -> scores). |
required |
Source code in birdnet_stm32/models/runners.py
predict(x_batch)
¶
Run the backbone and feed its embeddings to the classifier head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_batch
|
ndarray
|
Input batch in the backbone's expected shape and dtype. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Model outputs [B, C] as float32. |
Source code in birdnet_stm32/models/runners.py
load_keras_model(model_path)
¶
Load a project checkpoint with the canonical custom-layer registry.
allocated_interpreter(model_path=None, model_content=None, num_threads=None)
¶
Return a TFLite interpreter with its tensors allocated.
TFLite applies the XNNPACK delegate by default. XNNPACK refuses some valid INT8 graphs outright, for example a requantization scale of 256 or more, which PTQ produces when a branch calibrates to an almost empty range (a PWL hinge that never fires). The builtin reference kernels run those graphs, so the interpreter falls back to them with a warning instead of failing. Graphs XNNPACK accepts keep running on it, so their outputs are unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str | None
|
Path to a .tflite file. |
None
|
model_content
|
bytes | None
|
Serialized model, instead of a path. |
None
|
num_threads
|
int | None
|
Interpreter threads. |
None
|
Returns:
| Type | Description |
|---|---|
Interpreter
|
Interpreter with tensors allocated. |
Source code in birdnet_stm32/models/runners.py
load_model_runner(model_path, classifier_path='')
¶
Load a .keras or .tflite model and return a runner with predict().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str
|
Path to a saved model (.keras or .tflite). When
|
required |
classifier_path
|
str
|
Optional .tflite classifier head. Supplying it runs the split pair as one chained model. |
''
|
Returns:
| Type | Description |
|---|---|
KerasRunner | TFLiteRunner | ChainedTFLiteRunner
|
KerasRunner, TFLiteRunner, or ChainedTFLiteRunner instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a classifier head is paired with a non-TFLite backbone. |