Skip to content

Add cupyx.scipy.ndimage.grey_erosion and cupyx.scipy.ndimage.grey_dilation - #3216

Merged
mergify[bot] merged 4 commits into
cupy:masterfrom
anaruse:scipy_ndimage_grey_erosion_and_dilation
Mar 28, 2020
Merged

mergify[bot] merged 4 commits into
cupy:masterfrom
anaruse:scipy_ndimage_grey_erosion_and_dilation

Conversation

@anaruse

@anaruse anaruse commented Mar 23, 2020

Copy link
Copy Markdown
Contributor

This PR is to support scipy.ndimage.grey_erosion and scipy.ndimage.grey_dilation in CuPy.

@anaruse

anaruse commented Mar 23, 2020

Copy link
Copy Markdown
Contributor Author

Measured results on Quadro GV100 and CUDA 10.2
( grey_erosion, dtype:int32, footprint:None )

mean (stdev) [unit: ms] SciPy CuPy speedup
shape:(1000, 1000), size:3, structure:None 8.141 (0.096) 0.331 (0.007) 24.6
shape:(1000, 1000), size:5, structure:None 8.155 (0.080) 0.367 (0.005) 22.2
shape:(1000, 1000), size:7, structure:None 8.256 (0.113) 0.441 (0.016) 18.7
shape:(2000, 2000), size:3, structure:None 50.188 (17.763) 0.516 (0.002) 97.3
shape:(2000, 2000), size:5, structure:None 47.706 (17.059) 0.743 (0.010) 64.2
shape:(2000, 2000), size:7, structure:None 43.215 (12.248) 1.036 (0.002) 41.7
shape:(4000, 4000), size:3, structure:None 283.721 (2.885) 1.203 (0.003) 235.9
shape:(4000, 4000), size:5, structure:None 284.863 (2.372) 2.085 (0.005) 136.6
shape:(4000, 4000), size:7, structure:None 285.476 (2.791) 3.274 (0.002) 87.2
shape:(100, 100, 100), size:3, structure:None 10.205 (0.051) 0.352 (0.004) 29.0
shape:(200, 200, 200), size:3, structure:None 101.400 (0.636) 1.243 (0.003) 81.6
shape:(300, 300, 300), size:3, structure:None 341.785 (0.478) 3.484 (0.005) 98.1
shape:(1000, 1000), size:3, structure:random 37.608 (0.049) 0.329 (0.012) 114.3
shape:(1000, 1000), size:5, structure:random 111.643 (0.379) 0.397 (0.023) 281.1
shape:(1000, 1000), size:7, structure:random 222.572 (0.804) 0.481 (0.025) 462.6
shape:(2000, 2000), size:3, structure:random 151.798 (1.905) 0.565 (0.005) 268.5
shape:(2000, 2000), size:5, structure:random 447.465 (2.728) 0.784 (0.002) 570.8
shape:(2000, 2000), size:7, structure:random 891.057 (2.194) 1.092 (0.003) 816.3
shape:(4000, 4000), size:3, structure:random 619.138 (0.790) 1.262 (0.005) 490.8
shape:(4000, 4000), size:5, structure:random 1801.868 (1.595) 2.125 (0.005) 847.8
shape:(4000, 4000), size:7, structure:random 3577.451 (4.309) 3.350 (0.005) 1067.9
shape:(100, 100, 100), size:3, structure:random 120.812 (0.331) 0.403 (0.014) 299.5
shape:(200, 200, 200), size:3, structure:random 966.162 (1.004) 1.305 (0.004) 740.3
shape:(300, 300, 300), size:3, structure:random 3290.474 (1.351) 3.548 (0.003) 927.4

@anaruse

anaruse commented Mar 23, 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, 1000, 3],
    [2, 1000, 5],
    [2, 1000, 7],
    [2, 2000, 3],
    [2, 2000, 5],
    [2, 2000, 7],
    [2, 4000, 3],
    [2, 4000, 5],
    [2, 4000, 7],
    [3, 100, 3],
    [3, 200, 3],
    [3, 300, 3],
]

for st in (None, 'random'):

    for j in range(len(size_set)):
        ndim = int(size_set[j][0])
        x_size = int(size_set[j][1])
        shape = (x_size,) * ndim
        size = int(size_set[j][2])
        if st is None:
            structure = None
        else:
            structure = numpy.random.random((size,) * ndim) * 10
            structure = structure.astype(numpy.int32)
        
        cpu_time = []
        gpu_time = []
        for i in range(nmeasure+1):
            x = numpy.random.random(shape).astype(dtype)
            cp_x = cupy.array(x)
        
            t1 = time.time()
            y = ndimage.grey_erosion(x, size=size, structure=structure)
            elapsed_ms = (time.time() - t1) * 1000
            if i > 0:
                cpu_time = cpu_time + [elapsed_ms]
        
            t1 = time.time()
            cp_y = cpx_ndimage.grey_erosion(cp_x, size=size, structure=structure)
            cupy.cuda.runtime.deviceSynchronize()
            elapsed_ms = (time.time() - t1) * 1000
            if i > 0:
                gpu_time = gpu_time + [elapsed_ms]

            cupy.testing.assert_array_almost_equal(cp_y, y)
            
        print('shape:{}, size:{}, structure:{} | {:.3f} ({:.3f}) | {:.3f} ({:.3f}) | {:.1f}'.format(shape, size, st, mean(cpu_time), stdev(cpu_time), mean(gpu_time), stdev(gpu_time), mean(cpu_time) / mean(gpu_time)) )

@anaruse anaruse changed the title [WIP] Add cupyx.scipy.ndimage.grey_erosion and cupyx.scipy.ndimage.grey_dilation Add cupyx.scipy.ndimage.grey_erosion and cupyx.scipy.ndimage.grey_dilation Mar 23, 2020
@takagi takagi added the cat:feature New features/APIs label Mar 24, 2020
@takagi

takagi commented Mar 24, 2020

Copy link
Copy Markdown
Contributor

pfnCI, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit 6d6aed0:

@chainer-ci

Copy link
Copy Markdown
Member

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

@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 minor comments? After they will be fixed, I'll kick the CI again and will merge this PR.

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

def grey_erosion(input, size=None, footprint=None, structure=None, output=None,
mode='reflect', cval=0.0, origin=0):
"""Calculate a greyscale erosion

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.

Suggested change
"""Calculate a greyscale erosion
"""Calculates a greyscale erosion.

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

def grey_dilation(input, size=None, footprint=None, structure=None,
output=None, mode='reflect', cval=0.0, origin=0):
"""Calculate a greyscale dilation

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.

Suggested change
"""Calculate a greyscale dilation
"""Calculates a greyscale dilation.

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

def _normalize_sequence(x, ndim):
if not hasattr(x, '__getitem__') or isinstance(x, str):
return [x, ] * ndim

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.

Suggested change
return [x, ] * ndim
return [x] * ndim

@anaruse

anaruse commented Mar 26, 2020

Copy link
Copy Markdown
Contributor Author

Thanks for your review! I fixed the PR based on your feedback. Could you run CI tests?
Thanks!

@takagi

takagi commented Mar 28, 2020

Copy link
Copy Markdown
Contributor

pfnCI, test this please.

@pfn-ci-bot

Copy link
Copy Markdown
Collaborator

Successfully created a job for commit b0171d9:

@takagi takagi added this to the v8.0.0b2 milestone Mar 28, 2020
@takagi takagi added the st:test-and-merge (deprecated) Ready to merge after test pass. label Mar 28, 2020
@chainer-ci

Copy link
Copy Markdown
Member

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

@mergify
mergify Bot merged commit c49a9e3 into cupy:master Mar 28, 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.

4 participants