Skip to content

Commit

Permalink
Filter invalid polygon shapes (#2795)
Browse files Browse the repository at this point in the history
* Fix invalid polygon shape filter

* style reformat

* fix segmentation test

* style changes

* update for readability

* revert some changes
  • Loading branch information
eugene123tw authored Jan 22, 2024
1 parent e58b7f9 commit d175f67
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/otx/core/data/adapter/base_dataset_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,16 @@ def _prepare_label_information(

return {"category_items": category_items, "label_groups": label_groups, "label_entities": label_entities}

def _is_normal_polygon(self, annotation: DatumAnnotationType.polygon) -> bool:
def _is_normal_polygon(self, annotation: DatumAnnotationType.polygon, width: int, height: int) -> bool:
"""To filter out the abnormal polygon."""
x_points = [annotation.points[i] for i in range(0, len(annotation.points), 2)]
y_points = [annotation.points[i + 1] for i in range(0, len(annotation.points), 2)]
return min(x_points) < max(x_points) and min(y_points) < max(y_points)
x_points = annotation.points[::2] # Extract x-coordinates
y_points = annotation.points[1::2] # Extract y-coordinates

return (
min(x_points) < max(x_points) < width
and min(y_points) < max(y_points) < height
and annotation.get_area() > 0
)

def _is_normal_bbox(self, x1: float, y1: float, x2: float, y2: float) -> bool:
"""To filter out the abrnormal bbox."""
Expand Down
2 changes: 1 addition & 1 deletion src/otx/core/data/adapter/detection_dataset_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_otx_dataset(self) -> DatasetEntity:
self.task_type in (TaskType.INSTANCE_SEGMENTATION, TaskType.ROTATED_DETECTION)
and ann.type == DatumAnnotationType.polygon
):
if self._is_normal_polygon(ann):
if self._is_normal_polygon(ann, image.width, image.height):
shapes.append(self._get_polygon_entity(ann, image.width, image.height))
if self.task_type is TaskType.DETECTION and ann.type == DatumAnnotationType.bbox:
if self._is_normal_bbox(ann.points[0], ann.points[1], ann.points[2], ann.points[3]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_otx_dataset(self) -> DatasetEntity:
for ann in datumaro_item.annotations:
if ann.type == DatumAnnotationType.polygon:
# save polygons as-is, they will be converted to masks.
if self._is_normal_polygon(ann):
if self._is_normal_polygon(ann, image.width, image.height):
shapes.append(self._get_polygon_entity(ann, image.width, image.height))

if ann.type == DatumAnnotationType.mask:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class TestNormalize:
@pytest.mark.parametrize(
"mean,std,to_rgb,expected",
[
(1.0, 1.0, True, np.array([[[1.0, 0.0, 0.0]]], dtype=np.float32)),
(1.0, 1.0, False, np.array([[[-1.0, 0.0, 0.0]]], dtype=np.float32)),
([[[1.0, 1.0, 1.0]]], [[[1.0, 1.0, 1.0]]], True, np.array([[[1.0, 0.0, -1.0]]], dtype=np.float32)),
([[[1.0, 1.0, 1.0]]], [[[1.0, 1.0, 1.0]]], False, np.array([[[-1.0, 0.0, 1.0]]], dtype=np.float32)),
],
)
def test_call(self, mean: float, std: float, to_rgb: bool, expected: np.array) -> None:
Expand Down

0 comments on commit d175f67

Please sign in to comment.