liveBook · Manning

Mask

As explained in Deep Learning with PyTorch, Second Edition

Masking is the selective representation, concealment, inclusion, exclusion, or restriction of parts of data or a system according to specified criteria.

A mask marks which parts of data belong to a selected region or group. What happens to those parts depends on the task: an image mask can restrict where edits happen, a segmentation mask can identify an object’s pixels, and a Boolean mask can select samples for a calculation. section 2.2 section 15.4.1 section 13.6.1

For inpainting, picture a digital stencil laid over a photograph. White marks the area a diffusion model may repaint; black protects the rest. The image supplies the starting scene, the text prompt describes the desired change, and the mask says where that change may occur. Original content outside the mask is reinserted during denoising. section 2.2 section 2.2.1

Repaint a horse, not its background

Pass a horse image, a separately prepared mask image, and a prompt asking for a zebra in the same pose and lighting to StableDiffusionInpaintPipeline. The mask limits the requested replacement to the selected area. The pipeline is used as a pretrained tool; its components are not reimplemented for this example. section 2.2.2

For classification metrics, a mask selects samples rather than pixels. Comparing labels and predicted scores with a 0.5 threshold produces negative-label and negative-prediction Boolean masks. Inverting them produces positive masks. These selections let you count correct predictions and average loss separately for each class. The simple inversions rely on the problem having two classes; multiclass or multilabel data needs different logic. section 13.6.1

Listing 13.17 Masking for classification metrics
negLabel_mask = metrics_t[METRICS_LABEL_NDX] <= classificationThreshold
negPred_mask = metrics_t[METRICS_PRED_NDX] <= classificationThreshold

posLabel_mask = ~negLabel_mask
posPred_mask = ~negPred_mask

The comparisons create negative-class masks, and `~` creates their positive-class counterparts. section 13.6.1

In segmentation, a mask describes an image region pixel by pixel. A generated binary mask uses True for pixels belonging to an object and False for those outside it. Segment Anything can generate multiple masks for an image and assign confidence scores; its mask decoder combines image features and prompt information to produce them. section 15.4.1 section 15.4

Outline a suspected nodule

A CT dataset may provide a point marking a candidate nodule when training needs a pixel-level mask. Use the CT slice and point to create a mask, then pair images with masks for segmentation training. After fine-tuning SegFormer, compare its predicted masks on unseen slices with the reference masks. section 15.6.2 section 15.8 section 15.9

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through Boolean metric masks and pretrained image workflows, but does not reimplement the inpainting pipeline or Segment Anything.
Examples
Uses a horse-to-zebra edit, per-class nodule metrics, astronaut segmentation, and masks of nodules in CT slices.
What it assumes
Introduces image-mask colors and binary pixel values while using PyTorch tensors and pretrained model pipelines in examples.
What you can do after
Apply masks via StableDiffusionInpaintPipeline or Segment Anything, and construct Boolean masks for per-class metrics.
How it connects
Connects masks to diffusion inpainting, binary classification metrics, image segmentation, and promptable segmentation.

Also explained in

Compare how 7 books explain mask →

Related concepts

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