object_detect module¶
High-level functions for multi-class object detection.
This module provides convenience functions for training, evaluating, and running inference with Mask R-CNN models on COCO-format datasets, including support for the NWPU-VHR-10 remote sensing benchmark.
detections_to_geodataframe(detections, geotiff_path, class_names=None)
¶
Convert detections to a GeoDataFrame with geospatial coordinates.
Converts pixel-space bounding boxes to geospatial coordinates using the CRS and transform from the source GeoTIFF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detections
|
list
|
List of detection dicts, each with keys: mask, score, box (in pixel coords), label. |
required |
geotiff_path
|
str
|
Path to the source GeoTIFF (for CRS and transform). |
required |
class_names
|
list
|
List of class names (index 0 = background). |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
geopandas.GeoDataFrame: GeoDataFrame with columns: geometry, class_id, |
Any
|
class_name, score, area_pixels. |
Source code in geoai/object_detect.py
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 | |
download_nwpu_vhr10(output_dir='NWPU-VHR-10', overwrite=False)
¶
Download and extract the NWPU-VHR-10 dataset.
The NWPU-VHR-10 dataset contains 800 VHR (Very High Resolution) remote sensing images with 10 object classes: airplane, ship, storage_tank, baseball_diamond, tennis_court, basketball_court, ground_track_field, harbor, bridge, and vehicle. It has 3,775 annotated instances in COCO format (bounding boxes and instance segmentation masks).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
str
|
Path for the downloaded ZIP file and extracted dataset directory. Defaults to "NWPU-VHR-10". |
'NWPU-VHR-10'
|
overwrite
|
bool
|
Whether to overwrite existing files. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Path to the extracted dataset directory. |
Source code in geoai/object_detect.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
download_nwpu_vhr10_model(repo_id=NWPU_VHR10_HF_REPO, filename=NWPU_VHR10_HF_FILENAME)
¶
Download the pretrained NWPU-VHR-10 Mask R-CNN model from HuggingFace Hub.
Downloads a Mask R-CNN (ResNet-50 FPN) model trained on the NWPU-VHR-10 dataset for 10-class object detection on remote sensing imagery.
The model achieves the following performance on the validation set
- mAP@0.5: 0.709
- mAP@0.75: 0.518
- mAP@[0.5:0.95]: 0.459
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_id
|
str
|
HuggingFace Hub repository ID. Defaults to "giswqs/nwpu-vhr10-maskrcnn". |
NWPU_VHR10_HF_REPO
|
filename
|
str
|
Model filename in the repository. Defaults to "best_model.pth". |
NWPU_VHR10_HF_FILENAME
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Local path to the downloaded model weights file. |
Example
import geoai model_path = geoai.download_nwpu_vhr10_model() print(model_path) # local cache path
Source code in geoai/object_detect.py
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 | |
evaluate_multiclass_detector(model_path=None, images_dir='', annotations_path='', num_classes=11, class_names=None, num_channels=3, batch_size=4, device=None, num_workers=None, repo_id=None, verbose=True)
¶
Evaluate a trained multi-class detection model on a dataset.
Loads a trained model and computes COCO-style mAP metrics on the provided dataset.
If model_path is None, the pretrained NWPU-VHR-10 model is
automatically downloaded from HuggingFace Hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str
|
Path to trained model weights. If None, downloads the pretrained NWPU-VHR-10 model. |
None
|
images_dir
|
str
|
Directory containing evaluation images. |
''
|
annotations_path
|
str
|
Path to COCO-format annotations JSON. |
''
|
num_classes
|
int
|
Number of classes including background. Defaults to 11 (NWPU-VHR-10). |
11
|
class_names
|
list
|
List of class names (excluding background). |
None
|
num_channels
|
int
|
Number of image channels. Defaults to 3. |
3
|
batch_size
|
int
|
Evaluation batch size. Defaults to 4. |
4
|
device
|
device
|
Compute device. |
None
|
num_workers
|
int
|
Number of data loading workers. |
None
|
repo_id
|
str
|
HuggingFace Hub repository ID for downloading the model. Defaults to "giswqs/nwpu-vhr10-maskrcnn". |
None
|
verbose
|
bool
|
Whether to print results. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dict with mAP metrics. |
Source code in geoai/object_detect.py
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 | |
multiclass_detection(input_path, output_path, model_path=None, num_classes=11, class_names=None, window_size=512, overlap=256, confidence_threshold=0.5, nms_threshold=0.3, batch_size=4, num_channels=3, device=None, repo_id=None, **kwargs)
¶
Perform multi-class object detection on a GeoTIFF or image.
Loads a trained Mask R-CNN model and runs inference using a sliding window approach. Outputs a 2-band raster with class labels and instance IDs.
If model_path is None, the pretrained NWPU-VHR-10 model is
automatically downloaded from HuggingFace Hub with default
num_classes=11 and class_names set to NWPU-VHR-10 classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str
|
Path to input image (GeoTIFF, JPEG, PNG, etc.). |
required |
output_path
|
str
|
Path to save output raster. |
required |
model_path
|
str
|
Path to trained model weights (.pth file).
If None, downloads the pretrained NWPU-VHR-10 model from
HuggingFace Hub. If the path does not exist locally, it is
treated as a filename to download from |
None
|
num_classes
|
int
|
Number of classes including background. Defaults to 11 (NWPU-VHR-10). |
11
|
class_names
|
list
|
List of class names (index 0 = background). If None and using the pretrained model, defaults to NWPU-VHR-10 class names. |
None
|
window_size
|
int
|
Sliding window size. Defaults to 512. |
512
|
overlap
|
int
|
Window overlap in pixels. Defaults to 256. |
256
|
confidence_threshold
|
float
|
Minimum detection score. Defaults to 0.5. |
0.5
|
nms_threshold
|
float
|
IoU threshold for NMS. Defaults to 0.3. |
0.3
|
batch_size
|
int
|
Inference batch size. Defaults to 4. |
4
|
num_channels
|
int
|
Number of input image channels. Defaults to 3. |
3
|
device
|
device
|
Compute device. |
None
|
repo_id
|
str
|
HuggingFace Hub repository ID for downloading the model. Defaults to "giswqs/nwpu-vhr10-maskrcnn". |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Tuple of (output_path, inference_time, detections_list) where each |
float
|
detection is a dict with mask, score, box, and label. |
Example
import geoai
Use pretrained model (auto-downloads from HuggingFace)¶
result_path, time, dets = geoai.multiclass_detection( ... input_path="image.tif", ... output_path="detections.tif", ... )
Use a custom trained model¶
result_path, time, dets = geoai.multiclass_detection( ... input_path="image.tif", ... output_path="detections.tif", ... model_path="my_model.pth", ... num_classes=5, ... )
Source code in geoai/object_detect.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 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 | |
prepare_nwpu_vhr10(data_dir, output_dir=None, val_split=0.2, seed=42)
¶
Prepare NWPU-VHR-10 dataset for training.
Converts the original text-based annotations to COCO JSON format,
then splits the dataset into train/val sets. The original dataset uses
text files with (x1,y1),(x2,y2),class_id per line for bounding boxes.
Note: Only images with at least one annotation are included in the train/val splits. The 150 "negative" images in the NWPU-VHR-10 dataset (those without any target objects) are excluded from the splits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dir
|
str
|
Path to the extracted NWPU-VHR-10 directory. |
required |
output_dir
|
str
|
Output directory for organized data. If None, creates files alongside the original data. |
None
|
val_split
|
float
|
Fraction of data for validation. Defaults to 0.2. |
0.2
|
seed
|
int
|
Random seed for reproducibility. Defaults to 42. |
42
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with keys: - 'images_dir': Path to images directory - 'annotations_path': Path to the full annotations JSON - 'train_annotations': Path to train split annotations JSON - 'val_annotations': Path to val split annotations JSON - 'train_image_ids': List of training image IDs - 'val_image_ids': List of validation image IDs - 'class_names': List of class names (including background) - 'num_classes': Number of classes (including background) |
Source code in geoai/object_detect.py
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 | |
train_multiclass_detector(images_dir, annotations_path, output_dir, class_names=None, num_channels=3, batch_size=4, num_epochs=50, learning_rate=0.005, val_split=0.2, seed=42, pretrained=True, pretrained_model_path=None, device=None, num_workers=None, verbose=True)
¶
Train a multi-class object detection model using COCO-format annotations.
This is a convenience wrapper around train_MaskRCNN_model that automatically sets up the COCODetectionDataset with proper class mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images_dir
|
str
|
Directory containing training images. |
required |
annotations_path
|
str
|
Path to COCO-format annotations JSON file. |
required |
output_dir
|
str
|
Directory for model outputs. |
required |
class_names
|
list
|
List of class names including background. If None, extracted from annotations. |
None
|
num_channels
|
int
|
Number of image channels. Defaults to 3. |
3
|
batch_size
|
int
|
Training batch size. Defaults to 4. |
4
|
num_epochs
|
int
|
Number of training epochs. Defaults to 50. |
50
|
learning_rate
|
float
|
Initial learning rate. Defaults to 0.005. |
0.005
|
val_split
|
float
|
Validation split fraction. Defaults to 0.2. |
0.2
|
seed
|
int
|
Random seed. Defaults to 42. |
42
|
pretrained
|
bool
|
Whether to use pretrained backbone. Defaults to True. |
True
|
pretrained_model_path
|
str
|
Path to pretrained model. |
None
|
device
|
device
|
Compute device. |
None
|
num_workers
|
int
|
Number of data loading workers. |
None
|
verbose
|
bool
|
Whether to print progress. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Path to the best model checkpoint. |
Source code in geoai/object_detect.py
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 | |
visualize_multiclass_detections(image_path, detections, class_names=None, confidence_threshold=0.0, figsize=(15, 10), output_path=None, max_detections=200)
¶
Visualize multi-class detections overlaid on an image.
Draws colored bounding boxes with class labels and confidence scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_path
|
str
|
Path to the source image. |
required |
detections
|
list
|
List of detection dicts with mask, score, box, label. |
required |
class_names
|
list
|
List of class names (index 0 = background). |
None
|
confidence_threshold
|
float
|
Minimum score to display. Defaults to 0.0. |
0.0
|
figsize
|
tuple
|
Figure size (width, height). Defaults to (15, 10). |
(15, 10)
|
output_path
|
str
|
Path to save the figure. If None, displays. |
None
|
max_detections
|
int
|
Maximum detections to display. Defaults to 200. |
200
|
Source code in geoai/object_detect.py
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 | |