equalize
birdnet_stm32.conversion.equalize
¶
Equalize per-band INT8 ranges in the raw frontend without changing its function.
Every internal frontend tensor is quantized per tensor across all bands, and on the v1.2 raw model a single band sets each range: the median band's typical filterbank value is below one INT8 step. Three per-band rescalings are exact in float, so they redistribute the grid without retraining:
- Filterbank gain
s(stagefb). Multiply band c's quadrature kernels bys_c.|x|, the magnitude approximation and the per-band smoothing are all positively homogeneous per band, so every tensor up toband_bnscales bys_c;band_bnabsorbs it exactly (mean*s, variance*s^2, gamma corrected for epsilon). - PWL input gain
t(stagepwl_in). Scaleband_bngamma and beta byt_c(soband_reluscales byt_c) and divide thek0and hinge-shift input weights byt_c. - Hinge gain
a(stagehinge). Scale hinge i's shift weight and bias bya_i,c(its ReLU output scales bya_i,c) and divide its slopek_iby it.
Per-channel symmetric weight quantization is invariant to a per-output-channel scale, so the INT8 filterbank kernels are unchanged; what moves is how much of each activation grid a band gets. The PWL output tensors feed the backbone as spatial rows, where no per-band freedom exists, and are left alone.
Gains bring every band to the median band's p99.9 magnitude on calibration
inputs. Measured on the v1.2 raw model: +0.028 validation cMAP after
post-training quantization, +0.017 catalog cMAP after QAT, float output
unchanged to 6e-7. See docs/dev/int8-parity-plan.md.
band_percentiles(model, frontend, tensors, names)
¶
Per-band PERCENTILE of |x| for each named frontend tensor.
Source code in birdnet_stm32/conversion/equalize.py
spread(p)
¶
Tensor range over the median band's percentile: what equalization shrinks.
gains(p)
¶
Bring every active band to the median band's percentile; leave dead bands alone.
Source code in birdnet_stm32/conversion/equalize.py
equalize_raw_frontend(model, tensors, check, stages=STAGES)
¶
Equalize model's raw frontend in place and return a report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
Float Keras model with a raw |
required |
tensors
|
Sequence[ndarray]
|
Calibration inputs (with batch dimension) that set the gains. |
required |
check
|
Sequence[ndarray]
|
Held-out inputs on which the float output must not change. |
required |
stages
|
Iterable[str]
|
Subset of |
STAGES
|
Raises:
| Type | Description |
|---|---|
ValueError
|
The model is not a raw PWL frontend, or a stage is unknown. |
RuntimeError
|
The float output moved by more than |
Source code in birdnet_stm32/conversion/equalize.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |