-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Unify extract_patches to support both 2D and 3D patches #21980
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
Merged
hertschuh
merged 3 commits into
keras-team:master
from
MarcosAsh:unified-extract-patches
Jan 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2357,19 +2357,29 @@ def test_affine_transform_invalid_transform_rank(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kimage.affine_transform(images, invalid_transform) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_extract_patches_invalid_size(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size = (3, 3, 3) # Invalid size, too many dimensions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size = "5" # Invalid size type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| image = np.random.uniform(size=(2, 20, 20, 3)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with self.assertRaisesRegex( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TypeError, "Expected an int or a tuple of length 2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with self.assertRaisesRegex(TypeError, "Expected an int or a tuple"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kimage.extract_patches(image, size) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size = "5" # Invalid size type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size = (3, 3, 3, 3) # Invalid size, too many dimensions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with self.assertRaisesRegex( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TypeError, "Expected an int or a tuple of length 2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ValueError, "Expected a tuple of length 2 or 3" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kimage.extract_patches(image, size) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
2359
to
2369
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should be updated to reflect the improved validation in
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_extract_patches_unified_3d(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Test that extract_patches handles 3D volumes when size has 3 elements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # channels_last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| volume = np.random.uniform(size=(2, 20, 20, 20, 3)).astype("float32") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| patches = kimage.extract_patches(volume, (5, 5, 5)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(patches.shape, (2, 4, 4, 4, 375)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # unbatched | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| volume = np.random.uniform(size=(20, 20, 20, 3)).astype("float32") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| patches = kimage.extract_patches(volume, (5, 5, 5)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.assertEqual(patches.shape, (4, 4, 4, 375)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_map_coordinates_invalid_coordinates_rank(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Test mismatched dim of coordinates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| image = np.random.uniform(size=(10, 10, 3)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 docstrings for
images,size, anddata_formatcould be more explicit to improve clarity, especially with the new 3D support. The currentimagesdocstring assumeschannels_lastwithout stating it, thesizedocstring is ambiguous about integer values, and thedata_formatdocstring is a bit sparse. I suggest clarifying these points for a better user experience.