qat
birdnet_stm32.training.qat
¶
Quantization-Aware Training (QAT) via shadow-weight fake-quantization.
Manual QAT for Keras 3 (tensorflow-model-optimization is incompatible). Injects INT8 quantization noise during fine-tuning so the model learns weights that survive Post-Training Quantization with minimal accuracy loss.
No FakeQuant ops remain in the saved model — full STM32N6 NPU compatibility.
Usage::
python -m birdnet_stm32 train --data_path_train data/train \
--qat --checkpoint_path checkpoints/best_model.keras \
--epochs 10 --learning_rate 0.0001
QATCallback
¶
Bases: Callback
Shadow-weight fake-quantization callback for QAT fine-tuning.
Before each training step:
- Save full-precision (FP32) weight copies.
- Replace kernel weights with fake-quantized (INT8-simulated) versions. Biases are left at full precision (INT32 in TFLite).
After each training step:
- Compute optimizer delta (post-update weight minus pre-update quantized weight).
- Apply delta to full-precision weights.
This ensures gradients flow through quantized weights (approximate STE) while maintaining full-precision weight accumulation across steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_bits
|
int
|
Quantization bit width (default: 8). |
8
|
per_channel
|
bool
|
Per-channel (True) or per-tensor (False) quantization. |
True
|
Source code in birdnet_stm32/training/qat.py
on_train_begin(logs=None)
¶
Identify quantizable layers and report statistics.
Source code in birdnet_stm32/training/qat.py
on_train_batch_begin(batch, logs=None)
¶
Save FP weights and inject fake-quantized versions for forward pass.
Source code in birdnet_stm32/training/qat.py
on_train_batch_end(batch, logs=None)
¶
Transfer optimizer's gradient update to full-precision weights.
Source code in birdnet_stm32/training/qat.py
fake_quantize_weights(w, num_bits=8, per_channel=True, channel_axis=-1)
¶
Simulate INT8 quantization on a weight array (quantize then dequantize).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w
|
ndarray
|
Weight tensor (float32). |
required |
num_bits
|
int
|
Quantization bit width. |
8
|
per_channel
|
bool
|
Per-channel (True) or per-tensor (False) quantization. |
True
|
channel_axis
|
int
|
Axis for per-channel quantization. |
-1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Fake-quantized weight tensor (float32, same shape). |
Source code in birdnet_stm32/training/qat.py
freeze_batch_norm(model)
¶
Freeze all BatchNormalization layers (standard for QAT fine-tuning).
Prevents BN running statistics from drifting due to quantization noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
Keras model. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of frozen BN layers. |
Source code in birdnet_stm32/training/qat.py
run_qat(args)
¶
Run QAT fine-tuning from CLI args.
Loads a pretrained model, freezes BatchNorm layers, and fine-tunes
with shadow-weight fake-quantization. Saves the QAT model as
{checkpoint_path_stem}_qat.keras.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Parsed CLI arguments (checkpoint_path, data_path_train, epochs, learning_rate, batch_size, etc.). |
required |
Source code in birdnet_stm32/training/qat.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |