This is the final lecture of Volume 07. We began with LeNet reading digits and end with a model that can segment anything. SAM (Segment Anything Model, Meta AI, 2023) is a promptable, foundation-scale segmentation model: click a point, draw a box, or supply a mask, and it returns high-quality object masks—even for categories it never explicitly trained on. It fuses everything this volume taught: convolutional intuition, ViT backbones, and the promptable spirit of CLIP.
Learning Objectives
By the end of this lesson, students should be able to:
- Explain what makes SAM a “promptable” foundation model.
- Describe its three components: image encoder, prompt encoder, mask decoder.
- Explain the role of the SA-1B dataset (1B masks) in its generality.
- Perform point- and box-prompted segmentation with the
segment_anythingAPI. - Contrast SAM with task-specific models like Mask R-CNN.
- Synthesize the full Vol 07 arc from LeNet to SAM.
SAM is a promptable segmentation model with three parts: a heavy ViT image encoder (run once per image), a lightweight prompt encoder (points, boxes, or masks), and a fast mask decoder that combines the two to output masks in milliseconds. Because the image is encoded once, many prompts can be answered interactively in real time.
Why SAM Generalizes
SAM was trained on SA-1B—over 1 billion masks across 11 million images—built with a data engine where SAM itself helped annotate. This scale gives it a strong notion of “objectness” that transfers zero-shot to new domains: medical scans, satellite imagery, microscopy. It segments based on prompts and visual structure, not a fixed class list.
| Aspect | Mask R-CNN | SAM |
|---|---|---|
| Backbone | ResNet + FPN | ViT image encoder |
| Output | Masks for trained classes | Masks for any prompted object |
| Interaction | None (fixed) | Points, boxes, masks (promptable) |
| Generalization | Trained categories | Zero-shot to new domains |
| Year | 2017 | 2023 |
Running SAM in PyTorch
Reality: SAM produces masks but does not assign semantic class names. Pair it with a classifier or CLIP to label the masked regions—SAM answers “where is the object,” not “what is it.”
The image encoder is the expensive part; the mask decoder is cheap. Call set_image() once, then issue many predict() prompts against that cached encoding. Re-running set_image() for every click throws away SAM’s real-time interactivity.
Knowledge Check
- Short Answer: What are SAM’s three main components? Answer: Image encoder, prompt encoder, mask decoder.
- True/False: SAM can segment objects from a single clicked point. Answer: True.
- Multiple Choice: SAM’s image encoder is a: (a) LSTM, (b) ViT, (c) LeNet, (d) decision tree. Answer: (b).
- Short Answer: What dataset was SAM trained on? Answer: SA-1B (~1 billion masks).
- True/False: SAM assigns class labels to the masks it produces. Answer: False—it outputs masks, not labels.
- Multiple Choice: Which step is the expensive one to run once per image? (a) prompt encoder, (b) mask decoder, (c) image encoder, (d) NMS. Answer: (c).
- Short Answer: Name a prompt type SAM accepts. Answer: Point, box, or mask.
- True/False: SAM generalizes zero-shot to new domains like medical imaging. Answer: True.
- Multiple Choice: Compared to Mask R-CNN, SAM is: (a) class-fixed, (b) promptable and open-domain, (c) slower to prompt, (d) CNN-only. Answer: (b).
- Short Answer: How would you get semantic labels for SAM’s masks? Answer: Pair it with a classifier or CLIP.
Key Takeaways
- SAM is a promptable segmentation foundation model: point/box/mask → masks.
- Encode the image once with a ViT; answer many prompts fast via the decoder.
- Trained on SA-1B (~1B masks), it generalizes zero-shot across domains.
- It outputs masks without labels—combine with CLIP/classifiers for names.
- Volume 07 complete: from LeNet’s digits to universal, promptable vision.
Capstone project: Build an interactive tool—click a point, SAM returns a mask, then CLIP labels the masked region. Students combine three lectures into one working pipeline.
Synthesis discussion: Trace the volume’s arc: convolution (7.1) → tasks (7.2) → models (7.3). Ask students to place each model on a timeline and name the single advance it contributed.