liveBook · Manning

Segmentation mask

As explained in Deep Learning with Python, Third Edition

An image-shaped target annotation for segmentation where each pixel stores the class or region of the corresponding input pixel.

A segmentation mask turns an image-level recognition problem into a pixel-level target: each input pixel receives a class or region value. The mask keeps the input image’s spatial dimensions, uses one channel, and stores integer labels rather than display colors. section 11.2.1 section 11.4

A three-class pet mask

In the Oxford-IIIT Pets data, a photograph has a matching PNG mask with the same filename. Each mask pixel stores 1 for foreground, 2 for background, or 3 for contour. Loading it in grayscale preserves these labels; preprocessing can subtract 1 to create zero-based targets. section 11.2.1

When preparing masks for a model, keep these practical rules in view:

  • Pair each input image with its corresponding mask path, commonly using matching filenames in separate image and annotation directories. section 11.2.1
  • Use a single-channel integer array for the target, such as a batch shaped for height, width, and one channel. section 11.2.1
  • Treat mask values as labels, not ordinary image intensities; visualization may remap them to contrasting colors for inspection. section 11.2.1

Masks can also describe individual objects rather than fixed semantic classes. Segment Anything accepts an image with a point or box prompt and predicts candidate masks for the indicated object, which can then be compared with a ground-truth mask. section 11.3.2 section 11.3.4

def display_target(target_array):
    normalized_array = (target_array.astype("uint8") - 1) * 127       #1
    plt.axis("off")
    plt.imshow(normalized_array[:, :, 0])

img = img_to_array(load_img(target_paths[9], color_mode="grayscale")) #2
display_target(img)

Inspect a target mask and its label encoding with a small grayscale-display utility. section 11.2.1

import numpy as np
import random

img_size = (200, 200)                                                #1
num_imgs = len(input_img_paths)                                      #2

random.Random(1337).shuffle(input_img_paths)                         #3
random.Random(1337).shuffle(target_paths)                             #3

def path_to_input_image(path):
    return img_to_array(load_img(path, target_size=img_size))

def path_to_target(path):
    img = img_to_array(
        load_img(path, target_size=img_size, color_mode="grayscale")
    )
    img = img.astype("uint8") - 1                                    #4
    return img

input_imgs = np.zeros((num_imgs,) + img_size + (3,), dtype="float32") #5
targets = np.zeros((num_imgs,) + img_size + (1,), dtype="uint8")       #5
for i in range(num_imgs):                                              #5
    input_imgs[i] = path_to_input_image(input_img_paths[i])            #5
    targets[i] = path_to_target(target_paths[i])                     #5

Follow the NumPy preparation pipeline that resizes inputs and stores masks as single-channel integer targets. section 11.2.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains label values, file pairing, array shapes, visualization, and preprocessing, but defers detailed mask-decoder internals.
Examples
Uses Oxford-IIIT Pets masks, three integer labels, fruit-object prompts, candidate masks, and overlaid visualizations.
What you can do after
Build it from scratch by preparing single-channel targets and training an encoder-decoder model to reconstruct input-sized masks.
How it connects
Connects pixel-level image segmentation with ground-truth labels, point prompting, box prompting, and Conv2DTranspose decoders.

Also explained in

Compare how 4 books explain segmentation mask →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.