Skip to content

Add cupyx.scipy.ndimage.label - #3210

Merged
mergify[bot] merged 6 commits into
cupy:masterfrom
anaruse:scipy_ndimage_label
Mar 25, 2020
Merged

mergify[bot] merged 6 commits into
cupy:masterfrom
anaruse:scipy_ndimage_label

Conversation

@anaruse

@anaruse anaruse commented Mar 19, 2020

Copy link
Copy Markdown
Contributor

close #3119

@anaruse

anaruse commented Mar 19, 2020

Copy link
Copy Markdown
Contributor Author

Measured results on Quadro GV100 and CUDA 10.2
(dtype: int32, structure: None, output: None)

mean (stdev) [unit: ms] scipy.ndimage cupyx.scipy.ndimage speedup
shape:(4000, 4000), density:0.1 78.196 (1.510) 3.684 (0.019) 21.2
shape:(4000, 4000), density:0.2 96.615 (0.263) 4.031 (0.026) 24.0
shape:(4000, 4000), density:0.3 119.272 (0.320) 4.169 (0.009) 28.6
shape:(4000, 4000), density:0.4 146.165 (0.219) 4.254 (0.023) 34.4
shape:(4000, 4000), density:0.5 169.757 (0.558) 4.230 (0.012) 40.1
shape:(8000, 8000), density:0.1 326.453 (0.962) 12.326 (0.045) 26.5
shape:(8000, 8000), density:0.2 411.575 (0.891) 13.562 (0.044) 30.3
shape:(8000, 8000), density:0.3 504.746 (0.509) 14.157 (0.027) 35.7
shape:(8000, 8000), density:0.4 612.297 (0.970) 14.444 (0.039) 42.4
shape:(8000, 8000), density:0.5 707.270 (0.836) 14.414 (0.046) 49.1
shape:(200, 200, 200), density:0.1 59.186 (0.499) 2.493 (0.012) 23.7
shape:(200, 200, 200), density:0.2 79.735 (0.056) 2.656 (0.008) 30.0
shape:(200, 200, 200), density:0.3 106.947 (0.236) 2.721 (0.006) 39.3
shape:(200, 200, 200), density:0.4 133.922 (0.211) 2.680 (0.008) 50.0
shape:(200, 200, 200), density:0.5 149.658 (0.409) 2.630 (0.007) 56.9
shape:(300, 300, 300), density:0.1 203.323 (0.289) 6.415 (0.016) 31.7
shape:(300, 300, 300), density:0.2 280.825 (1.175) 7.002 (0.019) 40.1
shape:(300, 300, 300), density:0.3 372.073 (0.789) 7.164 (0.024) 51.9
shape:(300, 300, 300), density:0.4 455.918 (0.789) 6.657 (0.021) 68.5
shape:(300, 300, 300), density:0.5 509.090 (0.733) 6.402 (0.014) 79.5

@anaruse anaruse changed the title [WIP] Add cupyx.scipy.ndimage.label Add cupyx.scipy.ndimage.label Mar 19, 2020
@anaruse

anaruse commented Mar 19, 2020

Copy link
Copy Markdown
Contributor Author

test script

import numpy
import scipy.ndimage as ndimage
import cupy
import cupyx.scipy.ndimage as cpx_ndimage
import time
from statistics import mean, stdev

nmeasure = 10
dtype = numpy.int32
structure = None
size_set = [
    [2, 4000],
    [2, 8000],
    [3, 200],
    [3, 300],
]
density_set = [0.1, 0.2, 0.3, 0.4, 0.5]

for j in range(len(size_set)):
    ndim = int(size_set[j][0])
    size = int(size_set[j][1])
    shape = (size,) * ndim

    for density in density_set:
        cpu_time = []
        gpu_time = []
        for i in range(nmeasure+1):
            x = numpy.zeros(shape, dtype=dtype)
            x[numpy.random.random(shape) < density] = 1
            cp_x = cupy.array(x)

            t1 = time.time()
            labels, num_features = ndimage.label(x, structure=structure)
            elapsed_ms = (time.time() - t1) * 1000
            if i > 0:
                cpu_time = cpu_time + [elapsed_ms]

            t1 = time.time()
            labels, num_features = cpx_ndimage.label(cp_x, structure=structure)
            cupy.cuda.runtime.deviceSynchronize()
            elapsed_ms = (time.time() - t1) * 1000
            if i > 0:
                gpu_time = gpu_time + [elapsed_ms]
                                                                                                              
        print('shape:{}, density:{} | {:.3f} ({:.3f}) | {:.3f} ({:.3f}) | {:.1f}'.format(shape, density, mean(cpu_time), stdev(cpu_time), mean(gpu_time), stdev(gpu_time), mean(cpu_time) / mean(gpu_time)) )

@grlee77

grlee77 commented Mar 19, 2020

Copy link
Copy Markdown
Member

Thanks for adding this.

Not surprisingly, I got a bit lower acceleration factors of between 12 and 33 for your test cases on a 1080 GTX Ti GPU.

An edge case in the SciPy tests that currently fails here is for 0-dimensional arrays. SciPy has the following behavior:

in[1]: scipy.ndimage.label(np.array(5.))
Out[1]: (array(1, dtype=int32), 1)

in[2]: scipy.ndimage.label(np.zeros([]))
Out[2]: (array(0, dtype=int32), 0)

@grlee77

grlee77 commented Mar 19, 2020

Copy link
Copy Markdown
Member

possibly add to the docstring

    .. warning::

        This function may synchronize the device.

(_label transfers a scalar back to the CPU).

I think handling 0-dim arrays as in SciPy would also require synchronization in that code path as it is necessary to check the value in the array to determine what to return.

@anaruse

anaruse commented Mar 20, 2020

Copy link
Copy Markdown
Contributor Author

Thanks for your feedback, @grlee77 !

I added code and unit tests for 0-dim array cases and the warning about synchronization.

@grlee77 grlee77 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to me. I just left a couple of minor comments.

Comment thread cupyx/scipy/ndimage/measurements.py Outdated
input (cupy.ndarray): The input array.
structure (array_like or None): A structuring element that defines
feature connections. ```structure``` must be centersymmetric. If
None, structure is automatically generated with a squad

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
None, structure is automatically generated with a squad
None, structure is automatically generated with a squared

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks!


Args:
input (cupy.ndarray): The input array.
structure (array_like or None): A structuring element that defines

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is a little odd that this states "array-like" but a cupy.ndarray is not an acceptable array-like.

I see that there is NumPy-based manipulation of the structure in label which seems fine as such small array operations should be faster on the NumPy side. However, should a cupy.ndarray be allowed as input and it can be transfered to the host internally as needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I will make a cupy.ndarray available for structure.

@takagi

takagi commented Mar 23, 2020

Copy link
Copy Markdown
Contributor

pfnCI, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit 72df6d5:

@takagi takagi added the cat:feature New features/APIs label Mar 23, 2020
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 72df6d5, target branch master) succeeded!

@emcastillo emcastillo added this to the v8.0.0b2 milestone Mar 23, 2020

@takagi takagi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would you check some comments?

Comment thread cupyx/scipy/ndimage/measurements.py Outdated


def label(input, structure=None, output=None):
"""Label features in an array

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just to follow CuPy's convention in docstrings.

Suggested change
"""Label features in an array
"""Labels features in an array.

Comment thread cupyx/scipy/ndimage/measurements.py Outdated

.. seealso:: :func:`scipy.ndimage.label`
"""
assert isinstance(input, cupy.ndarray)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would you raise TypeError instead of AssertionError for an user input?

Comment thread cupyx/scipy/ndimage/measurements.py Outdated
maxlabel = 0
elif input.ndim == 0:
# 0-dim array
maxlabel = 0 if (input.item() == 0) else 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would you remove the parenthesis here?

Suggested change
maxlabel = 0 if (input.item() == 0) else 1
maxlabel = 0 if input.item() == 0 else 1

Comment thread cupyx/scipy/ndimage/measurements.py Outdated
maxlabel = 0 if (input.item() == 0) else 1
output[...] = maxlabel
else:
if output.dtype is not numpy.int32:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suppose != operator should be used here instead of is not operator as ndarray.dtype returns dtype object.

Suggested change
if output.dtype is not numpy.int32:
if output.dtype != numpy.int32:

Comment thread cupyx/scipy/ndimage/measurements.py Outdated
else:
y = output
maxlabel = _label(input, structure, y)
if output.dtype is not numpy.int32:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ditto.

Suggested change
if output.dtype is not numpy.int32:
if output.dtype != numpy.int32:

@takagi takagi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would you check some comments?

@anaruse

anaruse commented Mar 24, 2020

Copy link
Copy Markdown
Contributor Author

Thanks for your review, @takagi . I added fixes based on your comment. Could you check it again?

@takagi

takagi commented Mar 25, 2020

Copy link
Copy Markdown
Contributor

Thanks! pfnCI, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit dfba4f9:

@takagi takagi added the st:test-and-merge (deprecated) Ready to merge after test pass. label Mar 25, 2020
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit dfba4f9, target branch master) succeeded!

@mergify
mergify Bot merged commit 2eb6db9 into cupy:master Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:feature New features/APIs st:test-and-merge (deprecated) Ready to merge after test pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support scipy.ndimage.label

6 participants