classify module¶
The module for training semantic segmentation models for classifying remote sensing imagery.
classify_image(image_path, model_path, output_path=None, chip_size=1024, overlap=256, batch_size=4, colormap=None, **kwargs)
¶
Classify a geospatial image using a trained semantic segmentation model.
This function handles the full image classification pipeline with special attention to edge handling: 1. Process the image in a grid pattern with overlapping tiles 2. Use central regions of tiles for interior parts 3. Special handling for edges to ensure complete coverage 4. Merge results into a single georeferenced output
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_path
|
str
|
Path to the input GeoTIFF image. |
required |
model_path
|
str
|
Path to the trained model checkpoint. |
required |
output_path
|
str
|
Path to save the output classified image. Defaults to "[input_name]_classified.tif". |
None
|
chip_size
|
int
|
Size of chips for processing. Defaults to 1024. |
1024
|
overlap
|
int
|
Overlap size between adjacent tiles. Defaults to 256. |
256
|
batch_size
|
int
|
Batch size for inference. Defaults to 4. |
4
|
colormap
|
dict
|
Colormap to apply to the output image. Defaults to None. |
None
|
**kwargs
|
Additional keyword arguments for DataLoader. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
Path to the saved classified image. |
Source code in geoai/classify.py
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 812 813 814 815 816 817 818 819 820 821 822 823 824 825 |
|
classify_images(image_paths, model_path, output_dir=None, chip_size=1024, batch_size=4, colormap=None, file_extension='.tif', **kwargs)
¶
Classify multiple geospatial images using a trained semantic segmentation model.
This function accepts either a list of image paths or a directory containing images and applies the classify_image function to each image, saving the results in the specified output directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_paths
|
str or list
|
Either a directory path containing images or a list of paths to input GeoTIFF images. |
required |
model_path
|
str
|
Path to the trained model checkpoint. |
required |
output_dir
|
str
|
Directory to save the output classified images. Defaults to None (same directory as input images for a list, or a new "classified" subdirectory for a directory input). |
None
|
chip_size
|
int
|
Size of chips for processing. Defaults to 1024. |
1024
|
batch_size
|
int
|
Batch size for inference. Defaults to 4. |
4
|
colormap
|
dict
|
Colormap to apply to the output images. Defaults to None. |
None
|
file_extension
|
str
|
File extension to filter by when image_paths is a directory. Defaults to ".tif". |
'.tif'
|
**kwargs
|
Additional keyword arguments for the classify_image function. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
list |
List of paths to the saved classified images. |
Source code in geoai/classify.py
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 926 927 928 929 930 931 932 |
|
train_classifier(image_root, label_root, output_dir='output', in_channels=4, num_classes=14, epochs=20, img_size=256, batch_size=8, sample_size=500, model='unet', backbone='resnet50', weights=True, num_filters=3, loss='ce', class_weights=None, ignore_index=None, lr=0.001, patience=10, freeze_backbone=False, freeze_decoder=False, transforms=None, use_augmentation=False, seed=42, train_val_test_split=(0.6, 0.2, 0.2), accelerator='auto', devices='auto', logger=None, callbacks=None, log_every_n_steps=10, use_distributed_sampler=False, monitor_metric='val_loss', mode='min', save_top_k=1, save_last=True, checkpoint_filename='best_model', checkpoint_path=None, every_n_epochs=1, **kwargs)
¶
Train a semantic segmentation model on geospatial imagery.
This function sets up datasets, model, trainer, and executes the training process for semantic segmentation tasks using geospatial data. It supports training from scratch or resuming from a checkpoint if available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_root
|
str
|
Path to directory containing imagery. |
required |
label_root
|
str
|
Path to directory containing land cover labels. |
required |
output_dir
|
str
|
Directory to save model outputs and checkpoints. Defaults to "output". |
'output'
|
in_channels
|
int
|
Number of input channels in the imagery. Defaults to 4. |
4
|
num_classes
|
int
|
Number of classes in the segmentation task. Defaults to 14. |
14
|
epochs
|
int
|
Number of training epochs. Defaults to 20. |
20
|
img_size
|
int
|
Size of image patches for training. Defaults to 256. |
256
|
batch_size
|
int
|
Batch size for training. Defaults to 8. |
8
|
sample_size
|
int
|
Number of samples per epoch. Defaults to 500. |
500
|
model
|
str
|
Model architecture to use. Defaults to "unet". |
'unet'
|
backbone
|
str
|
Backbone network for the model. Defaults to "resnet50". |
'resnet50'
|
weights
|
bool
|
Whether to use pretrained weights. Defaults to True. |
True
|
num_filters
|
int
|
Number of filters for the model. Defaults to 3. |
3
|
loss
|
str
|
Loss function to use ('ce', 'jaccard', or 'focal'). Defaults to "ce". |
'ce'
|
class_weights
|
list
|
Class weights for loss function. Defaults to None. |
None
|
ignore_index
|
int
|
Index to ignore in loss calculation. Defaults to None. |
None
|
lr
|
float
|
Learning rate. Defaults to 0.001. |
0.001
|
patience
|
int
|
Number of epochs with no improvement after which training will stop. Defaults to 10. |
10
|
freeze_backbone
|
bool
|
Whether to freeze backbone. Defaults to False. |
False
|
freeze_decoder
|
bool
|
Whether to freeze decoder. Defaults to False. |
False
|
transforms
|
callable
|
Transforms to apply to the data. Defaults to None. |
None
|
use_augmentation
|
bool
|
Whether to apply data augmentation. Defaults to False. |
False
|
seed
|
int
|
Random seed for reproducibility. Defaults to 42. |
42
|
train_val_test_split
|
list
|
Proportions for train/val/test split. Defaults to [0.6, 0.2, 0.2]. |
(0.6, 0.2, 0.2)
|
accelerator
|
str
|
Accelerator to use for training ('cpu', 'gpu', etc.). Defaults to "auto". |
'auto'
|
devices
|
str
|
Number of devices to use for training. Defaults to "auto". |
'auto'
|
logger
|
object
|
Logger for tracking training progress. Defaults to None. |
None
|
callbacks
|
list
|
List of callbacks for the trainer. Defaults to None. |
None
|
log_every_n_steps
|
int
|
Frequency of logging training progress. Defaults to 10. |
10
|
use_distributed_sampler
|
bool
|
Whether to use distributed sampling. Defaults to False. |
False
|
monitor_metric
|
str
|
Metric to monitor for saving best model. Defaults to "val_loss". |
'val_loss'
|
mode
|
str
|
Mode for monitoring metric ('min' or 'max'). Use 'min' for losses and 'max' for metrics like accuracy. Defaults to "min". |
'min'
|
save_top_k
|
int
|
Number of best models to save. Defaults to 1. |
1
|
save_last
|
bool
|
Whether to save the model from the last epoch. Defaults to True. |
True
|
checkpoint_filename
|
str
|
Filename pattern for saved checkpoints. Defaults to "best_model_{epoch:02d}_{val_loss:.4f}". |
'best_model'
|
checkpoint_path
|
str
|
Path to a checkpoint file to resume training. |
None
|
every_n_epochs
|
int
|
Save a checkpoint every N epochs. Defaults to 1. |
1
|
**kwargs
|
Additional keyword arguments to pass to the datasets. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
object |
Trained SemanticSegmentationTask model. |
Source code in geoai/classify.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 112 113 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 159 160 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 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 |
|