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

feat: Add minimum_box_area_ratio to filter out poor, partial boxes #2484

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this as well. Maybe add a doc string to clip_to_image, explain the args and add an example there

)
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename passed to something more meaningful - valid_box maybe?

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
5 changes: 5 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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let us remove this from the augmentation layers. We need to keep this consistent with the other layers. Let us not touch this.

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 Expand Up @@ -302,6 +306,7 @@ def get_config(self):
"crop_size": self.crop_size,
"bounding_box_format": self.bounding_box_format,
"interpolation": self.interpolation,
"minimum_box_area_ratio": self.minimum_box_area_ratio,
"seed": self.seed,
}
)
Expand Down
Loading