Topology-aware diffusion-based flow field generation for 3D instance segmentation
topo generates 3D vector flow fields that point toward instance centers, enabling instance segmentation through particle tracking. It provides two strategies depending on object morphology:
- Direct flows — for convex shapes (nuclei, vesicles, etc.)
- Diffusion flows — topology-aware flows for non-convex shapes (mitochondria, cells)
For convex objects, each foreground voxel
At the center of mass itself (the sink),
For non-convex objects (e.g., mitochondria that curve back on themselves), direct vectors would cut through empty space. Instead, we solve the heat equation inside the object mask and take its gradient.
For each spatial axis
We then iteratively diffuse via the discrete Laplacian:
where
with
Boundary conditions:
| Region | Condition | Effect |
|---|---|---|
| True background ( |
Dirichlet: |
Flow points inward at real boundaries |
| Annotation boundary (unannotated region) | Neumann: |
Free diffusion — cut instances don't pile up at crop edges |
After convergence, the flow field is the normalized gradient:
where
The number of diffusion iterations scales with instance size:
where
At inference, each foreground voxel is tracked through the predicted flow field using Euler integration:
where
pip install topo
# With GPU support (PyTorch):
pip install topo[gpu]import numpy as np
from topo import generate_direct_flows, generate_diffusion_flows
# instance_mask: [D, H, W] int array where each unique ID is one instance
instance_mask = ...
# Direct flows (convex shapes)
flows = generate_direct_flows(instance_mask) # [3, D, H, W]
# Diffusion flows (non-convex shapes)
flows = generate_diffusion_flows(instance_mask, n_iter=200) # [3, D, H, W]from topo import compute_flow_targets
# instance_ids: [N_classes, D, H, W] — one channel per class
flows, class_fg = compute_flow_targets(
instance_ids,
class_names=["nuc", "mito", "ves"],
class_config={
"nuc": {"flow_type": "direct"},
"mito": {"flow_type": "diffusion", "diffusion_iters": 200},
"ves": {"flow_type": "direct"},
},
)
# flows: [N*3, D, H, W] — per-class unit flow vectors
# class_fg: [N, D, H, W] — per-class foreground masksfrom topo.flow_gpu import compute_flow_targets_gpu
# instance_ids: [B, N, D, H, W] torch.Tensor on GPU
flows, class_fg = compute_flow_targets_gpu(instance_ids)MIT

