Skip to main content

Mathematical Morphology module for PyTorch (CUDA), providing differentiable operators and learnable network layers.

Project description

Serron

Mathematical Morphology module for PyTorch (CUDA), providing differentiable operators and learnable network layers.

Install

Prebuilt wheels are published for CPython 3.14 on Linux (x86_64, aarch64) and Windows (x86_64). A working CUDA-enabled PyTorch (torch >= 2.12) must already be installed in the environment.

pip install serron

Usage

Alpha: the CUDA kernels are not wired up yet, so the operators below currently raise NotImplementedError.

Serron works on standard PyTorch tensors laid out as (N, C, H, W) and living on a CUDA device.

Functional operators

Stateless operators live in serron.functional and are re-exported at the top level. Each takes an input tensor, a structuring element, and an optional border mode:

import torch
import serron
from serron import BorderMode
from serron import structuring_element as se

x = torch.rand(1, 1, 256, 256, device="cuda")
kernel = se.disk(3, device="cuda")

eroded  = serron.erosion(x, kernel)
dilated = serron.dilatation(x, kernel)
opened  = serron.opening(x, kernel)
closed  = serron.closing(x, kernel)

# Derived operators
grad    = serron.gradient(x, kernel)   # dilate(x) - erode(x)
white   = serron.top_hat(x, kernel)    # x - open(x)
black   = serron.black_hat(x, kernel)  # close(x) - x

# Control border handling
eroded_reflect = serron.erosion(x, kernel, border=BorderMode.REFLECT)

Available operators: erosion, dilatation, opening, closing, gradient, top_hat, black_hat.

Structuring elements

serron.structuring_element builds common SE shapes on the requested device:

from serron import structuring_element as se

se.square(5, device="cuda")      # (5, 5) full square
se.cross(5, device="cuda")       # (5, 5) plus shape
se.disk(3, device="cuda")        # (7, 7) disk, radius 3
se.diamond(3, device="cuda")     # (7, 7) diamond, radius 3
se.from_tensor(my_weights)       # wrap an arbitrary 2-D tensor as a grayscale SE

Border modes

BorderMode controls how out-of-bounds neighbors are handled:

Mode Behavior
BorderMode.REPLICATE Repeat the edge value (default)
BorderMode.REFLECT Mirror across the edge
BorderMode.CONSTANT Pad with a constant

Learnable layers

serron also exposes torch.nn.Module layers with a learnable structuring element, so morphology can be trained end-to-end inside a network. Each layer takes the number of channels and a kernel_size:

import torch
from serron import Erosion2d, Dilation2d, Opening2d, Closing2d
from serron import BorderMode

layer = Erosion2d(channels=3, kernel_size=5, border=BorderMode.REPLICATE).cuda()

x = torch.rand(8, 3, 64, 64, device="cuda")
y = layer(x)          # forward pass; layer.weight is a trainable (C, k, k) SE
y.sum().backward()    # gradients flow into layer.weight

Available layers: Erosion2d, Dilation2d, Opening2d, Closing2d.

Building from source

Requires the CUDA 13.X toolkit (nvcc) and a matching torch build:

uv sync --package serron --no-dev --group build
uv build --package serron --wheel --no-build-isolation

License

MIT

Copyright © 2026 Vedran Hrabar.

Download files

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

Source Distribution

serron-0.0.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distributions

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

serron-0.0.1-cp314-cp314-win_amd64.whl (63.3 kB view details)

Uploaded CPython 3.14Windows x86-64

serron-0.0.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (46.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

serron-0.0.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (43.8 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

serron-0.0.1-cp313-cp313-win_amd64.whl (61.8 kB view details)

Uploaded CPython 3.13Windows x86-64

serron-0.0.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (46.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

serron-0.0.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (43.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

serron-0.0.1-cp312-cp312-win_amd64.whl (61.8 kB view details)

Uploaded CPython 3.12Windows x86-64

serron-0.0.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (46.0 MB view details)

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

serron-0.0.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (43.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

File details

Details for the file serron-0.0.1.tar.gz.

File metadata

  • Download URL: serron-0.0.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for serron-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a6defeb35007621b5931a8a6870823178f1fc5f0527a5980e8d5a622dee364cb
MD5 c6a704ec0712cb66a9c0375a2ca9702d
BLAKE2b-256 6c38444f4cc68c4baf1bad4de812d048a4c8dfc89a02e7cf129b07192ceb5b8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1.tar.gz:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: serron-0.0.1-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 63.3 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for serron-0.0.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 020e8897b1840395e41e661cdafe35e8bb59fa48d71a7beaf07806329e1843d7
MD5 e663ee9f7da8d6c7d4395669a388a885
BLAKE2b-256 750d32b213cc35f064e4b2bc795157cb52f255fb37db9942fcbc66c7dd8157b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp314-cp314-win_amd64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for serron-0.0.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ba3b4543ade5297bea3a9ac6febbcfb098b98b080a10d763ed056589b8cf5d8c
MD5 fee089b87ba6ea2f84c9e5f61e467e30
BLAKE2b-256 f95a0f8441f20f9621da21fd56c311b2d0f6529c9df84bc7d50f37fd25db98de

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for serron-0.0.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a8cb161b9fe06a5782f3db42a75b2003b3cfc7bad70633a53517381c3835e6f1
MD5 5accdd16b3781e46336b92a7bf6843b8
BLAKE2b-256 19f0279bb623e1fcb71070c1655a015da492a7c094b5c735226ca1d38eb5a75c

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: serron-0.0.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 61.8 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for serron-0.0.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 01c2be7a4f22e67553853c79b920f8a73d542d4f497a1d7c3aa071eb842de0f1
MD5 e001091283c395392e4782b2b81dad46
BLAKE2b-256 09e6d79798576c66b049bdb9dfacc23bf34bb84b13baa85e29a2dbb7ae2437d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp313-cp313-win_amd64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for serron-0.0.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 225c130d4e4945c73d7f47e5fa4d50bb10bde0373e79921e5992b86eff25ab70
MD5 f5a0218c89d402875c45b962c016024f
BLAKE2b-256 a54ef59ae9a2c536edef92426a29fbc60c7aa927db736432650012aca3687419

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for serron-0.0.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a05023118b8f5c933ceb230a67dbeb55947dfabee3b19ae5dc59b41a714c3375
MD5 e60c64dbd1f1c703eff3d5168ae03c2c
BLAKE2b-256 45b704ae997802c949e5ec9818075e7d627314ea20f78bd734868329aa9d7d5d

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: serron-0.0.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 61.8 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for serron-0.0.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 3d55801a5c1d10e8004076ee4c3f551db2b74355aa32333a28e0584f83067c8b
MD5 d50a00abb808b21be06068c9e614b6da
BLAKE2b-256 e871694832cd8e971af313231e4ad351bd1b48d84132297a4e56ca8bfea2bef4

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp312-cp312-win_amd64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for serron-0.0.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1633059b7db32cfa553d38e80bd58561e64f6d5f20585a618ceb62b3e717e212
MD5 90e79c46072039791c1d9d9e382e1486
BLAKE2b-256 406a8cb1e6766dc54ef05f58ad624fcc8db24a1c9c040f50e6bf3caa7632f941

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file serron-0.0.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for serron-0.0.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e6db3414d2262f6f579d03a2fcda7cf0791722a64f929bc3ebf6b76aa89936da
MD5 244e3388dbc690232283a67785b3a108
BLAKE2b-256 fa2720a194a3d3bdd9d661e830575cfbd4f43bd797cff1e5dcfbe1731f6dc910

See more details on using hashes here.

Provenance

The following attestation bundles were made for serron-0.0.1-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: publish-pypi.yml on vhrabar/serron

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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