Skip to content

Commit

Permalink
Speed up semantic segmentation annotations loading (#205)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Kozlovsky <[email protected]>
  • Loading branch information
sokovninn and kozlov721 authored Nov 8, 2024
1 parent 9c5d12a commit bfd3bbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions luxonis_ml/data/datasets/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,21 @@ def combine_to_numpy(
height: int,
width: int,
) -> np.ndarray:
seg = np.zeros((len(class_mapping), height, width), dtype=np.bool_)
for ann in annotations:
class_ = class_mapping.get(ann.class_, 0)
seg[class_, ...] |= ann.to_numpy(class_mapping, width, height)
seg = np.zeros((len(class_mapping), height, width), dtype=np.uint8)

masks = np.stack(
[ann.to_numpy(class_mapping, width, height) for ann in annotations]
)
classes = np.array(
[class_mapping.get(ann.class_, 0) for ann in annotations]
)

for i, class_ in enumerate(classes):
seg[class_, ...] = np.maximum(
seg[class_, ...], masks[i].astype(np.uint8)
)

return seg.astype(np.uint8)
return seg


class RLESegmentationAnnotation(SegmentationAnnotation):
Expand Down
2 changes: 1 addition & 1 deletion luxonis_ml/data/loaders/luxonis_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def _load_image_with_annotations(
anns, self.class_mappings[task], width=width, height=height
)
if self.add_background and task == LabelType.SEGMENTATION:
unassigned_pixels = np.sum(array, axis=0) == 0
unassigned_pixels = ~np.any(array, axis=0)
background_idx = self.class_mappings[task]["background"]
array[background_idx, unassigned_pixels] = 1

Expand Down

0 comments on commit bfd3bbc

Please sign in to comment.