Package: gpu-slic
cuda-slic: A CUDA implementation of the SLIC Superpixel algorithmSLICSLIC stands forsimple linear iterative clustering. SLIC uses
tiled k-means clustering to segment an input image into a set
of superpixels/supervoxels.This library was designed to segment large 2D/3D images coming from different
detectors at theDiamond Light Source. These images
can be very large so using a serial CPU code is out of the question.To speed up processing we use GPU acceleration to achieve great speed
improvements over alternative implementations.cuda-slicborrows its API
fromskimage.segmentation.slic.BenchmarkMachine: 8 Core Intel Xeon(R) W-2123 CPU @ 3.60GHz with NVIDIA Quadro P2000fromskimageimportdatafromcuda_slic.slicimportslicascuda_slicfromskimage.segmentationimportslicasskimage_slicblob=data.binary_blobs(length=500,n_dim=3,seed=2)n_segments=500**3/5**3# super pixel shape = (5,5,5)%timeit-r1-n1skimage_slic(blob,n_segments=n_segments,multichannel=False,max_iter=5)# 2min 28s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)%timeit-r1-n1cuda_slic(blob,n_segments=n_segments,multichannel=False,max_iter=5)# 13.1 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)CUDA JIT Compilationcuda-slicuses JIT compilation to covert CUDA kernels into GPU machine-code (PTX).
Two options are available for JIT compiliing CUDA code with python: Cupy or PyCUDA.
If PyCUDA is installed in the virtutalenv it is used by default. Otherwise Cupy is
used.To ease distributioncuda-slicis packaged into two independent packages that share
the same source-code but depend on a different libraries for JIT compilation:cuda-slicuses pycuda for JIT compilation.gpu-slicuses cupy for JIT compilation.Installing cuda-slic (with PyCUDA)pipinstallcuda-sliccuda-slicuses pycuda which has the following non-python
build dependencies:gcc and g++/gcc-c++ on Linux. MSVC++ compiler and C++ build-tools on Windows.the cudatoolkit for linking withcuda.h.and the following runtime dependencies:gcc and g++/gcc-c++ on Linux. MSVC++ compiler and C++ build-tools on Windows.the cudatoolkit for linking with cuda libraries.the nvcc compiler. Ships with newer cudatoolkit versions.See thepycuda docsfor
installation instructions.Installing gpu-slic (with Cupy)pipinstallgpu-slicgpu-slicuses Cupy which has the following non-python
build dependencies:gcc and g++/gcc-c++ on Linux.the cudatoolkit for linking with cuda libraries.the nvcc compiler. Ships with newer cudatoolkit versions.Note that when pip installing gpu-slic, cupy is installed as ansdistmeaning that your host must meet the compiling and linking requirements
of cupy.Check if gpu-slic is available on conda-forge to get
precompiled binaries of Cupy.See alsocupy docsfor
installation instructions.Usagefromcuda_slicimportslicfromskimageimportdata# 2D RGB imageimg=data.astronaut()labels=slic(img,n_segments=100,compactness=10)# 3D gray scalevol=data.binary_blobs(length=50,n_dim=3,seed=2)labels=slic(vol,n_segments=100,multichannel=False,compactness=0.1)# 3D multi-channel# volume with dimentions (z, y, x, c)# or video with dimentions (t, y, x, c)vol=data.binary_blobs(length=33,n_dim=4,seed=2)labels=slic(vol,n_segments=100,multichannel=True,compactness=1)DevelopmentPrerequisites:gcc and g++/gcc-c++ installed and available on PATH.cudatoolkit installed and CUDA_HOME pointing to its location.nvcccompiler. Ships with recent versions of cudatoolkit.Dependency ManagementWe usecondaas a dependency installer and virtual env manager.
A development environment can be created withcondaenvcreate-fenvironment.ymlnow you can activate the virtual env withconda activate cuda-slic,
and deactivate withconda deactivate.
To add a dependency, add it to theenvironment.ymlfile, then you can runcondaenvupdate-fenvironment.ymlto keepenvironment.ymlfile as lean as possible, development dependencies
are kept inrequirements_dev.txtand can be installed withcondainstall--filerequirements_dev.txt-cconda-forgeorpipinstall-rrequirements_dev.txtTestsIn thenotebooksfolder there are Jupyter notebooks
where the segmentation algorithm can be visually inspected.Our unit-testing framework of choice isPy.test.
The unit-tests can be run withcondaactivatecuda-slic
pipinstallpytest
pytestorcondaactivatecuda-slic
pipinstalltox
tox