topologysegmentation
PyTorch losses and metrics for topology-aware segmentation. Overlap losses stay high when a vessel or airway is broken in the middle. These terms penalize that: soft skeletons, persistence diagrams, and Betti matching, for a 2D frame or a 3D volume.
| Paper | Loss |
|---|---|
| Shit et al., clDice | cldice |
| Hu, Li, Samaras, Chen, NeurIPS 2019 | hu |
| Stucki et al., Betti matching, ICML 2023 | bm, bm3d |
| Wen et al., SAT | sat |
| González-Jiménez et al., T-Loss | tloss |
The package is on PyPI: pypi.org/project/topologysegmentation. Class and parameter reference: topologylosses.readthedocs.io.
pip install topologysegmentation # Dice, clDice, pooling, critical points, T-Loss
pip install "topologysegmentation[ph]" # also Hu, Betti matching, and SAT
import torch
from topologysegmentation import CLDiceLoss, DiceLoss, SumLoss, loss_shapes, topology_errors
logits = torch.randn(2, 1, 64, 64) # frame (N, 1, H, W) or volume (N, 1, D, H, W)
target = torch.zeros(2, 1, 64, 64)
target[:, :, 16:48, 16:48] = 1
criterion = SumLoss((DiceLoss(), 1.0), (CLDiceLoss(), 0.1))
value = criterion(logits, target) # one scalar; criterion.parts has each term
report = topology_errors(logits.sigmoid(), target)
loss_shapes() # {"dice": (2, 3), "sat": (2,), ...}
By default every loss applies sigmoid. Pass logits, or set sigmoid=False when the tensor is already a probability. topology_errors is eval-only and expects probabilities. Python 3.12 is required. bm3d needs a local build of Betti-Matching-3D: put its betti_matching module on PYTHONPATH.
Third-party licenses are listed in NOTICE. A short install walkthrough is in QUICKSTART.md.
Installation
The base package depends only on torch and numpy. Homology lives in the ph extra.
| Command | What you get |
|---|---|
pip install topologysegmentation |
dice, bce, focal, combined, cldice, tloss, pooling, critical, Dice/IoU, centerline Dice |
pip install "topologysegmentation[ph]" |
also hu, bm, sat, and Betti numbers |
pip install "topologysegmentation[plot]" |
plots for Betti matching |
pip install "topologysegmentation[all]" |
both extras |
Shapes
A frame is (N, 1, H, W). A volume is (N, 1, D, H, W). One frame may be (H, W) or a channel-free batch (N, H, W). A volume keeps the channel axis: (D, H, W) is read as a batch of frames. A side of length 1 is still that axis: a 1×N strip is a frame, and a slab of depth 1 is a volume.
loss_shapes() maps each trainable name to the spatial ranks it accepts. 2 is a frame, 3 is a volume.
| Ranks | Losses |
|---|---|
| 2 and 3 | dice, bce, focal, combined, cldice, tloss, pooling, critical, hu, bm3d |
| 2 | sat, bm |
sat and bm raise ValueError on a volume. For a volume, Betti matching is bm3d.
Losses
Each loss is its own class. Call Loss(logits, target). A one-off mix is ordinary addition, DiceLoss()(logits, target) + 0.1 * CLDiceLoss()(logits, target). SumLoss is that sum as one module: the forward returns a scalar, parts holds the weighted terms, and weights["cldice"] = 0.2 changes a weight. A weight of 0 skips that term.
What each class computes, and what each parameter changes, is written out in Losses.
| Name | What it does | Dependency | Weight next to Dice |
|---|---|---|---|
dice, bce, focal, combined |
Overlap and per-pixel losses. Any spatial rank | package | 1 |
cldice |
Soft skeleton, Shit et al., arXiv:2003.07311. Ten pooling iterations, frames and volumes | package | 0.1–0.3 |
tloss |
Robust Student-t likelihood for a noisy mask, per voxel. nu is learned. Any shape |
package | 0.1 |
pooling |
Multi-scale occupancy after max-pooling. A frame adds two axis projections, a volume adds three | package | 1 |
critical |
Endpoints, junctions, and isolated voxels. Eight neighbors on a frame, twenty-six on a volume | package | 1 |
hu |
Diagram matching toward birth 1 and death 0, extra bars toward the diagonal. Hu et al., NeurIPS 2019. Frames and volumes | ph |
0.1 |
bm |
Betti matching, Stucki et al., ICML 2023. Frames | ph |
0.1 |
bm3d |
The same objective through the C++ extension. Frames and volumes | compiled betti_matching |
0.1 |
sat |
Spatially aware diagram matching, divided by the image side length. Frames | ph |
0.1 |
Foreground voxels that touch at a corner are one component. betti_numbers on a frame is (β0, β1). On a volume it is (β0, β1, β2). An empty mask is all zeros. A single foreground voxel is (1, 0) on a frame and (1, 0, 0) on a volume.
Speed
One CPU (Apple M1), forward and backward together. Frames are VessMAP labels (annotator1, native 256×256), resized with nearest-neighbor interpolation. The prediction is that same mask dilated by one pixel, with 12% of the vessel pixels removed, passed as logits ±4. A leading number is the batch: 3×1024² is three such frames. sat and bm are frames only, so this table has no volumes.
| Loss | 256² | 512² | 1024² | 3×1024² | 4×256² |
|---|---|---|---|---|---|
dice |
0.4 ms | 0.6 ms | 3.1 ms | 7.8 ms | 0.7 ms |
bce |
0.5 ms | 1.5 ms | 4.7 ms | 13 ms | 1.0 ms |
focal |
1.2 ms | 2.8 ms | 12 ms | 35 ms | 3.1 ms |
combined |
1.1 ms | 1.9 ms | 7.2 ms | 25 ms | 2.0 ms |
tloss |
0.6 ms | 1.0 ms | 3.2 ms | 8.7 ms | 0.8 ms |
pooling |
6.7 ms | 25 ms | 0.11 s | 0.15 s | 9.3 ms |
critical |
3.3 ms | 9.3 ms | 47 ms | 0.12 s | 7.7 ms |
cldice |
99 ms | 0.41 s | 1.9 s | 5.3 s | 0.32 s |
hu |
0.26 s | 1.1 s | 4.5 s | 14 s | 1.0 s |
sat |
0.24 s | 1.0 s | 4.2 s | 13 s | 0.98 s |
bm |
0.64 s | 4.4 s | 73 s | 4.8 min | 2.8 s |
These masks have a handful of components and loops (for example (β0, β1) of (5, 0), (4, 7), (19, 4)). Times up to 512² are the median of three passes. At 1024² and for 3×1024², cldice, hu, sat, and bm are one pass after a warmup.
Metrics
dice_iou(probabilities, target) returns per-batch Dice and IoU after a 0.5 threshold. Frames and volumes both work. On a mask compared with itself the score is 1, including a single voxel.
centerline_dice(probabilities, target) is the Dice score between morphological skeletons.
betti_numbers(mask) is (β0, β1) or (β0, β1, β2). The binary count does not import gudhi. hu does, through the ph extra, because it needs the critical voxels of the diagram.
topology_errors(probabilities, target) reports the mean absolute Betti error (including betti_b2_mae on a volume), the centerline Dice, and a per-image breakdown. This is a validation report, not a loss. A frame does not grow a β2 term when one side is 1; a one-slice volume does.
Citation
GitHub shows a “Cite this repository” button from CITATION.cff. If you use a bundled loss in a paper, also cite the paper in the table above.
License
Original code is MIT, see LICENSE. T-Loss stays under Apache-2.0, so the package license expression is MIT AND Apache-2.0. The file table is in NOTICE.
Metadata
Release files for topologysegmentation 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| topologysegmentation-0.2.0.tar.gz | 68.2 kB | Without provenance | Details for topologysegmentation-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| topologysegmentation-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for topologysegmentation-0.2.0-py3-none-any.whl |
Total release size: 145.4 kB
Release files / topologysegmentation-0.2.0.tar.gz
| Download URL | topologysegmentation-0.2.0.tar.gz |
|---|---|
| Size | 68.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d311067541e8e8f15dbec064d1aa8264dfef323d4670760110878229e2fe06d6
|
|
BLAKE2b-256 checksum How to use checksums |
302b5dda1eb505efeec6bde012d4221639055e230ec1c053e9be19040bd14fd5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.12
|
Release files / topologysegmentation-0.2.0-py3-none-any.whl
| Download URL | topologysegmentation-0.2.0-py3-none-any.whl |
|---|---|
| Size | 77.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
348b570dfd87baef51af1c5ead1d98928f7f5564659439af06e391b7e3ede059
|
|
BLAKE2b-256 checksum How to use checksums |
b617a354574c2b3c84d73fd8c8af3a41b6979e6714057c41e458fb0914d5c001
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.12
|
Security for topologysegmentation 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.