Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support Ellipse Shape for InstSeg algo #4152

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 36 additions & 11 deletions src/otx/core/data/dataset/instance_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

from __future__ import annotations

from collections import defaultdict
from functools import partial
from typing import Callable

import numpy as np
import torch
from datumaro import Bbox, Ellipse, Image, Polygon
from datumaro import Dataset as DmDataset
from datumaro import Image, Polygon
from torchvision import tv_tensors

from otx.core.data.entity.base import ImageInfo
Expand Down Expand Up @@ -42,23 +43,47 @@ def _get_item_impl(self, index: int) -> InstanceSegDataEntity | None:
ignored_labels: list[int] = []
img_data, img_shape, _ = self._get_img_data_and_shape(img)

anno_collection: dict[str, list] = defaultdict(list)
for anno in item.annotations:
anno_collection[anno.__class__.__name__].append(anno)

gt_bboxes, gt_labels, gt_masks, gt_polygons = [], [], [], []

for annotation in item.annotations:
if isinstance(annotation, Polygon):
bbox = np.array(annotation.get_bbox(), dtype=np.float32)
if Polygon.__name__ in anno_collection: # Polygon for InstSeg has higher priority
for poly in anno_collection[Polygon.__name__]:
bbox = Bbox(*poly.get_bbox()).points
gt_bboxes.append(bbox)
gt_labels.append(annotation.label)
gt_labels.append(poly.label)

if self.include_polygons:
gt_polygons.append(annotation)
gt_polygons.append(poly)
else:
gt_masks.append(polygon_to_bitmap([annotation], *img_shape)[0])

# convert xywh to xyxy format
bboxes = np.array(gt_bboxes, dtype=np.float32) if gt_bboxes else np.empty((0, 4))
bboxes[:, 2:] += bboxes[:, :2]
gt_masks.append(polygon_to_bitmap([poly], *img_shape)[0])
elif Bbox.__name__ in anno_collection:
bboxes = anno_collection[Bbox.__name__]
gt_bboxes = [ann.points for ann in bboxes]
gt_labels = [ann.label for ann in bboxes]
for box in bboxes:
poly = Polygon(box.as_polygon())
if self.include_polygons:
gt_polygons.append(poly)
else:
gt_masks.append(polygon_to_bitmap([poly], *img_shape)[0])
elif Ellipse.__name__ in anno_collection:
for ellipse in anno_collection[Ellipse.__name__]:
bbox = Bbox(*ellipse.get_bbox()).points
gt_bboxes.append(bbox)
gt_labels.append(ellipse.label)
poly = Polygon(ellipse.as_polygon(num_points=10))
if self.include_polygons:
gt_polygons.append(poly)
else:
gt_masks.append(polygon_to_bitmap([poly], *img_shape)[0])
else:
msg = "No valid annotations found in the dataset."
raise ValueError(msg)

bboxes = np.stack(gt_bboxes, dtype=np.float32, axis=0) if gt_bboxes else np.empty((0, 4))
masks = np.stack(gt_masks, axis=0) if gt_masks else np.zeros((0, *img_shape), dtype=bool)
labels = np.array(gt_labels, dtype=np.int64)

Expand Down
1 change: 1 addition & 0 deletions src/otx/core/data/dataset/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(
)
self._tile_size = tile_size
self._tile_ann_func_map[AnnotationType.polygon] = OTXTileTransform._tile_polygon
self._tile_ann_func_map[AnnotationType.ellipse] = OTXTileTransform._tile_polygon
self.with_full_img = with_full_img

@staticmethod
Expand Down
Loading