validate
birdnet_stm32.conversion.validate
¶
Validation utilities for comparing Keras vs. TFLite model outputs.
cosine_similarity(a, b, eps=1e-08)
¶
Cosine similarity between two flattened arrays.
When both vectors have negligible magnitude (e.g., background/noise class predictions near zero), returns 1.0 since both models agree on "no detection".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
Flattened predictions from Keras. |
required |
b
|
ndarray
|
Flattened predictions from TFLite. |
required |
eps
|
float
|
Small constant to avoid division-by-zero. |
1e-08
|
Returns:
| Type | Description |
|---|---|
float
|
Cosine similarity in [-1, 1]. |
Source code in birdnet_stm32/conversion/validate.py
pearson_correlation(a, b, eps=1e-12)
¶
Pearson correlation coefficient between two flattened arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ndarray
|
Flattened predictions from Keras. |
required |
b
|
ndarray
|
Flattened predictions from TFLite. |
required |
eps
|
float
|
Small constant to guard against zero variance. |
1e-12
|
Returns:
| Type | Description |
|---|---|
float
|
Pearson r in [-1, 1]. |
Source code in birdnet_stm32/conversion/validate.py
parity_metrics(reference, candidate, rep_data_gen, label='')
¶
Compare two prediction functions sample by sample and summarize the gap.
Tail statistics are included alongside the means so a high average cannot hide a handful of badly quantized inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
Callable[[ndarray], ndarray]
|
Callable mapping one input batch to the reference output. |
required |
candidate
|
Callable[[ndarray], ndarray]
|
Callable mapping the same batch to the candidate output. |
required |
rep_data_gen
|
Callable[[], Iterable[list[ndarray]]]
|
Callable returning an iterable of |
required |
label
|
str
|
Optional prefix for the printed summary lines. |
''
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Distribution statistics for cosine similarity, MSE, MAE, and Pearson |
dict[str, float]
|
correlation. |
Source code in birdnet_stm32/conversion/validate.py
validate_models(keras_model, tflite_model_path, rep_data_gen)
¶
Compare Keras vs. TFLite predictions and print summary statistics.
Runs the TFLite interpreter without delegates to minimize numeric differences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keras_model
|
Model
|
Loaded Keras model. |
required |
tflite_model_path
|
str
|
Path to the converted .tflite model. |
required |
rep_data_gen
|
Callable[[], Iterable[list[ndarray]]]
|
Callable returning an iterable of [input_tensor]. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Distribution statistics for cosine similarity, MSE, MAE, and Pearson |
dict[str, float]
|
correlation. Tail statistics are included so a high mean cannot hide |
dict[str, float]
|
badly quantized inputs. |