scipy: ENH: `ndimage.morphology.binary_dilation` is very slow
I’m using ndimage.morphology.binary_dilation (via skimage.morphology.binary_dilation) to dilate a large (4096*4096 pixel image) binary array.
Per Scikit-Image docs:
binary_dilation[returns] fast binary morphological dilation of an image.This function returns the same result as greyscale dilation but performs faster for binary images.
However, binary_dilation is very slow - orders of magnitude slower than ndimage.morphology.grey_dilation, when used on a binary array. This has also been surfaced as an skimage issue, but I wanted to surface it here as well.
This can be reproduced via:
from scipy import ndimage
from skimage import data, morphology, transform
dilation_kernel_size = 40
coins = data.coins().astype(float)
bigger_coins = transform.resize(coins, (1000, 1200))
binary_coins = bigger_coins > 170
print('scipy.ndimage.morphology.binary_dilation')
%timeit ndimage.morphology.binary_dilation(binary_coins, structure=morphology.square(dilation_kernel_size))
print('scipy.ndimage.morphology.grey dilation')
%timeit ndimage.morphology.grey_dilation(binary_coins, size=(dilation_kernel_size, dilation_kernel_size))
which outputs:
scipy.ndimage.morphology.binary_dilation
1.64 s ± 6.89 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
scipy.ndimage.morphology.grey dilation
14.7 ms ± 218 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Is this expected behavior? From a quick look at the ndimage.morphology source code, the implementations are different, and perhaps the outputs are not identical, but are morphologically similar.
My question is if this performance difference is expected or not, or if it is mostly a misuse/ poor documentation on part of skimage.
Thanks!
Scipy/Numpy/Python version information:
import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)
1.6.1 1.20.2 sys.version_info(major=3, minor=7, micro=9, releaselevel='final', serial=0)
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 15 (11 by maintainers)
Commits related to this issue
- Speeding up cellmasking See https://github.com/scipy/scipy/issues/13991#issuecomment-839853868 for the reason changing this way — committed to ehgus/TCFile by ehgus 5 years ago
- Deprecate `ski.morphology.binary_` funcs Most of the time, these seem to be slower than our regular gray-type functions, e.g., `ski.morphology.binary_erosion` vs `ski.morphology.erosion`. A potential... — committed to lagru/scikit-image by lagru 2 years ago
- Deprecate `ski.morphology.binary_` funcs Most of the time, these seem to be slower than our regular gray-type functions, e.g., `ski.morphology.binary_erosion` vs `ski.morphology.erosion`. A potential... — committed to lagru/scikit-image by lagru 2 years ago
Its still super slow
That is really just a workaround, but I agree with @cameronbronstein that there is likely some underlying inefficiency in the binary vs. grey implementation that could be improved, so I would leave the issue open.