Fix RandomZoom zero-scale validation and missing labels KeyError#22250
Fix RandomZoom zero-scale validation and missing labels KeyError#22250hertschuh merged 1 commit intokeras-team:masterfrom
Conversation
Summary of ChangesHello @rstar327, 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 addresses two critical bugs related to image preprocessing layers. It refines 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. Changelog
Activity
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.
Code Review
This pull request introduces two important bug fixes for RandomZoom and BoundingBox. The change to RandomZoom correctly prevents a zero-scale factor by adjusting the validation range, which avoids potential singular matrix errors. The modification in BoundingBox.clip_to_image_size makes the 'labels' key optional, preventing KeyError when it's not present in the input dictionary. The changes are well-implemented and address the issues described. I've added one suggestion to refactor some duplicated code for better maintainability.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #22250 +/- ##
==========================================
- Coverage 82.97% 82.97% -0.01%
==========================================
Files 594 594
Lines 64595 64599 +4
Branches 10120 10123 +3
==========================================
+ Hits 53598 53599 +1
Misses 8396 8396
- Partials 2601 2604 +3
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:
|
amadhan882
left a comment
There was a problem hiding this comment.
Technical Review by Issue Author
Hi @rstar327, thanks for the quick PR to address the issues I reported in #22247!
I've reviewed your changes in random_zoom.py and bounding_box.py. While the core fixes are solid, I noticed some areas in bounding_box.py that could be optimized to prevent redundant operations and code duplication.
Technical Observations:
- Redundant Logic in
bounding_box.py:
In the updatedclip_to_image_sizelogic, the area calculation and label filtering (mapping invalid boxes to -1) are repeated within both thexyxyandrel_xyxyblocks.
if labels is not None:
areas = self._compute_area(boxes)
areas = ops.numpy.squeeze(areas, axis=-1)
labels = ops.numpy.where(areas > 0, labels, -1)Suggestion: Since this filtering logic is independent of the coordinate format (once clipped), it would be much cleaner to move it to a single point at the end of the method, right before creating the result dictionary. This avoids running the same ops twice in different branches.
-
Factor Range Validation:
The change inrandom_zoom.pytoinput_number <= -1.0correctly implements the "Fail-Fast" principle for the zero-scale scenario. This is a robust fix for the second part of the reported bug. -
KeyError Resolution:
Usingbounding_boxes.get("labels", None)and conditionally adding it back to theresultdictionary effectively resolves theKeyErrorwhen labels are missing.
Suggested Action:
I recommend refactoring bounding_box.py to consolidate the label-filtering logic to avoid redundancy. Other than that, the fix correctly addresses the stability issues.
Looking forward to seeing this merged!
Summary
Fixes two bugs in
RandomZoom:Zero-scale factor validation:
_check_factor_rangenow rejects-1.0(changed<to<=), which previously created a zero-scale transformation matrix (1.0 + (-1.0) = 0), leading to singular matrix errors or NaN outputs.Missing labels KeyError:
clip_to_image_sizeinBoundingBoxnow treats thelabelskey as optional using.get(), so bounding box dicts with onlyboxesno longer crash withKeyError: 'labels'.Fixes #22247