Function Signature
ThegetSegmentation function performs end-to-end tissue segmentation on whole slide images.
Segmentation.py (lines 192-225)
Description
This function provides a complete tissue segmentation pipeline that:- Loads pre-trained models based on the specified tissue type (colon, liver, or breast)
- Processes the WSI in patches using sliding window inference
- Applies optional augmentation (Test-Time Augmentation) for improved accuracy
- Generates probability maps showing prediction confidence
- Saves results as TIFF files including mask, probability map, and uncertainty map
- Returns the prediction as a numpy array for further processing
Models are automatically downloaded on first use and cached in
~/.DigiPathAI/ directory.Parameters
Path to the whole slide image (WSI) file in TIFF format.Supported formats:
.tiff, .svs, and other formats supported by OpenSlide.Example: '/path/to/slide.tiff'Size of patches (in pixels) extracted from the WSI for inference.Common values:
128, 256, 512Impact: Larger patches provide more context but require more GPU memory.Number of pixels to skip between consecutive patches during sliding window inference.Impact:
- Smaller stride = more overlap = better quality but slower
stride_size = patch_size= no overlap (fastest)stride_size = patch_size / 2= 50% overlap (recommended)
Number of patches to process simultaneously during inference.Impact: Higher values increase speed but require more GPU memory.Recommended values:
16-64 depending on GPU memory.Controls model ensemble behavior.
True: Uses a single model (specified bymodelparameter) for fast inferenceFalse: Ensembles 4 different models (inception, deeplabv3, densenet) for higher accuracy
List of Test-Time Augmentation (TTA) transforms to apply during inference.Allowed values:
'FLIP_LEFT_RIGHT': Horizontal flip'ROTATE_90': 90-degree rotation'ROTATE_180': 180-degree rotation'ROTATE_270': 270-degree rotation
['FLIP_LEFT_RIGHT', 'ROTATE_90']TTA improves robustness but increases inference time proportionally.
Whether to apply Conditional Random Fields (CRF) for post-processing.Note: Currently not active in the implementation (lines 327-331 are commented).
File path to save the probability map (raw model outputs before thresholding).Saved as a compressed TIFF file with JPEG compression.
File path to save the binary segmentation mask.Values are thresholded at 0.3: pixels with probability ≥ 0.3 become 255, others become 0.
File path to save the prediction uncertainty map (variance across models/augmentations).Higher values indicate less confident predictions.
Optional dictionary for tracking progress (used by the web server).The function updates
status['progress'] (0-100) and status['status'] (status message).Example: status = {'progress': 0, 'status': 'Starting...'}Pyramid level for mask processing in multi-resolution WSIs.
-1 uses the highest resolution level.Which model architecture to use when
quick=True.Allowed values:'dense': DenseNet-based model'inception': Inception-based model'deeplabv3': DeepLabV3 model
quick=True. When quick=False, all three models are ensembled.Tissue type for selecting the appropriate pre-trained models.Allowed values:
'colon': Colon/colorectal tissue (uses DigestPath models)'liver': Liver tissue (uses PAIP models)'breast': Breast tissue (uses Camelyon models)
Return Value
Binary segmentation mask as a numpy array.Shape:
(height, width) matching the WSI dimensionsData type: float32Values:255.0: Tissue (positive prediction with probability ≥ 0.3)0.0: Background (negative prediction)
0.3 (line 310 in Segmentation.py).Code Example
Here’s the example from the README showing typical usage:Advanced Example with TTA and Ensemble
Output Files
The function automatically saves three TIFF files:- Probability Map (
probs_path): Raw probability values (0.0 to 1.0) - Binary Mask (
mask_path): Thresholded segmentation (0 or 255) - Uncertainty Map (
uncertainty_path): Prediction variance scaled to 0-255
Performance Notes
Typical processing times (on NVIDIA V100):- Small WSI (10,000 x 10,000): 2-5 minutes (quick), 8-20 minutes (ensemble)
- Medium WSI (50,000 x 50,000): 15-30 minutes (quick), 60-120 minutes (ensemble)
- Large WSI (100,000 x 100,000): 60-120 minutes (quick), 4-8 hours (ensemble)
See Also
Parameter Reference
Detailed documentation for all parameters
Python API Overview
Overview of the Python API