-
Couldn't load subscription status.
- Fork 19.6k
Fix: correct effective crop_to_aspect_ratio logic and lint issues #21779
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?
Fix: correct effective crop_to_aspect_ratio logic and lint issues #21779
Conversation
…andling and tests
Summary of ChangesHello @utsab345, 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 refines the image resizing and saving functionalities within the Keras utilities. The primary focus is on fixing a subtle bug in 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 correctly refactors the crop_to_aspect_ratio logic in the Resizing layer to only trigger when aspect ratios differ, and also includes some linting fixes. My review focuses on improving robustness. I've suggested adding a check to prevent a potential ZeroDivisionError in resizing.py and fixing a bug in image_utils.py where a warning for RGBA to RGB conversion for JPEG images was missed when the file format was inferred from the path.
| source_aspect_ratio = input_width / input_height | ||
| target_aspect_ratio = self.width / self.height |
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.
To prevent a potential ZeroDivisionError if input_height or self.height is zero, it's safer to add a small epsilon to the denominators. This is a good defensive practice, especially since these values could be dynamic tensors.
| source_aspect_ratio = input_width / input_height | |
| target_aspect_ratio = self.width / self.height | |
| source_aspect_ratio = input_width / (input_height + self.backend.epsilon()) | |
| target_aspect_ratio = self.width / (self.height + self.backend.epsilon()) |
keras/src/utils/image_utils.py
Outdated
| if file_format is not None and file_format.lower() == "jpg": | ||
| file_format = "jpeg" | ||
| img = array_to_img(x, data_format=data_format, scale=scale) | ||
| if img.mode == "RGBA" and (file_format == "jpg" or file_format == "jpeg"): | ||
| if img.mode == "RGBA" and file_format == "jpeg": | ||
| warnings.warn( | ||
| "The JPG format does not support RGBA images, converting to RGB." | ||
| "The JPEG format does not support RGBA images, converting to RGB." | ||
| ) | ||
| img = img.convert("RGB") | ||
| img.save(path, format=file_format, **kwargs) |
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.
The current logic for warning about RGBA to RGB conversion for JPEG images has a flaw. It doesn't handle the case where file_format is None and the format is inferred from the file path (e.g., 'image.jpg'). In this scenario, PIL would convert the image silently, but no warning would be issued. The logic should be updated to infer the format from the path if it's not explicitly provided, ensuring the warning is always shown when appropriate.
if file_format is None and isinstance(path, (str, bytes, pathlib.Path)):
path_obj = pathlib.Path(path)
if path_obj.suffix:
file_format = path_obj.suffix[1:].lower()
# Normalize jpg → jpeg
if file_format is not None and file_format.lower() == "jpg":
file_format = "jpeg"
img = array_to_img(x, data_format=data_format, scale=scale)
if img.mode == "RGBA" and file_format == "jpeg":
warnings.warn(
"The JPEG format does not support RGBA images, converting to RGB."
)
img = img.convert("RGB")
img.save(path, format=file_format, **kwargs)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #21779 +/- ##
=======================================
Coverage 82.63% 82.64%
=======================================
Files 577 577
Lines 59249 59254 +5
Branches 9291 9291
=======================================
+ Hits 48963 48968 +5
Misses 7903 7903
Partials 2383 2383
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:
|
PR Description
This PR fixes the logic controlling crop_to_aspect_ratio inside transform_images() in the Resizing layer.
Summary of changes:
Ensures cropping only occurs when aspect ratios differ.
Adds a small epsilon (1e-6) for floating-point comparisons.
Fixes Ruff E501 long line comment warnings.
Verified that test_crop_to_aspect_ratio_no_op_when_aspects_match passes, confirming functional parity.
Related issue:
Fixes #21773