Skip to content
New issue

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

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

Already on GitHub? # to your account

Change sematic segmentation to consider bbox only annotations. #3996

Merged
merged 7 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 6 additions & 6 deletions src/otx/core/data/dataset/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import cv2
import numpy as np
import torch
from datumaro.components.annotation import Ellipse, Image, Mask, Polygon
from datumaro.components.annotation import Bbox, Ellipse, Image, Mask, Polygon, RotatedBbox
from torchvision import tv_tensors

from otx.core.data.dataset.base import Transforms
Expand Down Expand Up @@ -99,11 +99,11 @@ def _extract_class_mask(item: DatasetItem, img_shape: tuple[int, int], ignore_in
raise ValueError(msg, ignore_index)

# fill mask with background label if we have Polygon/Ellipse annotations
fill_value = 0 if isinstance(item.annotations[0], (Ellipse, Polygon)) else ignore_index
fill_value = 0 if isinstance(item.annotations[0], (Ellipse, Polygon, Bbox, RotatedBbox)) else ignore_index
class_mask = np.full(shape=img_shape[:2], fill_value=fill_value, dtype=np.uint8)

for mask in sorted(
[ann for ann in item.annotations if isinstance(ann, (Mask, Ellipse, Polygon))],
[ann for ann in item.annotations if isinstance(ann, (Mask, Ellipse, Polygon, Bbox, RotatedBbox))],
key=lambda ann: ann.z_order,
):
index = mask.label
Expand All @@ -112,7 +112,7 @@ def _extract_class_mask(item: DatasetItem, img_shape: tuple[int, int], ignore_in
msg = "Mask's label index should not be None."
raise ValueError(msg)

if isinstance(mask, (Ellipse, Polygon)):
if isinstance(mask, (Ellipse, Polygon, Bbox, RotatedBbox)):
polygons = np.asarray(mask.as_polygon(), dtype=np.int32).reshape((-1, 1, 2))
class_index = index + 1 # NOTE: disregard the background index. Objects start from index=1
this_class_mask = cv2.drawContours(
Expand Down Expand Up @@ -193,8 +193,8 @@ def __init__(
@property
def has_polygons(self) -> bool:
"""Check if the dataset has polygons in annotations."""
ann_types = {str(ann_type).split(".")[-1] for ann_type in self.dm_subset.ann_types()}
if ann_types & {"polygon", "ellipse"}:
# all polygon-like format should be considered as polygons
if {ann_type.name for ann_type in self.dm_subset.ann_types()} & {"polygon", "ellipse", "bbox", "rotated_bbox"}:
return True
return False

Expand Down
11 changes: 9 additions & 2 deletions tests/unit/core/data/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import cv2
import numpy as np
import pytest
from datumaro.components.annotation import Bbox, Label, LabelCategories, Mask, Polygon
from datumaro.components.annotation import AnnotationType, Bbox, Label, LabelCategories, Mask, Polygon
from datumaro.components.dataset import Dataset as DmDataset
from datumaro.components.dataset_base import DatasetItem
from datumaro.components.media import Image
Expand Down Expand Up @@ -89,7 +89,7 @@ def fxt_dm_item(request, tmpdir) -> DatasetItem:
media=media,
annotations=[
Label(label=0),
Bbox(x=0, y=0, w=1, h=1, label=0),
Bbox(x=200, y=200, w=1, h=1, label=0),
Mask(label=0, image=np.eye(10, dtype=np.uint8)),
Polygon(points=[399.0, 570.0, 397.0, 572.0, 397.0, 573.0, 394.0, 576.0], label=0),
],
Expand Down Expand Up @@ -133,6 +133,12 @@ def fxt_mock_dm_subset(mocker: MockerFixture, fxt_dm_item: DatasetItem) -> Magic
mock_dm_subset.__getitem__.return_value = fxt_dm_item
mock_dm_subset.__len__.return_value = 1
mock_dm_subset.categories().__getitem__.return_value = LabelCategories.from_iterable(_LABEL_NAMES)
mock_dm_subset.ann_types.return_value = [
AnnotationType.label,
AnnotationType.bbox,
AnnotationType.mask,
AnnotationType.polygon,
]
return mock_dm_subset


Expand All @@ -142,6 +148,7 @@ def fxt_mock_det_dm_subset(mocker: MockerFixture, fxt_dm_item_bbox_only: Dataset
mock_dm_subset.__getitem__.return_value = fxt_dm_item_bbox_only
mock_dm_subset.__len__.return_value = 1
mock_dm_subset.categories().__getitem__.return_value = LabelCategories.from_iterable(_LABEL_NAMES)
mock_dm_subset.ann_types.return_value = [AnnotationType.bbox]
return mock_dm_subset


Expand Down
36 changes: 36 additions & 0 deletions tests/unit/core/data/dataset/test_segmentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""Unit tests of classification datasets."""

from otx.core.data.dataset.segmentation import OTXSegmentationDataset
from otx.core.data.entity.segmentation import SegDataEntity


class TestOTXSegmentationDataset:
def test_get_item(
self,
fxt_mock_dm_subset,
) -> None:
dataset = OTXSegmentationDataset(
dm_subset=fxt_mock_dm_subset,
transforms=[lambda x: x],
mem_cache_img_max_size=None,
max_refetch=3,
)
assert isinstance(dataset[0], SegDataEntity)
assert "background" in [label_name.lower() for label_name in dataset.label_info.label_names]

def test_get_item_from_bbox_dataset(
self,
fxt_mock_det_dm_subset,
) -> None:
dataset = OTXSegmentationDataset(
dm_subset=fxt_mock_det_dm_subset,
transforms=[lambda x: x],
mem_cache_img_max_size=None,
max_refetch=3,
)
assert isinstance(dataset[0], SegDataEntity)
# OTXSegmentationDataset should add background when getting a dataset which includes only bbox annotations
assert "background" in [label_name.lower() for label_name in dataset.label_info.label_names]
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ commands =
{posargs}


[testenv:integration-test-{all, action, classification, multi_cls_classification, multi_label_classification, hlabel_classification, detection, rotated_detection, keypoint_detection, instance_segmentation, semantic_segmentation, visual_prompting_all, visual_prompting, zero_shot_visual_prompting, anomaly_classification, anomaly_detection, anomaly_segmentation}]
[testenv:integration-test-{all, action, classification, multi_cls_classification, multi_label_classification, hlabel_classification, detection, rotated_detection, keypoint_detection, instance_segmentation, semantic_segmentation, visual_prompting_all, visual_prompting, zero_shot_visual_prompting, anomaly, anomaly_classification, anomaly_detection, anomaly_segmentation}]
setenv =
CUBLAS_WORKSPACE_CONFIG=:4096:8
deps =
Expand All @@ -75,7 +75,7 @@ commands =

[testenv:perf-benchmark]
deps =
.[base,dev]
.[base,dev,ci_benchmark]
commands =
pytest -ra --showlocals --csv={toxworkdir}/{envname}-test.csv {posargs:tests/perf}

Expand Down
Loading