liveBook · Manning

Chapter 15: Using segmentation to find suspected nodules

What you will learn in chapter 15 of Deep Learning with PyTorch, Second Edition

The introduction explains why the existing nodule classifier needs an automatic way to find candidates in CT scans and places that need within a multistage project design.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why the existing nodule classifier needs an automatic way to find candidates in CT scans and places that need within a multistage project design.

Key terms: CT scan candidates, classifier, hand-annotated nodule candidate information, annotations.csv, candidates.csv, multiple steps, multistage project design, end-to-end

15.1 Utilizing a second model in our project

This section explains why the lung cancer detection project adds segmentation before classification and why it uses 2D CT slices.

Key terms: segmentation, voxels, classification, nodule, 2D slices, 3D, CT scan

15.2 Various types of segmentation

The section defines semantic segmentation for finding nodule candidates and contrasts it with instance segmentation and object detection.

Key terms: semantic segmentation, label mask, heatmap, nodule candidates, instance segmentation, object detection, bounding box

15.3 Semantic segmentation: Per-pixel classification

The section contrasts whole-image classification with segmentation, which preserves where objects appear in an image.

Key terms: classification, segmentation, image-like output, binary flags, convolution, downsampling, receptive field, feature detectors

15.3.1 The Segment Anything model (SAM)

Introduces Segment Anything (SAM) as a broadly applicable segmentation model and explains why its zero-shot capability makes it significant.

Key terms: Segment Anything model, SAM, segmentation masks, prompts, zero-shot segmentation, fine-tuning, foundational model, task-specific training data

15.4 SAM architecture

This section explains SAM’s main components and mask outputs, then highlights practical considerations when reusing an open-source model.

Key terms: image encoder, prompt encoder, mask decoder, embeddings, segmentation mask, ambiguity, output masks, confidence score, whole, part

15.4.1 Trying out an off-the-shelf model for our project

This subsection walks through running pretrained Segment Anything on a sample image and interpreting its automatically generated masks.

Key terms: checkpoint, pretrained weights, SamAutomaticMaskGenerator, masks, binary arrays, scores tensor, confidence scores

15.5 Using the SAM model directly

The section shows how to use a point prompt with SAM to target a specific region, then cautions that results on natural images may not carry over to CT scans.

Key terms: SamAutomaticMaskGenerator, point prompt, nodules, prompt encoder, embedding representation, patches, masks, domain gap, natural images, medical images

15.6 Updating the dataset for segmentation

The section explains why the segmentation dataset must change shape even though its CT scans and annotations remain the same.

Key terms: CT scans, annotation data, 3D data, 2D data

15.6.1 Working around SAM’s limitation on 2D data

The section explains how to use 2D CT slices with SAM despite the scans being 3D, and identifies the data and tradeoffs involved.

Key terms: volumetric 3D images, 2D slices, SAM, areas of interest, slice thickness, slice spacings, nodules, fine-tune, CT scan slices, masks of interest

15.6.2 Building the segmentation dataset

This section explains how to prepare 2D CT slices and their point annotations for a segmentation dataset, including the need for pixel masks and cached slices.

Key terms: annotated points, mask, base SAM, LunaDataset, series UID, xyz coordinates, CT slice, prepcache

15.6.3 Training a model to flag potential candidates

The section explains how to turn CT slices and SAM-generated masks into a dataset for fine-tuning a lighter segmentation model to flag potential nodule candidates.

Key terms: segmentation, classification model, potential candidates, pixel-perfect masks, SAM, point, segmentation masks, ground truth, fine-tuning dataset, SegFormer

15.7 Updating our training for fine-tuning

This section introduces the upcoming changes to the training setup for fine-tuning the new model.

15.7.1 How to fine-tune a model

Explains why the project fine-tunes a pretrained segmentation model and identifies the model and data used to set it up.

Key terms: point prompt, classification model, nodules of interest, fine-tuning, pretrained model, CT scan slices, masks, FineTuningDataset, dataloader, SegFormerForSemanticSegmentation

15.7.2 Using the AdamW optimizer

This section explains how Adam adjusts learning rates and why the project uses AdamW for SegFormer fine-tuning.

Key terms: Adam optimizer, SGD, learning rate, parameter, AdamW, Adam, weight decay, SegFormer, hyperparameters

15.7.3 Designing our training loop

The section explains how to preprocess CT images and masks for Segformer, train and validate the model, and inspect its losses in TensorBoard.

Key terms: CT scan images and masks, SegformerImageProcessor, Segformer, data loader, loss, model weights, epoch, average loss, validation set, eval()

15.7.4 Saving our model

The section explains why to save model parameters rather than the whole model, what a state dictionary contains, and how saved parameters can be loaded for later use.

Key terms: torch.save, parameters, pickle, model.state_dict(), state dictionary, weights, biases, torch.load, model.to(device), optimizer state

15.8 Inference and results

This section shows how to run the trained segmentation model on CT slices and inspect its predicted nodule masks alongside the images and ground-truth masks.

Key terms: inference, CT scan slices, masks, ground truth mask

15.9 Conclusion

The chapter concludes by summarizing how prompted SAM masks, a paired image-and-mask dataset, and a fine-tuned SegFormer model were used to segment suspected nodules in CT scans.

Key terms: Segment Anything model (SAM), segmentation masks, reference point, images, corresponding masks, dataset, SegFormer, fine-tuned, training and validation losses, TensorBoard

15.10 Exercises

15.11 Summary

Concepts explained in this chapter

← Chapter 14: Improving training with metrics and augmentation Chapter 16: Training models on multiple GPUs →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.