board_test
birdnet_stm32.deploy.board_test
¶
Board test: standalone firmware on STM32N6570-DK.
Full on-device pipeline — nothing is precomputed on the host: 1. Deploy model: stedgeai generate → patch NPU_Validation project → n6_loader build + flash. 2. Firmware on board: read WAV from SD card → frontend-specific preprocessing (raw normalization, STFT, or STFT + mel) → NPU inference → UART results. 3. This script captures UART output and parses per-file predictions. 4. Optionally, the same .tflite runs on the host over local copies of the SD card's WAV files, through the host's own evaluation preprocessing, and each file's board result is checked against it. That comparison is the point of the test: a model that ships has to compute on the device what it computes on the host.
Requires: - USB-connected STM32N6570-DK with an SD card containing audio/ WAV files. - pyserial (pip install pyserial).
BoardTestConfig
dataclass
¶
Configuration for standalone on-board inference tests.
Attributes:
| Name | Type | Description |
|---|---|---|
deploy_cfg |
DeployConfig
|
Base deployment configuration (paths to tools, model, etc.). |
model_config_path |
str
|
Path to the _model_config.json file. |
labels_path |
str
|
Path to the _labels.txt file. |
serial_port |
str
|
Serial port for UART capture (e.g. /dev/ttyACM0). |
top_k |
int
|
Number of top predictions to show per file. |
score_threshold |
float
|
Minimum score to display. |
timeout |
int
|
Maximum seconds to wait for firmware to finish. |
host_audio_dir |
str
|
Local copy of the SD card's audio/ folder. When set, the host scores the same files and the board is checked against it. |
parity_tolerance |
float
|
Margin within which a different top-1 is a tie, and within which a host score counts as borderline to the detection threshold. Larger score differences are flagged, not failed. |
detection_threshold |
float
|
Score at which the device reports a detection. |
Source code in birdnet_stm32/deploy/board_test.py
load_model_config(config_path)
¶
Load model configuration from JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to _model_config.json. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with model configuration. |
Source code in birdnet_stm32/deploy/board_test.py
load_labels(labels_path)
¶
Load class labels from a labels.txt file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels_path
|
str
|
Path to _labels.txt (one label per line). |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of label strings. |
Source code in birdnet_stm32/deploy/board_test.py
patch_project(project, model_cfg, num_classes, labels)
¶
Copy firmware sources into the NPU_Validation project and patch build files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project
|
Path
|
Path to the NPU_Validation project root. |
required |
model_cfg
|
dict
|
Model configuration dict from _model_config.json. |
required |
num_classes
|
int
|
Number of output classes. |
required |
labels
|
list[str]
|
Ordered list of class label strings. If empty, generates
placeholder |
required |
Returns a list of .bak paths that must be passed to restore_project.
Source code in birdnet_stm32/deploy/board_test.py
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 | |
restore_project(backups)
¶
Undo all project patches by restoring .bak files and removing new files.
For each entry in backups:
- If it is a directory (FatFs), remove it entirely.
- If it is a .bak path and the .bak exists, move it back over the original.
- If it is a .bak path but no .bak was created (file was new), delete
the original that we copied in.
Source code in birdnet_stm32/deploy/board_test.py
parse_serial_output(lines, labels, top_k=5, threshold=0.01)
¶
Parse firmware UART output into structured results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
list[str]
|
Raw serial lines captured from the board. |
required |
labels
|
list[str]
|
Class label list (for reference; firmware already prints names). |
required |
top_k
|
int
|
Max detections per file to keep. |
5
|
threshold
|
float
|
Minimum score fraction (0–1) to include. |
0.01
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with 'results' (list of per-file dicts), 'processed', 'errors', |
dict
|
'raw_lines'. |
Source code in birdnet_stm32/deploy/board_test.py
440 441 442 443 444 445 446 447 448 449 450 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 | |
firmware_top_k(scores, k, threshold)
¶
Return the class indices the firmware would print, in its order.
A replica of print_top_k in firmware/Src/main.c: a partial selection
sort that swaps only on a strictly greater score, stopping at the first
score below threshold. INT8 outputs tie often -- several classes can
saturate at the same code -- so the tie order has to be the firmware's,
not numpy's.
Source code in birdnet_stm32/deploy/board_test.py
host_reference_scores(model_path, model_cfg, audio_dir, board_files)
¶
Score the board's files on the host with the same .tflite.
Each file goes through the host's evaluation preprocessing
(make_chunks_for_file), and its first chunk is scored -- the chunk the
firmware reads. Using the host pipeline rather than a re-implementation of
the firmware is deliberate: a firmware frontend that diverges from what the
model was evaluated with has to show up as a parity failure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str
|
The .tflite deployed to the board. |
required |
model_cfg
|
dict
|
Its model config. |
required |
audio_dir
|
str | Path
|
Local copy of the SD card's audio/ folder. |
required |
board_files
|
list[str]
|
File names as the board printed them (FAT may upper-case). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Board file name -> [num_classes] host scores. |
Source code in birdnet_stm32/deploy/board_test.py
load_truth(audio_dir)
¶
Read file -> true_species from a manifest.csv beside or above audio_dir.
Source code in birdnet_stm32/deploy/board_test.py
compare_board_host(board_results, host_scores, labels, *, top_k, threshold, tolerance, detection_threshold=0.5, truth=None)
¶
Check that the board reports the same detections as the host.
A file agrees when both hold:
- the board's top-1 is the host's top-1, or a label the host scores within
toleranceof its own top-1 (a tie, not a disagreement); - board and host make the same call at
detection_threshold-- unless the host's score is withintoleranceof the threshold, where INT8 rounding on the NPU can legitimately tip it (flaggedborderline).
Score differences above tolerance that change neither are flagged
drift but do not fail the file. A broken model fails on top-1.
Returns:
| Type | Description |
|---|---|
dict
|
Dict with per-file |
Source code in birdnet_stm32/deploy/board_test.py
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 | |
print_parity(parity)
¶
Print the per-file host x board comparison and its verdict.
Source code in birdnet_stm32/deploy/board_test.py
run_board_test(cfg)
¶
Execute the full on-board inference test.
Steps: 1. stedgeai generate → produce NPU binary from the TFLite model. 2. Patch NPU_Validation project with our firmware sources. 3. n6_loader: copy network.c, build firmware, flash, run. 4. Capture UART output until '=== DONE ===' marker. 5. Restore the NPU_Validation project to its original state. 6. Parse and report results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
BoardTestConfig
|
Board test configuration. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict with 'results', 'processed', 'errors', 'raw_lines', 'labels'. |
Source code in birdnet_stm32/deploy/board_test.py
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 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 | |