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, and MosinLoss.
Installation
Install the package from Test PyPI. The verision 0.1.2 includes betti matching:
pip install -i https://test.pypi.org/simple/ topolosses==0.1.2
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):
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.
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
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.1.60.tar.gz.
File metadata
- Download URL: topolosses-0.1.60.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efc9366659ddfad692631b6b0242b68a34c28ed6c009779c8d9e5735bed3e037
|
|
| MD5 |
1cf230585e357befcb0170237d9cb19d
|
|
| BLAKE2b-256 |
49ea4ffaab65d22b0316493f5898e798ac01a6672c1838f8ffaf194fb061abbe
|
File details
Details for the file topolosses-0.1.60-pp311-pypy311_pp73-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.1.60-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 |
47df59b97cf37f1f1ac5a9297f2bc471e9f8073abf8fb27419e8e1c957dc40f2
|
|
| MD5 |
f15a47383103aeb1de51cf98670aea8f
|
|
| BLAKE2b-256 |
07d28ac991eaa288fdbf3856e58f1fe462062494e250e2f8fcddda8453585ba4
|
File details
Details for the file topolosses-0.1.60-pp310-pypy310_pp73-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.1.60-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 |
442cfddfbf843383e9a90a7a0c112b6731adae0eb53dbf76a601f7d5ec0011b0
|
|
| MD5 |
aac5affbcc30d64f8e811cf17e1dae7a
|
|
| BLAKE2b-256 |
bf917fd01f6b44ccaf2da6af85b3d02e7db9948f5d27a67b4aecc4c2116693c2
|
File details
Details for the file topolosses-0.1.60-cp313-cp313-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.1.60-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 |
1da5a2e3c20b60bce8b8e67e645f973631a6a4e25a56ca3b1b9c424be9ff29b0
|
|
| MD5 |
daa72e4150ac645e4779ff725d57b527
|
|
| BLAKE2b-256 |
9db567c51d21df57ff78acdd36af689b7499518d2c6c1993f46aee8f32366987
|
File details
Details for the file topolosses-0.1.60-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.1.60-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 |
4ddf99db07ab2a404d6d371241bff371e9a5e31317c68538bbec169a24ccfa61
|
|
| MD5 |
f9b23ebe8b24e43201c0871616a61150
|
|
| BLAKE2b-256 |
d3ff9c750eb99bdd0ad66a75fe664af8b394e166bc06624431ac41d516674292
|
File details
Details for the file topolosses-0.1.60-cp311-cp311-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.1.60-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 |
3ed1f9f3c8f46f43a80fbf1100aac3555aeb24fa7f7f19f28fb8eb033f429fa8
|
|
| MD5 |
22c116d23f8780f6a83f8b3a89abcf7e
|
|
| BLAKE2b-256 |
ac52d6ec27f92afeb17b5987a0aeef4886cdd2b53f41edbcc6667aacf637f700
|
File details
Details for the file topolosses-0.1.60-cp310-cp310-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: topolosses-0.1.60-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 |
4a0f936c323a8fe22bda348fd4276aca25bbdf0bb17060cbc527e01a1fd3820a
|
|
| MD5 |
163b4a25a864912dff2b9b7b74898774
|
|
| BLAKE2b-256 |
0c19cb5c588c760bd156547f8061e50a64a43684220b1c31b50fc28565cd4c1d
|