Skip to content

Commit

Permalink
feat: Add minimum_box_area_ratio to filter out poor, partial boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-coldwell committed Aug 29, 2024
1 parent ba2556c commit 1c92a2b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def main():
target_size=(512, 512),
scale_factor=(3 / 4, 4 / 3),
bounding_box_format="xyxy",
minimum_box_area_ratio=0.5
)
result = dataset.map(jittered_resize, num_parallel_calls=tf.data.AUTOTUNE)
demo_utils.visualize_data(result, bounding_box_format="xyxy")
Expand Down
9 changes: 6 additions & 3 deletions keras_cv/src/bounding_box/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _relative_area(boxes, bounding_box_format):

@keras_cv_export("keras_cv.bounding_box.clip_to_image")
def clip_to_image(
bounding_boxes, bounding_box_format, images=None, image_shape=None
bounding_boxes, bounding_box_format, images=None, image_shape=None, minimum_box_area_ratio=0.0
):
"""clips bounding boxes to image boundaries.
Expand All @@ -92,6 +92,7 @@ class ID set to -1, indicating that there is no object present in them.
images=images,
image_shape=image_shape,
)
original_areas = _relative_area(boxes, bounding_box_format="rel_xyxy")
boxes, classes, images, squeeze = _format_inputs(boxes, classes, images)
x1, y1, x2, y2 = ops.split(boxes, 4, axis=-1)
clipped_bounding_boxes = ops.concatenate(
Expand All @@ -106,17 +107,19 @@ class ID set to -1, indicating that there is no object present in them.
areas = _relative_area(
clipped_bounding_boxes, bounding_box_format="rel_xyxy"
)
area_ratios = ops.divide(areas, original_areas)
clipped_bounding_boxes = bounding_box.convert_format(
clipped_bounding_boxes,
source="rel_xyxy",
target=bounding_box_format,
images=images,
image_shape=image_shape,
)
passed = ops.logical_and(areas > 0.0, area_ratios > minimum_box_area_ratio)
clipped_bounding_boxes = ops.where(
ops.expand_dims(areas > 0.0, axis=-1), clipped_bounding_boxes, -1.0
ops.expand_dims(passed, axis=-1), clipped_bounding_boxes, -1.0
)
classes = ops.where(areas > 0.0, classes, -1)
classes = ops.where(passed, classes, -1)
nan_indices = ops.any(ops.isnan(clipped_bounding_boxes), axis=-1)
classes = ops.where(nan_indices, -1, classes)

Expand Down
4 changes: 4 additions & 0 deletions keras_cv/src/layers/preprocessing/jittered_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def __init__(
crop_size=None,
bounding_box_format=None,
interpolation="bilinear",
minimum_box_area_ratio=0.0,
seed=None,
**kwargs,
):
Expand Down Expand Up @@ -130,6 +131,8 @@ def __init__(
self.seed = seed

self.force_output_dense_images = True

self.minimum_box_area_ratio = minimum_box_area_ratio

def compute_ragged_image_signature(self, images):
ragged_spec = tf.RaggedTensorSpec(
Expand Down Expand Up @@ -252,6 +255,7 @@ def augment_bounding_boxes(
result,
image_shape=self.target_size + (3,),
bounding_box_format="yxyx",
minimum_box_area_ratio=self.minimum_box_area_ratio
)
result = bounding_box.convert_format(
result,
Expand Down

0 comments on commit 1c92a2b

Please sign in to comment.