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

Remove bounding box utils and refactor retinanet #2039

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

sineeli
Copy link
Collaborator

@sineeli sineeli commented Jan 8, 2025

  1. Moved non_max_suppression, anchor_generator, and box_matcher into the modeling layers for better integration.
  2. Removed redundant bounding box utilities and dependencies

@sineeli sineeli requested a review from mattdangerw January 9, 2025 16:27
@mattdangerw
Copy link
Member

mattdangerw commented Jan 13, 2025

Not sure what best to do about this test failure. @sineeli do you know the minimal version of Keras we would need for all of this?

@sineeli
Copy link
Collaborator Author

sineeli commented Jan 13, 2025

Not sure what best to do about this test failure. @sineeli do you know the minimal version of Keras we would need for all of this?

It would require latest release 3.8.0 which has iou function and box encoding utilities as well.

Copy link
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

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

Thanks! Left some comments

"`bounding_box.to_dense(bounding_boxes)` before passing your boxes "
"to `bounding_box.mask_invalid_detections()`."
)
validation.validate_bounding_boxes(bounding_boxes)
Copy link
Member

Choose a reason for hiding this comment

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

Should we consider making this public API in Keras? (Though we can keep this usage for now)

Copy link
Member

Choose a reason for hiding this comment

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

Or could we trigger this with public API? E.g. keras.utils.bounding_boxes.convert_format to self.bounding_box_format like we do in call?

Copy link
Member

Choose a reason for hiding this comment

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

I think need to either remove this or duplicate it here. import keras.src.layers.preprocessing.image_preprocessing.bounding_boxes will means KerasHub will not even import for older than the latest version of keras (including the keras currently preinstalled in colab images).

We don't want that.

keras_hub/src/layers/modeling/box_matcher.py Show resolved Hide resolved
keras_hub/src/layers/modeling/anchor_generator.py Outdated Show resolved Hide resolved
image_size=None,
scale=None,
offset=None,
bounding_box_format,
Copy link
Member

Choose a reason for hiding this comment

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

should we have a default for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I mean we can have same as default format used for weights, but it can vary as per user dataset.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but I think a default is still probably helpful? I see a lot of bounding_box_format="xyxy", defaults in Keras.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Our default weights are in yxyx. But yes we can have xyxy(torch) follows or yxyx(tensorflow) follows this. All the tensorflow datasets will have yxyx, I would say yxyx would be good option.

@@ -40,12 +55,14 @@ def call(self, inputs):
if self.norm_std:
x = x / self._expand_non_channel_dims(self.norm_std, x)

return x
return x, y
Copy link
Member

Choose a reason for hiding this comment

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

probably this should be keras.utils.pack_x_y_sample_weight(x, y, sample_weight)?

Copy link
Member

Choose a reason for hiding this comment

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

This still needs doing I think (otherwise you toss sample_weight).

Copy link
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

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

Still a few comments to address. The big one, we need to get rid of the private import of keras for that validation function. Let's remove or duplicate.

We might also want to set things up so the library still works with older keras versions, and throws an error when using an OD model that 3.8 is required. But I can help with that part.

image_size=None,
scale=None,
offset=None,
bounding_box_format,
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, but I think a default is still probably helpful? I see a lot of bounding_box_format="xyxy", defaults in Keras.

@@ -40,12 +55,14 @@ def call(self, inputs):
if self.norm_std:
x = x / self._expand_non_channel_dims(self.norm_std, x)

return x
return x, y
Copy link
Member

Choose a reason for hiding this comment

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

This still needs doing I think (otherwise you toss sample_weight).

"`bounding_box.to_dense(bounding_boxes)` before passing your boxes "
"to `bounding_box.mask_invalid_detections()`."
)
validation.validate_bounding_boxes(bounding_boxes)
Copy link
Member

Choose a reason for hiding this comment

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

I think need to either remove this or duplicate it here. import keras.src.layers.preprocessing.image_preprocessing.bounding_boxes will means KerasHub will not even import for older than the latest version of keras (including the keras currently preinstalled in colab images).

We don't want that.

@sineeli
Copy link
Collaborator Author

sineeli commented Jan 21, 2025

Oh yeah make sense, we can only import when we have Keras>=3.8.0 version and then throw error when this doesn't satisfy? I hope this works.

class NonMaxSuppression(keras.layers.Layer):
"""A Keras layer that decodes predictions of an object detection model.
Args:
bounding_box_format: The format of bounding boxes of input dataset.
Refer
TODO: link keras core bounding box docs
Refer:
for more details on supported bounding box formats.
Copy link
Collaborator

Choose a reason for hiding this comment

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

add link to keras API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants