qat
birdnet_stm32.training.qat
¶
Keras 3 quantization-aware fine-tuning for full-INT8 TFLite deployment.
The training graph simulates both quantized kernels and per-tensor activation requantization. It shares variables with a clean deployment graph, so the saved checkpoint contains no FakeQuant operators or training-only wrappers.
FakeQuantActivation
¶
Bases: Layer
Static per-tensor INT8 fake quantizer with an asymmetric zero point.
Source code in birdnet_stm32/training/qat.py
set_range(minimum, maximum)
¶
Move the grid, e.g. to the converter's range for the current weights.
Source code in birdnet_stm32/training/qat.py
call(inputs)
¶
Apply the same scalar affine grid used for TFLite activations.
Source code in birdnet_stm32/training/qat.py
get_config()
¶
Return serializable quantizer settings.
RangeRefresh
¶
Bases: Callback
Recalibrate QAT activation ranges on the current weights after each epoch.
QAT otherwise trains against ranges measured once on the starting weights, while conversion recalibrates the weights it is given. The longer and the faster fine-tuning moves the weights, the further the two grids drift.
Source code in birdnet_stm32/training/qat.py
fake_quantize_weights(w, num_bits=8, per_channel=True, channel_axis=-1)
¶
Quantize and dequantize a kernel on TFLite's symmetric INT8 grid.
Source code in birdnet_stm32/training/qat.py
calibrate_activation_ranges(model, dataset, max_samples=64)
¶
Measure absolute min/max activation ranges on real inputs, as the converter does.
Percentile-clipped ranges were measured and removed: p99.9 and p99.99 both scored below the full range (see docs/dev/int8-parity-plan.md).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
Deployment model to probe. |
required |
dataset
|
Iterable
|
Calibration data. |
required |
max_samples
|
int
|
Number of samples to observe. |
64
|
Source code in birdnet_stm32/training/qat.py
build_qat_model(deployment_model, activation_ranges)
¶
Build an activation-fake-quant graph sharing deployment model weights.
Source code in birdnet_stm32/training/qat.py
refresh_activation_ranges(qat_student, ranges)
¶
Move every fake quantizer to a new range; returns how many moved.
Source code in birdnet_stm32/training/qat.py
sync_frontend_weights(qat_model, deployment_model)
¶
Copy separately cloned custom-frontend weights into the clean model.
Source code in birdnet_stm32/training/qat.py
freeze_batch_norm(model)
¶
Freeze all BatchNormalization layers, including nested frontend BN.
Source code in birdnet_stm32/training/qat.py
run_qat(args)
¶
Fine-tune a pretrained model against weight and activation INT8 noise.
Source code in birdnet_stm32/training/qat.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |