Skip to content

Commit be79c8e

Browse files
JSabadinsokovninn
andauthored
Make bbox_area_threshold and min_bbox_visibility configurable (#348)
Co-authored-by: Nikita Sokovnin <[email protected]>
1 parent cf17cec commit be79c8e

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

luxonis_ml/data/augmentations/albumentations_engine.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,13 @@ def __init__(
282282
is_validation_pipeline: bool = False,
283283
min_bbox_visibility: float = 0.0,
284284
seed: int | None = None,
285+
bbox_area_threshold: float = 0.0004,
285286
):
286287
self.targets: dict[str, TargetType] = {}
287288
self.target_names_to_tasks = {}
288289
self.n_classes = n_classes
289290
self.image_size = (height, width)
291+
self.bbox_area_threshold = bbox_area_threshold
290292

291293
for task, task_type in targets.items():
292294
target_name = self.task_to_target_name(task)
@@ -572,7 +574,9 @@ def postprocess(
572574
task_name = get_task_name(task)
573575

574576
if target_type == "bboxes":
575-
out_labels[task], index = postprocess_bboxes(array)
577+
out_labels[task], index = postprocess_bboxes(
578+
array, self.bbox_area_threshold
579+
)
576580
bboxes_indices[task_name] = index
577581

578582
for target_name, target_type in self.targets.items():

luxonis_ml/data/augmentations/base_engine.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(
3030
is_validation_pipeline: bool,
3131
min_bbox_visibility: float = 0.0,
3232
seed: int | None = None,
33+
bbox_area_threshold: float = 0.0004,
3334
):
3435
"""Initialize augmentation pipeline from configuration.
3536
@@ -67,6 +68,9 @@ def __init__(
6768
pipeline (in which case some augmentations are skipped)
6869
@type min_bbox_visibility: float
6970
@param min_bbox_visibility: Minimum fraction of the original bounding box that must remain visible after augmentation.
71+
@type bbox_area_threshold: float
72+
@param bbox_area_threshold: Minimum area threshold for bounding boxes to be considered valid. In the range [0, 1].
73+
Default is 0.0004, which corresponds to a small area threshold to remove invalid bboxes and respective keypoints.
7074
@type seed: Optional[int]
7175
@param seed: Random seed for reproducibility. If None, a random seed will be used.
7276
If provided, it will be used to initialize the random number generator.

luxonis_ml/data/augmentations/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ def postprocess_mask(mask: np.ndarray) -> np.ndarray:
3939
return mask.transpose(2, 0, 1)
4040

4141

42-
def postprocess_bboxes(bboxes: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
43-
area_threshold = 0.0004 # 0.02 * 0.02 Small area threshold to remove invalid bboxes and respective keypoints.
42+
def postprocess_bboxes(
43+
bboxes: np.ndarray, area_threshold: float = 0.0004
44+
) -> tuple[np.ndarray, np.ndarray]:
4445
if bboxes.size == 0:
4546
return np.zeros((0, 5)), np.zeros((0,), dtype=np.uint8)
4647
ordering = bboxes[:, -1]

luxonis_ml/data/loaders/luxonis_loader.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def __init__(
4848
exclude_empty_annotations: bool = False,
4949
color_space: Literal["RGB", "BGR", "GRAY"] = "RGB",
5050
seed: int | None = None,
51+
min_bbox_visibility: float = 0.0,
52+
bbox_area_threshold: float = 0.0004,
5153
*,
5254
keep_categorical_as_strings: bool = False,
5355
update_mode: UpdateMode | Literal["all", "missing"] = UpdateMode.ALL,
@@ -95,6 +97,11 @@ def __init__(
9597
to C{"RGB"}.
9698
@type seed: Optional[int]
9799
@param seed: The random seed to use for the augmentations.
100+
@type min_bbox_visibility: float
101+
@param min_bbox_visibility: Minimum fraction of the original bounding box that must remain visible after augmentation.
102+
@type bbox_area_threshold: float
103+
@param bbox_area_threshold: Minimum area threshold for bounding boxes to be considered valid. In the range [0, 1].
104+
Default is 0.0004, which corresponds to a small area threshold to remove invalid bboxes and respective keypoints.
98105
@type exclude_empty_annotations: bool
99106
@param exclude_empty_annotations: Whether to exclude
100107
empty annotations from the final label dictionary.
@@ -218,6 +225,8 @@ def __init__(
218225
width,
219226
keep_aspect_ratio,
220227
seed,
228+
min_bbox_visibility,
229+
bbox_area_threshold,
221230
)
222231

223232
@override
@@ -434,6 +443,8 @@ def _init_augmentations(
434443
width: int | None,
435444
keep_aspect_ratio: bool,
436445
seed: int | None = None,
446+
min_bbox_visibility: float = 0.0,
447+
bbox_area_threshold: float = 0.0004,
437448
) -> AugmentationEngine | None:
438449
if isinstance(augmentation_config, PathType):
439450
with open(augmentation_config) as file:
@@ -475,6 +486,8 @@ def _init_augmentations(
475486
keep_aspect_ratio=keep_aspect_ratio,
476487
is_validation_pipeline="train" not in self.view,
477488
seed=seed,
489+
min_bbox_visibility=min_bbox_visibility,
490+
bbox_area_threshold=bbox_area_threshold,
478491
)
479492

480493
def _precompute_image_paths(self) -> None:

0 commit comments

Comments
 (0)