SAM (Segment Anything Model) takes an image plus a prompt and returns one or more pixel-level masks.
This is a short recap from lesson 4.1.5 because SAM is useful inside many vision applications.
Chapter 4.1 introduced SAM as an architecture. This chapter is about applications, and SAM shows up in almost all of them for one reason: it is the only model in the module that produces exact pixels.
Every other model gives you something approximate. CLIP gives a score, LLaVA gives a sentence, Qwen-VL gives a rectangle. When an application needs to actually cut something out, measure its area, or overlay it precisely, none of those are sufficient — and SAM gets called in to finish the job.
So treat this lesson as the reference page you will keep returning to as the later applications compose SAM with other models.
Think of selecting an object in a photo editor:
That interaction contains the whole design. Step 1 happens once and is expensive. Steps 2 to 4 happen many times and must feel instant. SAM's architecture is shaped around exactly that split, which is why the cost table below matters more than any other detail in this lesson.
Older segmentation models were tied to one dataset or object type. SAM was trained as a promptable foundation model, so the prompt — not a fixed label list — decides which region matters.
| Component | Plain job | How often it runs |
|---|---|---|
| Image encoder | Heavy ViT turns the whole image into a reusable embedding | Once per image |
| Prompt encoder | Turns a point, box, or rough mask into prompt features | For every prompt |
| Mask decoder | Combines image and prompt features to predict masks | For every prompt |
The reported architecture used a heavy MAE-pretrained ViT-H/16. Encoding an image took about 0.15 seconds on the reported hardware; the reusable decoder path took about 50 milliseconds. Exact speed changes with hardware, but the design lesson stays the same: heavy once, cheap many times.
Suppose a user clicks a flower, then clicks again to remove a leaf:
The expensive image encoder does not need to start over.
# Concept only: the important idea is what gets reused.
image_embedding = sam.encode_image(image) # expensive — run once
flower_mask = sam.decode(image_embedding, point=(420, 260))
refined_mask = sam.decode(
image_embedding,
positive_points=[(420, 260)],
negative_points=[(515, 300)],
)
| Prompt | How SAM reads it | Typical use |
|---|---|---|
| Foreground/background point | Position + learned point-type embedding | Fast interactive selection |
| Box | Positions of two corners | Precise single-object selection |
| Rough mask | Small convolutional encoder | Refine an existing mask |
| Text | Experimental CLIP-based path | Language-driven selection |
Points and boxes are the main reliable prompt types in the original SAM story.
Inside the mask decoder, information moves in both directions:
It is a short conversation between the prompt and the image, not a one-way lookup.
A point on a shirt is ambiguous. It could mean:
SAM predicts three candidate masks instead of pretending one interpretation is certainly correct. An extra head predicts a quality score for each mask.
A mask contains many easy background pixels and relatively few object pixels. A plain pixel loss can therefore learn to say “background” too often.
SAM combines:
The lecture’s mask-loss weighting is:
Mask loss = 20 × Focal loss + 1 × Dice loss
Dice loss is based on:
Dice loss = 1 − 2 × overlap / (predicted area + true area)
A separate MSE loss trains the mask-quality (IoU) prediction head.
IoU = intersection area / union area
1.0 → perfect overlap0.5 → only half-overlapping in a rough sense0.0 → no overlap| Stage | Human and model roles |
|---|---|
| Assisted manual | People label masks with SAM’s help; SAM is retrained |
| Semi-automatic | SAM proposes confident masks; people add missed objects |
| Fully automatic | A 32 × 32 point grid prompts SAM; stable masks are kept and duplicates removed |
The result described in the lecture: about 11 million licensed images and 1.1 billion masks — roughly 100 masks per image.
SAM is useful for background removal, medical annotation, robotics, rotoscoping, AR, and geospatial mapping.
For language + exact pixels, compose models:
This is the Grounded-SAM pattern: one model understands and locates; SAM refines the pixels.
SAM encodes an image once, then cheaply turns points, boxes, or masks into several pixel-level candidates with quality scores.