u-Segment3D ("Turn 2D segmentations into 3D")
A Python library for generating 3D consensus cell segmentation from 2D segmented stacks
:star2: Note on using Cellpose4 models :star2:
Oct 2026
do_mp=True (multiprocess gradient descent) now gives the same segmentation as the default serial path. Previously it thresholded the point density over the foreground only instead of the whole volume.
- the automatic cell probability threshold is no longer floored to 1 decimal point, which could make it jump by 0.1 between near-identical runs. A new parameter,
combine_cell_probs['prob_thresh_offset'] (default -0.05), is added to the multi-Otsu threshold instead. The old behaviour is still available with apply_one_d_p_thresh=True (now False by default), and setting prob_thresh explicitly still bypasses the automatic threshold. The printed threshold now also shows the raw multi-Otsu value.
- preprocessing is reproducible: background correction no longer depends on the number of numba threads.
- the CUDA (cupy) background correction works again instead of silently falling back to torch, and no longer depends on whether torch or cupy is imported first. Each "no CUDA / no gpu" fallback message now prints the error that caused it.
- note: default results may shift slightly as a result of these fixes.
Aug 2026
June 2025
- we have updated API to add compatibility with Cellpose4 changes. u-Segment3D should work with cellpose>=4.0.5. If this is not available from pip, you will need to install the github version of cellpose:
pip install git+https://www.github.com/mouseland/cellpose.git
- Note: In Cellpose4, the cellpose-SAM model ('cpsam') is the default. As of writing it seems 'cyto3', 'cyto2', 'cyto' etc. are no longer built-in. Therefore currently u-Segment3D will install as dependency Cellpose3. This will allow most flexibility for users.
:star2: Mar 2025 :star2:
- u-Segment3D is available in PyPI and can be directly installed with
pip install u-Segment3D
May 22, 2024
u-Segment3D is a Python library to merge 2D slice-by-slice segmented 3D volumes in x-y, x-z, or y-z views into a single consensus 3D segmentation. u-Segment3D can use the probabilities and gradients directly predicted by popular existing Neural Network segmentation models such as Cellpose (Direct Method) or operates on the provided 2D segmentations directly (Indirect Method). u-Segment3D does not require all 3 views, any number can be used, nor do each view need be generated by the same algorithm. u-Segment3D also provides a number of postprocessing algorithms to recover missing features after obtaining the 3D segmentation. Therefore it can also be used as a postprocessing module for 3D segmentations in general.
Despite the name, the postprocessing and segmentation functions in u-Segment3D have 2D equivalents which can be used to enhance 2D segmentation too. Please see the tutorials.
The primary motivation in developing u-Segment3D is to take advantage and leverage existing state-of-the-art 2D segmentation models for 3D segmentation without further data training.
It is associated with the paper, Universal consensus 3D segmentation of cells from 2D segmented stacks, Nature Methods, 2025, written by Felix Y. Zhou, Zach Marin, Clarence Yapp, Qiongjing Zou, Benjamin A. Nanes, Stephan Daetwyler, Andrew R. Jamieson, Md Torikul Islam, Edward Jenkins, Gabriel M. Gihana, Jinlong Lin, Hazel M.Borges, Bo-Jui Chang, Andrew Weems, Sean J. Morrison, Peter K. Sorger, Reto Fiolka, Kevin Dean, and Gaudenz Danuser.
The current library is still a work-in-progress. It is fully functional but documentation still require work and more tutorials are upcoming.
A universal 2D-to-3D segmentation algorithm
Dependencies
u-Segment3D has a number of dependencies detailed in pyproject.toml. GPU
dependencies are based on installing Cellpose to use as the default 2D
segmentation method in u-Segment3D. NVIDIA GPU installations use the CUDA 12
CuPy package (cupy-cuda12x). Install only one CuPy package in an environment.
Installation
u-Segment3D can be installed for all three major OS from PyPI. We recommend installing to a new conda environment, and install scikit-fmm package from the conda-forge channel:
conda create -n u_Segment3D_env python=3.9
conda install -n u_Segment3D_env -c conda-forge scikit-fmm
pip install u-Segment3D
u-Segment3D can also be installed by git cloning the repository and running pip in the cloned folder with python 3.9-3.12. We have developed on Python==3.9 on Red Hat Enterprise Linux Server 7.9. pyproject.toml configures the individual dependencies for each OS.
Linux
We suggest first creating a new conda environment for installation. Choose a
PyTorch build supported by both the installed NVIDIA driver and the target GPU.
For example, a CUDA 12.6 PyTorch build supports older Pascal GPUs, while newer
builds can be used for Ampere and Blackwell GPUs:
conda create -n u_Segment3D_env python=3.9
conda install -n u_Segment3D_env -c conda-forge scikit-fmm
conda activate u_Segment3D_env
pip install torch --index-url https://download.pytorch.org/whl/cu126
pip install .
On an HPC cluster, load a CUDA 12 toolkit compatible with the installed driver
before activating the environment so that CuPy can provide GPU image resizing.
Blackwell GPUs require CUDA 12.8 or newer for native support. If CuPy cannot use
CUDA, u-Segment3D falls back to its PyTorch implementation of image resizing;
this still uses the GPU when PyTorch CUDA is available, but it is slower.
Errors we have encountered:
- When running, we get an
icu error, AttributeError: module 'icu' has no attribute 'Locale'. Fix this by installing pyicu. This can be done using pip (requires gcc compiler, gcc>=6.3.0) or alternatively installation of a precompiled version with conda from the conda-forge channel
pip install pyicu # Requires compilation, check that gcc>=6.3.0 to be available on your linux distribution
conda install -n u_Segment3D_env pyicu -c conda-forge # tries to install a precompiled version.
pytorch does not have gpu available. Try installing a previous version for the respective cudatoolkit version. Alternatively, try installing a precompiled version using anaconda from the conda-forge channel. Refer to instructions for Windows below.
You see a print out along the lines of 'cuda not available. Falling back to pytorch'. This doesn't mean you don't have cuda. It means cuda drivers is likely installed in non-standard location or not installed properly so that not all cupy function is not available. Consequently u-Segment3D is using CUDA (if available) via pytorch.
Windows
conda create -n u_Segment3D_env python=3.9
conda activate u_Segment3D_env
pip install torch --index-url https://download.pytorch.org/whl/cu126
pip install .
On machine with NVIDIA graphics card, check to see if torch has been correctly installed with GPU by importing the library in Python.
import torch
print(torch.cuda.is_available())
If True, nothing needs to be done. If False, install a PyTorch build that
matches the supported CUDA version for the target GPU and driver, for example:
pip install torch --index-url https://download.pytorch.org/whl/cu126
See https://pytorch.org/get-started/previous-versions/ to check out other pytorch versions.
Alternatively, you may try to install pytorch through anaconda from the conda-forge channel:
conda install pytorch torchvision torchaudio pytorch-cuda=12.6 -c pytorch -c nvidia
Errors we have encountered:
- Installing
scikit-fmm fails, because it cannot be compiled. To resolve, install precompiled binary version through conda-forge before running pip install .
conda install -n u_Segment3D_env scikit-fmm -c conda-forge
MacOS
NVIDIA CUDA is not available for MacOS with Apple Silicon chips. Therefore don't install any of these dependencies.
conda create -n u_Segment3D_env python=3.9
conda install -n u_Segment3D_env -c conda-forge scikit-fmm
conda activate u_Segment3D_env
pip install .
Getting Started
The simplest way to get started is to check out the included notebook tutorials which aims to showcase various use cases of u-Segment3D for 3D segmentation and how parameters may be tuned and algorithms adapted. A pictorial diagram is provided to show how tutorial scripts relate:
Direct Method (cell gradients and probability maps as input) in a nutshell
If you have computed 2D slice-by-slice instance cell segmentations in xy, xz, and yz views, represented by their unit-length gradients and a 0-1 normalized cell probability maps then you can translate these into one 3D instance segmentation mask with a few lines of code:
import segment3D.parameters as uSegment3D_params
import segment3D.usegment3d as uSegment3D
# instantiate default parameters
aggregation_params = uSegment3D_params.get_2D_to_3D_aggregation_params()
# integrate labels_xy, labels_xz, labels_yz into one single 3D segmentation. Give a single-channel volume image, img we define its xy view as img, its xz view as img.transpose(1,2,0) and its yz view as img.transpose(2,0,1)
segmentation3D, (probability3D, gradients3D) = uSegment3D.aggregate_2D_to_3D_segmentation_direct_method(segmentations=[img_segment_2D_xy_probs,
img_segment_2D_xz_probs,
img_segment_2D_yz_probs],
gradients = [img_segment_2D_xy_flows,
img_segment_2D_xz_flows,
img_segment_2D_yz_flows],
params=aggregation_params,
savefolder=None,
basename=None)
Indirect Method (instance segmentation masks as input) in a nutshell
The easiest way to use u-Segment3D is the indirect method. If you have 2D slice-by-slice instance segmentation masks of cells in xy, xz, and yz views, then you can translate these into one 3D instance segmentation mask with a few lines of code: