-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Add RandomResizedCrop preprocessing layer #21929
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 RandomResizedCrop preprocessing layer #21929
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 Keras's image preprocessing capabilities by adding the Highlights
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 the RandomResizedCrop layer, a valuable addition for image data augmentation. The implementation is well-structured and includes a comprehensive test suite. I've identified a correctness issue in the handling of integer seeds which could lead to correlated random numbers where they should be independent. Additionally, I've suggested a refactoring for the compute_output_shape method to improve its clarity and robustness. Overall, this is a great contribution.
| if seed is None: | ||
| seed = self.generator |
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.
When an integer seed is provided, it is used for multiple separate random sampling operations (target_area, aspect_ratio, h_start, w_start). This will cause these operations to produce the same random value, which is incorrect as they should be independent. To fix this, when an integer seed is provided, it should be used to create a SeedGenerator instance, which will then provide a new seed for each random operation.
| if seed is None: | |
| seed = self.generator | |
| if isinstance(seed, int): | |
| seed = SeedGenerator(seed) | |
| elif seed is None: | |
| seed = self.generator |
| input_shape = list(input_shape) | ||
| if self.data_format == "channels_last": | ||
| if len(input_shape) == 4: | ||
| input_shape[1] = self.height | ||
| input_shape[2] = self.width | ||
| else: | ||
| input_shape[0] = self.height | ||
| input_shape[1] = self.width | ||
| else: | ||
| if len(input_shape) == 4: | ||
| input_shape[2] = self.height | ||
| input_shape[3] = self.width | ||
| else: | ||
| input_shape[1] = self.height | ||
| input_shape[2] = self.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.
This implementation of compute_output_shape can be simplified and made more robust by using self.height_axis and self.width_axis, which are already defined in __init__. This avoids hardcoding indices based on tensor rank and data_format, making the code more consistent with get_random_transformation.
input_shape = list(input_shape)
input_shape[self.height_axis] = self.height
input_shape[self.width_axis] = self.width
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21929 +/- ##
==========================================
- Coverage 82.63% 77.17% -5.47%
==========================================
Files 581 582 +1
Lines 60435 60525 +90
Branches 9482 9491 +9
==========================================
- Hits 49939 46708 -3231
- Misses 8054 11437 +3383
+ Partials 2442 2380 -62
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:
|
This PR adds
keras.layers.RandomResizedCrop, a preprocessing layer thatrandomly crops and resizes images to a fixed target size.
The layer:
This implementation supports image tensors only. For bounding boxes and
segmentation masks, users can rely on
keras_cv.layers.RandomResizedCrop.Closes #21822