-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Fix/random_crop validation behavior #21871
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/random_crop validation behavior #21871
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 primarily focuses on enhancing the robustness and correctness of 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 effectively resolves two critical issues within the RandomCrop layer. The changes ensure that center-cropping is correctly applied during validation/inference, fixing previous shape mismatches, and also corrects the logic to handle cases where input dimensions exactly match the target crop size. The new tests added are valuable for verifying this corrected behavior. I have one suggestion to further strengthen the new validation test by asserting the content of the cropped image, not just its shape.
| # Create a test image with distinct corners | ||
| if backend.config.image_data_format() == "channels_last": | ||
| test_image = np.zeros((4, 4, 3)) | ||
| # Mark corners with different values | ||
| test_image[0, 0] = [1, 0, 0] # Top-left red | ||
| test_image[0, 3] = [0, 1, 0] # Top-right green | ||
| test_image[3, 0] = [0, 0, 1] # Bottom-left blue | ||
| test_image[3, 3] = [1, 1, 0] # Bottom-right yellow | ||
| else: | ||
| test_image = np.zeros((3, 4, 4)) | ||
| # Mark corners with different values | ||
| test_image[0, 0, 0] = 1 # Top-left red | ||
| test_image[1, 0, 3] = 1 # Top-right green | ||
| test_image[2, 3, 0] = 1 # Bottom-left blue | ||
| test_image[0, 3, 3] = 1 # Bottom-right yellow (red channel) | ||
| test_image[1, 3, 3] = 1 # Bottom-right yellow (green channel) | ||
|
|
||
| # Test validation mode (should center crop) | ||
| validation_output = layer(test_image, training=False) | ||
|
|
||
| # Center crop should capture the middle 2x2 region | ||
| expected_shape = ( | ||
| (2, 2, 3) | ||
| if backend.config.image_data_format() == "channels_last" | ||
| else (3, 2, 2) | ||
| ) | ||
| self.assertEqual(validation_output.shape, expected_shape) |
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 test currently verifies that validation mode produces an output of the correct shape, which is a great check for the fix. To make it more robust, I suggest also asserting that the content of the output is correct. This can be done by creating a test image with a distinct pattern in the center and then verifying that the output of the layer matches that pattern. This will confirm that the layer is performing a center crop, not just any crop of the correct size.
| # Create a test image with distinct corners | |
| if backend.config.image_data_format() == "channels_last": | |
| test_image = np.zeros((4, 4, 3)) | |
| # Mark corners with different values | |
| test_image[0, 0] = [1, 0, 0] # Top-left red | |
| test_image[0, 3] = [0, 1, 0] # Top-right green | |
| test_image[3, 0] = [0, 0, 1] # Bottom-left blue | |
| test_image[3, 3] = [1, 1, 0] # Bottom-right yellow | |
| else: | |
| test_image = np.zeros((3, 4, 4)) | |
| # Mark corners with different values | |
| test_image[0, 0, 0] = 1 # Top-left red | |
| test_image[1, 0, 3] = 1 # Top-right green | |
| test_image[2, 3, 0] = 1 # Bottom-left blue | |
| test_image[0, 3, 3] = 1 # Bottom-right yellow (red channel) | |
| test_image[1, 3, 3] = 1 # Bottom-right yellow (green channel) | |
| # Test validation mode (should center crop) | |
| validation_output = layer(test_image, training=False) | |
| # Center crop should capture the middle 2x2 region | |
| expected_shape = ( | |
| (2, 2, 3) | |
| if backend.config.image_data_format() == "channels_last" | |
| else (3, 2, 2) | |
| ) | |
| self.assertEqual(validation_output.shape, expected_shape) | |
| # Create a test image with a distinct center | |
| if backend.config.image_data_format() == "channels_last": | |
| test_image = np.zeros((4, 4, 3)) | |
| # Center 2x2 region with ones | |
| test_image[1:3, 1:3, :] = 1.0 | |
| else: | |
| test_image = np.zeros((3, 4, 4)) | |
| # Center 2x2 region with ones | |
| test_image[:, 1:3, 1:3] = 1.0 | |
| # Test validation mode (should center crop) | |
| validation_output = layer(test_image, training=False) | |
| # Expected output is the center 2x2 region of ones | |
| if backend.config.image_data_format() == "channels_last": | |
| expected_output = np.ones((2, 2, 3)) | |
| else: | |
| expected_output = np.ones((3, 2, 2)) | |
| self.assertAllClose(validation_output, expected_output) |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21871 +/- ##
==========================================
- Coverage 82.57% 76.95% -5.63%
==========================================
Files 577 577
Lines 59586 59614 +28
Branches 9347 9356 +9
==========================================
- Hits 49205 45873 -3332
- Misses 7975 11409 +3434
+ Partials 2406 2332 -74
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:
|
|
@utsab345, please check original issue of whether to allow the choice of center crop or normal resize for validation images before merging. |
Fix RandomCrop validation behavior and edge cases
Fixes #21868
Problem Description
The
RandomCroplayer had two critical issues:During validation/inference, the layer did nothing - Images were returned unchanged instead of being center-cropped, causing shape mismatches between training and validation pipelines.
Strict inequality prevented valid crops - The condition
input_height > self.height and input_width > self.widthprevented random cropping when image dimensions exactly matched the target size (e.g., cropping 512x512 to 256x512).Changes Made
Core Fixes in
random_crop.py:get_random_transformation: Changed condition frominput_height > self.height and input_width > self.widthtoinput_height >= self.height and input_width >= self.widthto allow cropping when dimensions exactly matchtransform_images: Removedif training:condition to ensure cropping is applied during both training and validationEnhanced Testing in
random_crop_test.py:test_validation_center_crop()to verify validation mode performs center croppingtest_edge_case_exact_dimensions()to test cropping when image dimensions exactly match target sizeBehavior After Fix
Testing
Impact