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
SigmoidRunner
¶
Wrap a runner whose model emits logits, and return probabilities.
A model converted with --output_activation logit leaves the sigmoid to
its caller, which keeps its output off the INT8 1/256 probability grid.
Everything downstream — evaluation, the board comparison, the operational
gate — expects probabilities, so this puts the sigmoid back. It is 100
values per window; the cost is not measurable.
Source code in birdnet_stm32/models/runners.py
load_keras_model(model_path)
¶
Load a project checkpoint with the canonical custom-layer registry, computing in float32.
A checkpoint trained with --mixed_precision keeps a mixed_float16 policy in its layers, so
it would evaluate, calibrate and convert in float16; the TFLite converter rejects those graphs. Its
weights are float32 already: such a model is rebuilt with float32 policies and the same weights.
Source code in birdnet_stm32/models/runners.py
float32_model(model)
¶
model itself if every layer computes in float32, else a float32 rebuild with its weights.
Source code in birdnet_stm32/models/runners.py
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
runner_for_config(runner, config)
¶
Wrap runner in the sigmoid when config says the model emits logits.
Source code in birdnet_stm32/models/runners.py
load_model_runner(model_path, classifier_path='', config=None)
¶
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. |
''
|
config
|
dict | None
|
Optional model config. When it says |
None
|
Returns:
| Type | Description |
|---|---|
KerasRunner | TFLiteRunner | ChainedTFLiteRunner | SigmoidRunner
|
KerasRunner, TFLiteRunner, or ChainedTFLiteRunner instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a classifier head is paired with a non-TFLite backbone. |