semantic_segmentor

Semantic Segmentation Engine for Whole Slide Images (WSIs) using TIAToolbox.

This module defines the SemanticSegmentor class, which extends the PatchPredictor engine to support semantic segmentation workflows on digital pathology images. It leverages deep learning models from TIAToolbox to perform patch-level and WSI-level inference, and includes utilities for preprocessing, postprocessing, and saving predictions in various formats.

Key Components:

Classes: - SemanticSegmentorRunParams:

Configuration parameters for controlling runtime behavior during segmentation.

  • SemanticSegmentor:

    Core engine for performing semantic segmentation on image patches or WSIs.

Functions: - concatenate_none:

Concatenate arrays while gracefully handling None values.

  • merge_horizontal:

    Incrementally merge horizontal patches and update location arrays.

  • save_to_cache:

    Save intermediate canvas and count arrays to Zarr cache.

  • merge_vertical_chunkwise:

    Merge vertically chunked canvas and count arrays into a probability map.

  • store_probabilities:

    Store computed probability data in Zarr or Dask arrays.

  • prepare_full_batch:

    Align patch-level predictions with global output locations.

Example: >>> from tiatoolbox.models.engine.semantic_segmentor import SemanticSegmentor >>> segmentor = SemanticSegmentor(model=”fcn_resnet50_unet-bcss”) >>> wsis = [“slide1.svs”, “slide2.svs”] >>> output = segmentor.run(wsis, patch_mode=False) >>> >>> patches = [np.ndarray, np.ndarray] >>> segmentor = SemanticSegmentor(model=”fcn_resnet50_unet-bcss”) >>> output = segmentor.run(patches, patch_mode=True, output_type=”dict”)

Notes:

  • Supports both patch-based and WSI-based segmentation.

  • Compatible with TIAToolbox pretrained models and custom PyTorch models.

  • Outputs can be saved as dictionaries, Zarr arrays, or AnnotationStore databases.

  • Includes memory-aware caching and efficient merging strategies for large-scale inference.

Functions

clip_probabilities_to_shape

Clip probability chunk to target output shape and track written height.

concatenate_none

Concatenate arrays, handling None values gracefully.

get_wsi_output_shape

Return WSI output shape as (height, width) for the dataset if available.

merge_batch_to_canvas

Merge patch-level predictions into a single canvas.

merge_horizontal

Merge horizontal patches incrementally for each row of patches.

merge_vertical_chunkwise

Merge vertically chunked canvas and count arrays into a single probability map.

prepare_full_batch

Prepare full-sized output and count arrays for a batch of patch predictions.

save_to_cache

Incrementally save computed canvas and count arrays to Zarr cache.

store_probabilities

Store computed probability data into a Zarr dataset or accumulate in memory.

Classes

SemanticSegmentor

Semantic segmentation engine for digital histology images.

SemanticSegmentorRunParams

Runtime parameters for configuring the SemanticSegmentor.run() method.