-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Add fill_mode="crop" to RandomRotation #21977
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
base: master
Are you sure you want to change the base?
Add fill_mode="crop" to RandomRotation #21977
Conversation
Summary of ChangesHello @MalyalaKarthik66, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces a new "crop" fill mode to the RandomRotation layer, which pads, rotates, and then center-crops images to maintain the original shape and avoid border artifacts. The changes extend the layer's functionality to support structured inputs, including images, segmentation masks, and bounding boxes, ensuring synchronized transformations. This involved significant refactoring of the RandomRotation class, adding helper methods for padding, cropping, and affine transformations, and updating the __init__ method and documentation. New test cases were added to validate the "crop" mode's behavior, output shape preservation, handling of structured inputs, and the absence of fill_value pixels. Review comments suggest improving consistency by directly using the imported ops module for backend operations instead of self.backend or a local ops_backend alias in the _apply_rotation_with_crop and transform_bounding_boxes methods.
| translate_x=self.backend.numpy.zeros( | ||
| [transformation["batch_size"]] | ||
| ), | ||
| translate_y=self.backend.numpy.zeros( | ||
| [transformation["batch_size"]] | ||
| ), | ||
| scale=self.backend.numpy.ones([transformation["batch_size"]]), | ||
| shear_x=self.backend.numpy.zeros([transformation["batch_size"]]), | ||
| shear_y=self.backend.numpy.zeros([transformation["batch_size"]]), |
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.
For consistency, please use the imported ops module for backend operations (e.g., ops.numpy.zeros) instead of accessing it via self.backend.
translate_x=ops.numpy.zeros([transformation["batch_size"]]),
translate_y=ops.numpy.zeros([transformation["batch_size"]]),
scale=ops.numpy.ones([transformation["batch_size"]]),
shear_x=ops.numpy.zeros([transformation["batch_size"]]),
shear_y=ops.numpy.zeros([transformation["batch_size"]]),| ops_backend = self.backend | ||
| height = transformation["image_height"] | ||
| width = transformation["image_width"] | ||
| batch_size = transformation["batch_size"] | ||
|
|
||
| # Keras preprocessing bbox dict: {"boxes": ..., "labels": ...} | ||
| # Convert to xyxy for rotation, then clip and convert back. | ||
| bounding_boxes = converters.convert_format( | ||
| bounding_boxes, | ||
| source=self.bounding_box_format, | ||
| target="xyxy", | ||
| height=height, | ||
| width=width, | ||
| ) | ||
|
|
||
| boxes = bounding_boxes["boxes"] | ||
| boxes = converters.affine_transform( | ||
| boxes=boxes, | ||
| angle=transformation["angle"], | ||
| translate_x=ops_backend.numpy.zeros([batch_size]), | ||
| translate_y=ops_backend.numpy.zeros([batch_size]), | ||
| scale=ops_backend.numpy.ones([batch_size]), | ||
| shear_x=ops_backend.numpy.zeros([batch_size]), | ||
| shear_y=ops_backend.numpy.zeros([batch_size]), | ||
| height=height, | ||
| width=width, | ||
| ) |
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.
For consistency with the rest of the file, it's better to use the imported ops module for backend operations instead of creating a local ops_backend alias. This improves readability and adheres to the intended API usage.
height = transformation["image_height"]
width = transformation["image_width"]
batch_size = transformation["batch_size"]
# Keras preprocessing bbox dict: {"boxes": ..., "labels": ...}
# Convert to xyxy for rotation, then clip and convert back.
bounding_boxes = converters.convert_format(
bounding_boxes,
source=self.bounding_box_format,
target="xyxy",
height=height,
width=width,
)
boxes = bounding_boxes["boxes"]
boxes = converters.affine_transform(
boxes=boxes,
angle=transformation["angle"],
translate_x=ops.numpy.zeros([batch_size]),
translate_y=ops.numpy.zeros([batch_size]),
scale=ops.numpy.ones([batch_size]),
shear_x=ops.numpy.zeros([batch_size]),
shear_y=ops.numpy.zeros([batch_size]),
height=height,
width=width,
)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21977 +/- ##
=======================================
Coverage 82.69% 82.69%
=======================================
Files 588 588
Lines 61448 61511 +63
Branches 9622 9629 +7
=======================================
+ Hits 50812 50868 +56
- Misses 8147 8150 +3
- Partials 2489 2493 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
It would be very helpful to understand the logic if you could add a gist for this PR. You may refer to my sample gist below. https://colab.research.google.com/drive/1n_0OzVKFHz5g0bFNOjQNulAAIjBuXvOT#scrollTo=lNIsN7fVPl3P |
This PR adds
fill_mode="crop"tokeras.layers.RandomRotation.What’s new
fill_mode="crop"that removes border artifacts bypad → rotate → center-crop, while preserving the input shape.
correct rotation geometry.
images,segmentation_masks,bounding_boxes) with synchronized transformations.docstring.
Tests
fill_mode="constant"fill_modehandlingFixes #21954.