Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OD benchmarks and disallow calculating DetailedPRCurves when using AnnotationType.RASTER #726

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions core/benchmarks/object-detection/benchmark_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Polygon,
Prediction,
Raster,
ValorDetectionManager,
enums,
evaluate_detection,
)
Expand Down Expand Up @@ -235,11 +236,59 @@ def run_detailed_pr_curve_evaluation(groundtruths, predictions):
enums.MetricType.mAR,
enums.MetricType.mAPAveragedOverIOUs,
enums.MetricType.PrecisionRecallCurve,
enums.MetricType.DetailedPrecisionRecallCurve,
],
)
return evaluation


def run_base_evaluation_with_manager(groundtruths, predictions):
"""Run a base evaluation (with no PR curves) using ValorDetectionManager."""
manager = ValorDetectionManager()
manager.add_data(groundtruths=groundtruths, predictions=predictions)
return manager.evaluate()


def run_pr_curve_evaluation_with_manager(groundtruths, predictions):
"""Run a base evaluation with PrecisionRecallCurve included using ValorDetectionManager."""
manager = ValorDetectionManager(
metrics_to_return=[
enums.MetricType.AP,
enums.MetricType.AR,
enums.MetricType.mAP,
enums.MetricType.APAveragedOverIOUs,
enums.MetricType.mAR,
enums.MetricType.mAPAveragedOverIOUs,
enums.MetricType.PrecisionRecallCurve,
],
)

manager.add_data(groundtruths=groundtruths, predictions=predictions)

return manager.evaluate()


def run_detailed_pr_curve_evaluation_with_manager(groundtruths, predictions):
"""Run a base evaluation with PrecisionRecallCurve and DetailedPrecisionRecallCurve included using ValorDetectionManager."""

manager = ValorDetectionManager(
metrics_to_return=[
enums.MetricType.AP,
enums.MetricType.AR,
enums.MetricType.mAP,
enums.MetricType.APAveragedOverIOUs,
enums.MetricType.mAR,
enums.MetricType.mAPAveragedOverIOUs,
enums.MetricType.PrecisionRecallCurve,
enums.MetricType.DetailedPrecisionRecallCurve,
],
)

manager.add_data(groundtruths=groundtruths, predictions=predictions)

return manager.evaluate()


@dataclass
class DataBenchmark:
dtype: str
Expand Down Expand Up @@ -364,9 +413,13 @@ def run_benchmarking_analysis(
# run evaluations
eval_pr = None
eval_detail = None
eval_base = run_base_evaluation(groundtruths, predictions)
eval_base = run_base_evaluation_with_manager(
groundtruths, predictions
)
if compute_pr:
eval_pr = run_pr_curve_evaluation(groundtruths, predictions)
eval_pr = run_pr_curve_evaluation_with_manager(
groundtruths, predictions
)
if compute_detailed:
eval_detail = run_detailed_pr_curve_evaluation(
groundtruths, predictions
Expand Down Expand Up @@ -413,6 +466,7 @@ def run_benchmarking_analysis(
(AnnotationType.BOX, AnnotationType.BOX),
],
limits_to_test=[5000, 5000],
compute_detailed=False,
)

# run polygon benchmark
Expand All @@ -421,12 +475,13 @@ def run_benchmarking_analysis(
(AnnotationType.POLYGON, AnnotationType.POLYGON),
],
limits_to_test=[5000, 5000],
compute_detailed=False,
)

# run raster benchmark
run_benchmarking_analysis(
combinations=[
(AnnotationType.RASTER, AnnotationType.RASTER),
],
limits_to_test=[500, 500],
compute_detailed=False,
)
10 changes: 10 additions & 0 deletions core/tests/functional-tests/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,16 @@ def test_evaluate_detection_functional_test_with_rasters(
pr_metrics[0]["value"][value][threshold][metric] == expected_value
)

# test that we get a NotImplementedError if we try to calculate DetailedPRCurves with rasters
with pytest.raises(NotImplementedError):
evaluate_detection(
groundtruths=evaluate_detection_functional_test_groundtruths_with_rasters,
predictions=evaluate_detection_functional_test_predictions_with_rasters,
metrics_to_return=[
enums.MetricType.DetailedPrecisionRecallCurve,
],
)


def test_evaluate_mixed_annotations(
evaluate_mixed_annotations_inputs: tuple,
Expand Down
15 changes: 15 additions & 0 deletions core/tests/functional-tests/test_detection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,21 @@ def test_evaluate_detection_functional_test_with_rasters_with_ValorDetectionMana
pr_metrics[0]["value"][value][threshold][metric] == expected_value
)

# test that we get a NotImplementedError if we try to calculate DetailedPRCurves with rasters
manager = managers.ValorDetectionManager(
metrics_to_return=[
enums.MetricType.DetailedPrecisionRecallCurve,
],
)

manager.add_data(
groundtruths=evaluate_detection_functional_test_groundtruths_with_rasters,
predictions=evaluate_detection_functional_test_predictions_with_rasters,
)

with pytest.raises(NotImplementedError):
manager.evaluate()


def test_evaluate_mixed_annotations_with_ValorDetectionManager(
evaluate_mixed_annotations_inputs: tuple,
Expand Down
237 changes: 237 additions & 0 deletions core/tests/unit-tests/test_geometry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
import pytest
from valor_core import geometry
from valor_core.schemas import (
Expand Down Expand Up @@ -1041,6 +1042,242 @@ def test_calculate_iou():
assert expected == round(iou, 4)


def test_calculate_raster_iou():
filled_8x8 = np.full((8, 8), True)
filled_10x10 = (np.full((10, 10), True),)

series1 = pd.Series(
[
filled_10x10,
filled_10x10,
[
[True, True, True, True, True, False, False, False],
[True, True, True, True, True, False, False, False],
[True, True, True, True, True, False, False, False],
[True, True, True, True, True, False, False, False],
[True, True, True, True, True, False, False, False],
[True, True, True, True, True, False, False, False],
[True, True, True, True, True, False, False, False],
[True, True, True, True, True, False, False, False],
],
filled_8x8,
filled_8x8,
]
)

series2 = pd.Series(
[
[
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
],
[
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
[
True,
True,
True,
True,
True,
False,
False,
False,
False,
False,
],
],
[
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
],
[
[False, False, False, False, True, True, True, True],
[False, False, False, False, True, True, True, True],
[False, False, False, False, True, True, True, True],
[False, False, False, False, True, True, True, True],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False],
],
[
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[False, False, False, False, False, True, True, True],
[True, True, True, False, False, True, True, True],
[True, True, True, False, False, True, True, True],
[True, True, True, False, False, True, True, True],
[True, True, True, False, False, True, True, True],
],
]
)

result = geometry.calculate_raster_ious(series1, series2)
assert (result == [1, 0.5, 0, 0.25, 36 / 64]).all()

# check that we throw an error if the series aren't the same length
series1 = pd.Series(
[
filled_10x10,
filled_10x10,
]
)

series2 = pd.Series(
[
filled_10x10,
filled_10x10,
filled_10x10,
]
)
with pytest.raises(ValueError) as e:
geometry.calculate_raster_ious(series1, series2)
assert (
"Series of rasters must be the same length to calculate IOUs."
in str(e)
)

# check that we don't compare rasters that aren't the same size
series1 = pd.Series(
[
filled_10x10,
filled_10x10,
]
)

series2 = pd.Series(
[
filled_10x10,
filled_8x8,
]
)
with pytest.raises(ValueError) as e:
geometry.calculate_raster_ious(series1, series2)
assert "operands could not be broadcast together with shapes" in str(e)


def test_is_axis_aligned(box_points, skewed_box_points, rotated_box_points):
tests = [
{
Expand Down
Loading
Loading