onnx module¶
ONNX Runtime support for geospatial model inference.
This module provides ONNXGeoModel for loading and running inference with ONNX models on geospatial data (GeoTIFF), and export_to_onnx for converting existing PyTorch/Hugging Face models to ONNX format.
Supported tasks
- Semantic segmentation (e.g., SegFormer, Mask2Former)
- Image classification (e.g., ViT, ResNet)
- Object detection (e.g., DETR, YOLOS)
- Depth estimation (e.g., Depth Anything, DPT)
Requirements
- onnx
- onnxruntime (or onnxruntime-gpu for GPU acceleration)
Install with::
1 | |
Example
from geoai import export_to_onnx, ONNXGeoModel
Export a HuggingFace model to ONNX¶
export_to_onnx( ... "nvidia/segformer-b0-finetuned-ade-512-512", ... "segformer.onnx", ... task="semantic-segmentation", ... )
Load and run inference with the ONNX model¶
model = ONNXGeoModel("segformer.onnx", task="semantic-segmentation") result = model.predict("input.tif", output_path="output.tif")
ONNXGeoModel
¶
ONNX Runtime model for geospatial inference with GeoTIFF support.
This class mirrors the :class:~geoai.auto.AutoGeoModel API but uses
ONNX Runtime instead of PyTorch for inference, enabling deployment on
edge devices and environments without GPU drivers.
Attributes:
| Name | Type | Description |
|---|---|---|
session |
The |
|
task |
str
|
The model task (e.g. |
tile_size |
int
|
Tile size used for processing large images. |
overlap |
int
|
Overlap between adjacent tiles. |
metadata |
dict
|
Model metadata loaded from the sidecar JSON file. |
Example
model = ONNXGeoModel("segformer.onnx", task="semantic-segmentation") result = model.predict("input.tif", output_path="output.tif")
Source code in geoai/onnx.py
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 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 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 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 | |
__init__(model_path, task=None, providers=None, tile_size=1024, overlap=128, metadata=None)
¶
Load an ONNX model for geospatial inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str
|
Path to the |
required |
task
|
Optional[str]
|
Model task. One of |
None
|
providers
|
Optional[List[str]]
|
ONNX Runtime execution providers in priority order.
Defaults to |
None
|
tile_size
|
int
|
Tile size for processing large images. |
1024
|
overlap
|
int
|
Overlap between adjacent tiles (in pixels). |
128
|
metadata
|
Optional[Dict[str, Any]]
|
Optional pre-loaded metadata dict. When None the
constructor looks for |
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If model_path does not exist. |
ImportError
|
If onnxruntime is not installed. |
Source code in geoai/onnx.py
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 | |
load_geotiff(source, window=None, bands=None)
staticmethod
¶
Load a GeoTIFF file and return data with metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, DatasetReader]
|
Path to GeoTIFF file or open rasterio DatasetReader. |
required |
window
|
Optional[Window]
|
Optional rasterio Window for reading a subset. |
None
|
bands
|
Optional[List[int]]
|
List of band indices to read (1-indexed). |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Dict]
|
Tuple of (image array in CHW format, metadata dict). |
Source code in geoai/onnx.py
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 | |
load_image(source, window=None, bands=None)
staticmethod
¶
Load an image from various sources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, ndarray, Image]
|
Path to image file, numpy array, or PIL Image. |
required |
window
|
Optional[Window]
|
Optional rasterio Window (only for GeoTIFF). |
None
|
bands
|
Optional[List[int]]
|
List of band indices (only for GeoTIFF, 1-indexed). |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Optional[Dict]]
|
Tuple of (image array in CHW format, metadata dict or None). |
Source code in geoai/onnx.py
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 | |
mask_to_vector(mask, metadata, threshold=0.5, min_object_area=100, max_object_area=None, simplify_tolerance=1.0)
staticmethod
¶
Convert a raster mask to vector polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
Binary or probability mask array. |
required |
metadata
|
Dict
|
Geospatial metadata dictionary. |
required |
threshold
|
float
|
Threshold for binarizing probability masks. |
0.5
|
min_object_area
|
int
|
Minimum polygon area in pixels. |
100
|
max_object_area
|
Optional[int]
|
Maximum polygon area in pixels (optional). |
None
|
simplify_tolerance
|
float
|
Tolerance for polygon simplification. |
1.0
|
Returns:
| Type | Description |
|---|---|
Optional[GeoDataFrame]
|
GeoDataFrame with polygon geometries, or None if no valid |
Optional[GeoDataFrame]
|
polygons are found. |
Source code in geoai/onnx.py
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 | |
predict(source, output_path=None, output_vector_path=None, window=None, bands=None, threshold=0.5, box_threshold=0.3, min_object_area=100, simplify_tolerance=1.0, batch_size=1, return_probabilities=False, **kwargs)
¶
Run inference on a GeoTIFF or image.
This method follows the same interface as
:meth:~geoai.auto.AutoGeoModel.predict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, ndarray, Image]
|
Input image path, numpy array, or PIL Image. |
required |
output_path
|
Optional[str]
|
Path to save output GeoTIFF (segmentation / depth). |
None
|
output_vector_path
|
Optional[str]
|
Path to save vectorised output. |
None
|
window
|
Optional[Window]
|
Optional rasterio Window for reading a subset. |
None
|
bands
|
Optional[List[int]]
|
Band indices to read (1-indexed). |
None
|
threshold
|
float
|
Threshold for binary masks (segmentation). |
0.5
|
box_threshold
|
float
|
Confidence threshold for detections. |
0.3
|
min_object_area
|
int
|
Minimum polygon area in pixels for vectorization. |
100
|
simplify_tolerance
|
float
|
Tolerance for polygon simplification. |
1.0
|
batch_size
|
int
|
Batch size for tiled processing (reserved for future use). |
1
|
return_probabilities
|
bool
|
Whether to return probability maps. |
False
|
**kwargs
|
Any
|
Extra keyword arguments (currently unused). |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with results ( |
Dict[str, Any]
|
depending on the task, plus |
Example
model = ONNXGeoModel("segformer.onnx", ... task="semantic-segmentation") result = model.predict("input.tif", output_path="output.tif")
Source code in geoai/onnx.py
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 | |
save_vector(gdf, output_path, driver=None)
staticmethod
¶
Save a GeoDataFrame to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame to save. |
required |
output_path
|
str
|
Output file path. |
required |
driver
|
Optional[str]
|
File driver (auto-detected from extension if None). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the saved file. |
Source code in geoai/onnx.py
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 | |
export_to_onnx(model_name_or_path, output_path, task=None, input_height=512, input_width=512, input_channels=3, opset_version=17, dynamic_axes=None, simplify=True, device=None, **kwargs)
¶
Export a PyTorch / Hugging Face model to ONNX format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name_or_path
|
str
|
Hugging Face model name or local checkpoint path. |
required |
output_path
|
str
|
Path where the |
required |
task
|
Optional[str]
|
Model task. One of |
None
|
input_height
|
int
|
Height of the dummy input tensor (pixels). |
512
|
input_width
|
int
|
Width of the dummy input tensor (pixels). |
512
|
input_channels
|
int
|
Number of input channels (default 3 for RGB). |
3
|
opset_version
|
int
|
ONNX opset version (default 17). |
17
|
dynamic_axes
|
Optional[Dict[str, Dict[int, str]]]
|
Optional mapping of dynamic axes for variable-size inputs/outputs. When None a sensible default is used so that batch size and spatial dimensions are dynamic. |
None
|
simplify
|
bool
|
Whether to simplify the exported graph with
|
True
|
device
|
Optional[str]
|
Device used for tracing ( |
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to
|
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the saved ONNX file. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If required packages are missing. |
ValueError
|
If the task cannot be determined. |
Example
export_to_onnx( ... "nvidia/segformer-b0-finetuned-ade-512-512", ... "segformer.onnx", ... task="semantic-segmentation", ... ) 'segformer.onnx'
Source code in geoai/onnx.py
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 | |
onnx_image_classification(input_path, model_path, providers=None, **kwargs)
¶
Classify an image using an ONNX model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to input image or GeoTIFF. |
required |
model_path
|
str
|
Path to the ONNX model file. |
required |
providers
|
Optional[List[str]]
|
ONNX Runtime execution providers. |
None
|
**kwargs
|
Any
|
Additional arguments passed to :meth: |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with |
Dict[str, Any]
|
|
Example
result = onnx_image_classification("image.tif", "classifier.onnx") print(result["class"], result["label"])
Source code in geoai/onnx.py
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 | |
onnx_semantic_segmentation(input_path, output_path, model_path, output_vector_path=None, threshold=0.5, tile_size=1024, overlap=128, min_object_area=100, simplify_tolerance=1.0, providers=None, **kwargs)
¶
Perform semantic segmentation using an ONNX model on a GeoTIFF.
This is a convenience wrapper around :class:ONNXGeoModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to input GeoTIFF. |
required |
output_path
|
str
|
Path to save output segmentation GeoTIFF. |
required |
model_path
|
str
|
Path to the ONNX model file. |
required |
output_vector_path
|
Optional[str]
|
Optional path to save vectorised output. |
None
|
threshold
|
float
|
Threshold for binary masks. |
0.5
|
tile_size
|
int
|
Tile size for processing large images. |
1024
|
overlap
|
int
|
Overlap between tiles. |
128
|
min_object_area
|
int
|
Minimum object area for vectorization. |
100
|
simplify_tolerance
|
float
|
Tolerance for polygon simplification. |
1.0
|
providers
|
Optional[List[str]]
|
ONNX Runtime execution providers. |
None
|
**kwargs
|
Any
|
Additional arguments passed to :meth: |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with results. |
Example
result = onnx_semantic_segmentation( ... "input.tif", ... "output.tif", ... "segformer.onnx", ... output_vector_path="output.geojson", ... )
Source code in geoai/onnx.py
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 | |