This repository was archived by the owner on Mar 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
feat: Add minimum_box_area_ratio to filter out poor, partial boxes
#2484
Open
thomas-coldwell
wants to merge
2
commits into
keras-team:master
Choose a base branch
from
thomas-coldwell:feature/jittered-resize-min-box-area
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+12
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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( | ||
|
|
@@ -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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we rename passed to something more meaningful - |
||
| 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) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,7 @@ def __init__( | |
| crop_size=None, | ||
| bounding_box_format=None, | ||
| interpolation="bilinear", | ||
| minimum_box_area_ratio=0.0, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| ): | ||
|
|
@@ -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( | ||
|
|
@@ -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, | ||
|
|
@@ -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, | ||
| } | ||
| ) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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