liveBook · Manning

SAM

As explained in Deep Learning with PyTorch, Second Edition

A transformer-based image segmentation model designed to generalize to many objects and support prompt-driven, zero-shot mask prediction.

A classifier can decide whether a proposed CT-scan region is a nodule, but it cannot find candidate regions on its own. Segmentation helps with that earlier step by marking individual pixels. SAM is a pretrained, transformer-based model that generates segmentation masks, either automatically or with a prompt indicating what to select. section 15.0 section 15.1 section 15.3.1

Three components turn an image and an optional prompt into a mask:

  • The image encoder extracts features from image patches using a Vision Transformer. section 15.4
  • The prompt encoder represents points, boxes, or masks numerically. section 15.4
  • The mask decoder combines image features and prompt information to predict regions. For an ambiguous prompt, SAM can return three masks of different sizes with scores. section 15.4

Automatic masks versus a pointed-at object

With pretrained weights, SamAutomaticMaskGenerator produced 79 masks for an example image. Each mask was an image-sized binary array: True marked pixels belonging to a selected region. When the goal is a particular region instead, SamPredictor accepts an image and a positive point prompt; a point on a dog’s nose yielded masks associated with that point. section 15.4.1 section 15.5

image_path = "data/p1ch2/bobby.jpg"
image = Image.open(image_path).convert("RGB")
input_points = [[(320, 260)]]

input_points = np.array([(320, 260)])
predictor.set_image(np.array(image))
masks, _, _ = predictor.predict(input_points, point_labels=np.array([1]))

The point-prompt code shows the practical sequence: set the image, then call predict with point coordinates and a positive point label. section 15.5

For CT scans, a reference point can guide SAM toward a candidate nodule mask on a 2D slice without first training it on that specific task. That is distinct from the later task-specific training step: the nodule segmentation model fine-tuned for that step is SegFormer, not SAM. section 15.3.1 section 15.9

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains the three components and works through pretrained inference, but does not implement SAM from scratch.
Examples
Demonstrates automatic masks on an image, a point-guided dog-nose mask, and candidate nodule masks on CT images.
What it assumes
Introduces the segmentation workflow without requiring the earlier CT-classifier work, though that work provides context.
What you can do after
Apply it via the Segment Anything library to generate automatic masks or point-prompted masks with pretrained weights.
How it connects
Connects image segmentation and point prompts to Vision Transformers, mask prediction, CT scans, classification, and SegFormer.

Also explained in

Compare how 3 books explain SAM →

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.