Skip to main content

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.

The documentation is available here: https://j-falkenstein.github.io/topolosses/

Installation

You can install topolosses directly from PyPI:

pip install topolosses

Pre-built wheels are available for Linux platforms. If no compatible wheel exists, pip will compile from source locally. See working-with-source-code section for more details.

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(
    alpha=0.5,  # Weight for the topological component
    softmax=True,
    base_loss=clDiceLoss
)

result = loss.forward(prediction, target)

Common Arguments for Loss Functions

Since most topology-aware loss functions combine the sparse topological component with a dense region loss like Dice to ensure both shape accuracy and topological correctness, this project follows the same approach. By default, it uses Dice as the base loss, but you can easily replace it with any custom loss you prefer—or even use just the topology component if that’s all you need.

  • 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. The API reference is available here: https://j-falkenstein.github.io/topolosses/

Working with Source Code

If no binary for your plattform is available or if you want to modify the code (e.g., adjust a loss function), you’ll need to build the C++ extensions locally.

If no compatible wheel exists, pip will compile from source locally. To compile the C++ extension you require a C++ compiler, Python development headers, OpenCV, Boost, and Eigen libraries. (TODO: specify which versions are needed and where to locate the libraries). However, because this approach is very error prone it is better to clone source-code from GitHub <https://github.com/J-falkenstein/topolosses>_. You can tweak pyproject.toml and CMakeLists.txt to point at your local library paths.

  • Option 1: After cloning the repo you can tweak pyproject.toml and CMakeLists.txt to point at your local library paths. Then you can use python -m build to build the wheels and pip install {path}.whl.
  • Option 2: When not wanting to build but working directly inside the package it requires manual building of the C++ extensions. Might require adjusting the import statements. (TODO explain how to install c++ extensions)

Folder Structure

topolosses
├─ CMakeLists.txt
├─ LICENSE
├─ README.md
├─ pyproject.toml
├─ docs
   ├─ index.rst
   ├─ ...
└─ topolosses
   ├─ README.md
   ├─ __init__.py
   ├─ metrics
   └─ 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

Metadata

Release files for topolosses 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for topolosses 0.2.0
File Size Uploaded
topolosses-0.2.0.tar.gz 9.7 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for topolosses 0.2.0
File
topolosses-0.2.0-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl PyPy 3.11 PyPy 3.11 7.3 Linux glibc 2.34+ x86-64 Details
topolosses-0.2.0-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl PyPy 3.10 PyPy 3.10 7.3 Linux glibc 2.34+ x86-64 Details
topolosses-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.34+ x86-64 Details
topolosses-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details
topolosses-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.34+ x86-64 Details
topolosses-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.34+ x86-64 Details

Total release size: 524.7 MB

Release files / topolosses-0.2.0.tar.gz

Download URL topolosses-0.2.0.tar.gz
Size 9.7 MB
Tags Source
SHA-256 checksum
How to use checksums
0b7a8ad48023820b5038e6930a8af7bb464eecda39d989d397299b5b6c6defdd
BLAKE2b-256 checksum
How to use checksums
de45e44df90ee0557f2a761aaaff4a58fc587eb671df4631fc4f68349a63bdef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / topolosses-0.2.0-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl

Download URL topolosses-0.2.0-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl
Size 85.8 MB
Tags Linux glibc 2.34+ x86-64 PyPy 3.11 PyPy 3.11 7.3
SHA-256 checksum
How to use checksums
213019c80f76b4db940258d209523390322e09fdcd45a0ba20c7b4f065f622cf
BLAKE2b-256 checksum
How to use checksums
ea6a1ce3c6578b8ec63fec2c39dd02c40f1b64d6da18328bca748283df803efe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / topolosses-0.2.0-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl

Download URL topolosses-0.2.0-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl
Size 85.8 MB
Tags Linux glibc 2.34+ x86-64 PyPy 3.10 PyPy 3.10 7.3
SHA-256 checksum
How to use checksums
13b90382e5edd70b10b49590cee89cdeb6a79a37a11a666fe6754e60a50a0f10
BLAKE2b-256 checksum
How to use checksums
3802fe9ce085328b80174ab37801cb7d24518d63594cba15022c8a4518c5bce3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / topolosses-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl

Download URL topolosses-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl
Size 85.8 MB
Tags CPython 3.13 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
8b4405e83e899e8b1dc7bb70cd5bd84eaf79579300693c73893c5e32f2571c48
BLAKE2b-256 checksum
How to use checksums
c33270e53030de4276e506b8603442f09d0cd98fe628c6fa1dedc7a735a37401
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / topolosses-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL topolosses-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl
Size 85.8 MB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
84c8e18d599ff1a52d88def933badd95adbb96a8f08b40ffe60dd3308c8e859b
BLAKE2b-256 checksum
How to use checksums
454348f22e3a79a5355f4992394e52f258593911863d140bf21c3522b7407597
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / topolosses-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl

Download URL topolosses-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl
Size 85.8 MB
Tags CPython 3.11 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
953c69a5c541b2612d1d37475f3d65304d17915b53073b163b61e0b6336ae559
BLAKE2b-256 checksum
How to use checksums
b451f24eb9a1bb6f538afb5fcb392b5bd8c86ba1747f89d61c79e4d3050d2bd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / topolosses-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl

Download URL topolosses-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl
Size 85.8 MB
Tags CPython 3.10 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
2c2ca0ea29dd557ef505aade40382b6cbec1d5ff169403c89393ac4388846f0c
BLAKE2b-256 checksum
How to use checksums
4acc688b7bbafa952e61a1f2b952151ee879d0c3928a5ae30742a3d9b7df2f0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.2.0 This release

7 release files

0.1.70

7 release files

0.1.60

7 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page