liveBook · Manning

SAM

As explained in Deep Learning with Python, Third Edition

A pretrained segmentation model available through KerasHub for prompting-based segmentation tasks.

SAM is a pretrained segmentation model for identifying objects at pixel level from prompts rather than a fixed class list. Its broad capability comes from training on 11 million images, more than 1 billion masks, and diverse natural-image objects. section 11.3 section 11.4

Segmenting an object with a point

Load SAM through KerasHub's ImageSegmenter, provide an image, a foreground point, and its label, then inspect the returned masks. A point on a peach produces a good mask; a point on a banana can produce alternatives, including a mask covering both bananas. section 11.3.1 section 11.3.4

Use SAM with these prompt patterns:

  • Point prompts select an object with foreground points labeled 1; add background points labeled 0 when the intended region is ambiguous. section 11.3.4
  • Box prompts provide approximate top-left and bottom-right corners around the target object; precise boundaries are unnecessary. section 11.3.5
  • Each prompt can return four 256 × 256 candidate masks ranked by predicted match quality. section 11.3.4

SAM combines an image encoder, a prompt encoder, and a mask decoder. The image becomes an embedding, the prompt becomes an embedded vector, and the decoder predicts masks for the indicated object. Its architecture is explained at a high level rather than through encoder or decoder internals. section 11.3.2

import keras_hub

model = keras_hub.models.ImageSegmenter.from_preset("sam_huge_sa1b")

The KerasHub initialization listing shows the shortest path to downloading the sam_huge_sa1b preset and creating an ImageSegmenter. section 11.3.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains the high-level architecture and demonstrates point and box inference, while deferring prompt-encoder and mask-decoder internals.
Examples
Demonstrates peach, banana, and mango segmentation using foreground points, alternative masks, and an approximate box.
What it assumes
Assumes basic computer-vision context while teaching prompted segmentation and mask generation through concrete image examples.
What you can do after
Apply SAM through KerasHub to generate object masks from point or box prompts and inspect alternative predictions.
How it connects
Connects image segmentation with pretrained models, image encoders, prompt encoders, mask decoders, and object detection.

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 Python, Third Edition. The section links point to the parts of the book it draws on.