Skip to main content

A collection of losses and metrics for topology-preserving image segmentation.

Project description

topolosses

Topolosses is a Python package providing topology-aware losses for segmentation tasks. It includes losses that improve topological properties in segmentation models, such as DiceLoss, TopographLoss, BettiMatchingLoss, HutopoLoss, MosinLoss and WarpingLoss.

Installation

Install the package from Test PyPI.

pip install -i https://test.pypi.org/simple/ topolosses

Usage

Import the desired loss functions and implement the loss functions like any standard PyTorch loss:

from topolosses.losses import CLDiceLoss, DiceLoss, BettiMatchingLoss

# Create a c CLDice loss (which itself combines with Dice)
clDiceLoss = CLDiceLoss(
    softmax=True,
    include_background=True,
    smooth=1e-5,
    alpha=0.5,
    iter_=5,
    batch=True,
    base_loss=DiceLoss(
        softmax=True,
        smooth=1e-5,
        batch=True,
    ),
)

# Combine topological (BettiMatchingLoss) with base component (CLDiceLoss)
loss = BettiMatchingLoss(
    **input_param,
    alpha=0.5,  # Weight for the topological component
    softmax=True,
    base_loss=clDiceLoss
)

result = loss.forward(prediction, target)

Common Arguments for Loss Functions

  • include_background (bool):
    Includes the background in the topology-aware component computation. Default: False.

  • alpha (float):
    Weight for combining the topology-aware component and the base loss component. Default: 0.5.

  • sigmoid (bool):
    Applies sigmoid activation to the forward pass input before computing the topology-aware component. If using the default Dice loss, the sigmoid-transformed input is also used. For custom base losses, the raw input is passed. Default: False.

  • softmax (bool):
    Applies softmax activation to the forward pass input before computing the topology-aware component. If using the default Dice loss, the softmax-transformed input is also used. For custom base losses, the raw input is passed. Default: False.

  • use_base_component (bool):
    If False, only the topology-aware component is computed. Default: True.

  • base_loss (_Loss, optional):
    The base loss function used with the topology-aware component. Default: None.

Note: Each loss function also has specific arguments. These are documented within the code using docstrings, and can be easily accessed using Python's help() function or by exploring the source code.

Folder Structure

topolosses
├─ .DS_Store
├─ CMakeLists.txt
├─ LICENSE
├─ README.md
├─ pyproject.toml
└─ topolosses
   ├─ README.md
   ├─ __init__.py
   └─ losses
      ├─ __init__.py
      ├─ betti_matching
      │  ├─ __init__.py
      │  └─ src
      │     ├─ betti_matching_loss.py
      │     └─ ext
      │        └─ Betti-Matching-3D
      │           ├─ CMakeLists.txt
      │           ├─ LICENSE
      │           ├─ README.md
      │           ├─ src
      │           │  ├─ BettiMatching.cpp
      │           │  ├─ BettiMatching.h
      │           │  ├─ _BettiMatching.cpp
      │           │  ├─ config.h
      │           │  ├─ data_structures.cpp
      │           │  ├─ data_structures.h
      │           │  ├─ main.cpp
      │           │  ├─ npy.hpp
      │           │  ├─ src_1D
      │           │  │  ├─ 
      │           │  ├─ src_2D
      │           │  │  ├─ 
      │           │  ├─ src_3D
      │           │  │  ├─ 
      │           │  ├─ src_nD
      │           │  │  ├─ 
      │           │  ├─ utils.cpp
      │           │  └─ utils.h
      │           └─ utils
      │              ├─ functions.py
      │              └─ plots.py
      ├─ cldice
      │  ├─ __init__.py
      │  └─ src
      │     └─ cldice_loss.py
      ├─ dice
      │  ├─ __init__.py
      │  └─ src
      │     └─ dice_loss.py
      ├─ topograph
      │  ├─ __init__.py
      │  └─ src
      │     ├─ ext
      │     │  ├─ _topograph.cpp
      │     │  ├─ setup.py
      │     │  ├─ topograph.cpp
      │     │  └─ topograph.hpp
      │     └─ topograph_loss.py
      └─ utils.py

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

topolosses-0.1.70.tar.gz (2.0 MB view details)

Uploaded Source

Built Distributions

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

topolosses-0.1.70-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl (85.8 MB view details)

Uploaded PyPymanylinux: glibc 2.34+ x86-64

topolosses-0.1.70-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl (85.8 MB view details)

Uploaded PyPymanylinux: glibc 2.34+ x86-64

topolosses-0.1.70-cp313-cp313-manylinux_2_34_x86_64.whl (85.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

topolosses-0.1.70-cp312-cp312-manylinux_2_34_x86_64.whl (85.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

topolosses-0.1.70-cp311-cp311-manylinux_2_34_x86_64.whl (85.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

topolosses-0.1.70-cp310-cp310-manylinux_2_34_x86_64.whl (85.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

File details

Details for the file topolosses-0.1.70.tar.gz.

File metadata

  • Download URL: topolosses-0.1.70.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for topolosses-0.1.70.tar.gz
Algorithm Hash digest
SHA256 6a6a4469b06c68556e2c438626cf95089ee132fff4f5520997388ad91c99797f
MD5 a9331b7d30ce181799256670daf512ca
BLAKE2b-256 1d7ad4b72b82901167ae3a6ce48f9ad4a700a2d75266d444c001ad0c95c8522b

See more details on using hashes here.

File details

Details for the file topolosses-0.1.70-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for topolosses-0.1.70-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 b09e97bf4ba984a8328aecb550dc1323360839face9958e9f6b480cf502df445
MD5 76541f12733d866eb3a63a019469b67d
BLAKE2b-256 e3999c1efab6ba10711710da5a3cba8628205da19f796ec44da85e316543f32a

See more details on using hashes here.

File details

Details for the file topolosses-0.1.70-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for topolosses-0.1.70-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 edef254aa08da433b3e9df0fc2c8e27eb23193c3c233a0c296fe81d7a49250f2
MD5 9b084aec24410792cb06609aadbe2e59
BLAKE2b-256 f21d7854af35494533c909e08298433dfc63fbce4a782eccf19690135f897078

See more details on using hashes here.

File details

Details for the file topolosses-0.1.70-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for topolosses-0.1.70-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c495972697ef7bc1a161e37df623a2720c95556bdc86849c57c8a2ba8d57b441
MD5 1ced062f993107ab7a8e2bccc9e88077
BLAKE2b-256 5e099e4938cf034a83b451fe0a8417c694c8a3c3b56af29c9f992f149dae0439

See more details on using hashes here.

File details

Details for the file topolosses-0.1.70-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for topolosses-0.1.70-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 bd54c8707c804b466094a0df6f130fff623e2018c92fffba22f1e5d3440cdd0d
MD5 edfd958bc4febc38e54eca016bc8b7a1
BLAKE2b-256 30c43dc528238930a2eb8321155f50a97549cc64c59c0ce5f3d695876f69e526

See more details on using hashes here.

File details

Details for the file topolosses-0.1.70-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for topolosses-0.1.70-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 7740882ab6f384ba050c3dad993df96af3256a0eeba2ec992a8f11f577aee6f1
MD5 473b0ad5c39f1fb6f948c18615042f63
BLAKE2b-256 59b0bbb6284eeb28fc9dec1142f2971be54e8c144e70cb28460d001df22b7c40

See more details on using hashes here.

File details

Details for the file topolosses-0.1.70-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for topolosses-0.1.70-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 098d89ca0181307aabe6f3e7706e586318afc5ddf64c2795d0c62a4b9399195e
MD5 a828d67d72e83454caf439e7adca7861
BLAKE2b-256 1ced4a839de2bae0e99ceca8f25b30ab5eb75b3c44539da3b93f14d75b0ea1a0

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