Skip to content
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

Refactor gaussian_blur in numpy to utilize scipy.signal.convolve2d #20974

Merged
merged 1 commit into from
Feb 28, 2025

Conversation

shashaka
Copy link
Contributor

@shashaka shashaka commented Feb 27, 2025

To improve performance, I optimized the convolution logic in gaussian_blur by replacing the NumPy-based implementation with scipy.signal.convolve2d.
Below are the performance test results on my local machine:

  • Before Optimization:
%%time
%load_ext autoreload
%autoreload 2

from keras.src.backend.numpy.image import gaussian_blur

images = np.random.uniform(size=(2, 800, 800, 3)).astype("float32")
kernel_size = (3, 3)
sigma = np.random.uniform(size=(2,)).astype("float32")

transformed_image = gaussian_blur(images, sigma=sigma)

CPU times: user 7.59 s, sys: 0 ns, total: 7.59 s
Wall time: 7.59 s

  • After Optimization:
%%time
%load_ext autoreload
%autoreload 2

from keras.src.backend.numpy.image import gaussian_blur

images = np.random.uniform(size=(2, 800, 800, 3)).astype("float32")
kernel_size = (3, 3)
sigma = np.random.uniform(size=(2,)).astype("float32")

transformed_image = gaussian_blur(images, sigma=sigma)

CPU times: user 123 ms, sys: 0 ns, total: 123 ms
Wall time: 122 ms

@codecov-commenter
Copy link

codecov-commenter commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.43%. Comparing base (d1fb581) to head (3d21b6c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #20974      +/-   ##
==========================================
- Coverage   82.43%   82.43%   -0.01%     
==========================================
  Files         561      561              
  Lines       53204    53201       -3     
  Branches     8242     8241       -1     
==========================================
- Hits        43857    43854       -3     
  Misses       7338     7338              
  Partials     2009     2009              
Flag Coverage Δ
keras 82.24% <100.00%> (-0.01%) ⬇️
keras-jax 64.03% <0.00%> (+<0.01%) ⬆️
keras-numpy 58.85% <100.00%> (-0.01%) ⬇️
keras-openvino 32.61% <0.00%> (+<0.01%) ⬆️
keras-tensorflow 64.48% <0.00%> (+<0.01%) ⬆️
keras-torch 64.09% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Feb 28, 2025
@fchollet fchollet merged commit 21c8997 into keras-team:master Feb 28, 2025
9 checks passed
@shashaka shashaka deleted the gaussian_blur branch February 28, 2025 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kokoro:force-run ready to pull Ready to be merged into the codebase size:S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants