From 824a9140b68927f4ec0d25543fc0a585b6acd60f Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 7 Feb 2024 09:59:58 +0100 Subject: [PATCH] tests fixed --- supervision/detection/utils.py | 2 +- test/detection/test_utils.py | 69 ++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/supervision/detection/utils.py b/supervision/detection/utils.py index f075513a..f411df28 100644 --- a/supervision/detection/utils.py +++ b/supervision/detection/utils.py @@ -168,7 +168,7 @@ def mask_non_max_suppression( condition = (ious[i] > iou_threshold) & (categories[i] == categories) keep[i + 1 :] = np.where(condition[i + 1 :], False, keep[i + 1 :]) - return keep + return keep[sort_index.argsort()] def box_non_max_suppression( diff --git a/test/detection/test_utils.py b/test/detection/test_utils.py index 46aee255..d09348ff 100644 --- a/test/detection/test_utils.py +++ b/test/detection/test_utils.py @@ -136,9 +136,9 @@ def test_box_non_max_suppression( 0.5, np.array([]), DoesNotRaise(), - ), # single box with no category + ), # empty predictions and masks ( - np.array([[10, 10, 40, 40, 0.8]]), + np.array([[0, 0, 0, 0, 0.8]]), np.array( [ [ @@ -153,9 +153,9 @@ def test_box_non_max_suppression( 0.5, np.array([True]), DoesNotRaise(), - ), # single box with no category + ), # single mask with no category ( - np.array([[10, 10, 40, 40, 0.8, 0]]), + np.array([[0, 0, 0, 0, 0.8, 0]]), np.array( [ [ @@ -170,9 +170,9 @@ def test_box_non_max_suppression( 0.5, np.array([True]), DoesNotRaise(), - ), # single box with category + ), # single mask with category ( - np.array([[0, 0, 20, 20, 0.8], [50, 50, 70, 70, 0.9]]), + np.array([[0, 0, 0, 0, 0.8], [0, 0, 0, 0, 0.9]]), np.array( [ [ @@ -194,9 +194,9 @@ def test_box_non_max_suppression( 0.5, np.array([True, True]), DoesNotRaise(), - ), # two boxes with no category + ), # two masks non-overlapping with no category ( - np.array([[0, 0, 30, 30, 0.8], [20, 20, 50, 50, 0.9]]), + np.array([[0, 0, 0, 0, 0.8], [0, 0, 0, 0, 0.9]]), np.array( [ [ @@ -218,9 +218,9 @@ def test_box_non_max_suppression( 0.4, np.array([False, True]), DoesNotRaise(), - ), # two boxes with different category + ), # two masks partially overlapping with no category ( - np.array([[0, 0, 30, 30, 0.8, 0], [20, 20, 50, 50, 0.9, 1]]), + np.array([[0, 0, 0, 0, 0.8, 0], [0, 0, 0, 0, 0.9, 1]]), np.array( [ [ @@ -242,13 +242,13 @@ def test_box_non_max_suppression( 0.5, np.array([True, True]), DoesNotRaise(), - ), # two boxes with same category + ), # two masks partially overlapping with different category ( np.array( [ - [0, 0, 30, 30, 0.8, 0], - [15, 15, 45, 45, 0.85, 0], - [30, 30, 60, 60, 0.9, 1], + [0, 0, 0, 0, 0.8], + [0, 0, 0, 0, 0.85], + [0, 0, 0, 0, 0.9], ] ), np.array( @@ -258,7 +258,7 @@ def test_box_non_max_suppression( [False, True, True, False, False], [False, True, True, False, False], [False, False, False, False, False], - [False, False, False, True, False], + [False, False, False, False, False], ], [ [False, False, False, False, False], @@ -279,7 +279,44 @@ def test_box_non_max_suppression( 0.5, np.array([False, True, True]), DoesNotRaise(), - ), # three boxes with no category + ), # three masks with no category + ( + np.array( + [ + [0, 0, 0, 0, 0.8, 0], + [0, 0, 0, 0, 0.85, 1], + [0, 0, 0, 0, 0.9, 2], + ] + ), + np.array( + [ + [ + [False, False, False, False, False], + [False, True, True, False, False], + [False, True, True, False, False], + [False, False, False, False, False], + [False, False, False, False, False], + ], + [ + [False, False, False, False, False], + [False, True, True, False, False], + [False, True, True, False, False], + [False, True, True, False, False], + [False, False, False, False, False], + ], + [ + [False, False, False, False, False], + [False, True, True, False, False], + [False, True, True, False, False], + [False, False, False, False, False], + [False, False, False, False, False], + ], + ] + ), + 0.5, + np.array([True, True, True]), + DoesNotRaise(), + ), # three masks with different category ], ) def test_mask_non_max_suppression(