VerifiedarXiv:2304.0264331 min
Vision · Foundation models

SAM: Segment Anything

A segmentation model that outlines any object from a click or a box.

SAM runs a large image encoder once per image, then turns each prompt into three candidate masks in about 50 milliseconds. Its 1.1 billion training masks came from a loop in which the model labeled images, people corrected it, and the retrained model took over more of the work until it labeled 11 million images on its own.

Explaining the paperSegment AnythingKirillov, Mintun, Ravi, Mao, Rolland, Gustafson, Xiao, Whitehead, Berg, Lo, Dollár, Girshick · Meta AI (FAIR) · ICCV 2023 · arXiv:2304.02643 ↗

Before SAM, a segmentation model was trained on one dataset for a fixed list of classes. SAM is trained on a single task instead: given any prompt, return a valid mask.

Segmentation means labeling, for every pixel in an image, whether it belongs to a given object. The output is a mask: a grid the size of the image, 1 on the object and 0 elsewhere. For most of the 2010s the standard way to get one was to choose a dataset, fix its class list (COCO has 80), train a model for exactly those classes, and repeat for the next dataset. A model trained to find cars and people had no way to outline a coffee mug.

Language models had moved past that pattern. One model is pre-trained on a broad objective (predict the next token) and then pointed at new jobs with a prompt instead of new training. Segment Anything, from Meta's FAIR lab in April 2023, applies that pattern to segmentation. It has three parts designed together: a task (promptable segmentation), a model (SAM, the Segment Anything Model), and a dataset (SA-1B, 1.1 billion masks on 11 million images) produced by a data engine in which the model helped label the data it was trained on.

The page follows the paper's order. First the task and the ambiguity problem it creates, and how three output masks plus a min-over-three loss solve it. Then the model: why it is split into a heavy encoder and a light decoder, what tensors flow through the decoder for one click, what a mask is numerically, and the losses. Then the three-stage data engine, the filters that decided which automatic masks to keep, and the quality audit of SA-1B. Last, the zero-shot results, including the ones where SAM loses.

A foundation model for segmentation

A foundation model is pre-trained once on a broad task and then adapted to many narrower ones. In vision, the best-known example before SAM was CLIP, which trains an image encoder and a text encoder so that a photo and its caption land near each other in one embedding space. After training you can classify images into categories you name in words, with no labeled examples of those categories. The paper calls that zero-shot transfer: evaluating a model on data distributions and tasks it was not trained on, with no fine-tuning.

CLIP names what is in an image; it does not say which pixels belong to it. Segmentation also lacks what made CLIP possible: the web has billions of image-caption pairs and almost no pixel-accurate outlines. The authors therefore set the goal as a promptable model pre-trained on a task broad enough that, afterwards, a new segmentation problem can be solved by choosing the right prompt, for image distributions the model never saw. Section 7 of the paper tests that claim on 23 datasets and five tasks.

The promptable task

A prompt is anything that indicates what to segment: one or more foreground or background points, a rough box, a coarse mask, or, experimentally, a phrase of text. The promptable segmentation task asks for a valid mask for any prompt.

"Valid" is defined for ambiguous prompts. A single click on a shirt pocket could mean the pocket, the shirt, or the person wearing it. The task does not require the model to pick the one the user had in mind; it requires the output to be a reasonable mask for at least one of those interpretations. A pocket-shaped mask is valid, a shirt-shaped mask is valid, and a mask covering half the shirt and part of the background is not.

The same definition serves as the pre-training objective and as the interface for downstream use. For pre-training, each ground-truth mask generates a sequence of simulated prompts (a point inside it, a noisy box around it, corrective clicks) and the model's outputs are scored against that mask. For downstream use, a new problem is solved by generating prompts: a detector's boxes for instance segmentation, a grid of points for "segment everything". The simulated-prompt training comes from interactive segmentation, where a user clicks until the mask is right, but the goal differs. An interactive segmenter needs to be right after enough clicks. SAM needs a valid mask after the first click, because the data engine's last stage prompts it with single points and no human checks each answer.

Three masks per click

Suppose the model outputs one mask per prompt, and training shows it the pocket click paired with the pocket mask a third of the time, the shirt mask a third of the time, and the person mask a third of the time. The per-pixel loss is minimized by predicting the average of the three targets: probability near 1 on the pocket, near 2/3 on the rest of the shirt, near 1/3 on the rest of the person. Thresholded at 0.5 that is the shirt, and the pocket and person are never produced. As a soft map it is a blurred compromise that matches none of the three.

SAM predicts three masks for a single prompt. The paper's reason for three: when a click is ambiguous, the candidate objects are usually nested (subpart inside part inside whole) and the nesting is rarely more than three levels deep. Figure 1 shows the pocket example. The point sits inside all three regions, so all three masks are valid for it.

Figure 1 · one prompt, three valid masks
whole
Drag the foreground point and use the slider to highlight each of the three nested masks (subpart, part, whole), each with its estimated IoU. On the pocket all three contain the point, so all three are valid at once. The confidence values are illustrative; the mechanism is what the figure shows.

Training has one ground-truth mask per prompt and three predictions. SAM computes the loss of each prediction against the ground truth and back-propagates only the smallest:

Lmask=min⁡i ∈ {1,2,3}[ 20 FL(mi,g)+DL(mi,g) ]\mathcal{L}_{\text{mask}} = \min_{i\,\in\,\{1,2,3\}}\Big[\,20\,\mathrm{FL}(m_i, g) + \mathrm{DL}(m_i, g)\,\Big]
(1)

Here mim_i are the three predicted masks, gg is the ground truth, and FL\mathrm{FL} and DL\mathrm{DL} are the focal and dice losses defined in the losses section. If the ground truth is the shirt and the three losses come out 0.90, 0.12 and 0.60, only output 2 receives a gradient, and it moves further toward the shirt. Outputs 1 and 3 are left where they are, so the next time the ground truth is the pocket, one of them is likely to have the lowest loss and be the one that moves. Over many prompts the three outputs divide the interpretations between them. The technique is multiple-choice learning (Guzman-Rivera et al., 2012): an ensemble of outputs covers the plausible answers and only the closest is graded. Summing the loss over all three outputs would pull each of them to the same average mask as a single output.

Figure 2 runs that argument on a toy. Three nested discs of radius 4, 10 and 18 are the possible ground truths for one central click. Each training step samples one of them. Three heads each have 24 free parameters, one logit per ring. With the gradient going only to the lowest-loss head, init A ends with each head matching one disc (best IoU 1.00 for all three targets). Switch to "all heads" and every head converges to the same staircase of probabilities: about 1.00, 0.62 and 0.34 across the three zones at the last step (the exact average of the targets is 1, 2/3 and 1/3; each noisy step moves the values around it). Thresholded at 0.5 that is the part disc for every head, and the subpart (best IoU 0.16) and the whole (0.31) are never produced.

Figure 2 · min-over-three loss vs summed loss
step 900
best IoU per target: subpart 1.00 · part 1.00 · whole 1.00
Press Play or scrub through 900 training steps. The amber disc is the ground truth sampled at that step; a bright ring marks the head that received the gradient. Toggle the loss rule and the initialization. Init B shows a failure of the min rule: one head starts farther from every target than the others, is never selected, and never trains. A toy with per-ring logits, not SAM's network.

Init B is a known weakness of winner-take-all training: if two outputs start closer to every target than the third, the third never has the lowest loss and receives no gradient. Rupprecht et al. (2017) proposed passing a small share of the gradient to the losing outputs to fix it. The SAM paper does not report dead outputs; in the real network the three outputs share the decoder and differ only in their learned tokens and final MLPs, which is a different starting point from three independent heads.

An application that wants a single mask needs a way to choose among the three. SAM adds a small head that predicts, for each mask, its estimated IoU: the overlap the model expects that mask to have with the object it covers (IoU is defined below). The head is trained by regression against the true IoU of each predicted mask, and the mask with the highest estimate is returned. The estimate ranks the model's own candidates; it is not a calibrated probability, and on an image unlike its training data it can be high for a wrong mask.

A fourth output token covers prompts that are not ambiguous. With two points, or a box plus a point, the three outputs would usually be near-duplicates, so whenever more than one prompt is given SAM returns only the mask from this fourth token, trained with an ordinary loss; for a single prompt the fourth mask is never returned.

Encode once, decode per prompt

The authors wanted SAM usable interactively in a web annotation tool: click, see the mask, click again. That requires roughly 50 milliseconds per prompt, and a large vision Transformer on a 1024 x 1024 image takes far longer than that. SAM is split along the line between computation that depends only on the image and computation that depends on the prompt.

The first prompt on a new image pays for the encoder. Every later prompt on the same image reuses the embedding and costs only the decode, so K prompts on one image cost (encoder + 50K) ms in total, or (encoder + 50K)/K ms per prompt. Drag the prompt count in Figure 3 and read the amortized cost; Figure 4 shows the same budget as a timeline.

Figure 3 · amortizing the heavy encoder
8 · 800ms
The image encoder runs once and produces a reusable embedding; each prompt after that is a ∼50\sim 50 ms decode, so the amortized cost per prompt falls toward the 50 ms floor as the prompt count grows. The encoder cost shown is illustrative; the 50 ms decode is from the paper.
Figure 4 · where the 50 ms goes
K = 8
Top row: the first prompt on a fresh image pays for the image encoder once, then a thin ∼50 ms slice for prompt encoder, decoder, mask and IoU^\widehat{\mathrm{IoU}} head. Bottom row: prompts 2 through KK on the same embedding, 50 ms each. Only the 50 ms total is from the paper; the sub-segments inside it are illustrative (the paper publishes no per-block decoder latencies), and the encoder bar is drawn long enough to dominate without claiming a measured ratio. The top row's time axis is compressed so the 50 ms slice stays legible.

This split is also why SAM is not real-time end to end. The 50 ms covers the prompt encoder and decoder. The project's FAQ quotes about 0.15 s for the ViT-H encoder on an NVIDIA A100 GPU; that number is not in the paper, and the paper's own limitations section says SAM's overall performance is not real-time with a heavy image encoder.

Inside the mask decoder

Figure 5 steps through one foreground click with the shapes of the released ViT-H model. The decoder works on tokens, a short list of 256-dimensional vectors. For one click there are 7: the click itself, a padding point the code appends when no box is given, four learned mask tokens (one per possible output mask) and one learned IoU token. The image embedding is treated as a second set of 4,096 vectors, one per location of the 64×6464 \times 64 grid.

Figure 5 · one click through SAM, with tensor shapes
6 / 9
Click a stage, or use Prev and Next, to read what happens there. Amber stages run once per image; teal stages run once per prompt. Shapes are for one foreground click with no box and no mask prompt, taken from the official code (build_sam.py, prompt_encoder.py, mask_decoder.py, transformer.py).

Each decoder block does four things in order: self-attention among the 7 tokens, cross-attention from the tokens to the 4,096 image vectors (so the output tokens gather information about the clicked region), an MLP on each token, and cross-attention from the image vectors back to the tokens (so the image embedding is updated with the prompt). The positional encoding is added to the image vectors in every attention layer, and the original prompt tokens are re-added to the updated tokens, so the click location stays available at every step. The MLP is wide (2048) but runs on only 7 vectors; the cross-attention layers, which touch all 4,096 image vectors, work at 128 dimensions instead of 256 to save compute.

After two blocks the image embedding is upsampled 4x by two transposed convolutions, from 64×64×25664 \times 64 \times 256 to 256×256×32256 \times 256 \times 32. Each of the four mask tokens goes through its own 3-layer MLP and comes out as a 32-dimensional vector vv. The mask logit (the raw score before a sigmoid turns it into a probability) at every location of the upsampled grid is the dot product of vv with the 32-dimensional feature ex,ye_{x,y} there, and the probability is its sigmoid:

M(x,y)=σ(⟨v, ex,y⟩)M(x,y) = \sigma\big(\langle v,\, e_{x,y}\rangle\big)
(2)

Read (2) as a linear classifier applied at every pixel. The weights vv are produced by the decoder from this prompt, so the paper calls it a dynamic linear classifier: each prompt gets a new 32-weight classifier, and the mask is that classifier evaluated at all 256×256=65,536256 \times 256 = 65{,}536 locations in one matrix multiply. Pixels whose features point the same way as vv get a large positive logit; pixels whose features point away get a negative one. The logit maps are upsampled to the input resolution and thresholded at 0 (probability 0.5). Figure 6 reduces this to 2-dimensional features so the direction of vv can be a dial.

Figure 6 · the mask is a dot product
v∠80° · align 0.74
Equation (2) on a toy grid. Left, each cell's feature vector (the object region's features point one way, the background's roughly the other, with noise). Right, the mask: teal intensity is σ(⟨v, e⟩) per cell and the bright contour is the 0.5 threshold. Rotate vv onto the object's feature direction (the amber notch) and the mask settles onto the object; at 90 degrees off, every dot product is near zero and the mask is noise. Toy 2D features; the real model uses 32-dimensional features on a 256 x 256 grid, and the decoder, not a slider, sets vv.

The IoU token goes through its own MLP to four estimated IoUs, one per mask token. With one prompt, the code returns masks 1 to 3 and their scores; with more than one, it returns only mask 0. The paper describes mask 0 as a "fourth" token added for the multi-prompt case; the code stores it first, which changes the indexing and not the behavior.

Losses and simulated clicks

Mask quality is measured by intersection over union: the area where the predicted mask and the ground truth overlap, divided by the area covered by either.

IoU(A,B)=∣A∩B∣∣A∪B∣\mathrm{IoU}(A,B) = \frac{|A \cap B|}{|A \cup B|}
(3)

It runs from 0 (no overlap) to 1 (identical). A worked case: ground truth 1,000 pixels, prediction 900 pixels, 850 of them on the object. The union is 1,000 + 900 − 850 = 1,050, so IoU = 850 / 1,050 = 0.81. Averaged over a benchmark it is reported as mIoU, the main metric in the paper's point-prompt experiments. In Figure 7, drag the prediction and watch the value; the dashed mark at 0.90 is the level most of SA-1B clears in the quality audit described later.

Figure 7 · IoU, the yardstick
IoU is the overlap divided by the union of the ground truth and the prediction. Drag the teal mask to change it. The dashed mark sits at 0.900.90, where 94% of SA-1B's automatic masks land when checked against a human correction.

IoU of a thresholded mask is a step function of the logits and has no useful gradient, so training uses two differentiable losses in a 20:120{:}1 ratio of focal to dice, a recipe SAM takes from DETR and MaskFormer. Focal loss (Lin et al., 2017) is per-pixel cross-entropy with a factor that shrinks the loss on pixels the model already gets right:

FL(pt)=− αt (1−pt)γ log⁡pt\mathrm{FL}(p_t) = -\,\alpha_t\,(1 - p_t)^{\gamma}\,\log p_t
(4)

where ptp_t is the predicted probability of the correct label for a pixel. The factor (1−pt)γ(1-p_t)^{\gamma} does the down-weighting. With γ=2\gamma = 2 (the default in Lin et al.; the SAM paper does not state its value), a pixel predicted correctly at pt=0.9p_t = 0.9 has its cross-entropy multiplied by 0.01, while an uncertain pixel at pt=0.5p_t = 0.5 keeps a quarter of it. Most pixels of a mask are easy interior or background pixels, so the gradient concentrates on the uncertain ones along the boundary. The separate weight αt\alpha_t balances foreground against background and plays no part in the easy-versus-hard weighting.

Dice loss (Milletari et al., 2016) is one minus the Dice overlap coefficient:

Dice(A,B)=2 ∣A∩B∣∣A∣+∣B∣,DL=1−Dice\mathrm{Dice}(A,B) = \frac{2\,|A \cap B|}{|A| + |B|}, \qquad \mathrm{DL} = 1 - \mathrm{Dice}
(5)

In the worked case, Dice = 2 · 850 / (1,000 + 900) = 0.89, and in general Dice = 2 IoU / (1 + IoU). In training, AA is the soft probability map rather than a binary mask, which makes (5) differentiable. Because it is a ratio of areas, dice gives a 50-pixel object the same weight as a 50,000-pixel one, which a per-pixel average does not. (V-Net's original loss squares the terms in the denominator; (5) is the plain coefficient.)

The prompts come from a simulated interactive session with 11 rounds per mask. The first prompt is, with equal probability, a point sampled uniformly inside the mask or its bounding box with noise (standard deviation 10% of the box side, capped at 20 pixels). Each of the next 8 rounds adds a point sampled from the error region between the current prediction and the ground truth: a foreground point if the prediction missed that area, a background point if it covered extra area. The previous round's best mask is fed back as a mask prompt, as raw logits rather than a thresholded mask. Two rounds add no new point, one inserted at random among the 8 and one at the end, so the model learns to improve its own mask from the mask prompt alone. From the second round on the prompt has more than one element, so those rounds use the single fourth mask.

# Sketch of one training step on (image, ground-truth mask g), after App. A.
# The public repository ships inference code only.
emb = image_encoder(img)                 # once; reused by all 11 rounds
prompt = [rand_point_in(g) if coin() else noisy_box(g)]  # box noise: 10%, <=20px
rounds = ["click"] * 8
rounds.insert(randint(0, 8), "none")     # one empty round at a random spot
rounds.append("none")                    # one at the end: 1 + 8 + 2 = 11 rounds
prev, total = None, 0
for r in ["first"] + rounds:
    masks, iou_hat = decoder(emb, prompt, prev)  # 3 masks for one prompt, else 1
    per_mask = [20 * focal(m, g) + dice(m, g) for m in masks]
    total += min(per_mask)               # eq. (1): only the best mask is graded
    total += mse(iou_hat, [iou(m > 0, g) for m in masks])   # weight 1.0
    prev = masks[argmax(iou_hat)]        # its raw logits become the mask prompt
    if r == "click":
        prompt.append(rand_point_in_error(prev, g))  # fg if missed, bg if extra
total.backward()                         # one optimizer step for all 11 rounds

Eleven decoder passes per mask per step are affordable because the decoder costs under 1% of the encoder, whose output all 11 rounds reuse. The rest of the recipe, from Appendix A: AdamW with learning rate 8e−4 after a 250-iteration warmup, 90k iterations (about 2 epochs of SA-1B) with the rate cut 10x at 60k and 86,666, batch of 256 images with up to 64 masks per GPU, weight decay 0.1, drop path 0.4, layer-wise learning-rate decay 0.8, no data augmentation, and initialization from MAE weights. The paper's model card gives the cost as 256 A100 GPUs for 68 hours.

The data engine

Training SAM needs a very large set of masks, and producing masks cheaply needs a good segmentation model. The paper breaks this circle with a data engine: the current model assists annotators, the new masks retrain the model, and the better model takes over more of the labeling in the next round. It ran in three stages:

Figure 8 · the data-engine flywheel
1 / 3
Step through the three stages. The effort shifts from human to model, masks per image climb (44, 72, ~100), and the cumulative mask count goes from 4.3M to 10.2M to 1.1B. The final dataset is 99.1% automatic. Numbers are from the paper; the human/model split is illustrative of who annotates.

Automatic masks and the stability filter

The fully automatic stage (Appendix B) prompts more than the 32 x 32 grid. It also cuts 20 zoomed-in crops from the original high-resolution image, 4 from a 2 x 2 layout with a 16 x 16 point grid each and 16 from a 4 x 4 layout with an 8 x 8 grid each. That is 1,024 + 4 · 256 + 16 · 64 = 3,072 points per image, and up to three masks per point, about 9,000 candidates before filtering. SA-1B averages about 100 masks per image, so roughly 99% of candidates are discarded by these steps:

  1. Confidence. Keep masks whose estimated IoU exceeds 0.88.
  2. Stability. Keep masks whose stability score, defined below, is at least 0.95.
  3. Coverage. Drop masks covering 95% or more of the image.
  4. Duplicates. Neighboring grid points on the same object return near-identical masks. Non-maximum suppression (NMS) sorts masks by score and drops any whose bounding box overlaps an already-kept box with IoU above 0.7. It runs within each crop (ranked by estimated IoU) and then across crops (preferring the more zoomed-in crop).
  5. Cleanup. Remove connected components and fill holes smaller than 100 pixels. The paper estimates about 4% of masks had spurious small components and another 4% small holes.

The stability score uses the fact that SAM outputs logits, not a binary mask. Threshold the logit map at 0 and you get the mask. Threshold it at +1 and at −1 and you get two more: the +1 mask is inside the −1 mask, and the band between them holds the pixels whose logit is within 1 of the decision boundary (probability between 0.27 and 0.73). The stability score is their IoU, which for nested masks is the area of the +1 mask divided by the area of the −1 mask. If the logits fall steeply at the object edge, the band is thin and the score is close to 1. If they fall slowly, the model is unsure where the object ends, the band is wide, and the score drops. The paper's main text phrases this as thresholding the probability map at 0.5 ± δ\delta; the released setting of ±1 in logits corresponds to δ≈0.23\delta \approx 0.23.

For a disc of radius RR whose logit drops from +1 to −1 across a band bb pixels wide, the two masks are discs of radius R−b/2R - b/2 and R+b/2R + b/2:

stability=(R−b/2R+b/2)2\text{stability} = \left(\frac{R - b/2}{R + b/2}\right)^{2}
(6)

At R=40R = 40 pixels, a 0.8-pixel band gives (39.6/40.4)2=0.961(39.6/40.4)^2 = 0.961 and the mask is kept; a 2-pixel band gives (39/41)2=0.905(39/41)^2 = 0.905 and it is dropped. The same 0.8-pixel band on a disc of radius 10 gives (9.6/10.4)2=0.852(9.6/10.4)^2 = 0.852, also dropped. For a fixed band width, small and thin objects score lower, because the band is a larger share of their area. Equation (6) is this page's disc calculation, not a formula from the paper; the code computes the ratio of pixel counts for whatever shape the mask has.

Figure 9 · the stability filter
0.8 px
40 px
In the disc panel the white contour is logit 0 (the mask), the dashed amber contours are logits +1 and −1, and the amber band is the uncertain region between them. The curve plots the probability across the edge with the three thresholds. Widen the band or shrink the object and watch the score cross the 0.95 cutoff that SAM's automatic mask generator uses.

Stability and estimated IoU are separate signals. The estimated IoU is a learned output of the decoder; the stability score is computed from the logit map with no learned parameters. The engine requires both. The model that generated SA-1B was also a special version of SAM: trained only on the manual and semi-automatic masks, for 177,656 iterations instead of 90k, with large-scale jitter augmentation, point and mask prompts only, and a 3-layer decoder instead of 2.

SA-1B and how good its masks are

SA-1B has 1.11.1 billion masks on 1111 million licensed, privacy-protecting images (faces and license plates blurred), about 100100 masks per image. That is 400×400\times more masks and 11×11\times more images than Open Images, the largest segmentation dataset before it. The images average 3300 x 4950 pixels and are released downsampled to a shortest side of 1500 pixels. 99.1% of the masks were generated fully automatically, and SA-1B ships only the automatic masks; the 10.2 million hand-drawn masks from stages 1 and 2 would be 0.9% of 1.1 billion.

To check the automatic masks, the authors took 500500 images (about 5050k masks) and asked professional annotators to correct every mask with brush and eraser tools. Comparing each automatic mask with its corrected version, 94% had IoU above 0.900.90 and 97% above 0.750.75. For comparison the paper cites prior work putting agreement between two human annotators at 85 to 91% IoU; that range is a baseline from other studies, not a measurement on SA-1B. In an ablation, training on the automatic masks alone scores only about 0.50.5 mIoU below training on all masks from the three stages, which is why the release includes only the automatic ones.

Zero-shot transfer

The paper tests five tasks with no fine-tuning: segmenting from a single point on 23 datasets, edge detection, object proposals, instance segmentation, and text-to-mask. The last four differ from the training task and are each solved by constructing prompts.

Single point

The comparison is against RITM, a strong interactive segmenter, on 23 datasets covering egocentric, microscopy, X-ray, underwater, aerial, simulated, driving and painting images. Scoring only SAM's highest-ranked mask against the ground truth, SAM beats RITM on 1616 of the 2323. With an "oracle" that picks whichever of SAM's three masks best matches the ground truth, SAM beats RITM on all 2323. The gap between the two scorings is the cost of choosing the wrong nesting level: the right mask was among the three, but the estimated IoU ranked another one first.

Figure 10 · single-point, vs RITM across 23 datasets
confident
Each square is one dataset. Scoring SAM's most-confident mask, SAM beats RITM on 16 of 23. Switch to the oracle (the best of SAM's three masks against the truth) and SAM wins on all 23. The grid is an abstract tally of the counts, not specific named datasets.

A human study on the same task rated masks from 1 to 10. SAM's mean ratings fell between 7 and 9, above RITM, and above an ablated SAM with a single output mask (which in turn rated above RITM). This held on datasets such as DRAM and IBD, where SAM scores worse than RITM on the automatic metric.

Instance segmentation

Here SAM is one component in a pipeline. A detector (ViTDet-H) finds boxes; each box goes to SAM as a prompt; SAM returns the mask. No part of SAM is trained on COCO or LVIS. Against ViTDet's own masks, trained directly on those datasets, SAM trails on mask AP (average precision, the standard detection metric): 46.546.5 versus 51.051.0 on COCO and 44.744.7 versus 46.646.6 on LVIS. In a human study, raters preferred SAM's masks, which have crisper boundaries. The authors hypothesize that ViTDet learns the specific biases of COCO's masks, whose ground-truth quality the human study rates relatively low, while SAM, trained on neither dataset, cannot exploit them; AP rewards matching the annotations, biases included.

Figure 11 · instance segmentation: metric vs humans
mask AP
Toggle between the two verdicts. Prompted with a detector's boxes, SAM trails dataset-trained ViTDet on mask AP (46.5 vs 51.0 on COCO, 44.7 vs 46.6 on LVIS); in the human study, raters score SAM's masks higher. The human-study bars are the mean ratings from the paper's Figure 11, on its 1 to 10 scale.

Edge detection

On BSDS500, SAM is prompted with a 16 x 16 grid of foreground points (768 masks, 3 per point), and edges are read off the boundaries of the resulting masks, although SAM was never trained on edges. It reaches a recall at 50% precision of 0.928 (ODS 0.768, OIS 0.786, AP 0.794): it finds most annotated edges but also marks many the benchmark did not annotate, so precision is lower, and it trails methods trained on BSDS500.

Object proposals

Object proposals are class-agnostic candidate regions that a later stage classifies; the benchmark is LVIS v1. Using the automatic mask generator as a proposal source, SAM beats ViTDet-H on medium, large, rare and common objects, and trails it on small and frequent ones.

Text to mask

This experiment needed its own training run. For every manually collected mask larger than 1002100^2 pixels, the CLIP image embedding of that region was used as the prompt. At inference the CLIP text embedding of a phrase such as "a wheel" goes in its place. This works to the extent that CLIP maps a picture of a wheel and the words "a wheel" to nearby vectors. The authors call it exploratory and not entirely robust, and the released models take points, boxes and masks.

Encoder size

In the ablations, ViT-H improves substantially over ViT-B but only marginally over ViT-L, and the authors concluded that scaling the image encoder further did not appear fruitful at the time.

What SAM does not do

SAM's masks are class-agnostic. It outputs a mask for an object without a label saying what the object is. Semantic segmentation (a class for every pixel) and panoptic segmentation (a class and an instance for every pixel) cannot be obtained by prompting it, and the paper says it is unclear how to design simple prompts that would do so. Labels have to come from whatever SAM is combined with, such as the detector in the instance-segmentation experiment.

It does not top the specialist benchmarks. It trails ViTDet on mask AP, trails edge detectors that learn BSDS500's annotation style, and the authors expect dedicated interactive segmenters to beat it when many clicks are given. The limitations section lists missed fine structures, occasional small disconnected components that should not be there, and boundaries less crisp than slower methods that zoom in on the object. The estimated IoU it reports ranks its own masks and does not certify them.

The paper leaves open whether SAM is a foundation model; the authors write that it "remains to be seen by how it is used in the community". The paper does show a recipe that works at this scale: a task general enough to pre-train on and to reuse through prompts, a model split so that the per-prompt part costs 50 ms, and a data engine that turned about 10 million human-drawn masks into 1.1 billion. Its successor, SAM 2 (2024), applies the same promptable interface to video.

Provenance Verified against primary literatureHow we verify
MAE (He et al., 2022)Self-supervised pretraining of the ViT-H image encoder: mask 75% of patches and reconstruct them. SAM starts from these weights.
ViT (Dosovitskiy et al., 2021) + ViTDet (Li et al., 2022)The patch Transformer backbone and its high-resolution adaptation: 14x14 windowed attention with 4 global blocks (build_sam.py: global blocks 7, 15, 23, 31 of 32).
CLIP (Radford et al., 2021)Shared image/text embedding space; the text-to-mask experiment trains on CLIP image embeddings and prompts with CLIP text embeddings.
DETR (Carion et al., 2020) + MaskFormer (Cheng et al., 2021)Decoder lineage (learned output tokens, mask = token-to-pixel dot product) and the 20:1 focal-to-dice loss. Focal loss from Lin et al. 2017, dice from Milletari et al. 2016 (V-Net).
Multiple-choice learning (Guzman-Rivera et al., 2012)Several outputs, gradient only through the lowest loss. The paper cites Charpiat 2008, Guzman-Rivera 2012 and Li 2018. Rupprecht et al. 2017 show winner-take-all can leave a hypothesis that never wins.
Official code (facebookresearch/segment-anything)mask_decoder.py L49-64: 1 IoU token + 4 mask tokens, upscaling to 256/8 = 32 channels, per-token 3-layer MLPs; L102-107: maps 1..3 for multimask, map 0 otherwise; L144: mask = hyper_in @ upscaled embedding.
Official code, automatic masksautomatic_mask_generator.py L39-45: 32 points per side, predicted-IoU cutoff 0.88, stability cutoff 0.95 with logit offset 1.0, box NMS 0.7. utils/amg.py L156-176: stability = |logits > 1| / |logits > -1|.
caveatSAM’s “estimated IoU” is a number the model regresses with a mean-squared-error loss so it can rank its three masks (and, in the data engine, filter them at 0.88). It is not a calibrated probability, not a guarantee of mask quality, and not the same quantity as the true IoU against ground truth or the mIoU used to evaluate the model.

Questions you might still have

?

Were the billion masks drawn by people?
No. 99.1% of SA-1B was generated fully automatically, and the released dataset contains only automatic masks. People drew about 10.2 million masks in the first two data-engine stages (4.3M assisted-manual, 5.9M semi-automatic), which trained the model that produced the rest.

?

If SAM is never trained on text, how does it segment from a text prompt?
In a separate, modified training run, each mask larger than 100 x 100 pixels was prompted with the CLIP image embedding of that region. At inference the CLIP text embedding of a phrase goes in its place, which works because CLIP puts matching images and text near each other. The paper calls this exploratory and not robust, and the released models take points, boxes and masks only.

?

Is SAM state of the art at segmentation?
It is the most general model in the paper, not the best at each task. Prompted with a detector’s boxes it trails ViTDet on instance-segmentation mask AP (46.5 vs 51.0 on COCO, 44.7 vs 46.6 on LVIS), though human raters prefer its masks. It lags edge detectors trained on BSDS500, and it cannot do semantic or panoptic segmentation by prompting.

?

Why three masks, and not one or five?
One output trained on ambiguous clicks learns the average of the valid answers, a blurred mask. The authors found nested objects under a click are usually at most three deep (whole, part, subpart), so three outputs cover most cases. Training back-propagates only the lowest of the three losses, so each output is pushed toward the answer it already fits best.

?

Does the 50 ms mean SAM runs in real time?
Only the prompt encoder and mask decoder run in about 50 ms, on a browser CPU, given an image embedding that already exists. The ViT-H image encoder is paid once per image before any clicking and is far heavier; the authors say SAM is not real-time end to end with that encoder.

?

Can I trust the score SAM reports for a mask?
Use it to compare SAM’s own candidate masks for one prompt, which is what it was trained for. It is a regression output, not a probability, and it can be high on a wrong mask for an image unlike the training data. The data engine therefore combined it with a stability check and human quality audits.

?

What happened after SAM?
Meta released SAM 2 in 2024, which extends promptable segmentation to video with a memory of earlier frames. SAM itself is unchanged by that work; its backbone is the ViT covered in the ViT explainer on this site.

Footnotes & further reading

  1. The paper: Kirillov, Mintun, Ravi, Mao, Rolland, Gustafson, Xiao, Whitehead, Berg, Lo, Dollár, Girshick, Segment Anything (Meta AI / FAIR, ICCV 2023). Model, dataset and demo at segment-anything.com; code at facebookresearch/segment-anything. The ~0.15 s encoder time is from the project FAQ, quoted in issue #344.
  2. The image encoder is an MAE-pretrained Vision Transformer (ViT-H/16), with the high-resolution windowed-attention adaptation from ViTDet. See also the ViT and MAE explainers.
  3. Text prompts use the text encoder of CLIP; the train-on-image, prompt-with-text experiment relies on CLIP's shared image-text space.
  4. The mask losses: focal loss from Lin et al. (RetinaNet) and dice loss from Milletari et al. (V-Net). The 20:1 combination and the token-based mask decoder follow DETR and MaskFormer. Training uses AdamW.
  5. Predicting several outputs and supervising only the best is multiple-choice learning: Guzman-Rivera et al. (2012), extended to deep networks by Lee et al. (2016). The never-selected-hypothesis problem and its relaxed fix are in Rupprecht et al. (2017). The interactive baseline SAM is measured against is RITM.