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.
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):
IfFalse, 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
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 Distribution
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 topolosses-0.2.0.tar.gz.
File metadata
- Download URL: topolosses-0.2.0.tar.gz
- Upload date:
- Size: 9.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b7a8ad48023820b5038e6930a8af7bb464eecda39d989d397299b5b6c6defdd
|
|
| MD5 |
0896f858cc8177b0647b720f54813d2d
|
|
| BLAKE2b-256 |
de45e44df90ee0557f2a761aaaff4a58fc587eb671df4631fc4f68349a63bdef
|
File details
Details for the file topolosses-0.2.0-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.2.0-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 85.8 MB
- Tags: PyPy, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
213019c80f76b4db940258d209523390322e09fdcd45a0ba20c7b4f065f622cf
|
|
| MD5 |
b1ee18a8350cff2b14975a23e4240bf2
|
|
| BLAKE2b-256 |
ea6a1ce3c6578b8ec63fec2c39dd02c40f1b64d6da18328bca748283df803efe
|
File details
Details for the file topolosses-0.2.0-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.2.0-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 85.8 MB
- Tags: PyPy, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13b90382e5edd70b10b49590cee89cdeb6a79a37a11a666fe6754e60a50a0f10
|
|
| MD5 |
e322e583d90078701c6e53d12b0a6f7d
|
|
| BLAKE2b-256 |
3802fe9ce085328b80174ab37801cb7d24518d63594cba15022c8a4518c5bce3
|
File details
Details for the file topolosses-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 85.8 MB
- Tags: CPython 3.13, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b4405e83e899e8b1dc7bb70cd5bd84eaf79579300693c73893c5e32f2571c48
|
|
| MD5 |
f78db21d4269b229700d250241f9b90f
|
|
| BLAKE2b-256 |
c33270e53030de4276e506b8603442f09d0cd98fe628c6fa1dedc7a735a37401
|
File details
Details for the file topolosses-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.2.0-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 85.8 MB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
84c8e18d599ff1a52d88def933badd95adbb96a8f08b40ffe60dd3308c8e859b
|
|
| MD5 |
468093b964085242a71a73b7116eb48c
|
|
| BLAKE2b-256 |
454348f22e3a79a5355f4992394e52f258593911863d140bf21c3522b7407597
|
File details
Details for the file topolosses-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.2.0-cp311-cp311-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 85.8 MB
- Tags: CPython 3.11, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
953c69a5c541b2612d1d37475f3d65304d17915b53073b163b61e0b6336ae559
|
|
| MD5 |
5dfb3ac58d3f6654b33517529b91f526
|
|
| BLAKE2b-256 |
b451f24eb9a1bb6f538afb5fcb392b5bd8c86ba1747f89d61c79e4d3050d2bd0
|
File details
Details for the file topolosses-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.2.0-cp310-cp310-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 85.8 MB
- Tags: CPython 3.10, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c2ca0ea29dd557ef505aade40382b6cbec1d5ff169403c89393ac4388846f0c
|
|
| MD5 |
8716e81cfd4b2f8eada5caaf65e9476a
|
|
| BLAKE2b-256 |
4acc688b7bbafa952e61a1f2b952151ee879d0c3928a5ae30742a3d9b7df2f0e
|