-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GPU support for ndmorph, binary morphological functions (#157)
* GPU support for ndmorph subpackage * Fix ndmorph default structure for cupy arrays
- Loading branch information
1 parent
91fe6e1
commit 91c4955
Showing
4 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
import scipy.ndimage | ||
|
||
from ._dispatcher import Dispatcher | ||
|
||
__all__ = [ | ||
"dispatch_binary_dilation", | ||
"dispatch_binary_erosion", | ||
"dispatch_binary_structure", | ||
] | ||
|
||
dispatch_binary_dilation = Dispatcher(name="dispatch_binary_dilation") | ||
dispatch_binary_erosion = Dispatcher(name="dispatch_binary_erosion") | ||
dispatch_binary_structure = Dispatcher(name='dispatch_binary_structure') | ||
|
||
|
||
# ================== binary_dilation ================== | ||
@dispatch_binary_dilation.register(np.ndarray) | ||
def numpy_binary_dilation(*args, **kwargs): | ||
return scipy.ndimage.binary_dilation | ||
|
||
|
||
@dispatch_binary_dilation.register_lazy("cupy") | ||
def register_cupy_binary_dilation(): | ||
import cupy | ||
import cupyx.scipy.ndimage | ||
|
||
@dispatch_binary_dilation.register(cupy.ndarray) | ||
def cupy_binary_dilation(*args, **kwargs): | ||
return cupyx.scipy.ndimage.binary_dilation | ||
|
||
|
||
# ================== binary_erosion ================== | ||
@dispatch_binary_erosion.register(np.ndarray) | ||
def numpy_binary_erosion(*args, **kwargs): | ||
return scipy.ndimage.binary_erosion | ||
|
||
|
||
@dispatch_binary_erosion.register_lazy("cupy") | ||
def register_cupy_binary_erosion(): | ||
import cupy | ||
import cupyx.scipy.ndimage | ||
|
||
@dispatch_binary_erosion.register(cupy.ndarray) | ||
def cupy_binary_erosion(*args, **kwargs): | ||
return cupyx.scipy.ndimage.binary_erosion | ||
|
||
|
||
# ================== generate_binary_structure ================== | ||
@dispatch_binary_structure.register(np.ndarray) | ||
def numpy_binary_structure(*args, **kwargs): | ||
return scipy.ndimage.generate_binary_structure | ||
|
||
|
||
@dispatch_binary_structure.register_lazy("cupy") | ||
def register_cupy_binary_structure(): | ||
import cupy | ||
import cupyx.scipy.ndimage | ||
|
||
@dispatch_binary_structure.register(cupy.ndarray) | ||
def cupy_binary_structure(*args, **kwargs): | ||
return cupyx.scipy.ndimage.generate_binary_structure |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import dask.array as da | ||
import numpy as np | ||
import pytest | ||
|
||
from dask_image import ndmorph | ||
|
||
cupy = pytest.importorskip("cupy", minversion="7.7.0") | ||
|
||
|
||
@pytest.fixture | ||
def array(): | ||
s = (10, 10) | ||
a = da.from_array(cupy.arange(int(np.prod(s)), | ||
dtype=cupy.float32).reshape(s), chunks=5) | ||
return a | ||
|
||
|
||
@pytest.mark.cupy | ||
@pytest.mark.parametrize("func", [ | ||
ndmorph.binary_closing, | ||
ndmorph.binary_dilation, | ||
ndmorph.binary_erosion, | ||
ndmorph.binary_opening, | ||
]) | ||
def test_cupy_ndmorph(array, func): | ||
"""Test convolve & correlate filters with cupy input arrays.""" | ||
result = func(array) | ||
result.compute() |