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

Speed up semantic segmentation annotations loading #205

Merged
merged 9 commits into from
Nov 8, 2024
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
Loading