Paper commentary
Segment Anything
- Problem Segmentation has no web-scale data source to match the text corpora behind language foundation models, so a promptable segmentation model must be built together with its task and its training data.
- Method A promptable segmentation task, a Segment Anything Model (SAM) with a cached image encoder, a prompt encoder and a lightweight mask decoder, and a data engine that gathers masks into SA-1B.
- Results Reached 59.3 AR@1000 zero-shot on LVIS v1 for object proposals, against 63.0 for ViTDet-H.
Why segmentation needs a foundation model
Language models pre-trained on web-scale text generalize zero-shot, but segmentation has no comparable source of web-scale data. Vision-language encoders such as CLIP cover only part of computer vision, and many segmentation problems have no abundant training data.
The authors frame the work around three linked questions: which task gives zero-shot generalization, which architecture fits that task, and which data can train both. Each answer depends on the others, so the task, the model and the data are designed as one system.
What a prompt asks for
The promptable segmentation task asks for a valid mask for every prompt, not only for prompts that have a single clear answer.
A point on a shirt is ambiguous, since it may mean the shirt or the person wearing it, and the model should return a reasonable mask for at least one of these objects. The authors compare this requirement to a language model giving a coherent answer to an ambiguous question.
The same task serves as the pre-training objective. Training simulates prompt sequences and compares predicted masks against ground truth, adapted from interactive segmentation. Unlike interactive segmentation, which aims for a valid mask only after enough user input, this objective asks for a valid mask at every prompt, which is what makes ambiguous prompts workable for automatic annotation.
Zero-shot transfer follows from this design: a detector's output can serve as a prompt, so a cat box detector turns SAM into a cat instance segmenter. The experiments cover five example tasks in addition to automatic dataset labeling.
Image embedding, prompt encoder and mask decoder
SAM has a powerful image encoder that computes an image embedding, a prompt encoder that embeds the prompt, and a lightweight mask decoder that combines both sources to predict masks. Separating the image encoder from the prompt encoder and mask decoder means the image embedding is computed once and its cost is shared across prompts.
The mask decoder needs less than 1% of the image encoder's compute, so many iterations on one image add only a small overhead.
The image encoder is a ViT-H initialized from an MAE pre-trained model and run at 1024×1024 input size, which is why training is spread across 256 GPUs. Only the prompt encoder and mask decoder must answer each new prompt, so the expensive part is paid once per image.
Multiple candidate masks and the IoU head
A single prompt can match several valid masks, and a standard model would learn to average them. SAM instead predicts from a small set of output tokens, by default three masks, which the authors take to cover whole, part and subpart levels of nested masks.
During training the loss is computed for each of the three predictions, but only the lowest-loss prediction is backpropagated. A small head on an extra output token estimates the IoU of each predicted mask, which is used to rank the candidates for use in applications.
That fourth mask is never returned for a single prompt, which keeps the unambiguous case on a regular gradient signal.
Mask prediction is supervised by focal loss and dice loss in a 20:1 ratio of focal to dice, and the IoU head uses mean-square-error with a constant scaling factor of 1.0. The authors found auxiliary deep supervision after each decoder layer unhelpful, unlike the work they follow.
Simulated interactive training and training recipe
Each training sample first receives either a foreground point or a box, chosen with equal probability.
After the first prediction, new points come from the error region between the previous mask and the ground truth, as foreground for false negatives and background for false positives. The unthresholded mask logits from the previous iteration are passed back to the model, and when several masks are returned the one with the highest predicted IoU is used.
Training uses 11 iterations: one initial prompt, 8 sampled points, and two iterations with no new external input, one inserted at random among the sampled points and one at the end. The authors report diminishing returns after 8 sampled points, and tested up to 16.
The optimizer is AdamW with beta1 0.9 and beta2 0.999, a linear warmup over 250 iterations, and an initial learning rate of 8e−4 after warmup. The default model trains for 90k iterations with batch size 256, weight decay 0.1, drop path 0.4 and layer-wise learning rate decay 0.8, with no data augmentation.
For the ViT-B and ViT-L variants the authors used 180k iterations with batch size 128 across 128 GPUs, with different learning rate, layer decay and drop path values.
Data engine and the SA-1B dataset
The data engine has three stages: assisted-manual annotation with SAM helping annotators, semi-automatic annotation where SAM proposes masks for likely objects, and fully automatic generation from a regular grid of foreground points. The fully automatic stage yields about 100 masks per image on average.
The authors state that it has 400× more masks than any existing segmentation dataset they compare against.
For automatic generation, a 32×32 point grid covers the full image, and 20 zoomed crops come from 2×2 and 4×4 overlapping windows with 16×16 and 8×8 point grids. Greedy box-based NMS with threshold 0.7 runs first within each crop, ranked by predicted IoU, and then across crops, ranked from most to least zoomed-in.
Masks are kept when predicted IoU is at least 88.0 and when their stability IoU between the -1 and +1 thresholded masks is at least 95.0. Masks covering 95% or more of the image are removed, and components below 100 pixels are removed, while holes below 100 pixels are filled.
Zero-shot object proposals on LVIS v1
Proposals come from a slightly modified automatic mask generation pipeline, scored with AR@1000 on LVIS v1 against a ViTDet-H baseline. The ViTDet-H baseline is the detector used as a proposal generator, a comparison the authors describe as demanding.
ViTDet-H reaches 63.0 AR@1000 overall, and SAM reaches 59.3 overall with no training for proposals and no access to LVIS images or annotations. The ambiguity-unaware variant of SAM, labeled single out., reaches 54.9 overall.
On medium objects SAM scores 81.6 against 80.8 for ViTDet-H, and on rare categories it scores 65.8 against 58.3. On large objects the table gives 86.9 for SAM against 87.0 for ViTDet-H, so the claim of an advantage on large objects is not borne out by those two numbers.
SAM trails ViTDet-H on small objects, 45.5 against 51.7, and on frequent categories, 59.1 against 63.1, and the authors attribute the lead on frequent categories to LVIS-specific annotation biases ViTDet-H learned from LVIS training.
Instance segmentation, human ratings and text prompts
For instance segmentation, a ViTDet detector supplies boxes and SAM segments each box, which illustrates composing SAM inside a larger system. On COCO, mask AP is 46.5 for SAM against 51.0 for the fully supervised ViTDet-H, and on LVIS v1 it is 44.7 against 46.6.
In a human study on a 1 to 10 mask quality scale, applied to LVIS ground truth boxes, SAM's masks average 8.1 and ViTDet-H's average 7.9. The legend in the rating figure places LVIS ground truth at 8.6 and COCO ground truth at 7.6, with 95% confidence intervals reported.
The authors hypothesize that ViTDet-H learns the specific biases of COCO and LVIS masks, which a zero-shot method such as SAM cannot exploit. Lower AP paired with higher human ratings is read by the authors as a sign that ViTDet-H exploits these biases.
At inference the text is encoded with CLIP's text encoder and that embedding is given to SAM as the prompt.
Qualitative examples show that simple phrases such as 'a wheel' and nuanced ones such as 'beaver tooth grille' can select objects. When a text prompt picks the wrong object, adding a point often fixes the prediction.
Ablations on data stages, data volume and encoder size
Each data engine stage raises mIoU when added cumulatively.
Training on all three stages with manual and semi-automatic masks oversampled by 10× was the best mixed setup, but it complicates training. Training only on automatic masks gives about 0.5 mIoU less than using all data, so the authors use automatic masks by default.
At 0.1M images the mIoU declines sharply under every setting. At 1M images, about 10% of SA-1B and still about 100M masks, the results are comparable to using all 11M images.
ViT-H improves substantially over ViT-B but gives only marginal gains over ViT-L, so the authors do not expect further encoder scaling to help at this time. Smaller encoders may still be preferred in some settings.
Limitations
SAM can miss fine structures and sometimes hallucinates small disconnected components. Its boundaries are less crisp than those of more computationally intensive zoom-in methods.
With many input points, the authors expect dedicated interactive segmentation methods to outperform SAM, since SAM targets generality rather than high-IoU interaction.
Prompts are processed in real time, but overall performance is not real-time with a heavy image encoder. The text-to-mask work is described as exploratory and not entirely robust, and simple prompts for semantic and panoptic segmentation remain unclear.
In the zero-shot setting there may be several valid ground truth masks for one input, and the model card asks users to account for this.
Paper info
- Authors
- Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alexander C. Berg, Wan-Yen Lo, Piotr Dollár, Ross Girshick
- Venue
- arXiv · Preprint
- First published
- 2023-4-5
- Added
- 2026-10-20
- Updated
- 2023-4-5
- Version
- v1
- arXiv
- 2304.02643
- Category
- cs.CV · cs.AI · cs.LG
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alexander C. Berg, Wan-Yen Lo, Piotr Dollár, Ross Girshick (2023). Segment Anything. arXiv:2304.02643v1. https://arxiv.org/abs/2304.02643v1
BibTeX
@misc{kirillov2023segment,
title = {{Segment Anything}},
author = {Alexander Kirillov and Eric Mintun and Nikhila Ravi and Hanzi Mao and Chloe Rolland and Laura Gustafson and Tete Xiao and Spencer Whitehead and Alexander C. Berg and Wan-Yen Lo and Piotr Dollár and Ross Girshick},
year = {2023},
eprint = {2304.02643},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2304.02643v1}
}