-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Add sobel_edges to keras.ops.image #22334
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
Open
rstar327
wants to merge
4
commits into
keras-team:master
Choose a base branch
from
rstar327:add-sobel-edges-op
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+307
−0
Open
Changes from 1 commit
Commits
Show all changes
4 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
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
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
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
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 |
|---|---|---|
|
|
@@ -221,6 +221,18 @@ def test_gaussian_blur(self): | |
| out = kimage.gaussian_blur(x) | ||
| self.assertEqual(out.shape, (None, 3, 20, 20)) | ||
|
|
||
| def test_sobel_edges(self): | ||
| # Test channels_last | ||
| x = KerasTensor([None, 20, 20, 3]) | ||
| out = kimage.sobel_edges(x) | ||
| self.assertEqual(out.shape, (None, 20, 20, 3, 2)) | ||
|
|
||
| # Test channels_first | ||
| backend.set_image_data_format("channels_first") | ||
| x = KerasTensor([None, 3, 20, 20]) | ||
| out = kimage.sobel_edges(x) | ||
| self.assertEqual(out.shape, (None, 3, 20, 20, 2)) | ||
|
|
||
| def test_elastic_transform(self): | ||
| # Test channels_last | ||
| x = KerasTensor([None, 20, 20, 3]) | ||
|
|
@@ -1903,6 +1915,34 @@ def test_gaussian_blur(self): | |
| self.assertEqual(tuple(out.shape), tuple(ref_out.shape)) | ||
| self.assertAllClose(ref_out, out, atol=1e-2, rtol=1e-2) | ||
|
|
||
| def test_sobel_edges(self): | ||
| # Test channels_last | ||
| backend.set_image_data_format("channels_last") | ||
| np.random.seed(42) | ||
| x = np.random.uniform(size=(2, 10, 10, 3)).astype("float32") | ||
| out = kimage.sobel_edges(x, data_format="channels_last") | ||
| self.assertEqual(out.shape, (2, 10, 10, 3, 2)) | ||
|
|
||
| # Test single image | ||
| x_single = np.random.uniform(size=(10, 10, 3)).astype("float32") | ||
| out_single = kimage.sobel_edges(x_single, data_format="channels_last") | ||
| self.assertEqual(out_single.shape, (10, 10, 3, 2)) | ||
|
|
||
| # Test channels_first | ||
| backend.set_image_data_format("channels_first") | ||
| x_cf = np.random.uniform(size=(2, 3, 10, 10)).astype("float32") | ||
| out_cf = kimage.sobel_edges(x_cf, data_format="channels_first") | ||
| self.assertEqual(out_cf.shape, (2, 3, 10, 10, 2)) | ||
|
|
||
| # Test edge detection on known pattern: vertical edge | ||
| backend.set_image_data_format("channels_last") | ||
| img = np.zeros((1, 5, 5, 1), dtype="float32") | ||
| img[0, :, 3:, 0] = 1.0 | ||
| result = kimage.sobel_edges(img, data_format="channels_last") | ||
| dx = result[0, :, :, 0, 1] | ||
| # The horizontal gradient should be non-zero near the edge | ||
| self.assertTrue(np.any(np.abs(np.array(dx)) > 0)) | ||
|
Comment on lines
+1937
to
+1944
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. To make this test more robust, you could also assert that the vertical gradient # Test edge detection on known pattern: vertical edge
backend.set_image_data_format("channels_last")
img = np.zeros((1, 5, 5, 1), dtype="float32")
img[0, :, 3:, 0] = 1.0
result = kimage.sobel_edges(img, data_format="channels_last")
dy = result[0, :, :, 0, 0]
dx = result[0, :, :, 0, 1]
# The horizontal gradient should be non-zero near the edge.
self.assertTrue(np.any(np.abs(np.array(dx)) > 0))
# The vertical gradient should be zero for a vertical edge.
self.assertAllClose(dy, np.zeros_like(dy))
# Test edge detection on known pattern: horizontal edge
img = np.zeros((1, 5, 5, 1), dtype="float32")
img[0, 3:, :, 0] = 1.0
result = kimage.sobel_edges(img, data_format="channels_last")
dy = result[0, :, :, 0, 0]
dx = result[0, :, :, 0, 1]
# The vertical gradient should be non-zero near the edge.
self.assertTrue(np.any(np.abs(np.array(dy)) > 0))
# The horizontal gradient should be zero for a horizontal edge.
self.assertAllClose(dx, np.zeros_like(dx)) |
||
|
|
||
| def test_gaussian_blur_even_kernel_size(self): | ||
| """Test gaussian_blur with even kernel sizes""" | ||
| # This test is specific to the numpy backend fix | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.