distillation
birdnet_stm32.training.distillation
¶
Frozen-teacher consistency losses for quantization-aware fine-tuning.
QAT perturbs a model that already converged. Hard labels alone do not tell the perturbed student where the original decision surface was, so it optimizes the supervised loss together with a Bernoulli KL divergence and a cosine agreement term against a frozen copy of the pre-compression checkpoint. The tail term targets the worst samples in each batch, which is where a quantized detector actually loses ranking quality.
DistilledModel
¶
Bases: Model
Train a perturbed student against labels and a frozen float teacher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
student
|
Model
|
Functional model holding the perturbed (quantized or masked) forward graph. Its variables are shared with the deployment model. |
required |
teacher
|
Model
|
Frozen copy of the pre-compression checkpoint. |
required |
distillation_weight
|
float
|
Weight of the per-output Bernoulli KL divergence. |
DEFAULT_DISTILLATION_WEIGHT
|
cosine_weight
|
float
|
Weight of the mean per-sample cosine distance. |
DEFAULT_COSINE_WEIGHT
|
cosine_tail_weight
|
float
|
Weight of the worst-sample cosine distance. |
DEFAULT_COSINE_TAIL_WEIGHT
|
cosine_tail_fraction
|
float
|
Fraction of each batch entering the tail term. |
DEFAULT_COSINE_TAIL_FRACTION
|
Source code in birdnet_stm32/training/distillation.py
compute_loss(x, y, y_pred, sample_weight=None, training=True)
¶
Add multi-label Bernoulli KL divergence to supervised BCE.
Source code in birdnet_stm32/training/distillation.py
all_layers(model)
¶
Return every nested layer of model exactly once, in graph order.
Source code in birdnet_stm32/training/distillation.py
validate_loss_weights(weights)
¶
Raise if a teacher-consistency weighting is outside its valid range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
dict[str, float]
|
Mapping with the four |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any weight is negative or the tail fraction is not in (0, 1]. |