Loading imgutils/detect/similarity.py +0 −1 Original line number Diff line number Diff line Loading @@ -97,7 +97,6 @@ def bboxes_similarity(bboxes1: List[BBoxTyping], bboxes2: List[BBoxTyping], # import here for faster launching speed from scipy.optimize import linear_sum_assignment row_ind, col_ind = linear_sum_assignment(-iou_matrix) print(iou_matrix) similarities = iou_matrix[row_ind, col_ind] if mode == 'max': return float(similarities.max()) Loading test/detect/test_similarity.py +55 −0 Original line number Diff line number Diff line Loading @@ -51,6 +51,25 @@ class TestBBoxFunctions: with pytest.raises(ValueError, match="Length of bboxes lists not match"): bboxes_similarity(sample_bboxes, sample_bboxes[:-1]) @pytest.mark.parametrize("bboxes1, bboxes2, mode, expected", [ ([(0, 0, 2, 2), (3, 3, 5, 5)], [(1, 1, 3, 3), (4, 4, 6, 6)], 'mean', 0.14285714285714285), ([(0, 0, 2, 2), (3, 3, 5, 5)], [(1, 1, 3, 3), (4, 4, 6, 6)], 'max', 0.14285714285714285), ([(0, 0, 1, 1), (2, 2, 3, 3)], [(0.5, 0.5, 1.5, 1.5), (2.5, 2.5, 3.5, 3.5)], 'mean', 0.14285706122453645), ([(0, 0, 2, 2), (3, 3, 5, 5)], [(0.5, 0.5, 2.5, 2.5), (3.5, 3.5, 5.5, 5.5)], 'mean', 0.39130427977316873), ([(1, 1, 3, 3), (4, 4, 6, 6)], [(0, 0, 2, 2), (3, 3, 5, 5)], 'max', 0.14285714285714285), ([(0, 0, 1, 1), (2, 2, 4, 4)], [(0.25, 0.25, 1.25, 1.25), (2.25, 2.25, 4.25, 4.25)], 'mean', 0.5057785572753247), ([(1, 1, 2, 2), (3, 3, 4, 4)], [(1.25, 1.25, 2.25, 2.25), (3.25, 3.25, 4.25, 4.25)], 'mean', 0.3913040756145561), ([(0, 0, 3, 3), (4, 4, 7, 7)], [(1, 1, 4, 4), (5, 5, 8, 8)], 'mean', 0.2857142857142857), ([(1, 1, 4, 4), (5, 5, 8, 8)], [(0, 0, 3, 3), (4, 4, 7, 7)], 'max', 0.2857142857142857), ([(0, 0, 2, 2), (3, 3, 5, 5)], [(0.75, 0.75, 2.75, 2.75), (3.75, 3.75, 5.75, 5.75)], 'mean', 0.24271840889811122), ]) def test_bboxes_similarity(self, bboxes1, bboxes2, mode, expected): similarity = bboxes_similarity(bboxes1, bboxes2, mode) assert pytest.approx(similarity, 0.0001) == expected def test_detection_similarity_max(self, sample_detections): result = detection_similarity(sample_detections, sample_detections, mode='max') assert isinstance(result, float) Loading @@ -73,3 +92,39 @@ class TestBBoxFunctions: def test_detection_similarity_unequal_length(self, sample_detections): with pytest.raises(ValueError, match="Length of bboxes not match on label"): detection_similarity(sample_detections, sample_detections[:-1]) @pytest.mark.parametrize("detect1, detect2, mode, expected", [ ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((1, 1, 3, 3), 'car', 0.85), ((4, 4, 6, 6), 'person', 0.75)], 'mean', 0.14285714285714285), ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((1, 1, 3, 3), 'car', 0.85), ((4, 4, 6, 6), 'person', 0.75)], 'max', 0.14285714285714285), ([((0, 0, 1, 1), 'bike', 0.7), ((2, 2, 3, 3), 'dog', 0.6)], [((0.5, 0.5, 1.5, 1.5), 'bike', 0.65), ((2.5, 2.5, 3.5, 3.5), 'dog', 0.55)], 'mean', 0.14285706122453645), ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((0.5, 0.5, 2.5, 2.5), 'car', 0.85), ((3.5, 3.5, 5.5, 5.5), 'person', 0.75)], 'mean', 0.39130427977316873), ([((1, 1, 3, 3), 'car', 0.9), ((4, 4, 6, 6), 'person', 0.8)], [((0, 0, 2, 2), 'car', 0.85), ((3, 3, 5, 5), 'person', 0.75)], 'max', 0.14285714285714285), ([((0, 0, 1, 1), 'bike', 0.7), ((2, 2, 4, 4), 'dog', 0.6)], [((0.25, 0.25, 1.25, 1.25), 'bike', 0.65), ((2.25, 2.25, 4.25, 4.25), 'dog', 0.55)], 'mean', 0.5057785572753247), ([((1, 1, 2, 2), 'car', 0.9), ((3, 3, 4, 4), 'person', 0.8)], [((1.25, 1.25, 2.25, 2.25), 'car', 0.85), ((3.25, 3.25, 4.25, 4.25), 'person', 0.75)], 'mean', 0.3913040756145561), ([((0, 0, 3, 3), 'car', 0.9), ((4, 4, 7, 7), 'person', 0.8)], [((1, 1, 4, 4), 'car', 0.85), ((5, 5, 8, 8), 'person', 0.75)], 'mean', 0.2857142857142857), ([((1, 1, 4, 4), 'car', 0.9), ((5, 5, 8, 8), 'person', 0.8)], [((0, 0, 3, 3), 'car', 0.85), ((4, 4, 7, 7), 'person', 0.75)], 'max', 0.2857142857142857), ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((0.75, 0.75, 2.75, 2.75), 'car', 0.85), ((3.75, 3.75, 5.75, 5.75), 'person', 0.75)], 'mean', 0.24271840889811122), ]) def test_detection_similarity(self, detect1, detect2, mode, expected): similarity = detection_similarity(detect1, detect2, mode) assert pytest.approx(similarity, 0.0001) == expected Loading
imgutils/detect/similarity.py +0 −1 Original line number Diff line number Diff line Loading @@ -97,7 +97,6 @@ def bboxes_similarity(bboxes1: List[BBoxTyping], bboxes2: List[BBoxTyping], # import here for faster launching speed from scipy.optimize import linear_sum_assignment row_ind, col_ind = linear_sum_assignment(-iou_matrix) print(iou_matrix) similarities = iou_matrix[row_ind, col_ind] if mode == 'max': return float(similarities.max()) Loading
test/detect/test_similarity.py +55 −0 Original line number Diff line number Diff line Loading @@ -51,6 +51,25 @@ class TestBBoxFunctions: with pytest.raises(ValueError, match="Length of bboxes lists not match"): bboxes_similarity(sample_bboxes, sample_bboxes[:-1]) @pytest.mark.parametrize("bboxes1, bboxes2, mode, expected", [ ([(0, 0, 2, 2), (3, 3, 5, 5)], [(1, 1, 3, 3), (4, 4, 6, 6)], 'mean', 0.14285714285714285), ([(0, 0, 2, 2), (3, 3, 5, 5)], [(1, 1, 3, 3), (4, 4, 6, 6)], 'max', 0.14285714285714285), ([(0, 0, 1, 1), (2, 2, 3, 3)], [(0.5, 0.5, 1.5, 1.5), (2.5, 2.5, 3.5, 3.5)], 'mean', 0.14285706122453645), ([(0, 0, 2, 2), (3, 3, 5, 5)], [(0.5, 0.5, 2.5, 2.5), (3.5, 3.5, 5.5, 5.5)], 'mean', 0.39130427977316873), ([(1, 1, 3, 3), (4, 4, 6, 6)], [(0, 0, 2, 2), (3, 3, 5, 5)], 'max', 0.14285714285714285), ([(0, 0, 1, 1), (2, 2, 4, 4)], [(0.25, 0.25, 1.25, 1.25), (2.25, 2.25, 4.25, 4.25)], 'mean', 0.5057785572753247), ([(1, 1, 2, 2), (3, 3, 4, 4)], [(1.25, 1.25, 2.25, 2.25), (3.25, 3.25, 4.25, 4.25)], 'mean', 0.3913040756145561), ([(0, 0, 3, 3), (4, 4, 7, 7)], [(1, 1, 4, 4), (5, 5, 8, 8)], 'mean', 0.2857142857142857), ([(1, 1, 4, 4), (5, 5, 8, 8)], [(0, 0, 3, 3), (4, 4, 7, 7)], 'max', 0.2857142857142857), ([(0, 0, 2, 2), (3, 3, 5, 5)], [(0.75, 0.75, 2.75, 2.75), (3.75, 3.75, 5.75, 5.75)], 'mean', 0.24271840889811122), ]) def test_bboxes_similarity(self, bboxes1, bboxes2, mode, expected): similarity = bboxes_similarity(bboxes1, bboxes2, mode) assert pytest.approx(similarity, 0.0001) == expected def test_detection_similarity_max(self, sample_detections): result = detection_similarity(sample_detections, sample_detections, mode='max') assert isinstance(result, float) Loading @@ -73,3 +92,39 @@ class TestBBoxFunctions: def test_detection_similarity_unequal_length(self, sample_detections): with pytest.raises(ValueError, match="Length of bboxes not match on label"): detection_similarity(sample_detections, sample_detections[:-1]) @pytest.mark.parametrize("detect1, detect2, mode, expected", [ ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((1, 1, 3, 3), 'car', 0.85), ((4, 4, 6, 6), 'person', 0.75)], 'mean', 0.14285714285714285), ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((1, 1, 3, 3), 'car', 0.85), ((4, 4, 6, 6), 'person', 0.75)], 'max', 0.14285714285714285), ([((0, 0, 1, 1), 'bike', 0.7), ((2, 2, 3, 3), 'dog', 0.6)], [((0.5, 0.5, 1.5, 1.5), 'bike', 0.65), ((2.5, 2.5, 3.5, 3.5), 'dog', 0.55)], 'mean', 0.14285706122453645), ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((0.5, 0.5, 2.5, 2.5), 'car', 0.85), ((3.5, 3.5, 5.5, 5.5), 'person', 0.75)], 'mean', 0.39130427977316873), ([((1, 1, 3, 3), 'car', 0.9), ((4, 4, 6, 6), 'person', 0.8)], [((0, 0, 2, 2), 'car', 0.85), ((3, 3, 5, 5), 'person', 0.75)], 'max', 0.14285714285714285), ([((0, 0, 1, 1), 'bike', 0.7), ((2, 2, 4, 4), 'dog', 0.6)], [((0.25, 0.25, 1.25, 1.25), 'bike', 0.65), ((2.25, 2.25, 4.25, 4.25), 'dog', 0.55)], 'mean', 0.5057785572753247), ([((1, 1, 2, 2), 'car', 0.9), ((3, 3, 4, 4), 'person', 0.8)], [((1.25, 1.25, 2.25, 2.25), 'car', 0.85), ((3.25, 3.25, 4.25, 4.25), 'person', 0.75)], 'mean', 0.3913040756145561), ([((0, 0, 3, 3), 'car', 0.9), ((4, 4, 7, 7), 'person', 0.8)], [((1, 1, 4, 4), 'car', 0.85), ((5, 5, 8, 8), 'person', 0.75)], 'mean', 0.2857142857142857), ([((1, 1, 4, 4), 'car', 0.9), ((5, 5, 8, 8), 'person', 0.8)], [((0, 0, 3, 3), 'car', 0.85), ((4, 4, 7, 7), 'person', 0.75)], 'max', 0.2857142857142857), ([((0, 0, 2, 2), 'car', 0.9), ((3, 3, 5, 5), 'person', 0.8)], [((0.75, 0.75, 2.75, 2.75), 'car', 0.85), ((3.75, 3.75, 5.75, 5.75), 'person', 0.75)], 'mean', 0.24271840889811122), ]) def test_detection_similarity(self, detect1, detect2, mode, expected): similarity = detection_similarity(detect1, detect2, mode) assert pytest.approx(similarity, 0.0001) == expected