Skip to main content

Binned GLCM 5 Features implemented in CuPy

Project description

GLCM Bin 5 on CuPy

This is a CuPy reimplementation of my glcmbin5 on my other repository.

This directly utilizes CUDA to speed up the processing of GLCM.

Installation

Python >= 3.7

First, you need to install this

pip install glcm-cupy

Then, you need CuPy. You need to install CuPy manually, as it's dependent on the version of CUDA you have.

I recommend using conda-forge as it worked for me :)

For CUDA 11.6, we use

conda install -c conda-forge cupy cudatoolkit=11.6

Replace the version you have on the arg.

conda install -c conda-forge cupy cudatoolkit=__._

Usage

The usage is simple:

>>> from glcm_cupy import GLCM
>>> import numpy as np
>>> from PIL import Image
>>> ar = np.asarray(Image.open("image.jpg"))
>>> ar.shape
(1080, 1920, 3)
>>> g = GLCM(...).run(ar)
>>> g.shape
(1074, 1914, 3, 8)

The last dimension of g is the GLCM Features.

To retrieve a specific GLCM Feature:

>>> from glcm_cupy import CONTRAST
>>> g[..., CONTRAST].shape
(1074, 1914, 3)

You may also consider simply glcm if you're not reusing GLCM()

>>> from glcm_cupy import glcm
>>> g = glcm(ar, ...)

Example: Processing an Image

Features

These are the features implemented.

  • HOMOGENEITY = 0
  • CONTRAST = 1
  • ASM = 2
  • MEAN_I = 3
  • MEAN_J = 4
  • VAR_I = 5
  • VAR_J = 6
  • CORRELATION = 7

Don't see one you need? Raise an issue, I'll (hopefully) add it.

Radius & Step Size

  • The radius defines the window radius for each GLCM window.
  • The step size defines the distance between each window.
    • If it's diagonal, it treats a diagonal step as 1. It's not the euclidean distance.

Binning

To reduce GLCM processing time, you can specify bin_from & bin_to.

This will bin the image from a range to another.

I highly recommend using this to reduce time taken before raising it.

E.g.

I have an RGB image with a max value of 255.

I limit the max value to 31. This reduces the processing time.

GLCM(..., bin_from=256, bin_to=32).run(ar)

The lower the max value, the smaller the GLCM required. Thus allowing for more GLCMs to run concurrently.

Direction

By default we have the following directions to run GLCM on.

  • East: Direction.EAST
  • South East: Direction.SOUTH_EAST
  • South: Direction.SOUTH
  • South West: Direction.SOUTH_WEST

For each direction, the GLCM will be bi-directional.

We can specify only certain directions here.

>>> from glcm_cupy import GLCM
>>> GLCM()
>>> g = GLCM(directions=(Direction.SOUTH_WEST, Direction.SOUTH))

The result of these directions will be averaged together.

Notes

Q: Why did my image shrink?

The image shrunk due to step_size & radius.

The amount of shrink per XY Dimension is size - 2 * step_size - 2 * radius

Q: What's the difference between this and glcmbin5?

This is the faster one, and easier to use. I highly recommend avoiding glcmbin5 as it has C++, which means you need to compile manually.

It's the first version of GLCM I made.

CUDA Notes

Why is the kernel split into 4?

The kernel is split into 4 sections

  1. GLCM Creation
  2. Features (ASM, Contrast, Homogeneity, GLCM Mean I, GLCM Mean J)
  3. Features (GLCM Variance I, GLCM Variance J)
  4. Features (GLCM Correlation)

The reason why it's split is due to (2) being reliant on (1), and (3) on (2), ... .

There are some other solutions tried

  1. __syncthreads() will not work as we require to sync all blocks.
    1. We can't put all calculations in a block due to the thread limit of 512, 1024, 2048.
    2. We require 256 * 256 threads minimum to support a GLCM of max value 255.
  2. Cooperative Groups imposes a 24 block limit.

Thus, the best solution is to split the kernel.

Atomic Add

Threads cannot write to a single pointer in parallel, information will be overwritten and lost. This is the Race Condition.

In order to avoid this, we use Atomic Functions.

... it is guaranteed to be performed without interference from other threads

Custom Atomic Add

Currently atomicAdd() doesn't have the signature to support uint8 or unsigned char. We get this implementation from this StackOverflow Answer

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

glcm_cupy-0.1.6.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

glcm_cupy-0.1.6-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file glcm_cupy-0.1.6.tar.gz.

File metadata

  • Download URL: glcm_cupy-0.1.6.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.13 Windows/10

File hashes

Hashes for glcm_cupy-0.1.6.tar.gz
Algorithm Hash digest
SHA256 0a2eeb2b2ca6b576ef522ce5a808f0db134c7d28fd5c3db2df5e3e84c5ce612f
MD5 2eeb0c436f930180385b541a9141c612
BLAKE2b-256 4e2b045b4d26f54e07183e8bff8ea700e6b615c3ed7e44e33797bcb3668c2818

See more details on using hashes here.

File details

Details for the file glcm_cupy-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: glcm_cupy-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.13 Windows/10

File hashes

Hashes for glcm_cupy-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ff5208660fa27deba401d80d8dd9ebf3ec61ca332f291a4ea254a400a8bf69d4
MD5 5edd2c12e266926466b18ae99ceef47f
BLAKE2b-256 8b21e0f26b2bb60d0625d8fa4277e2798304eec840037eab253649e9b50d0538

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page