DL0178 SAM: Promptable Segmentation

How does SAM (Segment Anything Model) use promptable Vision Transformer encoders for zero-shot object segmentation, and what makes its prompt interface generalizable to unseen object categories?

Answer

SAM splits segmentation into one expensive image step and a very cheap prompt step. A ViT-H image encoder (MAE-pretrained, about 632M parameters) takes a 1024×1024 image, converts it into 64 \times 64 patch tokens, and emits a single image embedding of shape 64 \times 64 \times 256 that is computed once and cached. A tiny prompt encoder maps whatever the user supplies (clicks, a box, a coarse mask, text) into 256-dimensional tokens, and a two-block two-way cross-attention mask decoder reads the cached embedding together with those tokens to produce three mask logits plus three predicted IoU scores in roughly 50 ms. Generalization to unseen categories comes from the task definition rather than from any category machinery: the objective is “return a valid mask for this prompt”, there is no classification head and no fixed label set, so the model only ever learns a geometry-to-region mapping, supervised by 1.1B masks over 11M images (SA-1B) that a model-in-the-loop data engine collected without class labels. The one thing a category-free interface cannot resolve is ambiguity, and SAM handles it by predicting three nested candidates and back-propagating only the lowest-loss one.

(1) Asymmetric Compute: the ViT encoder runs once per image (about 0.15 s on a GPU) while the decoder runs once per prompt (about 50 ms, light enough for a browser), which is what makes interactive clicking feasible.
(2) Prompts Become Tokens: points and boxes are Fourier positional encodings of their coordinates plus a learned type embedding; a coarse mask prompt is instead embedded by a small convolutional stack and added elementwise to the image embedding.
(3) Two-Way Attention Decoder: prompt tokens attend to image features and image features attend back to the tokens, so a handful of tokens can reshape a dense 4096-token feature map at negligible cost.
(4) Dynamic Mask Head: each mask token is turned by an MLP into the weights of a linear classifier that is dotted with the 4x-upscaled embedding, so the mask is produced by a per-prompt hypernetwork rather than by fixed per-class filters.
(5) Ambiguity-Aware Output: three masks (subpart, part, whole) with a min-over-masks loss and an IoU-prediction token for ranking, which prevents the averaging that destroys single-output models on ambiguous clicks.
(6) Composable Interface: because the prompt is only geometry, any upstream module can drive SAM (a detector’s box, a CLIP text embedding, or a regular grid of points for fully automatic mask generation) with no retraining.

Architecture diagram showing an input image passing through a ViT-H encoder into a cached 64 by 64 by 256 image embedding on the top row, prompts passing through a small prompt encoder into prompt tokens plus three mask tokens and one IoU token on the bottom row, a dashed path where a dense mask prompt is convolved and added to the image embedding, both paths feeding a lightweight two-block two-way cross-attention mask decoder, which outputs three mask logits and three predicted IoU scores

Figure 1: The whole design is an amortization argument. Everything expensive is a function of the image alone and is cached; everything that depends on the prompt is a few million parameters of decoder. Note the absence of any classification branch: the output is a region and a quality score, never a category.

The reason this interface transfers to categories that never appeared in training is that the prompt carries no semantics at all. A click is a coordinate, a box is two coordinates, and the encoder was pretrained to represent generic visual structure, so segmenting a species of coral or an industrial part reuses exactly the same computation as segmenting a cat. What must be learned instead is the notion of “object-ness at the scale the prompt implies”, and SA-1B provides that at massive scale precisely because its data engine was label-free: annotators clicked regions, then a partially trained SAM proposed masks that annotators only corrected, then the final stage generated masks automatically from a 32 \times 32 point grid with NMS and stability filtering. Ambiguity is the residual difficulty. A single click on a shirt pocket is consistent with the pocket, the shirt, and the person, so a single-output model trained with an averaged loss learns a blurred compromise. SAM emits three masks and only the one with the lowest loss receives gradient, which forces the three slots to specialize by scale.

Three side-by-side stylized figures of a person wearing a shirt with a pocket, each marked by the same star-shaped point prompt on the pocket; the first panel highlights only the pocket as the subpart mask, the second highlights the shirt as the part mask, the third highlights the entire person as the whole mask, each labeled with a different predicted IoU score

Figure 2: One prompt, three valid answers. The min-over-masks loss back-propagates only through whichever slot best matches the ground-truth region, so slot 1 drifts toward subparts, slot 2 toward parts, and slot 3 toward whole objects. The IoU-prediction token then supplies the ranking the interface needs when no human is in the loop.

Mathematical Formulation:
F = E_{\mathrm{img}}(I) + \mathrm{conv}(m)
p_k = \mathrm{PE}(x_k, y_k) + t_{\mathrm{type}(k)}
P = (p_1, \ldots, p_{N_p}, o_1, o_2, o_3, o_{\mathrm{iou}})
(Z, H) = \mathrm{Dec}(F, P)
M_j = \langle \mathrm{MLP}_j(H), \mathrm{Up}_4(Z) \rangle
\mathcal{L}_j = 20\,\ell_{\mathrm{focal}}(M_j) + \ell_{\mathrm{dice}}(M_j)
\mathcal{L} = \min_{j \in \{1,2,3\}} \mathcal{L}_j

Where:

  • F \in \mathbb{R}^{64 \times 64 \times 256} is the cached image embedding, E_{\mathrm{img}} the ViT-H encoder applied to the 1024×1024 image I, and m an optional coarse mask prompt whose convolutional embedding is added elementwise.
  • p_k is the token for sparse prompt k, built from a random Fourier positional encoding \mathrm{PE} of its coordinates plus a learned embedding t_{\mathrm{type}} for foreground point, background point, box corner, or text.
  • P is the decoder’s token sequence: N_p prompt tokens plus three learned mask tokens o_j and one IoU token o_{\mathrm{iou}}.
  • Z is the updated image feature map and H the updated tokens after two blocks of two-way cross-attention; \mathrm{Up}_4 is a pair of transposed convolutions giving a 256 \times 256 map.
  • M_j is mask j‘s logit map, formed by dotting the dynamically predicted weights \mathrm{MLP}_j(H) with every spatial location of \mathrm{Up}_4(Z), then bilinearly resized to 1024×1024.
  • \ell_{\mathrm{focal}} and \ell_{\mathrm{dice}} are combined in a 20:1 ratio, and only the argmin slot receives gradient; the IoU head is trained separately with an MSE loss against the realized IoU.

Latency Accounting For K Prompts On One Image:
C_{\mathrm{enc}} = O(N^2 d), \quad N = 4096
C_{\mathrm{dec}} = O(N_t N d)
T(K) = t_{\mathrm{enc}} + K \, t_{\mathrm{dec}}
T(32) = 150\ \mathrm{ms} + 32 \times 50\ \mathrm{ms}
T(32) = 1750\ \mathrm{ms}

The quadratic term belongs entirely to the encoder over N = 4096 tokens, while the decoder is linear in the N_t \approx 8 tokens it carries, which is why 32 clicks cost 1.75 s with caching but 6.4 s if the ViT is re-run each time. Automatic mask generation exploits the same asymmetry: a 32 \times 32 grid of 1,024 point prompts, run in batches against one cached embedding, yields a full mask inventory for the image without ever re-encoding it.

Line chart of total latency in milliseconds versus number of prompts on a single image, comparing a caching design that pays 150 milliseconds once plus 50 milliseconds per prompt against a design that re-runs the encoder for every prompt at 200 milliseconds each, with an annotation marking 1.75 seconds versus 6.4 seconds at 32 prompts

Figure 3: Why the encoder/decoder split is the architecture, not an optimization. Caching turns the marginal cost of a prompt from 200 ms into 50 ms, and the gap widens with every additional click, which is exactly the regime an interactive annotation tool or a 1,024-point automatic sweep lives in.

PropertyPoint clicksBoxCoarse maskText
Encoding pathSparse: Fourier positional encoding plus fg/bg type embeddingSparse: two corner tokens with corner-specific embeddingsDense: conv stack added elementwise to the image embeddingSparse: a CLIP text embedding used as one token
What it pins downLocation only, so scale stays ambiguousLocation and extent, which resolves most ambiguityFull spatial support, used for iterative refinementSemantics with no localization
Typical sourceHuman clicks, or a 32×32 grid for automatic generationAn upstream detector such as Grounding DINOSAM’s own previous output in a refinement loopA user query in the exploratory prototype
Main failure modeWrong scale slot chosen when no human ranks the three masksLoose or overlapping boxes leak into neighboring instancesErrors in the input mask are reinforced across iterationsWeakest of the four; not a released production interface

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *