|
7 | 7 |
|
8 | 8 | import logging as log |
9 | 9 | import operator |
| 10 | +import warnings |
10 | 11 | from copy import deepcopy |
11 | 12 | from itertools import product |
12 | 13 | from typing import TYPE_CHECKING, Callable |
@@ -372,14 +373,17 @@ def _get_item_impl(self, index: int) -> TileDetDataEntity: # type: ignore[overr |
372 | 373 | img = item.media_as(Image) |
373 | 374 | img_data, img_shape, _ = self._get_img_data_and_shape(img) |
374 | 375 |
|
375 | | - bbox_anns = [ann for ann in item.annotations if isinstance(ann, Bbox)] |
| 376 | + gt_bboxes = [ann for ann in item.annotations if isinstance(ann, Bbox)] |
| 377 | + |
| 378 | + if empty_anno := len(gt_bboxes) == 0: |
| 379 | + warnings.warn(f"Empty annotation for image {item.id}!", stacklevel=2) |
376 | 380 |
|
377 | 381 | bboxes = ( |
378 | | - np.stack([ann.points for ann in bbox_anns], axis=0).astype(np.float32) |
379 | | - if len(bbox_anns) > 0 |
380 | | - else np.zeros((0, 4), dtype=np.float32) |
| 382 | + np.empty((0, 4), dtype=np.float32) |
| 383 | + if empty_anno |
| 384 | + else np.stack([ann.points for ann in gt_bboxes], axis=0).astype(np.float32) |
381 | 385 | ) |
382 | | - labels = torch.as_tensor([ann.label for ann in bbox_anns]) |
| 386 | + labels = torch.as_tensor([ann.label for ann in gt_bboxes]) |
383 | 387 |
|
384 | 388 | tile_entities, tile_attrs = self.get_tiles(img_data, item, index) |
385 | 389 |
|
@@ -476,11 +480,14 @@ def _get_item_impl(self, index: int) -> TileInstSegDataEntity: # type: ignore[o |
476 | 480 | else: |
477 | 481 | gt_masks.append(polygon_to_bitmap([annotation], *img_shape)[0]) |
478 | 482 |
|
| 483 | + if empty_anno := len(gt_bboxes) == 0: |
| 484 | + warnings.warn(f"Empty annotation for image {item.id}", stacklevel=2) |
| 485 | + |
479 | 486 | # convert xywh to xyxy format |
480 | | - bboxes = np.array(gt_bboxes, dtype=np.float32) |
| 487 | + bboxes = np.empty((0, 4), dtype=np.float32) if empty_anno else np.stack(gt_bboxes, dtype=np.float32) |
481 | 488 | bboxes[:, 2:] += bboxes[:, :2] |
482 | 489 |
|
483 | | - masks = np.stack(gt_masks, axis=0) if gt_masks else np.zeros((0, *img_shape), dtype=bool) |
| 490 | + masks = np.stack(gt_masks, axis=0) if gt_masks else np.empty((0, *img_shape), dtype=bool) |
484 | 491 | labels = np.array(gt_labels, dtype=np.int64) |
485 | 492 |
|
486 | 493 | tile_entities, tile_attrs = self.get_tiles(img_data, item, index) |
|
0 commit comments