Skip to content

Performance improvements for Canny feature detection - #5162

Closed
grlee77 wants to merge 5 commits into
scikit-image:masterfrom
grlee77:performance_canny
Closed

grlee77 wants to merge 5 commits into
scikit-image:masterfrom
grlee77:performance_canny

Conversation

@grlee77

@grlee77 grlee77 commented Dec 28, 2020 •

Copy link
Copy Markdown
Contributor

UPDATE

I have marked this as WIP due to a similar preceding PR at #4342. It probably makes more sense to merge this PR into that one.

Description

This PR makes multiple independent changes to the Canny feature detection code to improve performance. It will be easiest to review the commits in isolation as they each modify independent things. The table below shows the benchmark result for each commit here.

time_canny (ms) commit
1160 59889f5 (same as v0.18.1)
1090 769235e
1020 5757c11
988 5ab8429
791 a69f6a8
22 CuPy-based implementation equivalent to a69f6a8 (not in this PR)

Commits 5ab8429 and a69f6a8 only help for the case when mask=None. Overall, the reduction in runtime for the benchmark is 32% in the unmasked case and 12% in the masked case.

Commit 5757c11 gives only a minor improvement here, but makes an order of magnitude difference if using CuPy for the array module (cupyx.scipy.ndimage.sum is not very efficient for a large number of very small labeled regions). The poor performance of this function in a CuPy-based implementation was the reason I started looking at performance here in the first place.

Commit a69f6a8 provides a substantial benefit, but this commit does result in small differences near the image edge as generated via results from the following script. Qualitatively, they look equivalent to me, but the results are not pixelwise identical to before near the edge.

import numpy as np
from scipy import ndimage as ndi
from skimage import color, data, feature, util

image = color.rgb2gray(data.astronaut())
result = feature.canny(image, sigma=1.5, use_quantiles=True)

canny_compare

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.

tile up to size 2048, 2048 to get closer to 1 s runtime for canny
We really only need to know if sum > 0, not the actual value, so can use
numpy.unique on the masked values instead.
When mask=None, we can avoid binary morphology and just set the edge
values to zero instead.
avoid overhead of smoothing the mask when it is all ones.
@grlee77

grlee77 commented Dec 28, 2020

Copy link
Copy Markdown
Contributor Author

The CI failure is unrelated.

@rfezzani

Copy link
Copy Markdown
Member

Hi @grlee77, I think that I implemented some of the improvement proposed in this PR in #4342, plus a global refactoring that improves code readability I think. May be can we merge these two PRs?

@grlee77

grlee77 commented Dec 29, 2020

Copy link
Copy Markdown
Contributor Author

Hi @grlee77, I think that I implemented some of the improvement proposed in this PR in #4342, plus a global refactoring that improves code readability I think. May be can we merge these two PRs?

Thanks for pointing out that PR. I had not noticed it previously, but should have done a search here first! I do like the clarity of the larger refactoring in yours. Let me review it and then see about adding any needed commits to your branch (it does not appear to have a corresponding change to 5757c11, which was the key change here on the GPU side).

@grlee77 grlee77 changed the title Performance improvements for Canny feature detection WIP: Performance improvements for Canny feature detection Dec 29, 2020
@grlee77 grlee77 mentioned this pull request Dec 29, 2020
2 of 9 tasks
@rfezzani

Copy link
Copy Markdown
Member

👍 Yes I noticed your excellent use of np.unique and wanted to implement it in #4342 😉

@grlee77

grlee77 commented Dec 29, 2020

Copy link
Copy Markdown
Contributor Author

I have now added the performance improvements here to #4342, so will close this PR.

@grlee77 grlee77 closed this Dec 29, 2020
@grlee77 grlee77 changed the title WIP: Performance improvements for Canny feature detection Performance improvements for Canny feature detection Mar 11, 2021
@grlee77
grlee77 deleted the performance_canny branch July 8, 2021 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants