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
add_to_histogram(hist, name, values, ranges, bins)
¶
Accumulate values into hist[name] over the tensor's calibrated range (skipped when it cannot be binned).
Source code in birdnet_stm32/training/qat.py
percentile_range(lo, hi, counts, percentile)
¶
Cut a histogrammed range to percentile at each end as the converter does (the low end only if negative).
Source code in birdnet_stm32/training/qat.py
calibrate_activation_ranges(model, dataset, max_samples=64, percentile=None)
¶
Measure activation ranges on real inputs, as the converter does.
Min/max by default. With percentile (run_qat passes the converter's
range_percentile, a function of the tensor name), a second pass histograms every tensor over
its min/max and cuts the range to that percentile, as
quantize.clip_activation_ranges cuts the deployed model's ranges since 2.0;
the model input and the output logits keep min/max there and here. Without it
QAT would train against coarser grids than the converted model has: on the B0
probe, min/max ranges outside the frontend turned a 0.997-cosine conversion into
a 0.71 one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
Deployment model to probe. |
required |
dataset
|
Iterable
|
Calibration data (iterated twice with |
required |
max_samples
|
int
|
Number of samples to observe. |
64
|
percentile
|
float | Callable[[str], float] | None
|
None for min/max, or the per-tensor percentile kept at each end. |
None
|
Source code in birdnet_stm32/training/qat.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
build_qat_model(deployment_model, activation_ranges, logit_output=True)
¶
Build an activation-fake-quant graph sharing deployment model weights.
A sigmoid head is simulated as the release converts it: INT8 logits, the
sigmoid applied after (convert --output_activation logit, every release
from 1.5). logit_output=False also puts the probabilities on the INT8
output grid of a sigmoid-output conversion (1/256 steps), which floors every
score below ~0.002: QAT trained against that grid while the releases shipped
logits (2.2's N6-B0: simulated logits -15 where the float model has -9).
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
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 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 | |