Skip to main content

Morphological operations with CUDA acceleration

Project description

Morphocore

Morph### Installation from Source

git clone git@gitlab.lre.epita.fr:aurelien.hurand/morphocore.git
cd morphocore
pip install . --no-build-isolation

CPU-Only Installation (without CUDA):

MORPHO_WITH_CUDA=0 pip install . --no-build-isolation

This is useful when:

  • You don't have an NVIDIA GPU
  • CUDA Toolkit is not installed
  • You want faster compilation times
  • You're deploying on CPU-only serverss a Python library designed to integrate mathematical morphology into neural networks. It provides differentiable (float and double) and optimized morphological operations, enabling deep learning architectures to leverage powerful image processing tools from mathematical morphology.

Main Features

Installation

Installation from PyPi:

pip install morphocore

❗If you have compatibility issue with the version compile on Pypi please compile the library from source like below !

Installation from source:

git clone git@gitlab.lre.epita.fr:aurelien.hurand/morphocore.git
cd morphocore
pip install . --no-build-isolation

Requirements for Pre-compiled Package (PyPI)

If you install from PyPI using pip install morphocore, you'll need:

  • Python >= 3.10
  • PyTorch >= 2.6
  • CUDA >= 12.3

If your environment doesn't meet these requirements, please compile from source instead.

API Reference

Functional Interface

dilation(input, weight, channel_merge_mode="max")

Performs morphological dilation on the input tensor.

Parameters:

  • input (Tensor): Input tensor of shape (B, C, H, W)
  • weight (Tensor): Structuring element of shape (out_channels, in_channels, kH, kW)
  • channel_merge_mode (str): Channel combination method - "max", "min", "sum", "mean", or "identity"

Returns:

  • output (Tensor): Dilated tensor of shape (B, out_channels, H, W)
from morphocore.functional import dilation

# Basic usage
result = dilation(image, structuring_element, "sum")

# With different merge modes
max_result = dilation(image, kernel, "max")    # Maximum across channels
sum_result = dilation(image, kernel, "sum")    # Sum across channels
mean_result = dilation(image, kernel, "mean")  # Average across channels
identity_result = dilation(image, kernel, "identity")  # No channel merging, keeping all channels linear in_channels = out_channels

erosion(input, weight, channel_merge_mode="max")

Performs morphological erosion on the input tensor.

Parameters: Same as dilation() Returns: Same as dilation()

from morphocore.functional import erosion

result = erosion(image, structuring_element, "sum")

smorph(input, weight, channel_merge_mode="max", alpha=0.0)

Smooth approximation of morphological operations using softmax.

Parameters:

  • Same as dilation() plus:
  • alpha (float): Control parameter for either dilation or erosion behaviour
    • When alpha is large -> smorph behaves like dilation
    • When alpha is very negative -> smorph behaves like erosion
    • When alpha is close to 0 -> then smorph is something between an erosion and a dilation.
from morphocore.functional import smorph

# Soft approximation of morphological operations
result = smorph(image, kernel, "sum", alpha=0.0)

Neural Network Modules

Mnn.Dilation(in_channels, out_channels, kernel_size, channel_merge_mode="max")

Learnable dilation layer for neural networks.

Parameters:

  • in_channels (int): Number of input channels
  • out_channels (int): Number of output channels
  • kernel_size (int or tuple): Size of the morphological kernel
  • channel_merge_mode (str): Channel merge strategy
import morphocore.nn as Mnn

# Create a learnable dilation layer
dilation_layer = Mnn.Dilation(
    in_channels=3, 
    out_channels=16, 
    kernel_size=(3, 3), 
    channel_merge_mode="sum"
)
output = dilation_layer(input_tensor)

Mnn.Erosion(in_channels, out_channels, kernel_size, channel_merge_mode="max")

Learnable erosion layer for neural networks.

erosion_layer = Mnn.Erosion(
    in_channels=3, 
    out_channels=16, 
    kernel_size=(5, 5), 
    channel_merge_mode="mean"
)
output = erosion_layer(input_tensor)

Mnn.SMorph(in_channels, out_channels, kernel_size, channel_merge_mode="max", alpha=1.0)

Smooth morphological layer using softmax approximation.

smorph_layer = Mnn.SMorph(
    in_channels=3, 
    out_channels=8, 
    kernel_size=(3, 3), 
    channel_merge_mode="sum",
    alpha=2.0
)
output = smorph_layer(input_tensor)

Channel Merge Modes Explained

  • "sum": Sums values across input channels
  • "mean": Averages values across input channels

Project Structure

  • morphocore/functional/: Basic morphological functions
  • morphocore/nn/: PyTorch modules for neural networks
  • morphocore/functional/csrc/: C++/CUDA source code for acceleration
  • tests/: Unit, functional and benchmark tests

Dependencies

  • Python >= 3.8
  • PyTorch
  • NumPy

Contributing

Contributions are welcome! Please submit issues and pull requests.

License

This project is licensed under the MIT License.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

morphocore-0.9.0.29-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (19.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

morphocore-0.9.0.29-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (19.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

morphocore-0.9.0.29-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (19.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file morphocore-0.9.0.29-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for morphocore-0.9.0.29-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e372e3f3da36d4b19c24faa8b2eb6e2817e0cc6712d0170d6ae707461e8ed831
MD5 1370b522ed8174af90c45cacfb14154d
BLAKE2b-256 829c55fd5243955608753fdd3a954ea26090e22e0e08d6a8ee9116fda04759a8

See more details on using hashes here.

File details

Details for the file morphocore-0.9.0.29-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for morphocore-0.9.0.29-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a73a31981990e9b8b9bbdd875ee7763c17025dc66bc3134ca261bf19ca554444
MD5 76dcb4a4f263338ada4ea23a08e23021
BLAKE2b-256 d72bb46754fc27eeee4e4be38a93466eb0b48adffb1042377b2a8f7915e74984

See more details on using hashes here.

File details

Details for the file morphocore-0.9.0.29-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for morphocore-0.9.0.29-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4777a5400e5018275badbf8a8b1e87cc14c7e9657c35f20c9c5fa50dd4f34ff3
MD5 445a810bc83491f61fcc89cf2d359ecc
BLAKE2b-256 a233427fccb0568bb5cfb58f6a4749de9c21bc283db7854d5e2f229c62b748ad

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