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
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 returning an iterable of [input_tensor]. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dict with keys 'cosine_mean', 'mse_mean', 'mae_mean', 'pearson_mean'. |