Skip to content

AI UNet Segmentation

The AI UNet Segmentation command runs semantic segmentation using a pretrained U-Net model exported as TorchScript. Unlike AI Stardist Segmentation, U-Net produces only a foreground/background mask - it has no notion of individual object instances. Touching objects must be separated with a follow-up step (see Splitting touching objects below).

U-Net’s defining feature is its symmetric shape: an encoder repeatedly downsamples the image to build up coarse, high-level context, and a decoder mirrors it back up to full resolution - but at each level, the decoder also receives the encoder’s same-resolution features directly via a “skip connection”, so fine spatial detail lost during downsampling doesn’t have to be reconstructed from scratch.

An encoder downsamples for context; a decoder upsamples back to full resolution, with skip connections carrying fine detail across

ParameterDescription
Model pathPath to a TorchScript-exported U-Net model (.pt / .pth).
Object classThe segmentation class assigned to pixels classified as foreground.
Probability thresholdProbability above which a pixel is classified as foreground. Range: 0.0–1.0 (default 0.5).
Output modeHow to interpret a multi-channel model output - SoftmaxClasses or IndependentChannels. Ignored for single-channel outputs. See Output mode.
Foreground channelIndex of the channel holding the foreground probability, used only when the output has more than one channel. Range: 0–16 (default 1).
Boundary channelIndex of an optional boundary channel for boundary-aware models. Set to -1 to disable (default). Range: −1–16.
Boundary thresholdBoundary probability at or above which a pixel is excluded from the foreground. Only used when Boundary channel is enabled. Range: 0.0–1.0 (default 0.5).

The model must accept a [1, 1, H, W] single-channel float tensor and return either:

  • a [1, 1, H, W] tensor of per-pixel foreground probabilities (the model already applies its final sigmoid), or
  • a [1, C, H, W] tensor with more than one channel, in which case Output mode and Foreground channel decide how the foreground probability is extracted.
ModeUse for
SoftmaxClassesA mutually-exclusive class head (e.g. a 2-class background/foreground classifier). Softmax is applied across channels first, then Foreground channel is read (typically 1 for a 2-class head).
IndependentChannelsIndependent, already-activated probability maps that are not mutually exclusive - e.g. a foreground-mask channel plus a separate boundary channel. Foreground channel is read directly, with no softmax.

U-Net emits one shared class for “foreground”, so two touching objects become a single connected blob after segmentation. How you split them depends on the model:

Models that predict an explicit boundary channel - e.g. bioimage.io’s affable-shark (NucleiSegmentationBoundaryModel) - let you carve the boundary out as a thin gap between objects:

  1. Set Output mode to IndependentChannels.
  2. Set Boundary channel to the model’s boundary channel index (commonly 1).
  3. Set Boundary threshold (~0.5). A pixel is foreground only where the foreground probability reaches Probability threshold and the boundary probability stays below Boundary threshold.
  4. Chain a plain Connected Components afterward - no watershed needed:
AI UNet Segmentation (foreground_channel: 0, boundary_channel: 1) → Connected Components → Extract ROIs

Discarding the boundary channel and relying on watershed instead is the most common reason touching objects “won’t split” - a distance-map watershed can’t separate a blob that has no waist, and the waist information lives in the boundary channel.

If the model gives only a foreground mask (no boundary), chain a distance-map watershed instead:

AI UNet Segmentation → Connected Components → Watershed → Extract ROIs

Connected Components labels each blob; Watershed re-splits any blob containing more than one object. This only works when touching objects form a pinched “peanut” shape - heavily overlapping objects with no waist cannot be split from the mask alone.

Olaf Ronneberger, Philipp Fischer, and Thomas Brox, “U-Net: Convolutional Networks for Biomedical Image Segmentation,” Medical Image Computing and Computer-Assisted Intervention (MICCAI), 2015, pp. 234-241.