↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading… Image encoder once per image · mask decoder once per prompt · every head recorded · encoder attention rows kept for 18 patches
Loading recorded inference…

Promptable segmentation

Segmentation labels which pixels belong to an object. The Segment Anything Model (SAM) makes it promptable: given an image and a prompt (a click, a box or a rough mask), it returns masks of whatever the prompt points at, without being told what kind of object it is. A prompt is often ambiguous, since a click on a dog's tongue could mean the tongue, the face or the whole dog. SAM therefore returns three candidate masks, each with its own predicted quality.

SAM is split by cost. A heavy image encoder runs once per image; a light prompt encoder and mask decoder run once per prompt, so trying another prompt reuses the image embedding. The page records SAM ViT-B (93,735,472 parameters, of which 89,670,912 are in the image encoder and 4,058,340 in the mask decoder) on a 1000 × 750 photo of a beagle, with two prompts: a click on the tongue and a box around the dog.

The image encoder

The Image Encoder ViT-B/16 row is a Vision Transformer at high resolution. The photo is resized to 1024 × 768 and padded to 1024 × 1024, then cut into 16-pixel patches: a 64 × 64 grid of 4,096 tokens, with no [CLS]. A learned 64 × 64 × 768 table adds absolute positions, and every attention also adds learned relative positions along rows and columns, so position enters twice:

Global attention over 4,096 tokens costs 16.8 million scores per head. Most blocks therefore attend only within 14 × 14 windows: the grid is padded to 70 × 70 and cut into 25 windows, 25 × 196² scores per head. Blocks 3, 6, 9 and 12 attend globally and carry information between windows. The badge above the row says which kind each block is, and the head card greys out keys in other windows. This layout comes from ViTDet; the encoder's weights started from MAE pre-training.

Each block is pre-norm with an exact-GELU MLP, 768 → 3072 → 768. The Neck reduces the output to the image embedding, a 64 × 64 × 256 map: a 1 × 1 convolution to 256 channels, then a 3 × 3 convolution, each followed by a LayerNorm.

Prompts as tokens

The prompt encoder turns each point into a 256-dimensional token. It encodes the point's position with random Fourier features, a fixed random matrix followed by sines and cosines, and adds a learned embedding for the point's type (background, foreground, or one of the two box corners):

Fourier features let a network resolve fine detail in low-dimensional inputs such as coordinates. The same encoding, evaluated at every patch centre, gives the image embedding its positions . A click is one point plus a padding token, a learned “not a point” vector; a box is its two corners. With no mask prompt a learned no-mask vector is added to every image position.

The decoder works on 7 tokens of 256: a learned IoU token, four learned mask tokens and the two prompt tokens. The IoU token and mask tokens are the same for every image and prompt; they become specific only through attention.

The two-way mask decoder

The Prompt Encoder & Mask Decoder row runs a two-layer transformer that updates both sides. In each layer the tokens attend to each other (Token Self Attention), then to the image (Token → Image), pass through a ReLU MLP, 256 → 2048 → 256, and finally the image attends back to the tokens (Image → Token). Cross-attention narrows to 128 dimensions in 8 heads, and positions are added to queries and keys at every attention:

Unlike the encoder, the decoder is post-norm, . A final token-to-image attention follows the two layers. Two transposed convolutions then upscale the image embedding four times, to a 256 × 256 map of 32 channels. Each mask token passes through its own MLP to a 32-vector , and the mask is a dot product at every pixel:

All the image-specific work is in ; the token only chooses which combination of its 32 channels to read. A separate head maps the IoU token to four predicted IoUs. The logits are resized to the photo and thresholded at 0.

Three masks for an ambiguous click

The Masks column shows the candidates. For the click on the tongue they nest: mask 1 is the whole dog (44.0 % of the photo), mask 2 the tongue and nose (3.0 %) and mask 3 the tongue alone (1.9 %). Their predicted IoUs are 0.743, 0.884 and 0.934, so mask 3 ranks highest. For the box all three cover about half the photo and mask 1 ranks highest at 0.981; the single-mask output, token 0, scores 0.988. A predicted IoU is the model's own estimate of a mask's quality, not an overlap measured against anything.

Training makes the candidates differ. SAM computes the loss of each of the three masks against the ground truth but backpropagates only the lowest. Each mask token can therefore specialise in a scale, a part or the whole, instead of all three averaging the valid answers. The loss is focal loss plus dice loss on the mask, with a regression of the predicted IoU on the true one.

Where it came from

Kirillov et al. introduced SAM in 2023 with the SA-1B dataset, over 1 billion masks on 11 million images, collected in a loop in which the model helped annotators and later annotated on its own. Its encoder is a ViT pre-trained as a masked autoencoder (MAE), windowed as in ViTDet. Its decoder is a modified transformer decoder inspired by DETR and MaskFormer, ending in a dynamic mask head. SAM 2 extended the model to video with a memory of earlier frames.

The next tab, SAM 3, segments every instance of a concept named in text, on the same photo.

Papers

  1. Segment Anything Alexander Kirillov et al. · ICCV 2023 SAM, the model this page records: promptable segmentation with an image encoder, a prompt encoder and a two-way mask decoder, and the SA-1B dataset.
  2. Masked Autoencoders Are Scalable Vision Learners Kaiming He et al. · CVPR 2022 MAE pre-training, where SAM's image encoder started.
  3. Exploring Plain Vision Transformer Backbones for Object Detection Yanghao Li, Hanzi Mao, Ross Girshick, Kaiming He · ECCV 2022 ViTDet: windowed attention with a few global blocks and relative positions, the layout of SAM's encoder.
  4. An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale Alexey Dosovitskiy et al. · ICLR 2021 The Vision Transformer that SAM's encoder adapts to 1024-pixel inputs.
  5. Fourier Features Let Networks Learn High Frequency Functions in Low Dimensional Domains Matthew Tancik et al. · NeurIPS 2020 Random Fourier features of coordinates, SAM's encoding of points and positions.
  6. Per-Pixel Classification is Not All You Need for Semantic Segmentation Bowen Cheng, Alexander G. Schwing, Alexander Kirillov · NeurIPS 2021 MaskFormer: masks as dot products of query embeddings with per-pixel features, one model behind SAM's decoder.
  7. SAM 2: Segment Anything in Images and Videos Nikhila Ravi et al. · ICLR 2025 SAM extended to video with a streaming memory.

See also