Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Understand masks, class values, probability maps and large-image inference

1. Motivation

All the segmentation functions that you will use in this lesson — training, inference and evaluation — operate on masks with specific shape and value conventions. Getting comfortable with these conventions now will save you time later.


2. Core idea

A segmentation model performs image-to-mask prediction: given an input image with height H and width W, it outputs a raster of the same H and W, where every pixel holds a predicted class index. This is fundamentally different from classification (one label for the whole image) and detection (a variable-length list of boxes); segmentation always produces exactly one prediction per pixel, with no separate step for describing objects or entire scenes.


3. Working with masks

A. Binary and multi-class masks

A binary mask has two possible pixel values: typically 0 for background and 1 for the target class (building, water, and so on).

0 = background  (not tree)
1 = target   (tree)

A multi-class mask extends this to more than two values, one integer per class, for example 0 through 12 for a 13-class land-cover scheme, with 0 conventionally reserved for background or “no data.”

For example:

0 = background
1 = water
2 = forest
3 = cropland
4 = impervious surface

B. Probability maps

In addition to a hard class prediction, a segmentation model can also produce a probability map: a multi-band raster where each band corresponds to one class, and pixel values range from 0 to 1, representing the model’s confidence that a given pixel belongs to that class. This is the pixel-level equivalent of the confidence score you used per-detection in L06.

For a two-class example:

pixel:
background → 0.08
tree   → 0.92

A probability map is useful for spotting uncertain regions, for example along class boundaries, and for setting a custom confidence threshold rather than accepting the model’s default hard classification.

C. Sliding-window inference

Real rasters are almost always larger than a model’s expected input size. Tiled inference (also called sliding-window inference in this context) solves this the same way it did in L06: a fixed-size window slides across the full raster with some overlap between neighboring windows, the model predicts each window independently, and the overlapping predictions are stitched into one seamless output raster. The overlap smooths out any inconsistency at window edges, the same purpose it served for detection tiling.

parameterexplanation
Tile sizeDefines the dimensions of each model input (e.g., 512 × 512 pixels). Larger tiles provide more spatial context but require more memory.
StrideThe distance the window moves between neighboring tiles during training. For example, a tile size of 512 with a stride of 256 creates a 50% overlap.
OverlapOverlapping windows used during inference to reduce visible seams and improve prediction quality near tile edges before stitching them into a continuous raster.

D. Raster predictions and vector outputs

A model’s direct output is always a raster: a grid of class values aligned with the input imagery. For many downstream uses, especially GIS workflows, vector polygons are more useful, for example to compute an area, join with other vector layers, or filter by shape. Converting a raster mask into vector polygons is a separate, deliberate post-processing step, not something the model does automatically; you will practice this conversion on the binary-workflow page. Vectorization does not make the original prediction more accurate. It only changes its representation.


4. Python reactivation

A mask, once loaded, is a NumPy array, the same data structure you have used for raster bands in earlier lessons. array.shape tells you its height and width; numpy.unique(array) lists the distinct values actually present, which is exactly the quick check recommended above before training on a new dataset.

You can count target pixels with a Boolean expression:

The same logic scales to large raster masks.

For a multi-class mask:

This combines familiar array operations with the output of a deep-learning model.


5. Common pitfalls


6. Mini task

A colleague shows you a segmentation mask array with array.shape == (512, 512) and numpy.unique(array) returning [0, 1, 2, 3, 4]. Without seeing the imagery, what can you conclude about the task, and what would you ask before trusting the mask?

Sample solution

The shape confirms a single-band, 512-by-512 raster, consistent with a class-index mask rather than a probability map (which would have multiple bands). Five distinct values means this is a multi-class mask with five classes (commonly 0 as background plus four target classes, though that assumption should be confirmed). Before trusting it, you would want to know what each integer value actually represents, since without a class-name mapping, “class 3” is meaningless, and whether 0 truly represents background or is itself a valid class in this particular dataset.


7. Key takeaways