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

Improve rank filtering performance by removing use of footprint kernel when possible #485

Merged
merged 3 commits into from
Feb 2, 2023

Conversation

grlee77
Copy link
Contributor

@grlee77 grlee77 commented Jan 24, 2023

In many cases such as rank filtering, if all elements of the kernel array are equal to one, then there is no reason to pay the overhead of allocating the footprint or checking its values during the filtering operation.

This MR is a small update to vendored CuPy ndimage code to avoid overhead of footprint creation and use when possible.

Will update with benchmark results of acceleration vs. the kernel size. It is approximately a 2x improvement for small kernels (e.g. 3x3, 5x5, 3x3x3) approaching no acceleration for larger kernel sizes.

can avoid one conditional in the inner loop when it is non that there are no
non-zero footprint entries

set all_weights_nonzero=True for separable min or max filters too

omit weights when possible for rank kernels
@grlee77 grlee77 added improvement Improves an existing functionality non-breaking Introduces a non-breaking change performance Performance improvement labels Jan 24, 2023
@grlee77 grlee77 requested a review from a team as a code owner January 24, 2023 20:11
@grlee77
Copy link
Contributor Author

grlee77 commented Jan 24, 2023

Here is a benchmark on a 2048x2048 2D image. (not sure why there is a spike specifically at size 13x13)

median_accel

@grlee77
Copy link
Contributor Author

grlee77 commented Jan 24, 2023

This did require one API addition not present in scikit-image. Namely, one can provide an integer size instead of a footprint if a fully populated square footprint is desired. (as available in the API of cupyx.scipy.median_filter)

@gigony gigony added this to the v23.02.00 milestone Jan 24, 2023
@codecov-commenter
Copy link

Codecov Report

Base: 92.95% // Head: 92.92% // Decreases project coverage by -0.03% ⚠️

Coverage data is based on head (3586751) compared to base (8a66080).
Patch coverage: 85.29% of modified lines in pull request are covered.

Additional details and impacted files
@@               Coverage Diff                @@
##           branch-23.02     #485      +/-   ##
================================================
- Coverage         92.95%   92.92%   -0.03%     
================================================
  Files               130      130              
  Lines              9775     9795      +20     
================================================
+ Hits               9086     9102      +16     
- Misses              689      693       +4     
Impacted Files Coverage Δ
...on/cucim/src/cucim/skimage/filters/_median_hist.py 81.31% <81.81%> (-0.31%) ⬇️
python/cucim/src/cucim/skimage/filters/_median.py 81.96% <91.66%> (+0.48%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@gigony gigony left a comment

Choose a reason for hiding this comment

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

Thanks @grlee77 ! Looks good to me!

@grlee77
Copy link
Contributor Author

grlee77 commented Feb 2, 2023

/merge

@rapids-bot rapids-bot bot merged commit 5b59e73 into rapidsai:branch-23.02 Feb 2, 2023
rapids-bot bot pushed a commit that referenced this pull request Mar 8, 2023
closes #520

#485 Introduced a bug in rank filtering kernels that was not caught by our current test cases
(so this bug is only present in the 23.02 release)

This MR fixes the bug and adds test coverage for this case.

Authors:
  - Gregory Lee (https://github.com/grlee77)
  - https://github.com/jakirkham

Approvers:
  - https://github.com/jakirkham

URL: #521
rapids-bot bot pushed a commit that referenced this pull request Apr 5, 2023
This is a follow-up to #485 allowing the overhead of creating a weights array of uniform values to be omitted when possible for all binary morphology operators. This gives a large performance benefit for small footprints (and small images) with the benefit becoming minimal for large footprint size.

As currently implemented, a shape tuple can be passed to `structure` to indicate the equivalent of an array of ones of that shape. This is a deviation from SciPy's API which does not currently allow this. I am not positive if this is the best API choice or if it might be better to instead add a `size` keyword-only argument like the grayscale morphology functions currently have.

The footprint generation methods `square`, `rectangle`, `cube` etc. use this new tuple format when possible to reduce overhead.

I will post benchmarks below.

Authors:
  - Gregory Lee (https://github.com/grlee77)

Approvers:
  - Gigon Bae (https://github.com/gigony)

URL: #517
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improves an existing functionality non-breaking Introduces a non-breaking change performance Performance improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants