landcover_train module¶
Landcover Classification Training Module
This module extends the base geoai training functionality with specialized components for discrete landcover classification, including: - Enhanced loss functions with boundary weighting - Per-class frequency weighting for imbalanced datasets - Configurable ignore_index handling - Additional validation metrics
Key Features: - Maintains full compatibility with base geoai workflow - Adds optional advanced loss computation modes - Provides flexible ignore_index configuration - Optimized for multi-class landcover segmentation
Author: ValHab Project Date: November 2025
DiceLoss
¶
Bases: Module
Dice loss for semantic segmentation.
Computes the Sørensen–Dice coefficient between predictions and targets, which measures region overlap. Effective for class-imbalanced datasets because it evaluates per-class overlap rather than per-pixel accuracy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smooth
|
float
|
Smoothing constant to avoid division by zero. |
1.0
|
ignore_index
|
int
|
Target value that is ignored and does not contribute to the loss. |
-100
|
reduction
|
str
|
Reduction to apply: |
'mean'
|
weight
|
Optional[Tensor]
|
Optional per-class weights tensor of shape |
None
|
Source code in geoai/landcover_train.py
161 162 163 164 165 166 167 168 169 170 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 | |
forward(inputs, targets)
¶
Compute Dice loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Predictions of shape |
required |
targets
|
Tensor
|
Ground truth of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss when |
Tensor
|
or a per-class tensor of shape |
Source code in geoai/landcover_train.py
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 | |
FocalLoss
¶
Bases: Module
Focal Loss for addressing class imbalance in segmentation.
Reference: Lin, T. Y., Goyal, P., Girshick, R., He, K., & Dollár, P. (2017). Focal loss for dense object detection. ICCV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
Weighting factor in range (0,1) to balance positive/negative examples |
1.0
|
|
gamma
|
Exponent of the modulating factor (1 - p_t)^gamma |
2.0
|
|
ignore_index
|
Specifies a target value that is ignored |
-100
|
|
reduction
|
Specifies the reduction to apply to the output |
'mean'
|
|
weight
|
Manual rescaling weight given to each class |
None
|
Source code in geoai/landcover_train.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
forward(inputs, targets)
¶
Forward pass of focal loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Predictions (N, C, H, W) where C = number of classes |
required | |
targets
|
Ground truth (N, H, W) with class indices |
required |
Returns:
| Type | Description |
|---|---|
|
Loss value |
Source code in geoai/landcover_train.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
LandcoverCrossEntropyLoss
¶
Bases: Module
Enhanced CrossEntropyLoss with optional ignore_index and class weights.
This extends the standard CrossEntropyLoss with more flexible ignore_index handling, specifically designed for landcover classification tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
Optional[Tensor]
|
Manual rescaling weight given to each class |
None
|
ignore_index
|
Union[int, bool]
|
Specifies a target value that is ignored. - False: No values ignored (standard behavior) - int: Specific class index to ignore (e.g., 0 for background) |
False
|
reduction
|
str
|
Specifies the reduction to apply ('mean', 'sum', 'none') |
'mean'
|
Source code in geoai/landcover_train.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
forward(input, target)
¶
Compute cross entropy loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Predictions (N, C, H, W) where C = number of classes |
required |
target
|
Tensor
|
Ground truth (N, H, W) with class indices |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Loss value |
Source code in geoai/landcover_train.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
TverskyLoss
¶
Bases: Module
Tversky loss for semantic segmentation.
Generalises the Dice loss by allowing asymmetric weighting of false
positives and false negatives. Setting alpha = beta = 0.5 recovers
the standard Dice loss. Increasing beta relative to alpha
penalises false negatives more, which improves recall on rare classes.
Reference
Salehi, S. S. M., Erdogmus, D., & Gholipour, A. (2017). Tversky loss function for image segmentation using 3D fully convolutional deep networks. MLMI Workshop, MICCAI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Weight for false positives. |
0.5
|
beta
|
float
|
Weight for false negatives. |
0.5
|
smooth
|
float
|
Smoothing constant to avoid division by zero. |
1.0
|
ignore_index
|
int
|
Target value that is ignored. |
-100
|
reduction
|
str
|
|
'mean'
|
weight
|
Optional[Tensor]
|
Optional per-class weights tensor of shape |
None
|
Source code in geoai/landcover_train.py
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 | |
forward(inputs, targets)
¶
Compute Tversky loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Predictions of shape |
required |
targets
|
Tensor
|
Ground truth of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss when |
Tensor
|
or a per-class tensor of shape |
Source code in geoai/landcover_train.py
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 | |
UnifiedFocalLoss
¶
Bases: Module
Unified Focal Loss combining distribution-based and region-based losses.
Implements the framework from Yeung et al. (2021) which unifies focal cross-entropy (distribution-based) and focal Tversky (region-based) losses into a single compound loss. This is particularly effective for semantic segmentation with severe class imbalance.
The combined loss is::
1 | |
where L_dist is focal cross-entropy and L_region is focal Tversky.
Reference
Yeung, M., Sala, E., Schönlieb, C.-B., & Rundo, L. (2021). Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medical image segmentation. Computerized Medical Imaging and Graphics, 95, 102026.
Note
Inspired by the implementation in the
terrainseg <https://github.com/maxwell-geospatial/terrainseg>_
package by Maxwell (2026).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lambda_
|
float
|
Balance between distribution and region components.
|
0.5
|
gamma
|
float
|
Focusing parameter for both components. Higher values down-weight easy examples more aggressively. |
0.75
|
delta
|
float
|
Tversky false-negative weight; false-positive weight is
|
0.6
|
smooth
|
float
|
Smoothing constant for the Tversky denominator. |
1.0
|
ignore_index
|
int
|
Target value that is ignored. |
-100
|
weight
|
Optional[Tensor]
|
Per-class weights for the distribution (focal CE) component. |
None
|
region_weight
|
Optional[Tensor]
|
Per-class weights for the region (focal Tversky)
component. Falls back to |
None
|
use_log_cosh
|
bool
|
Apply |
False
|
Source code in geoai/landcover_train.py
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 | |
forward(inputs, targets)
¶
Compute unified focal loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Predictions of shape |
required |
targets
|
Tensor
|
Ground truth of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss value. |
Source code in geoai/landcover_train.py
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 | |
compute_class_weights(labels_dir, num_classes, ignore_index=-100, custom_multipliers=None, max_weight=50.0, use_inverse_frequency=True)
¶
Compute class weights for imbalanced datasets with optional custom multipliers and maximum weight cap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels_dir
|
str
|
Directory containing label files |
required |
num_classes
|
int
|
Number of classes |
required |
ignore_index
|
Union[int, bool]
|
Class index to ignore when computing weights. - If int: specific class to ignore (pixels will be excluded from weight calc) - If False: no class ignored (all classes contribute to weights) |
-100
|
custom_multipliers
|
Optional[Dict[int, float]]
|
Custom multipliers for specific classes after inverse frequency calculation. Format: {class_id: multiplier} Example: {1: 0.5, 7: 2.0} - reduce class 1 weight by half, double class 7 weight |
None
|
max_weight
|
float
|
Maximum allowed weight value to prevent extreme values (default: 50.0) |
50.0
|
use_inverse_frequency
|
bool
|
Whether to compute inverse frequency weights. - True (default): Compute inverse frequency weights, then apply custom multipliers - False: Use uniform weights (1.0) for all classes, then apply custom multipliers |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of class weights (num_classes,) with custom adjustments and maximum weight cap applied |
Source code in geoai/landcover_train.py
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 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 | |
evaluate_sparse_iou(model, images_dir, labels_dir, num_classes, num_channels=3, batch_size=8, background_class=0, ignore_index=False, device=None, verbose=True)
¶
Evaluate a trained model using sparse labels IoU.
This function is designed for incomplete/sparse ground truth where background (0) means "unlabeled" rather than "definitely not this class". Predictions in background areas are NOT penalized as false positives.
Use this for post-training evaluation when your training masks are incomplete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Trained segmentation model |
required |
images_dir
|
str
|
Directory containing validation images |
required |
labels_dir
|
str
|
Directory containing validation labels |
required |
num_classes
|
int
|
Number of classes |
required |
num_channels
|
int
|
Number of input channels (default: 3) |
3
|
batch_size
|
int
|
Batch size for evaluation (default: 8) |
8
|
background_class
|
int
|
Class ID for background/unlabeled pixels (default: 0) |
0
|
ignore_index
|
Union[int, bool]
|
Class to ignore during evaluation. - If int: specific class index to ignore - If False: no class ignored (default) |
False
|
device
|
Optional[device]
|
Torch device (auto-detected if None) |
None
|
verbose
|
bool
|
Print detailed results (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing: |
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Dict[str, Any]
|
|
Example
model = torch.load("best_model.pth") results = evaluate_sparse_iou( ... model=model, ... images_dir="tiles/images", ... labels_dir="tiles/labels", ... num_classes=18, ... background_class=0, ... ) print(f"Sparse IoU: {results['mean_sparse_iou']:.4f}")
Source code in geoai/landcover_train.py
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 | |
get_landcover_loss_function(loss_name='crossentropy', num_classes=2, ignore_index=-100, class_weights=None, use_class_weights=False, focal_alpha=1.0, focal_gamma=2.0, device=None, smooth=1.0, tversky_alpha=0.5, tversky_beta=0.5, ufl_lambda=0.5, ufl_gamma=0.75, ufl_delta=0.6, region_weights=None, use_log_cosh=False)
¶
Get loss function configured for landcover segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_name
|
str
|
Name of loss function. One of |
'crossentropy'
|
num_classes
|
int
|
Number of classes. |
2
|
ignore_index
|
Union[int, bool]
|
Class index to ignore, or |
-100
|
class_weights
|
Optional[Tensor]
|
Manual class weights tensor. |
None
|
use_class_weights
|
bool
|
Whether to use class weights. |
False
|
focal_alpha
|
float
|
Alpha parameter for focal loss. |
1.0
|
focal_gamma
|
float
|
Gamma parameter for focal loss. |
2.0
|
device
|
Optional[device]
|
Device to place loss function on. |
None
|
smooth
|
float
|
Smoothing constant for Dice / Tversky denominator. |
1.0
|
tversky_alpha
|
float
|
False-positive weight for Tversky loss. |
0.5
|
tversky_beta
|
float
|
False-negative weight for Tversky loss. |
0.5
|
ufl_lambda
|
float
|
Balance between distribution and region components in
Unified Focal Loss ( |
0.5
|
ufl_gamma
|
float
|
Focusing parameter for Unified Focal Loss. |
0.75
|
ufl_delta
|
float
|
Tversky false-negative weight for Unified Focal Loss
(false-positive weight is |
0.6
|
region_weights
|
Optional[Tensor]
|
Per-class weights for the region component of
Unified Focal Loss. Falls back to class_weights if |
None
|
use_log_cosh
|
bool
|
Apply |
False
|
Returns:
| Type | Description |
|---|---|
Module
|
Configured loss function. |
Source code in geoai/landcover_train.py
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 | |
landcover_iou(pred, target, num_classes, ignore_index=False, smooth=1e-06, mode='mean', boundary_weight_map=None, background_class=None)
¶
Calculate IoU for landcover classification with multiple weighting options.
Supports four IoU calculation modes: 1. "mean": Simple mean IoU across all classes 2. "perclass_frequency": Weight by per-class pixel frequency 3. "boundary_weighted": Weight by distance to class boundaries 4. "sparse_labels": For incomplete ground truth - only penalize FP where GT is positive (does NOT penalize predictions in unlabeled/background areas)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
Tensor
|
Predicted classes (N, H, W) or logits (N, C, H, W) |
required |
target
|
Tensor
|
Ground truth (N, H, W) |
required |
num_classes
|
int
|
Number of classes |
required |
ignore_index
|
Union[int, bool]
|
Class index to ignore (default: None) |
False
|
smooth
|
float
|
Smoothing factor to avoid division by zero |
1e-06
|
mode
|
str
|
IoU calculation mode ("mean", "perclass_frequency", "boundary_weighted", "sparse_labels") |
'mean'
|
boundary_weight_map
|
Optional[Tensor]
|
Optional boundary weights (N, H, W) |
None
|
background_class
|
Optional[int]
|
Background/unlabeled class for sparse_labels mode (default: 0) |
None
|
Returns:
| Type | Description |
|---|---|
Union[float, Tuple[float, List[float], List[int]]]
|
If mode == "mean": float (mean IoU) |
Union[float, Tuple[float, List[float], List[int]]]
|
If mode == "perclass_frequency": tuple (weighted IoU, per-class IoUs, class counts) |
Union[float, Tuple[float, List[float], List[int]]]
|
If mode == "boundary_weighted": float (boundary-weighted IoU) |
Union[float, Tuple[float, List[float], List[int]]]
|
If mode == "sparse_labels": tuple (sparse IoU, per-class IoUs, per-class recall, per-class precision) |
Source code in geoai/landcover_train.py
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 | |
train_segmentation_landcover(images_dir, labels_dir, output_dir, input_format='directory', architecture='unet', encoder_name='resnet34', encoder_weights='imagenet', num_channels=3, num_classes=2, batch_size=8, num_epochs=50, learning_rate=0.001, weight_decay=0.0001, seed=42, val_split=0.2, print_freq=10, verbose=True, save_best_only=True, plot_curves=False, device=None, checkpoint_path=None, resume_training=False, target_size=None, resize_mode='resize', num_workers=None, loss_function='crossentropy', ignore_index=0, use_class_weights=False, focal_alpha=1.0, focal_gamma=2.0, smooth=1.0, tversky_alpha=0.5, tversky_beta=0.5, ufl_lambda=0.5, ufl_gamma=0.75, ufl_delta=0.6, region_weights=None, use_log_cosh=False, custom_multipliers=None, max_class_weight=50.0, use_inverse_frequency=True, validation_iou_mode='standard', boundary_alpha=1.0, background_class=0, training_callback=None, **kwargs)
¶
Train a semantic segmentation model with landcover-specific enhancements.
This is a standalone version that wraps geoai.train.train_segmentation_model with landcover-specific loss functions, class weights, and metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images_dir
|
str
|
Directory containing training images. |
required |
labels_dir
|
str
|
Directory containing training labels. |
required |
output_dir
|
str
|
Directory to save model checkpoints and training history. |
required |
input_format
|
str
|
Data format ( |
'directory'
|
architecture
|
str
|
Model architecture (default: |
'unet'
|
encoder_name
|
str
|
Encoder backbone (default: |
'resnet34'
|
encoder_weights
|
Optional[str]
|
Pretrained weights ( |
'imagenet'
|
num_channels
|
int
|
Number of input channels (default: 3). |
3
|
num_classes
|
int
|
Number of output classes (default: 2). |
2
|
batch_size
|
int
|
Training batch size (default: 8). |
8
|
num_epochs
|
int
|
Number of training epochs (default: 50). |
50
|
learning_rate
|
float
|
Initial learning rate (default: 0.001). |
0.001
|
weight_decay
|
float
|
Weight decay for optimizer (default: 1e-4). |
0.0001
|
seed
|
int
|
Random seed for reproducibility (default: 42). |
42
|
val_split
|
float
|
Validation split ratio (default: 0.2). |
0.2
|
print_freq
|
int
|
Frequency of training progress prints (default: 10). |
10
|
verbose
|
bool
|
Enable verbose output (default: True). |
True
|
save_best_only
|
bool
|
Only save best model checkpoint (default: True). |
True
|
plot_curves
|
bool
|
Plot training curves at end (default: False). |
False
|
device
|
Optional[device]
|
Torch device (auto-detected if |
None
|
checkpoint_path
|
Optional[str]
|
Path to checkpoint for resuming training. |
None
|
resume_training
|
bool
|
Whether to resume from checkpoint (default: False). |
False
|
target_size
|
Optional[Tuple[int, int]]
|
Target size for resizing images |
None
|
resize_mode
|
str
|
How to resize ( |
'resize'
|
num_workers
|
Optional[int]
|
Number of dataloader workers (default: auto). |
None
|
loss_function
|
str
|
Loss function name. One of |
'crossentropy'
|
ignore_index
|
Union[int, bool]
|
Class index to ignore during training (default: 0).
Set to |
0
|
use_class_weights
|
bool
|
Whether to compute and use class weights (default: False). |
False
|
focal_alpha
|
float
|
Focal loss alpha parameter (default: 1.0). |
1.0
|
focal_gamma
|
float
|
Focal loss gamma parameter (default: 2.0). |
2.0
|
smooth
|
float
|
Smoothing constant for Dice / Tversky denominator (default: 1.0). |
1.0
|
tversky_alpha
|
float
|
False-positive weight for Tversky loss (default: 0.5). |
0.5
|
tversky_beta
|
float
|
False-negative weight for Tversky loss (default: 0.5). Increase relative to tversky_alpha to improve recall. |
0.5
|
ufl_lambda
|
float
|
Balance between distribution and region components in Unified Focal Loss (default: 0.5). |
0.5
|
ufl_gamma
|
float
|
Focusing parameter for Unified Focal Loss (default: 0.75). |
0.75
|
ufl_delta
|
float
|
Tversky false-negative weight inside Unified Focal Loss (default: 0.6). |
0.6
|
region_weights
|
Optional[Tensor]
|
Per-class weights for the region component of
Unified Focal Loss. Falls back to class_weights if |
None
|
use_log_cosh
|
bool
|
Apply |
False
|
custom_multipliers
|
Optional[Dict[int, float]]
|
Custom class weight multipliers
|
None
|
max_class_weight
|
float
|
Maximum allowed class weight (default: 50.0). |
50.0
|
use_inverse_frequency
|
bool
|
Use inverse frequency for weights (default: True). |
True
|
validation_iou_mode
|
str
|
IoU calculation mode for validation
(default: |
'standard'
|
boundary_alpha
|
float
|
Boundary importance factor for wIoU mode (default: 1.0). |
1.0
|
background_class
|
int
|
Class ID for background/unlabeled pixels in sparse_labels mode (default: 0). |
0
|
training_callback
|
Optional[callable]
|
Optional callback function for automatic metric tracking. |
None
|
**kwargs
|
Any
|
Additional arguments passed to base training function. |
{}
|
Returns:
| Type | Description |
|---|---|
Module
|
Trained model |
Example
from landcover_train import train_segmentation_landcover
model = train_segmentation_landcover( ... images_dir="tiles/images", ... labels_dir="tiles/labels", ... output_dir="models/landcover_001", ... num_classes=5, ... loss_function="focal", ... ignore_index=0, # Ignore background ... use_class_weights=True, ... custom_multipliers={1: 1.5, 4: 0.8}, # Boost class 1, reduce class 4 ... max_class_weight=50.0, ... use_inverse_frequency=True, # Use inverse frequency weighting ... validation_iou_mode="boundary_weighted", # Focus on boundaries ... boundary_alpha=2.0, # Moderate boundary emphasis ... )
Source code in geoai/landcover_train.py
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 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 | |