samstars.api¶
samstars.api
¶
Top-level runtime API for samstars.
SamStars
¶
Load a trained bundle and reuse its model files and stored defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Path
|
Bundle directory containing manifest.json and the trained artifacts. |
required |
Use train to create a bundle and segment to apply it to source rasters
or prepared tiles. Instance methods accept the same processing controls as
the top-level functions.
Source code in samstars/api.py
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 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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 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 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 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 | |
load(model)
classmethod
¶
Load a model bundle or return an existing SamStars object.
Source code in samstars/api.py
253 254 255 256 257 258 | |
segment(*, out, storage, tiles_dir=None, img=None, chm=None, den=None, rgb_bands=None, nir_band=None, texture_band=None, device='cpu', stardist_input_key=None, stardist_use_aux=None, stardist_prob_thr=None, stardist_nms_thr=None, stardist_window_size_m=None, stardist_window_overlap_m=None, stardist_dedup_iou_thr=None, stardist_cpu=False, stardist_resume=False, stardist_resume_from_labels=False, stardist_skip_sources=None, stardist_skip_sources_file=None, run_sam=True, run_stardist=True, skip_existing_sam=True, log_refinement=False, fast=None, fg_extra=None, crop_smooth_sigma=None, smooth_sigma=None, post_smooth_sigma=None)
¶
Run segmentation; fg_extra=None reuses the bundle count (80 for older bundles).
Source code in samstars/api.py
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 | |
train(*, tiles_dir=None, img=None, chm=None, den=None, crowns=None, sam_base_ckpt, out, storage, rgb_bands=None, nir_band=None, texture_band=None, device='cpu', chip_size=512, chip_stride=384, sam_epochs=15, sam_min_iters=0, sam_lr=5e-05, fg_extra=DEFAULT_FG_EXTRA, stardist_epochs=50, stardist_min_iters=0, val_fraction=0.2, crop_smooth_sigma=2.0, smooth_sigma=4.0, post_smooth_sigma=0.0, thin_seeds=True, thin_dist=0.8, seed_d_min=4.5, seed_min_dist_px=4, seed_gauss_sigma=2, seed_min_samples=1, seed_merge_radius=3.0, seed_min_canopy_height=2.0, stardist_input_key='index', stardist_use_aux=True, stardist_normalize_inputs=True, stardist_augment=True, stardist_window_size_m=None, stardist_window_overlap_m=0.0, stardist_dedup_iou_thr=0.0, stardist_cpu=False, fast=False)
classmethod
¶
Train a model bundle; fg_extra controls extra foreground prompts (0 = seed only).
Source code in samstars/api.py
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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 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 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 | |
SamStarsResult
dataclass
¶
Paths returned by a segmentation run.
Attributes:
| Name | Type | Description |
|---|---|---|
out |
Path
|
Final output directory. |
crowns |
Path
|
Crown GeoPackage path, produced when refinement runs. |
labels_dir |
Path | None
|
Label-raster directory when exposed by the run, otherwise None. |
storage |
str
|
Intermediate storage policy: persistent or temp. |
work_dir |
Path | None
|
Retained working directory for persistent runs, otherwise None. |
Source code in samstars/api.py
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 | |
to_dict()
¶
Return a JSON-serializable summary.
Source code in samstars/api.py
206 207 208 209 210 211 212 213 214 | |
segment(*, model, out, storage, tiles_dir=None, img=None, chm=None, den=None, rgb_bands=None, nir_band=None, texture_band=None, device='cpu', stardist_input_key=None, stardist_use_aux=None, stardist_prob_thr=None, stardist_nms_thr=None, stardist_window_size_m=None, stardist_window_overlap_m=None, stardist_dedup_iou_thr=None, stardist_cpu=False, stardist_resume=False, stardist_resume_from_labels=False, stardist_skip_sources=None, stardist_skip_sources_file=None, run_sam=True, run_stardist=True, skip_existing_sam=True, log_refinement=False, fast=None, fg_extra=None, crop_smooth_sigma=None, smooth_sigma=None, post_smooth_sigma=None)
¶
Segment source rasters or prepared tiles using a trained model bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Path | SamStars
|
Model bundle path or a loaded SamStars instance. |
required |
out
|
Path
|
Segmentation output directory, separate from the model bundle. |
required |
storage
|
str
|
persistent keeps intermediates and checkpoints; temp retains final crown outputs after deleting temporary intermediates. |
required |
tiles_dir
|
Path | None
|
Prepared tile directories. Supply this instead of img/chm/den. |
None
|
img
|
Path | None
|
Source imagery. Channel content must match the model's training data. |
None
|
chm
|
Path | None
|
Canopy heights in meters, aligned to the imagery during preparation. |
None
|
den
|
Path | None
|
First-return counts or density, consistent with training units. |
None
|
rgb_bands
|
tuple[int, int, int] | None
|
One-based red, green, blue source indices. None reuses the bundle setting, or (1, 2, 3) when no setting was stored. |
None
|
nir_band
|
int | None
|
Optional NIR source index. None reuses the bundle setting. |
None
|
texture_band
|
int | None
|
Optional texture source index. None reuses the bundle setting. |
None
|
device
|
str
|
PyTorch device for proposals: cpu, cuda, or mps. |
'cpu'
|
stardist_input_key
|
str | None
|
Override the refinement input key stored in the bundle. |
None
|
stardist_use_aux
|
bool | None
|
Override use of the normalized CHM refinement channel. |
None
|
stardist_prob_thr
|
float | None
|
Refinement probability threshold; set with stardist_nms_thr. |
None
|
stardist_nms_thr
|
float | None
|
Refinement suppression threshold; set with stardist_prob_thr. |
None
|
stardist_window_size_m
|
float | None
|
Internal refinement window size in projected map units. |
None
|
stardist_window_overlap_m
|
float | None
|
Window overlap in the same map units. |
None
|
stardist_dedup_iou_thr
|
float | None
|
Final cross-source polygon deduplication threshold. Zero disables it; positive values below one retain the higher-score polygon when pairwise intersection-over-union reaches the threshold. |
None
|
stardist_cpu
|
bool
|
Run refinement in a subprocess with CUDA hidden. |
False
|
stardist_resume
|
bool
|
Reuse complete refinement checkpoints in a persistent run. |
False
|
stardist_resume_from_labels
|
bool
|
Rebuild checkpoints from labels; also requires stardist_resume. Recovered polygons have no confidence score. |
False
|
stardist_skip_sources
|
str | None
|
Comma-separated proposal source names to skip. |
None
|
stardist_skip_sources_file
|
Path | None
|
Text file with one source name per line. |
None
|
run_sam
|
bool
|
Run proposal generation. False reuses saved proposal records. |
True
|
run_stardist
|
bool
|
Run refinement. False stops after proposal generation. |
True
|
skip_existing_sam
|
bool
|
Skip complete proposal outputs already present under out. |
True
|
log_refinement
|
bool
|
Show detailed refinement logs. |
False
|
fast
|
bool | None
|
Override the bundle's batched proposal-decoding setting. |
None
|
fg_extra
|
int | None
|
Override the maximum additional foreground points per seed. None reuses the bundle setting, or 80 for older bundles. Zero keeps only the seed as foreground. Negative points and boxes are unchanged. |
None
|
crop_smooth_sigma
|
float | None
|
Override per-crop proposal smoothing, in pixels. |
None
|
smooth_sigma
|
float | None
|
Override proposal mosaic smoothing, in pixels. |
None
|
post_smooth_sigma
|
float | None
|
Override smoothing after canopy-index fusion, in pixels. |
None
|
Returns:
| Type | Description |
|---|---|
SamStarsResult
|
Output paths in a SamStarsResult. Final crowns are written to |
SamStarsResult
|
out/crowns/crowns.gpkg when refinement runs. Overlapping tiles can leave |
SamStarsResult
|
overlapping polygons; deduplication does not guarantee disjoint crowns. |
Resume only with the same inputs, model, and settings. Use a new output directory when changing them. Band overrides apply to source preparation; prepared tile channels are used as supplied.
Source code in samstars/api.py
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 | |
train(*, tiles_dir=None, img=None, chm=None, den=None, crowns=None, sam_base_ckpt, out, storage, rgb_bands=None, nir_band=None, texture_band=None, device='cpu', chip_size=512, chip_stride=384, sam_epochs=15, sam_min_iters=0, sam_lr=5e-05, fg_extra=DEFAULT_FG_EXTRA, stardist_epochs=50, stardist_min_iters=0, val_fraction=0.2, crop_smooth_sigma=2.0, smooth_sigma=4.0, post_smooth_sigma=0.0, thin_seeds=True, thin_dist=0.8, seed_d_min=4.5, seed_min_dist_px=4, seed_gauss_sigma=2, seed_min_samples=1, seed_merge_radius=3.0, seed_min_canopy_height=2.0, stardist_input_key='index', stardist_use_aux=True, stardist_normalize_inputs=True, stardist_augment=True, stardist_window_size_m=None, stardist_window_overlap_m=0.0, stardist_dedup_iou_thr=0.0, stardist_cpu=False, fast=False)
¶
Train a model bundle from a labeled scene or labeled tile directories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tiles_dir
|
Path | None
|
Directory of consistently raw or prepared labeled tiles. Supply this instead of the four scene inputs. |
None
|
img
|
Path | None
|
RGB or multiband source imagery with a projected CRS and square pixels. |
None
|
chm
|
Path | None
|
Canopy height raster in meters; aligned to img during preparation. |
None
|
den
|
Path | None
|
First-return count or density raster; aligned to img during preparation. |
None
|
crowns
|
Path | None
|
Crown GeoPackage with one polygon feature per tree. |
None
|
sam_base_ckpt
|
Path
|
Base ViT-L checkpoint used to initialize proposal training. |
required |
out
|
Path
|
Destination model bundle directory. Use a new path for each experiment. |
required |
storage
|
str
|
persistent retains intermediates under out/_training; temp retains only the finished bundle. |
required |
rgb_bands
|
tuple[int, int, int] | None
|
One-based source indices in red, green, blue order. Defaults to (1, 2, 3), without inferring a sensor from the band count. |
None
|
nir_band
|
int | None
|
Optional source NIR index for the NDVI cost term. Omit for RGB. |
None
|
texture_band
|
int | None
|
Optional source texture index; otherwise use mean RGB intensity. |
None
|
device
|
str
|
PyTorch device for proposal training: cpu, cuda, or mps. |
'cpu'
|
chip_size
|
int
|
Training chip width and height in pixels. |
512
|
chip_stride
|
int
|
Spacing between training chips in pixels. |
384
|
sam_epochs
|
int
|
Number of proposal-decoder training epochs. |
15
|
sam_min_iters
|
int
|
Minimum proposal training iterations; may increase epochs. |
0
|
sam_lr
|
float
|
Proposal-decoder learning rate. |
5e-05
|
fg_extra
|
int
|
Maximum additional foreground points per seed, a nonnegative integer. Zero keeps only the seed as foreground; the default is 80. Used for decoder training and the proposals that train refinement, and stored in the bundle for later segmentation. |
DEFAULT_FG_EXTRA
|
stardist_epochs
|
int
|
Number of refinement training epochs. |
50
|
stardist_min_iters
|
int
|
Minimum refinement training iterations. |
0
|
val_fraction
|
float
|
Fraction of refinement records assigned to validation. This is a record split, not a spatial holdout. |
0.2
|
crop_smooth_sigma
|
float
|
Proposal smoothing scale within each crop, in pixels. |
2.0
|
smooth_sigma
|
float
|
Proposal mosaic smoothing scale, in pixels. |
4.0
|
post_smooth_sigma
|
float
|
Optional smoothing after canopy-index fusion, in pixels. |
0.0
|
thin_seeds
|
bool
|
Thin nearby seed points after detection. |
True
|
thin_dist
|
float
|
Seed thinning distance in projected CRS units. |
0.8
|
seed_d_min
|
float
|
Minimum peak value, applied to both CHM and density. |
4.5
|
seed_min_dist_px
|
int
|
Seed peak-search radius in pixels. |
4
|
seed_gauss_sigma
|
int
|
Seed detection smoothing scale in pixels. |
2
|
seed_min_samples
|
int
|
Minimum samples in a seed cluster. |
1
|
seed_merge_radius
|
float
|
Candidate grouping distance in projected CRS units. |
3.0
|
seed_min_canopy_height
|
float
|
Minimum canopy height at a seed, in meters. |
2.0
|
stardist_input_key
|
str
|
Refinement input record key, usually index. |
'index'
|
stardist_use_aux
|
bool
|
Include normalized CHM as an auxiliary refinement channel. |
True
|
stardist_normalize_inputs
|
bool
|
Normalize refinement inputs per channel. |
True
|
stardist_augment
|
bool
|
Apply training augmentation to refinement samples. |
True
|
stardist_window_size_m
|
float | None
|
Default refinement window size for later segmentation, in map units, normally meters; None processes each proposal raster whole. |
None
|
stardist_window_overlap_m
|
float
|
Overlap between refinement windows in map units. |
0.0
|
stardist_dedup_iou_thr
|
float
|
Default final polygon deduplication threshold; zero disables it, positive values below one suppress matching overlaps. |
0.0
|
stardist_cpu
|
bool
|
Run refinement training in a subprocess with CUDA hidden. |
False
|
fast
|
bool
|
Use batched proposal decoding. |
False
|
Returns:
| Type | Description |
|---|---|
SamStars
|
A SamStars instance backed by the saved bundle. Band settings and |
SamStars
|
segmentation defaults are recorded in manifest.json. Prepared tiles |
SamStars
|
retain their existing channel order and cost rasters. |
Source code in samstars/api.py
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 | |