|
1 | | -# Copyright (C) 2024 Intel Corporation |
| 1 | +# Copyright (C) 2024-2025 Intel Corporation |
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | import pytest |
5 | | -from datumaro.components.annotation import AnnotationType, Bbox, Label, Polygon |
| 5 | +from datumaro.components.annotation import AnnotationType, Bbox, Ellipse, Label, Polygon |
6 | 6 | from datumaro.components.dataset import Dataset as DmDataset |
7 | 7 | from datumaro.components.dataset_base import DatasetItem |
8 | 8 |
|
9 | | -from otx.data.utils.pre_filtering import pre_filtering |
| 9 | +from otx.data.utils.pre_filtering import is_valid_anno_for_task, pre_filtering |
| 10 | +from otx.types.task import OTXTaskType |
10 | 11 |
|
11 | 12 |
|
12 | 13 | @pytest.fixture() |
@@ -80,7 +81,166 @@ def test_pre_filtering(fxt_dm_dataset_with_unannotated: DmDataset, unannotated_i |
80 | 81 | filtered_dataset = pre_filtering( |
81 | 82 | dataset=fxt_dm_dataset_with_unannotated, |
82 | 83 | data_format="datumaro", |
| 84 | + task=OTXTaskType.MULTI_CLASS_CLS, |
83 | 85 | unannotated_items_ratio=unannotated_items_ratio, |
84 | 86 | ) |
85 | 87 | assert len(filtered_dataset) == 82 + int(len(empty_items) * unannotated_items_ratio) |
86 | 88 | assert len(filtered_dataset.categories()[AnnotationType.label]) == 3 |
| 89 | + |
| 90 | + |
| 91 | +@pytest.fixture() |
| 92 | +def fxt_dataset_item() -> DatasetItem: |
| 93 | + """Create a sample dataset item for testing.""" |
| 94 | + return DatasetItem( |
| 95 | + id="test_item", |
| 96 | + subset="train", |
| 97 | + media=None, |
| 98 | + annotations=[], |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +class TestIsValidAnnoForTask: |
| 103 | + """Test cases for is_valid_anno_for_task function.""" |
| 104 | + |
| 105 | + @pytest.mark.parametrize( |
| 106 | + ("task", "annotation", "expected"), |
| 107 | + [ |
| 108 | + # DETECTION task tests |
| 109 | + (OTXTaskType.DETECTION, Bbox(x=0, y=0, w=10, h=10, label=0), True), |
| 110 | + (OTXTaskType.DETECTION, Bbox(x=0, y=0, w=-1, h=-1, label=0), False), # Invalid bbox |
| 111 | + (OTXTaskType.DETECTION, Bbox(x=10, y=10, w=5, h=5, label=0), True), |
| 112 | + (OTXTaskType.DETECTION, Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0), False), # Wrong type |
| 113 | + (OTXTaskType.DETECTION, Ellipse(x1=0, y1=0, x2=10, y2=10, label=0), False), |
| 114 | + (OTXTaskType.DETECTION, Label(label=0), False), # Wrong type |
| 115 | + # INSTANCE_SEGMENTATION task tests |
| 116 | + (OTXTaskType.INSTANCE_SEGMENTATION, Bbox(x=0, y=0, w=10, h=10, label=0), True), |
| 117 | + (OTXTaskType.INSTANCE_SEGMENTATION, Bbox(x=0, y=0, w=-1, h=-1, label=0), False), # Invalid bbox |
| 118 | + (OTXTaskType.INSTANCE_SEGMENTATION, Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0), True), |
| 119 | + (OTXTaskType.INSTANCE_SEGMENTATION, Polygon(points=[0, 0, 0, 0, 0, 0], label=0), False), # Invalid polygon |
| 120 | + (OTXTaskType.INSTANCE_SEGMENTATION, Ellipse(x1=0, y1=0, x2=10, y2=10, label=0), True), |
| 121 | + (OTXTaskType.INSTANCE_SEGMENTATION, Label(label=0), False), # Wrong type |
| 122 | + # Other task types (should use default is_valid_annot behavior) |
| 123 | + (OTXTaskType.MULTI_LABEL_CLS, Bbox(x=0, y=0, w=10, h=10, label=0), True), |
| 124 | + (OTXTaskType.MULTI_LABEL_CLS, Bbox(x=0, y=0, w=-1, h=-1, label=0), False), # Invalid bbox |
| 125 | + (OTXTaskType.MULTI_LABEL_CLS, Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0), True), |
| 126 | + (OTXTaskType.MULTI_LABEL_CLS, Polygon(points=[0, 0, 0, 0, 0, 0], label=0), False), # Invalid polygon |
| 127 | + (OTXTaskType.MULTI_LABEL_CLS, Ellipse(x1=0, y1=0, x2=10, y2=10, label=0), True), |
| 128 | + (OTXTaskType.MULTI_LABEL_CLS, Label(label=0), True), # Label is always valid |
| 129 | + (OTXTaskType.SEMANTIC_SEGMENTATION, Bbox(x=0, y=0, w=10, h=10, label=0), True), |
| 130 | + (OTXTaskType.SEMANTIC_SEGMENTATION, Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0), True), |
| 131 | + (OTXTaskType.SEMANTIC_SEGMENTATION, Ellipse(x1=0, y1=0, x2=10, y2=10, label=0), True), |
| 132 | + (OTXTaskType.SEMANTIC_SEGMENTATION, Label(label=0), True), |
| 133 | + (OTXTaskType.ANOMALY, Bbox(x=0, y=0, w=10, h=10, label=0), True), |
| 134 | + (OTXTaskType.ANOMALY, Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0), True), |
| 135 | + (OTXTaskType.ANOMALY, Ellipse(x1=0, y1=0, x2=10, y2=10, label=0), True), |
| 136 | + (OTXTaskType.ROTATED_DETECTION, Bbox(x=0, y=0, w=10, h=10, label=0), True), |
| 137 | + (OTXTaskType.ROTATED_DETECTION, Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0), True), |
| 138 | + (OTXTaskType.ROTATED_DETECTION, Ellipse(x1=0, y1=0, x2=10, y2=10, label=0), True), |
| 139 | + (OTXTaskType.ROTATED_DETECTION, Label(label=0), False), |
| 140 | + ], |
| 141 | + ) |
| 142 | + def test_is_valid_anno_for_task( |
| 143 | + self, |
| 144 | + fxt_dataset_item: DatasetItem, |
| 145 | + task: OTXTaskType, |
| 146 | + annotation, |
| 147 | + expected: bool, |
| 148 | + ) -> None: |
| 149 | + """Test is_valid_anno_for_task with various task types and annotations. |
| 150 | +
|
| 151 | + Args: |
| 152 | + fxt_dataset_item: The dataset item to test with |
| 153 | + task: The task type to test |
| 154 | + annotation: The annotation to test |
| 155 | + expected: Expected result (True if valid, False if invalid) |
| 156 | + """ |
| 157 | + result = is_valid_anno_for_task(fxt_dataset_item, annotation, task) |
| 158 | + assert result == expected, f"Expected {expected} for task {task} with annotation {type(annotation).__name__}" |
| 159 | + |
| 160 | + def test_detection_task_with_valid_bbox(self, fxt_dataset_item: DatasetItem) -> None: |
| 161 | + """Test DETECTION task with valid bounding box.""" |
| 162 | + bbox = Bbox(x=5, y=5, w=20, h=15, label=0) |
| 163 | + result = is_valid_anno_for_task(fxt_dataset_item, bbox, OTXTaskType.DETECTION) |
| 164 | + assert result is True |
| 165 | + |
| 166 | + def test_detection_task_with_invalid_bbox(self, fxt_dataset_item: DatasetItem) -> None: |
| 167 | + """Test DETECTION task with invalid bounding box (negative dimensions).""" |
| 168 | + bbox = Bbox(x=10, y=10, w=-5, h=-5, label=0) |
| 169 | + result = is_valid_anno_for_task(fxt_dataset_item, bbox, OTXTaskType.DETECTION) |
| 170 | + assert result is False |
| 171 | + |
| 172 | + def test_detection_task_with_zero_dimension_bbox(self, fxt_dataset_item: DatasetItem) -> None: |
| 173 | + """Test DETECTION task with zero dimension bounding box.""" |
| 174 | + bbox = Bbox(x=10, y=10, w=0, h=0, label=0) |
| 175 | + result = is_valid_anno_for_task(fxt_dataset_item, bbox, OTXTaskType.DETECTION) |
| 176 | + assert result is False |
| 177 | + |
| 178 | + def test_detection_task_with_wrong_annotation_type(self, fxt_dataset_item: DatasetItem) -> None: |
| 179 | + """Test DETECTION task with non-bbox annotation types.""" |
| 180 | + polygon = Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0) |
| 181 | + ellipse = Ellipse(x1=0, y1=0, x2=10, y2=10, label=0) |
| 182 | + label = Label(label=0) |
| 183 | + |
| 184 | + assert is_valid_anno_for_task(fxt_dataset_item, polygon, OTXTaskType.DETECTION) is False |
| 185 | + assert is_valid_anno_for_task(fxt_dataset_item, ellipse, OTXTaskType.DETECTION) is False |
| 186 | + assert is_valid_anno_for_task(fxt_dataset_item, label, OTXTaskType.DETECTION) is False |
| 187 | + |
| 188 | + def test_instance_segmentation_task_with_valid_annotations(self, fxt_dataset_item: DatasetItem) -> None: |
| 189 | + """Test INSTANCE_SEGMENTATION task with valid annotation types.""" |
| 190 | + bbox = Bbox(x=0, y=0, w=10, h=10, label=0) |
| 191 | + polygon = Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0) |
| 192 | + ellipse = Ellipse(x1=0, y1=0, x2=10, y2=10, label=0) |
| 193 | + |
| 194 | + assert is_valid_anno_for_task(fxt_dataset_item, bbox, OTXTaskType.INSTANCE_SEGMENTATION) is True |
| 195 | + assert is_valid_anno_for_task(fxt_dataset_item, polygon, OTXTaskType.INSTANCE_SEGMENTATION) is True |
| 196 | + assert is_valid_anno_for_task(fxt_dataset_item, ellipse, OTXTaskType.INSTANCE_SEGMENTATION) is True |
| 197 | + |
| 198 | + def test_instance_segmentation_task_with_invalid_annotations(self, fxt_dataset_item: DatasetItem) -> None: |
| 199 | + """Test INSTANCE_SEGMENTATION task with invalid annotation types.""" |
| 200 | + invalid_bbox = Bbox(x=0, y=0, w=-1, h=-1, label=0) |
| 201 | + invalid_polygon = Polygon(points=[0, 0, 0, 0, 0, 0], label=0) # Degenerate polygon |
| 202 | + label = Label(label=0) # Wrong type |
| 203 | + |
| 204 | + assert is_valid_anno_for_task(fxt_dataset_item, invalid_bbox, OTXTaskType.INSTANCE_SEGMENTATION) is False |
| 205 | + assert is_valid_anno_for_task(fxt_dataset_item, invalid_polygon, OTXTaskType.INSTANCE_SEGMENTATION) is False |
| 206 | + assert is_valid_anno_for_task(fxt_dataset_item, label, OTXTaskType.INSTANCE_SEGMENTATION) is False |
| 207 | + |
| 208 | + def test_other_task_types_use_default_validation(self, fxt_dataset_item: DatasetItem) -> None: |
| 209 | + """Test that other task types use the default is_valid_annot behavior.""" |
| 210 | + valid_bbox = Bbox(x=0, y=0, w=10, h=10, label=0) |
| 211 | + invalid_bbox = Bbox(x=0, y=0, w=-1, h=-1, label=0) |
| 212 | + valid_polygon = Polygon(points=[0, 0, 10, 0, 10, 10, 0, 10], label=0) |
| 213 | + invalid_polygon = Polygon(points=[0, 0, 0, 0, 0, 0], label=0) |
| 214 | + label = Label(label=0) |
| 215 | + |
| 216 | + # Test with CLASSIFICATION task |
| 217 | + assert is_valid_anno_for_task(fxt_dataset_item, valid_bbox, OTXTaskType.MULTI_CLASS_CLS) is True |
| 218 | + assert is_valid_anno_for_task(fxt_dataset_item, invalid_bbox, OTXTaskType.MULTI_CLASS_CLS) is False |
| 219 | + assert is_valid_anno_for_task(fxt_dataset_item, valid_polygon, OTXTaskType.MULTI_CLASS_CLS) is True |
| 220 | + assert is_valid_anno_for_task(fxt_dataset_item, invalid_polygon, OTXTaskType.MULTI_CLASS_CLS) is False |
| 221 | + assert is_valid_anno_for_task(fxt_dataset_item, label, OTXTaskType.MULTI_CLASS_CLS) is True |
| 222 | + |
| 223 | + # Test with SEMANTIC_SEGMENTATION task |
| 224 | + assert is_valid_anno_for_task(fxt_dataset_item, valid_bbox, OTXTaskType.SEMANTIC_SEGMENTATION) is True |
| 225 | + assert is_valid_anno_for_task(fxt_dataset_item, invalid_bbox, OTXTaskType.SEMANTIC_SEGMENTATION) is False |
| 226 | + assert is_valid_anno_for_task(fxt_dataset_item, valid_polygon, OTXTaskType.SEMANTIC_SEGMENTATION) is True |
| 227 | + assert is_valid_anno_for_task(fxt_dataset_item, invalid_polygon, OTXTaskType.SEMANTIC_SEGMENTATION) is False |
| 228 | + assert is_valid_anno_for_task(fxt_dataset_item, label, OTXTaskType.SEMANTIC_SEGMENTATION) is True |
| 229 | + |
| 230 | + def test_edge_cases(self, fxt_dataset_item: DatasetItem) -> None: |
| 231 | + """Test edge cases for annotation validation.""" |
| 232 | + # Very small but valid bbox |
| 233 | + small_bbox = Bbox(x=0, y=0, w=0.1, h=0.1, label=0) |
| 234 | + assert is_valid_anno_for_task(fxt_dataset_item, small_bbox, OTXTaskType.DETECTION) is True |
| 235 | + |
| 236 | + # Bbox with equal coordinates (should be invalid) |
| 237 | + equal_bbox = Bbox(x=5, y=5, w=0, h=0, label=0) |
| 238 | + assert is_valid_anno_for_task(fxt_dataset_item, equal_bbox, OTXTaskType.DETECTION) is False |
| 239 | + |
| 240 | + # Polygon with minimal valid area |
| 241 | + minimal_polygon = Polygon(points=[0, 0, 1, 0, 1, 1, 0, 1], label=0) |
| 242 | + assert is_valid_anno_for_task(fxt_dataset_item, minimal_polygon, OTXTaskType.INSTANCE_SEGMENTATION) is True |
| 243 | + |
| 244 | + # Degenerate polygon (should be invalid) |
| 245 | + degenerate_polygon = Polygon(points=[0, 0, 0, 0, 0, 0], label=0) |
| 246 | + assert is_valid_anno_for_task(fxt_dataset_item, degenerate_polygon, OTXTaskType.INSTANCE_SEGMENTATION) is False |
0 commit comments