Evaluation
Functions for comparing detections against ground truth: converting crown polygons to points, matching predictions to reference trees, and computing precision, recall, and IoU statistics.
evaluate
¶
Attributes¶
Classes¶
Functions:¶
assess_matches(matches, n_ground_truth, n_predictions)
¶
Calculate precison, recall and F1 score to evaluate the matches Args: matches (List): List of matched pairs n_ground_truth (int): Number of treetops in ground truth (treetop_set_1) n_predictions (int): Number of treetops in predictions (treetop_set_2)
Returns:
| Type | Description |
|---|---|
|
precision, recall, f1_score: floats |
Source code in tree_detection_framework/evaluation/evaluate.py
compute_matched_ious(ground_truth_boxes, predicted_boxes)
¶
Compute IoUs for matched pairs of ground truth and predicted boxes. This uses the Hungarian algorithm to find the optimal assignment. Args: ground_truth_boxes (list): List of ground truth polygons. predicted_boxes (list): List of predicted polygons. Returns: list: List of IoUs for matched pairs.
Source code in tree_detection_framework/evaluation/evaluate.py
compute_precision_recall(ious, num_gt, num_pd, threshold=0.4)
¶
Compute precision and recall based on IoUs. Args: ious (list): List of IoUs for matched pairs. num_gt (int): Number of ground truth boxes. num_pd (int): Number of predicted boxes. threshold (float): IoU threshold for considering a match. Returns: tuple: Precision and recall values.
Source code in tree_detection_framework/evaluation/evaluate.py
match_points(treetop_set_1, treetop_set_2, height_column_1=None, height_column_2=None, chm_path=None, bboxes=None, height_threshold=lambda h: 0.5 * h, distance_threshold=lambda h: 0.1 * h + 1, fillin_method=None, use_height_in_distance=0.0, vis=False, vis_mode=3)
¶
Matches treetop detections from two datasets based on spatial proximity and tree height similarity.
This function attempts to pair each treetop in treetop_set_1 with at most
one treetop in treetop_set_2 such that:
- Horizontal distance is below a maximum threshold.
- Height difference is within an allowed range.
Matching is greedy, preferring the closest pairs first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
treetop_set_1
|
RegionDetections | RegionDetectionsSet | GeoDataFrame The reference (ground truth) treetop detections. |
required | |
treetop_set_2
|
RegionDetections | RegionDetectionsSet | GeoDataFrame
The predicted treetop detections to be matched against |
required | |
height_column_1
|
str, optional
Column name in |
required | |
height_column_2
|
str, optional
Column name in |
required | |
chm_path
|
PATH_TYPE, optional
Path to a canopy height model (CHM) raster, used when |
required | |
bboxes
|
RegionDetectionsSet, optional
Bounding boxes for computing height values when |
required | |
height_threshold
|
float or callable Max allowed height difference during matching. - If float provided, it is considered a constant +/- tolerance in meters. Pass in np.inf to disable - If callable provided, it should accept a heights array and return the tolerance for each point. Default allows +/-50% the reference treetop's height |
required | |
distance_threshold
|
float or callable, optional Max allowed horizontal distance for a match in meters. - If float provided, it is a constant value. Pass in np.inf to disable - If callable is provided, it should accept a heights array and return distance thresholds. |
required | |
fillin_method
|
['chm', 'bbox'], optional
Method to fill in height values if they are not provided via |
required | |
use_height_in_distance
|
float, optional Weight to scale height difference when computing combined distance metric for sorting matches. If 0, height is not included in the sorting distance, but still used for validity checks. Defaults to 0. |
required | |
vis
|
bool, default=False If True, plot the matched treetop points and their connecting lines. |
required | |
vis_mode
|
int, default = 3 - 1: Show only matched points - 2: Show all points from both sets, highlighting matches - 3: Same as mode 2, but crop the plot to the smaller set's bounds + buffer |
required |
Source code in tree_detection_framework/evaluation/evaluate.py
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 | |
polygons_to_points(detections, method, chm_path=None, height_column='height', crown_geometry_column='crown_geometry', erosion_distance=0.0)
¶
Convert polygon geometries to point geometries using the specified method. The original polygon geometries are stored in a new column, and height values are populated when CHM sampling is performed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detections
|
RegionDetections | RegionDetectionsSet | GeoDataFrame
|
GeoDataFrame, RegionDetections, or RegionDetectionsSet with polygon geometries to convert as points. |
required |
method
|
Literal['centroid', 'chm_max']
|
Method to use to derive the representative point from each polygon.
- "centroid": Use the centroid of each polygon. If |
required |
chm_path
|
Optional[str]
|
Path to a canopy height model (CHM) raster. Required when
|
None
|
height_column
|
str
|
Name of the column to write sampled height values into.
Raises a ValueError if this column already exists in |
'height'
|
crown_geometry_column
|
str
|
Name of the column in which the original polygon geometries are stored after points are derived and set as the default geometry column. Defaults to "crown_geometry". |
'crown_geometry'
|
erosion_distance
|
float
|
Distance (in CHM CRS units) to erode each polygon inward
before finding the maximum CHM pixel. Only used when |
0.0
|
Returns:
gpd.GeoDataFrame: A copy of detections with -
- Active geometry replaced with point geometries (Point).
- Original polygon geometries stored in crown_geometry_column.
- height_column populated when CHM sampling is performed.
- All other columns preserved unchanged.