Foundation Model

Segment Anything Model

The foundation model that changed computer vision forever. SAM can segment any object in any image with just a single click—without ever seeing that object type before.

Meta AIApril 2023636M Parameters1B+ Masks Trained
11M
Images in SA-1B
1.1B
Mask Annotations
400×
More Masks Than Prior
~50ms
Per-Mask Inference

Deep Dive Contents

Before April 2023, image segmentation was a closed-world problem. You trained a model to segment cats, and it could segment cats. Dogs? Train another model. Medical images? Another model. Every new domain required collecting thousands of labeled examples, training for days, and hoping the model generalized within that narrow slice of the visual world.

The Old World (Pre-SAM)

  • Train separate models for each object category
  • Need 10,000+ labeled masks per category
  • Models fail on unseen object types
  • Interactive segmentation was slow and clunky
  • No unified approach across domains

The SAM World

  • One model segments everything
  • Zero-shot transfer to new objects
  • Works on medical, satellite, microscopy images
  • Interactive: click, box, or describe
  • 50ms per mask—real-time interaction

The GPT-3 Moment for Vision

SAM did for segmentation what GPT-3 did for language: it created a foundation model that could be prompted to perform tasks it was never explicitly trained for. Just as GPT-3 showed that scale + diverse data = emergent generalization, SAM showed that training on 1 billion masks creates a model that understands the concept of “object” at a fundamental level—regardless of what that object is.

Why This Matters Beyond Academia

Medical Imaging

Segment tumors, organs, cells—without domain-specific training

Autonomous Vehicles

Segment any obstacle, even novel ones never seen in training

Creative Tools

One-click object removal, background replacement, compositing

SAM wasn't just a larger model—it required three simultaneous breakthroughs: a new task definition, a new model architecture, and a new data engine. Any one of these alone wouldn't have been enough. The magic was in their combination.

Breakthrough 1: Promptable Segmentation Task

Previous segmentation tasks asked: “Segment all cats in this image.” SAM asks: “Given this prompt (a point, box, or mask), segment the object it refers to.” This reframing is crucial because:

  • The model doesn't need to know object categories—just object boundaries
  • Prompts provide the “what to segment” signal that replaces category labels
  • The same architecture handles clicks, boxes, and rough masks as input

Breakthrough 2: Efficient Architecture

SAM's architecture is designed for amortized inference: compute the expensive image embedding once, then generate multiple masks cheaply. This enables real-time interaction.

Image Encoder
~0.15s (one time)
ViT-H with 632M params
Prompt Encoder
~1ms
Lightweight positional encoding
Mask Decoder
~50ms
2-layer transformer + upsampling

Breakthrough 3: The Data Engine

You can't train a foundation model without foundation-scale data. But 1 billion high-quality masks didn't exist. Meta built a data flywheel:

1
Assisted Manual (4.3M masks)

Professional annotators used early SAM to speed up labeling. SAM proposes, humans correct.

2
Semi-Automatic (5.9M masks)

SAM auto-detects obvious objects; humans label the rest. Model improves, auto-detects more.

3
Fully Automatic (1.1B masks)

Grid of 32×32 points → SAM generates all plausible masks → filter by confidence and NMS.

The Virtuous Cycle

Better model → faster annotation → more data → better model. This cycle is why SAM's dataset is 400× larger than any previous segmentation dataset. The model literally helped create its own training data.

SAM's architecture is elegantly simple: three components, each with a clear responsibility. The design philosophy is separation of concerns—the expensive computation (understanding the image) is done once, while the cheap computation (responding to prompts) can be done many times.

632M
Encoder params
~8M
Prompt + Decoder
~50ms
Per additional mask

Image Encoder

632M parameters

A ViT-H (Vision Transformer Huge) pre-trained with MAE (Masked Autoencoder). Processes the input image into a rich 64×64×256 feature map that captures all visual information needed for segmentation.

Prompt Encoder

~4M parameters

Converts user prompts (points, boxes, masks) into embeddings that can interact with image features. Points and boxes become positional encodings; masks are convolved into the embedding space.

Mask Decoder

~4M parameters

A lightweight transformer that fuses image and prompt embeddings, then upsamples to produce the final mask. Outputs multiple masks + confidence scores to handle ambiguity.

Why This Design is Genius

Amortized Cost: The image encoder is expensive (~150ms) but runs once. Each subsequent prompt only needs the fast decoder (~50ms). Click 10 times? Pay 150ms + 10×50ms = 650ms total, not 10×150ms.
Modular Upgrades: You can swap the image encoder for a better one (like SAM 2's Hiera) without changing the prompt/mask components. Each part evolves independently.

The image encoder is a ViT-H/16 (Vision Transformer Huge with 16×16 patches) that was pre-trained using MAE (Masked Autoencoder) on ImageNet. This combination—huge capacity + self-supervised pre-training—gives SAM its remarkable ability to understand diverse images.

Architecture Specifications

Input Resolution1024 × 1024
Patch Size16 × 16 pixels
Number of Patches64 × 64 = 4,096
Embedding Dimension1,280
Transformer Layers32
Attention Heads16
Total Parameters632M
Output Feature Map64 × 64 × 256

Why MAE Pre-training?

MAE randomly masks 75% of image patches, then trains the model to reconstruct them. This forces the model to learn:

  • Global context: Understanding whole objects from partial views
  • Local details: Reconstructing fine textures and edges
  • Spatial relationships: How parts relate to wholes

Windowed Attention

Global self-attention on 4,096 tokens would be O(n²) = 16.7M operations per layer. SAM uses:

  • 14×14 local windows: Most layers attend within windows
  • 4 global layers: Layers 5, 11, 17, 23 use global attention
  • Result: 5× faster than full global attention

The Output: 64×64×256 Feature Map

The encoder's 1,280-dim embeddings are projected down to 256 dimensions, giving us a dense spatial feature map where each of the 4,096 positions encodes rich information about a 16×16 pixel region. This map is:

Spatially Dense

Every 16×16 region has its own embedding—no information loss from pooling

Semantically Rich

Each embedding knows about the whole image through attention

Reusable

Computed once, used for any number of prompts

Model Size Comparison

SAM-B (Base)
91M
SAM-L (Large)
308M
SAM-H (Huge)
632M

All numbers are for the image encoder only. The flagship model uses ViT-H.

The prompt encoder translates human intent into a language the mask decoder understands. It handles three types of prompts: sparse prompts (points and boxes) anddense prompts (masks). Each type is encoded differently, but all produce embeddings that can be combined with image features.

Prompt Encoding

PE(512, 410) + fg_embed

Sparse tokens: 1 (1 foreground point)

How it works: Each point becomes a 256-dim embedding via Fourier positional encoding + a learned type embedding (foreground/background). Boxes are encoded as 2 corner points with special corner embeddings. All sparse prompts are concatenated into a sequence that the mask decoder attends to.

Point Encoding
embed = PE(x, y) + type_embed

PE uses sin/cos at multiple frequencies

Box Encoding
embed = [PE(x₁,y₁) + tl, PE(x₂,y₂) + br]

Two corners with top-left/bottom-right embeddings

Sparse Prompts: Points

A click at position (x, y) becomes a 256-dimensional embedding through:

embed(point) = PE(x, y) + type_embed
  • PE(x, y): Fourier positional encoding of coordinates
  • type_embed: Learned embedding for “foreground” or “background”
  • Foreground points say “include this”; background points say “exclude this”

Sparse Prompts: Boxes

A bounding box is encoded as two points: top-left and bottom-right corners.

embed(box) = [PE(x₁,y₁) + corner₁, PE(x₂,y₂) + corner₂]
  • Each corner has its own learned type embedding
  • The decoder learns that boxes mean “segment something inside this region”
  • Boxes are more informative than single points—less ambiguity

Dense Prompts: Masks

When you have a rough mask (from a previous iteration or another source), SAM can refine it:

Input: 256×256 binary mask
↓ 2× Conv layers
Output: 64×64×256 dense embedding

The mask is downsampled to match the image embedding resolution

Added element-wise to image embeddings
Enables iterative refinement: output mask → input → better mask
Critical for interactive editing workflows

Combining Multiple Prompts

SAM can handle multiple prompts simultaneously: several foreground points, background points, a box, and a mask—all at once. Sparse prompts are concatenated into a sequence; dense prompts are added to image features. This flexibility lets users iteratively refine selections: “include this, exclude that, refine this edge.”

The mask decoder is where image understanding meets user intent. It's a lightweight transformer (~4M parameters) that fuses image embeddings with prompt embeddings, then upsamples to produce high-resolution masks. Despite its simplicity, this component does the heavy lifting of actually producing the segmentation.

Step 1: Input Assembly

Image embeddings (64×64×256) + prompt embeddings + 4 output tokens (3 masks + 1 IoU)

Two-Way Attention

Unlike standard cross-attention, SAM's decoder has bidirectional flow: tokens attend to image, AND image attends to tokens. This lets the image “understand” the prompt.

Why Only 2 Layers?

The heavy lifting is done by the image encoder. The decoder just needs to route information between already-rich representations. More layers didn't improve quality.

Decoder Architecture

1
Input Preparation

Image embeddings (64×64×256) + prompt embeddings + learned output tokens. Output tokens are like “queries” that will become the final masks.

2
Two-Way Transformer (2 layers)

Each layer performs: (1) self-attention on tokens, (2) cross-attention from tokens to image, (3) MLP, (4) cross-attention from image to tokens. This bidirectional flow is key.

3
Upsampling (64×64 → 256×256)

Two transposed convolution layers upsample the image features 4× while the output tokens become per-mask MLP heads that predict mask logits.

4
Final Prediction

Each output token's MLP produces weights that are dot-producted with the upsampled features to produce a 256×256 mask. Final resize to original resolution.

Why Two-Way Attention?

Standard cross-attention only goes one direction (queries attend to keys). SAM's bidirectional design lets image features also attend to prompts. This means:

  • Image features become “aware” of what the user wants
  • Prompt tokens gather relevant visual context
  • Both sides refine each other over two layers

IoU Prediction Head

Each output token also produces an IoU score—the model's confidence that this mask is correct. This is crucial for:

  • Ranking multiple mask proposals
  • Filtering low-confidence masks in automatic mode
  • Telling users “I'm not sure about this one”

Resolution Trade-off

The decoder outputs 256×256 masks, which are then bilinearly upsampled to the original resolution. This is a deliberate trade-off: higher internal resolution would be more accurate but slower. For most use cases, 256×256 is sufficient—edges are refined by the final resize, and the perceptual difference is minimal.

SA-1B (Segment Anything 1 Billion) is the largest segmentation dataset ever created, and its existence is inseparable from SAM itself. The dataset and model were built together in a co-evolutionary process where each improved the other.

Dataset Statistics

11M
Images
Licensed, diverse sources
1.1B
Masks
~100 masks per image
99.1%
Auto-Generated
Fully automatic pipeline
94%
Quality Rating
Human evaluation

Phase 1: Assisted Manual

4.3M masks from 120k images

Professional annotators used a browser-based tool with an early SAM model. SAM predicted masks from clicks; annotators corrected errors. Each image took ~14 seconds to fully annotate.

Phase 2: Semi-Automatic

5.9M masks from 180k images

SAM now auto-detected “confident” masks. Annotators only labeled objects SAM missed. Average annotation time dropped to ~7 seconds per image as SAM caught more objects.

Phase 3: Fully Automatic

1.1B masks from 11M images

32×32 grid of points → SAM generates all plausible masks → NMS and confidence filtering. No human involvement. This is where the billion masks came from.

Comparison to Prior Datasets

DatasetImagesMasksMasks/Image
COCO118k896k7.6
LVIS100k1.3M13
Open Images1.7M2.7M1.6
SA-1B11M1.1B100

Geographic Diversity

Unlike ImageNet (heavily US/European), SA-1B explicitly sought geographic balance. Images come from all inhabited continents, with deliberate oversampling of underrepresented regions. This matters because visual concepts vary by culture—buildings, food, clothing, signs all look different globally.

Privacy & Ethics

All faces and license plates were automatically blurred before release. Images were licensed from stock photo providers (not scraped from the web). Meta also conducted extensive bias audits—SAM performs equally well across perceived gender, skin tone, and age groups.

Training SAM required solving a chicken-and-egg problem: you need a good model to generate training data, but you need training data to make a good model. The solution was iterative co-training—improving model and data in alternating cycles.

Training Progression

Round 1
SA-1B v1

Train on COCO + LVIS + public datasets. Use this model for assisted manual annotation → 4.3M masks.

Round 2
SA-1B v2

Retrain on 4.3M masks. Now good enough for semi-automatic mode → 5.9M additional masks.

Round 3
SA-1B v3

Retrain on 10.2M masks. Quality high enough for fully automatic mode → 1.1B masks.

Final
Released

Final training on all 1.1B masks. This is the released SAM model.

Loss Functions

SAM uses a combination of losses to train the mask prediction:

L_focal = focal cross-entropy
L_dice = 1 - 2|A∩B|/(|A|+|B|)
L_iou = MSE(predicted_iou, actual_iou)

Focal loss handles class imbalance; Dice loss ensures good overlap; IoU loss trains the confidence head.

Training Hyperparameters

OptimizerAdamW
Learning Rate8e-4 (base)
Weight Decay0.1
Batch Size256
Training Steps~90k (2 epochs)
GPUs256 A100s

Prompt Simulation During Training

Since users will provide diverse prompts, training must simulate this diversity:

  • Random points: Sample points uniformly within ground-truth masks (foreground) and outside (background)
  • Noisy boxes: Ground-truth boxes with random jitter (simulating imprecise human drawing)
  • Mask prompts: Previous iteration's output (train for iterative refinement)
  • Mixed prompts: Combinations of the above (most realistic)

The most remarkable property of SAM is zero-shot generalization: it segments objects it has never seen in domains it was never trained on. This isn't transfer learning (fine-tuning on target domain)—it's true zero-shot, using SAM exactly as released.

Domains SAM Handles (Zero-Shot)

  • Medical: X-rays, CT scans, MRI, microscopy
  • Satellite: Aerial imagery, building footprints, agriculture
  • Scientific: Cell cultures, astronomical objects, materials
  • Art: Paintings, drawings, digital art, historical documents
  • Underwater: Marine life, coral reefs, underwater structures
  • Industrial: Defect detection, part segmentation, quality control

Why Does This Work?

SAM learned the concept of “object boundary” rather than specific object categories:

  • Visual universals: Edges, textures, and depth cues work across domains
  • Prompt grounding: The click says “segment HERE” regardless of what “here” is
  • Scale of training: 1B masks include enough edge cases to generalize

Benchmark: 23 Diverse Datasets

Meta evaluated SAM on 23 datasets spanning different domains, image types, and segmentation tasks. Key finding: SAM matches or exceeds supervised baselines on most datasets—without any training on them.

16/23
Datasets where SAM wins
5/23
Within 5% of best
2/23
Significant gap
0
Complete failures

Where Zero-Shot Struggles

SAM performs worse on: (1) extremely low-contrast images (infrared, certain medical modalities), (2) highly abstract boundaries (semantic segmentation like “road” vs “sidewalk”), and (3) very fine structures (hair strands, thin wires). These often need domain-specific fine-tuning.

A single click is inherently ambiguous. Click on a person's eye—do you want the eye, the face, or the whole person? SAM's solution: output multiple masks at different granularity levels, each with a confidence score. The user (or downstream system) picks the right one.

The Three Mask Scales

Whole

Largest valid object containing the click

Part

A meaningful sub-component

Subpart

Fine-grained detail

How It Works

The mask decoder has 3 output tokens instead of 1. Each token is trained to specialize in a different granularity level:

  • Token 0: Learns to predict whole objects
  • Token 1: Learns to predict parts
  • Token 2: Learns to predict subparts

During training, each token is matched to its closest ground-truth mask (Hungarian matching), which naturally causes specialization.

IoU Confidence Ranking

Each mask comes with a predicted IoU score (0 to 1). This lets you:

  • Default to highest: Just use the mask with highest IoU
  • Show all three: Let user pick the right granularity
  • Filter low confidence: Discard masks below threshold

Example: Click on a Car Wheel

Mask 1 (IoU: 0.92)

Just the wheel hub cap

Mask 2 (IoU: 0.89)

The entire wheel including tire

Mask 3 (IoU: 0.85)

The whole car

All three are “correct” segmentations—they just answer different interpretations of “what did the user mean?”

When Ambiguity Disappears

With sufficient prompts (multiple points, or a box), ambiguity resolves. SAM then returns a single mask (single-mask mode). Boxes are especially unambiguous: “segment the primary object inside this region.”

Explore SAM's architecture components interactively. These demos illustrate the key design decisions that make SAM work.

Architecture Flow

632M
Encoder params
~8M
Prompt + Decoder
~50ms
Per additional mask

Prompt Encoding

Prompt Encoding

PE(512, 410) + fg_embed

Sparse tokens: 1 (1 foreground point)

How it works: Each point becomes a 256-dim embedding via Fourier positional encoding + a learned type embedding (foreground/background). Boxes are encoded as 2 corner points with special corner embeddings. All sparse prompts are concatenated into a sequence that the mask decoder attends to.

Point Encoding
embed = PE(x, y) + type_embed

PE uses sin/cos at multiple frequencies

Box Encoding
embed = [PE(x₁,y₁) + tl, PE(x₂,y₂) + br]

Two corners with top-left/bottom-right embeddings

Mask Decoder Mechanics

Step 1: Input Assembly

Image embeddings (64×64×256) + prompt embeddings + 4 output tokens (3 masks + 1 IoU)

Two-Way Attention

Unlike standard cross-attention, SAM's decoder has bidirectional flow: tokens attend to image, AND image attends to tokens. This lets the image “understand” the prompt.

Why Only 2 Layers?

The heavy lifting is done by the image encoder. The decoder just needs to route information between already-rich representations. More layers didn't improve quality.

SAM was a breakthrough, but not a solution to all problems. Understanding its limitations helps you know when to use it and when to look elsewhere.

SAM's Limitations

  • No video: Each frame is independent—no temporal consistency
  • Slow encoder: 150ms per image limits real-time video
  • Fine structures: Hair, fur, thin objects are often imprecise
  • Semantic confusion: Can't distinguish “all dogs” from “this dog”
  • No text prompts: Points and boxes only (text was experimental)
  • Memory intensive: ViT-H requires significant GPU RAM

SAM 2 Improvements

  • Video support: Temporal memory propagates masks across frames
  • 6× faster: Hiera encoder replaces ViT for real-time video
  • Better boundaries: Improved training on fine structures
  • SA-V dataset: 51k videos with 600k+ masklets
  • Occlusion handling: Tracks objects through temporary occlusions
  • Streaming mode: Process video frame-by-frame efficiently

SAM vs SAM 2: When to Use Which

Use SAM (v1) when:
  • • Working with single images
  • • You need the extensively-tested original model
  • • Memory is extremely constrained
  • • Using existing SAM-based pipelines
Use SAM 2 when:
  • • Working with video or image sequences
  • • Real-time performance is needed
  • • Tracking objects across frames
  • • Better boundary precision is required

The Bigger Picture

SAM demonstrated that foundation models work for vision, not just language. The pattern—massive data, promptable interface, zero-shot transfer—is now being applied to 3D (SAM3D), medical imaging (MedSAM), and beyond. The era of training task-specific vision models is ending; the era of prompting general vision models has begun.

🎯 SAM: The Complete Picture

SAM proved that with enough scale, diversity, and the right task formulation, a single model can learn to segment anything. It's not just a model—it's a new paradigm for computer vision.

632M
Parameters
1.1B
Training Masks
~50ms
Per Mask
Object Types