Morphological operations with CUDA acceleration
Project description
Morphocore
Morphocore is 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
- Primary morphological operations (dilation, erosion)
- Smorph (An alpha softmax based approximation of dilation or erosion : https://www.lre.epita.fr/dload/papers/hermary.22.jmiv.pdf)
- CPU and CUDA implementations (Related work: https://github.com/Manza12/nnMorpho)
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 channelsout_channels(int): Number of output channelskernel_size(int or tuple): Size of the morphological kernelchannel_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 functionsmorphocore/nn/: PyTorch modules for neural networksmorphocore/functional/csrc/: C++/CUDA source code for accelerationtests/: 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file morphocore-0.7.0.29-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: morphocore-0.7.0.29-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 17.0 MB
- Tags: CPython 3.12, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e47193a86bc88b18827366764a7e3cea7440d56ae4a3f81fc004f141d5a473b4
|
|
| MD5 |
92d1cde76e59f18200212d5c3b791059
|
|
| BLAKE2b-256 |
befef1f050810e8a16ab8a7a7cf11dcdd4d35cf863c41ebc2e67bb4333fd6c28
|
File details
Details for the file morphocore-0.7.0.29-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: morphocore-0.7.0.29-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 17.0 MB
- Tags: CPython 3.11, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d6db77d22f56ac64133f44dced19811dadc8eca6b2c32ee49dab5b190b0f54e
|
|
| MD5 |
11958c73013a3783612c5d8701aa53b7
|
|
| BLAKE2b-256 |
bbbd1456a4cc876e7d77e40fb540afaff229794768c1bf046136e0c2fb5399a0
|
File details
Details for the file morphocore-0.7.0.29-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: morphocore-0.7.0.29-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 16.9 MB
- Tags: CPython 3.10, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca14287ab184b9ca4589480063f8cf332de3b1b353196b9ede02f594066c76e3
|
|
| MD5 |
d88130312e4c837f439644914e6daaa8
|
|
| BLAKE2b-256 |
7a0c8ef1456ebdd5bc317d4fda02f1c3c2097667d200e810de829d9d57883b99
|