Learning Objectives
- Distinguish SAM (visual prompts only), Grounded SAM / SAM 2 (detector + SAM), and SAM 3 (native text prompts via Promptable Concept Segmentation)
- Explain the SAM 3 architecture: shared backbone + image detector + memory-based video tracker + presence head + decoupled detector-tracker design
- Use Hugging Face
transformersSAM 3 integration for text-prompted detection, segmentation, and video tracking - Pick between SAM 3, Grounded SAM 2, YOLO-World, and SAM-MI based on latency, concept complexity, and deployment target
The Problem
The 2023 SAM was a visual-prompt-only model: you click a point or draw a box and it returns a mask. For "give me all the oranges in this photo" you needed a detector (Grounding DINO) to produce boxes, then SAM to segment each. Grounded SAM turned this into a pipeline, but it was a cascade of two frozen models with inevitable error accumulation.
SAM 3 (Meta, Nov 2025, ICLR 2026) collapsed the cascade. It accepts a short noun phrase or an image exemplar as prompt and returns all matching masks and instance IDs in a single forward pass. That is Promptable Concept Segmentation (PCS). Combined with the March 2026 Object Multiplex update (SAM 3.1), it tracks multiple instances of the same concept through video efficiently.
This lesson is about the structural shift this represents. 2D seg, detection, and text-image grounding have merged into one model. The production question is no longer "which pipeline do I chain together" but "which promptable model handles my use case end-to-end."
The Concept
The three generations
flowchart LR
subgraph SAM1["SAM (2023)"]
A1["Image + point/box prompt"] --> A2["ViT encoder"] --> A3["Mask decoder"]
A3 --> A4["Mask for that prompt"]
end
subgraph GSAM2["Grounded SAM 2 (2024)"]
B1["Text"] --> B2["Grounding DINO"] --> B3["Boxes"] --> B4["SAM 2"] --> B5["Masks + tracking"]
B6["Image"] --> B2
B6 --> B4
end
subgraph SAM3["SAM 3 (2025)"]
C1["Text OR image exemplar"] --> C2["Shared backbone"]
C3["Image"] --> C2
C2 --> C4["Image detector + memory tracker<br/>+ presence head"]
C4 --> C5["All matching masks<br/>+ instance IDs"]
end
style SAM1 fill:#e5e7eb,stroke:#6b7280
style GSAM2 fill:#fef3c7,stroke:#d97706
style SAM3 fill:#dcfce7,stroke:#16a34a
Promptable Concept Segmentation
A "concept prompt" is a short noun phrase ("yellow school bus", "striped red umbrella", "hand holding a mug") or an image exemplar. The model returns segmentation masks for every instance in the image that matches the concept, plus a unique instance ID per match.
This differs from classic visual-prompt SAM in three ways:
- No per-instance prompting required — one text prompt returns all matches.
- Open-vocabulary — the concept can be anything describable in natural language.
- Returns multiple instances at once rather than one mask per prompt.
Key architectural pieces
- Shared backbone — a single ViT processes the image. Both the detector head and the memory-based tracker read from it.
- Presence head — predicts whether the concept is present in the image at all. Decouples "is this here?" from "where is it?". Reduces false positives on absent concepts.
- Decoupled detector-tracker — image-level detection and video-level tracking have separate heads so they do not interfere.
- Memory bank — stores per-instance features across frames for video tracking (same mechanism SAM 2 used).
Training at scale
SAM 3 was trained on 4 million unique concepts generated by a data engine that iteratively annotates and corrects using AI + human review. The new SA-CO benchmark contains 270K unique concepts, 50x larger than prior benchmarks. SAM 3 reaches 75-80% of human performance on SA-CO and doubles existing systems on image + video PCS.
SAM 3.1 Object Multiplex
March 2026 update: Object Multiplex introduces a shared-memory mechanism for joint tracking of many instances of the same concept at once. Previously, tracking N instances meant N separate memory banks. Multiplex collapses that into one shared memory with per-instance queries. Result: substantially faster multi-object tracking without sacrificing accuracy.
Where Grounded SAM still matters in 2026
- When you need a specific open-vocabulary detector swapped in (DINO-X, Florence-2).
- When the SAM 3 license (gated on HF) is a blocker.
- When you need more control over the detector threshold than SAM 3 exposes.
- For research / ablation work on the detector component.
Modular pipelines still have a place. For most production work, SAM 3 is the simpler answer.
YOLO-World vs SAM 3
- YOLO-World — open-vocabulary detector only (no masks). Real-time. Best when you need boxes at high fps.
- SAM 3 — full segmentation + tracking. Slower but richer output.
Production split: YOLO-World for fast detection-only pipelines (robotics navigation, fast dashboards), SAM 3 for anything that needs masks or tracking.
SAM-MI efficiency
SAM-MI (2025-2026) addresses SAM's decoder bottleneck. Key ideas:
- Sparse point prompting — uses a few well-chosen points instead of dense prompts; reduces decoder calls by 96%.
- Shallow mask aggregation — merges rough mask predictions into one sharper mask.
- Decoupled mask injection — decoder receives pre-computed mask features instead of re-running.
Result: ~1.6× speedup over Grounded-SAM on open-vocabulary benchmarks.
Output format for the three models
All return the same general structure (boxes + labels + scores + masks + IDs), which is helpful — your pipeline downstream does not have to branch on which model ran.
Interactive figure coming later.
Build It
Step 1: Prompt construction
Build a helper that turns a user sentence into a list of SAM 3 concept prompts. This is the boundary where "what the user typed" meets "what the model consumes".
def split_concepts(sentence):
"""
Heuristic splitter for multi-concept prompts.
Returns list of short noun phrases.
"""
for sep in [",", ";", "and", "or", "&"]:
if sep in sentence:
parts = [p.strip() for p in sentence.replace("and ", ",").split(",")]
return [p for p in parts if p]
return [sentence.strip()]
print(split_concepts("cats, dogs and balloons"))
SAM 3 accepts one concept per forward pass; for multi-concept queries, loop or batch them.
Step 2: Post-processing helpers
Turn SAM 3's raw outputs into a clean list of detections that match our Phase 4 Lesson 16 pipeline contract.
from dataclasses import dataclass
from typing import List
@dataclass
class ConceptDetection:
concept: str
instance_id: int
box: tuple # (x1, y1, x2, y2)
score: float
mask_rle: str # run-length encoded
def rle_encode(binary_mask):
flat = binary_mask.flatten().astype("uint8")
runs = []
prev, count = flat[0], 0
for v in flat:
if v == prev:
count += 1
else:
runs.append((int(prev), count))
prev, count = v, 1
runs.append((int(prev), count))
return ";".join(f"{v}x{c}" for v, c in runs)
RLE keeps response payloads small even for many high-resolution masks. The same format works across SAM 2, SAM 3, Grounded SAM 2.
Step 3: A unified open-vocab segmentation interface
Wrap whatever backend you have (SAM 3, Grounded SAM 2, YOLO-World + SAM 2) behind a single method. Your downstream code does not change when the backend does.
from abc import ABC, abstractmethod
import numpy as np
class OpenVocabSeg(ABC):
@abstractmethod
def detect(self, image: np.ndarray, concept: str) -> List[ConceptDetection]:
...
class StubOpenVocabSeg(OpenVocabSeg):
"""
Deterministic stub used for pipeline testing when real models are not loaded.
"""
def detect(self, image, concept):
h, w = image.shape[:2]
return [
ConceptDetection(
concept=concept,
instance_id=0,
box=(w * 0.2, h * 0.3, w * 0.5, h * 0.8),
score=0.89,
mask_rle="0x100;1x50;0x200",
),
ConceptDetection(
concept=concept,
instance_id=1,
box=(w * 0.55, h * 0.25, w * 0.85, h * 0.75),
score=0.74,
mask_rle="0x80;1x40;0x220",
),
]
The real SAM3OpenVocabSeg subclass would wrap transformers.Sam3Model and Sam3Processor.
Step 4: Hugging Face SAM 3 usage (reference)
For the actual model, the transformers integration:
from transformers import Sam3Processor, Sam3Model
import torch
processor = Sam3Processor.from_pretrained("facebook/sam3")
model = Sam3Model.from_pretrained("facebook/sam3").eval()
inputs = processor(images=pil_image, return_tensors="pt")
inputs = processor.set_text_prompt(inputs, "yellow school bus")
with torch.no_grad():
outputs = model(**inputs)
masks = processor.post_process_masks(
outputs.masks, inputs.original_sizes, inputs.reshaped_input_sizes
)
boxes = outputs.boxes
scores = outputs.scores
One prompt, all matches returned in a single call.
Step 5: Measure what Grounded SAM 2 gave you for free
An honest benchmark: what happens when you replace Grounded SAM 2 with SAM 3 in a real pipeline?
- Latency: SAM 3 saves one forward pass (no separate detector) but the model itself is heavier; usually net-neutral or a slight speedup.
- Accuracy: SAM 3 substantially better on rare or compositional concepts ("striped red umbrella"). Similar on common single-word concepts.
- Flexibility: Grounded SAM 2 lets you swap detectors (DINO-X, Florence-2, Grounding DINO 1.5); SAM 3 is monolithic.
Conclusion: SAM 3 is the default for 2026 open-vocab seg. Grounded SAM 2 is still the right answer when you need detector flexibility or different license terms.
Use It
Production deployment patterns:
- Real-time annotation — SAM 3 + CVAT's label-as-text-prompt feature. Annotators select a label name; SAM 3 pre-labels every matching instance. Review and correct.
- Video analytics — SAM 3.1 Object Multiplex for multi-object tracking; feed frames to the memory-based tracker.
- Robotics — SAM 3 for open-vocab manipulation ("pick up the red cup"); runs as a planning primitive.
- Medical imaging — SAM 3 fine-tuned on medical concepts; requires access request on HF.
Ultralytics wraps SAM 3 in its Python package:
from ultralytics import SAM
model = SAM("sam3.pt")
results = model(image_path, prompts="yellow school bus")
Same interface as YOLO and SAM 2.
Ship It
This lesson produces:
outputs/prompt-open-vocab-stack-picker.md— a prompt that picks SAM 3 / Grounded SAM 2 / YOLO-World / SAM-MI based on latency, concept complexity, and licensing.outputs/skill-concept-prompt-designer.md— a skill that turns user utterances into well-formed SAM 3 concept prompts (splitting, disambiguation, fallbacks).
Exercises
- (Easy) Run SAM 3 on 10 images with concept prompts you choose. Compare against SAM 2 + Grounding DINO 1.5 on the same images. Report which concepts each model missed.
- (Medium) Build a "click-to-include / click-to-exclude" UI on top of SAM 3: a text prompt returns candidate instances; user clicks keep which ones count as positive. Output the final concept set as JSON.
- (Hard) Fine-tune SAM 3 on a custom concept set (e.g. 5 types of electronic components) with 20 labelled images each. Compare to zero-shot SAM 3 on the same test set; measure mask IoU improvement.
Key Terms
| Term | What people say | What it actually means |
|---|---|---|
| Open-vocabulary segmentation | "Segment by text" | Produce masks for objects described in natural language, not a fixed label set |
| PCS | "Promptable Concept Segmentation" | SAM 3's core task — given a noun-phrase or image exemplar, segment all matching instances |
| Concept prompt | "The text input" | Short noun phrase or image exemplar; not a full sentence |
| Presence head | "Is it here?" | SAM 3 module that decides whether the concept exists in the image before localisation |
| SA-CO | "SAM 3 benchmark" | 270K-concept open-vocabulary segmentation benchmark; 50x larger than prior open-vocab benchmarks |
| Object Multiplex | "SAM 3.1 update" | Shared-memory multi-object tracking; fast joint tracking of many instances |
| Grounded SAM 2 | "Modular pipeline" | Detector + SAM 2 cascade; still relevant when detector swap matters |
| SAM-MI | "Efficient SAM variant" | Mask Injection for 1.6x speedup over Grounded-SAM |
Further Reading
Code
main.py
python phases/04-computer-vision/24-sam3-open-vocab-segmentation/code/main.pyOpen on GitHub ↗
Take-away files
prompt-open-vocab-stack-picker.md
---
name: prompt-open-vocab-stack-picker
description: Pick SAM 3 / Grounded SAM 2 / YOLO-World / SAM-MI based on latency, concept complexity, and licensing
phase: 4
lesson: 24
---
You are an open-vocabulary vision stack selector.
## Inputs
- `task_output`: masks | boxes | tracking_over_video
- `concept_complexity`: single_word | short_phrase | compositional
- `latency_target_ms`: p95 per frame
- `license_need`: permissive | commercial_ok | research_ok
- `deployment`: cloud_gpu | edge | browser
## Decision
Rules fire top-down; first match wins. License constraints act as hard filters — if a rule's default model violates the caller's `license_need`, skip to the next rule rather than overriding.
1. `task_output == boxes` and `latency_target_ms <= 50` -> **YOLO-World** (or OV-DINO).
2. `task_output == masks` and `concept_complexity == compositional` -> **SAM 3** (PCS handles descriptive prompts best).
3. `task_output == masks` and `license_need == permissive` -> **Grounded SAM 2** with Apache-licensed detector (Florence-2 / Grounding DINO 1.5).
4. `task_output == tracking_over_video` with many instances -> **SAM 3.1 Object Multiplex**.
5. `deployment == edge` and `task_output == masks` -> **SAM-MI** or MobileSAM + lightweight open-vocab detector.
6. `deployment == browser` -> YOLO-World ONNX + MobileSAM or an edge distilled variant.
## Output
```
[stack]
model: <name>
backend: <transformers / ultralytics / mmseg>
precision: float16 | bfloat16 | int8
[pipeline]
1. <preprocess>
2. <inference>
3. <postprocess (NMS, RLE encode, tracking association)>
[expected latency]
p50 / p95 estimates for target hardware
[caveats]
- license notes
- concept-set limitations
- known failure modes
```
## Rules
- If `concept_complexity == compositional` ("striped red umbrella", "hand holding a mug"), favour SAM 3 over YOLO-World; open-vocab detectors struggle with descriptive modifiers.
- If the dataset is domain-specific (medical, satellite, industrial defect), recommend Grounded SAM 2 with a domain-tuned detector; SAM 3 may not have seen the concepts at scale.
- For production at <100ms p95, require INT8 or FP16; never ship FP32 on edge.
- For SAM 3, always note the HF access-request gate on the checkpoint.
skill-concept-prompt-designer.md
--- name: skill-concept-prompt-designer description: Turn user utterances into well-formed SAM 3 concept prompts with splitting, disambiguation, and fallbacks version: 1.0.0 phase: 4 lesson: 24 tags: [sam3, open-vocab, prompt-engineering, segmentation] --- # Concept Prompt Designer SAM 3's accuracy depends heavily on how the concept prompt is phrased. This skill normalises free-form user utterances into prompts that SAM 3 handles well. ## When to use - Building a UI that accepts natural-language object queries. - Exposing SAM 3 through an API where upstream callers send sentences. - Debugging poor SAM 3 matches — often the prompt is malformed, not the model. ## Inputs - `utterance`: raw user string. - `context`: optional domain hint (e.g. "surveillance", "medical", "retail"). - `max_concepts`: maximum concepts to extract per utterance; default 5. ## Rules SAM 3 prefers - **Short noun phrases, not sentences.** `"cat"` wins over `"there is a cat"`. - **Concrete nouns.** `"skateboard"` wins over `"thing to ride on"`. - **Modifiers immediately before the noun.** `"red car"` wins over `"car that is red"`. - **Lowercase.** SAM 3 is robust but empirically slightly better on lowercase inputs. - **Singular or plural.** Both work; plural helps when multiple instances are expected. ## Steps 1. **Tokenise by common separators** — comma, semicolon, "and", "or", "&". 2. **Drop filler prefixes** — "find", "show me", "segment", "detect", "locate", "a", "an", "the". 3. **Keep prepositional modifiers** only if they are visual — `"striped red umbrella"` yes, `"umbrella from yesterday"` no (the `"from yesterday"` is not in-image). 4. **Disambiguate collisions** using the optional `context`: - `"window"` in surveillance context -> `"building window"`. - `"window"` in medical context -> often error; suggest user clarify. 5. **Fallback** to the exact string if splitting yields zero concepts *and* the utterance contains at least one concrete noun. If no concrete noun can be extracted, do not emit a concept — return only warnings and ask the user to clarify (see Rules). 6. **Cap at `max_concepts`.** If more concepts were extracted than the caller asked for, keep the first `max_concepts` in utterance order and emit the rest under `dropped` with reason `"exceeded max_concepts"`. This keeps latency bounded when a user pastes a long enumeration. ## Output format ``` [designed prompts] utterance: <original> concepts: ["concept_1", "concept_2", ...] dropped: ["filler_1", ...] warnings: ["concept too abstract", "may match many classes", ...] [sam3 calls] For each concept run: sam3.detect(image, concept) Merge outputs with distinct concept tags per detection. ``` ## Examples ``` in: "can you find me a cat or two dogs?" out: ["cat", "dogs"] dropped: ["can you find me", "a", "or two", "?"] note: "dogs" kept plural because the utterance says "two dogs" — plural hint preserved. in: "segment the big red truck and the blue sedan" out: ["big red truck", "blue sedan"] dropped: ["segment", "the", "and"] in: "thing near the door" out: ["door"] warnings: ["'thing' is too abstract for SAM 3; fell back to 'door'"] in: "striped red umbrella, green hat, pink balloon" out: ["striped red umbrella", "green hat", "pink balloon"] ``` ## Rules - Never pass sentences longer than 8 words to SAM 3 — accuracy drops above that. - When an utterance contains no extractable concrete nouns, do not run SAM 3; return the warnings and ask for clarification. - Do not split on punctuation inside quoted strings; preserve `"black and white cat"` as one concept if it is quoted. - Always log the original utterance and the derived concepts for production debugging.