TL;DR
SAM 2 is a promptable segmentation model for images and videos. A user clicks or draws a box on any frame, and the model returns the object's mask on that frame and then follows the object through the rest of the video, so later clicks can correct it. It works by adding a memory of past frames and predictions to the original Segment Anything design. The paper also builds a large video segmentation dataset, collected with the model in the loop.
Builds on: Segment Anything, Attention Mechanism, Vision Transformer (ViT), Masked Autoencoder, Intersection over Union · Leads to: Image Segmentation
Why it mattered
Before SAM 2, interactive video segmentation was a patchwork. Image models segmented each frame separately, which flickered and needed a click per frame, while video object segmentation methods needed a precise mask on the first frame and then tracked it on their own. SAM 2 combines the two modes in one model. A person can start on any frame, and a single refinement click on a later frame can recover an object the model has lost.
The paper reports that its data engine made annotation much faster: the time per annotated frame fell from tens of seconds in the first phase, which used the image model SAM on every frame, to a few seconds with the full SAM 2 in the loop. The authors release the model, the SA-V dataset and the training and demo code, so other groups can build on them.
The problem
Segmenting a single image is a question of where the object's boundary lies. Segmenting a video adds two problems. The first is consistency: the mask must follow the same object as it moves, changes shape, is occluded and comes back into view. The second is interaction: a user must be able to fix errors at any moment without starting over.
Earlier pipelines solved these with separate parts. A per-frame image model, such as Segment Anything, has no memory, so every frame that needs a fix must be annotated from scratch. A tracker that propagates a mask forward has no way to take a new click that changes earlier understanding of the object. The paper's task, promptable visual segmentation, asks for one model that does both. Prompts can be positive or negative clicks, boxes or masks, and they can arrive on any frame. The model must answer right away with a mask on the prompted frame, then propagate that mask through the video.
How it works
Here is the flow for one tiny case: a two-frame video of a ball, with a single positive click on frame 1.
- Encode the frame once. The image encoder, a hierarchical Hiera transformer pre-trained with masked autoencoding, turns frame 1 into a stack of feature maps at several resolutions. Check: the encoder runs once per frame for the whole session, not once per click.
- Condition on memory. On frame 1 there is no past, so the memory attention step passes the features through with no memory to attend to. Check: the same block would attend to stored memories on later frames.
- Decode with the click. The prompt encoder turns the click into a positional embedding plus a learned embedding for "positive click". The mask decoder combines these with the frame features and outputs several candidate masks, since one click can mean a whole object or a part of it. It also outputs an IoU score for each mask, and the one with the highest score is used. Check: a single click is ambiguous, so the model answers with options and a ranking.
- Write a memory. The memory encoder downsamples the predicted mask, adds it to the frame's unconditioned features, and stores the result in a memory bank. The mask decoder's output token is also stored as a small object pointer. Check: the memory now describes the ball on frame 1 in both spatial and semantic form.
- Propagate to frame 2. Frame 2 is encoded, then memory attention lets its features attend to the frame 1 memory and object pointers. The decoder predicts the ball's mask on frame 2 with no new click. Check: nothing on frame 2 was prompted, yet the prediction uses frame 1's memory.
- Accept a correction. If the ball is lost on frame 2, the user clicks it there. That click enters the memory bank as a prompted frame, so the fix propagates forward. Check: one correction is enough when memory still holds the object, which is the paper's central interaction claim.
flowchart LR A[New frame] --> B[Image encoder] M[Memory bank of past masks and pointers] --> C[Memory attention] B --> C P[Click or box on any frame] --> D[Mask decoder] C --> D D --> E[Mask and presence score] E --> F[Memory encoder] F --> M
The paper frames the design as a generalisation of SAM to video. The prompt encoder and mask decoder come from SAM, with a few changes. The model adds a presence head, since an object may be absent in some frames because of occlusion. It also adds skip connections from the high-resolution encoder features to the decoder, so that thin structures keep detail.
The math
Let be the features of frame from the image encoder. Memory attention updates them using the memory bank , which holds spatial memories and object pointers. Each block uses scaled dot-product attention,
where is the key dimension. The queries come from the current frame, and the keys and values come from the memory bank. This is the same operation as in Attention Mechanism, with the memory playing the role of the context.
Step 4 in symbols: writing the memory
The memory encoder takes the predicted mask and the frame features, and produces a spatial memory . The paper describes it as downsampling the mask with a convolutional module, adding it element-wise to the unconditioned frame embedding, and fusing the result with lightweight convolutions. The memory bank keeps two queues: up to recent unprompted frames, and up to prompted frames. Both store spatial features, and the object pointers are stored separately as vectors.
Step 3 in symbols: the mask decoder and its IoU score
For each candidate mask the decoder predicts a score that estimates its Intersection over Union with the true mask. The paper uses a sigmoid on the IoU logits, so the score lies between 0 and 1, and supervises it with an L1 loss. When there are several candidate masks on the first click, all of their IoU predictions are supervised, but only the best mask is supervised for the segmentation loss. This encourages the model to learn when a candidate is poor.
Training objective
The model is trained on sequences of 8 frames, with up to 2 frames that receive simulated prompts. The total loss is a weighted sum of four terms, with weights in the ratio 20:1:1:1 for
where and supervise the mask, is the mean absolute error on the IoU prediction, and is the cross-entropy on whether the object is present. The focal term is a weighted form of Cross-Entropy Loss that down-weights easy pixels, which matters because the object covers only part of each frame.
Worked examples
Setup: one frame, a positive click, and the model returns candidate masks with scores of 0.9 and 0.4.
- The model returns two candidate masks: a small one for the ball's centre and a large one for the whole table the ball sits on.
- The IoU predictions are 0.9 and 0.4.
- The model keeps the mask with the higher predicted IoU, 0.9, and writes it into memory.
- The user does nothing more on this frame.
Interpretation: a click is ambiguous, so the model shows what it can, ranks its guesses, and remembers the highest-ranked one.
Setup: a training sample where the focal loss is 0.10, the dice loss is 0.30, the IoU error is 0.05 and the object presence loss is 0.20. Compute the total loss.
- Multiply the focal loss by 20: 0.10 × 20 = 2.00.
- Multiply the dice loss by 1: 0.30.
- The IoU term has weight 1: 0.05.
- The presence term has weight 1: 0.20.
- Add the four terms: 2.00 + 0.30 + 0.05 + 0.20 = 2.55.
Interpretation: the mask terms dominate the total because the focal weight is 20, so the model spends most of its learning effort on the shape of the mask rather than on the presence or the IoU estimate.
Setup: a video of a dog runs across a park, and a second dog crosses in front of it at frame 40. The tracker follows the wrong dog from frame 40 onward. The annotator adds a positive click on the first dog at frame 60, and a negative click on the second dog at frame 60.
- The click at frame 60 is stored as a prompted frame in the memory bank, with the positive and negative labels.
- Memory attention on frame 61 attends to the frame 60 memory, the frame 1 memory and recent frames.
- The mask decoder now has two signals: the corrected mask from frame 60, and the recent predictions that drifted onto the wrong dog.
- The result is a new mask on frame 61 that follows the first dog, which then propagates forward.
Interpretation: the single correction works because the memory still holds the first dog's appearance from before the crossing. The paper's limitation section warns that the model can still confuse objects that look alike, so corrections remain necessary in crowds.
Try it: a clip has a red ball and an identical blue ball of the same size. The model tracks the red ball correctly until they touch, then switches to the blue one. Using the limitations, say what the annotator should do and why one correction may not be enough. Answer: add a correction on a frame after the crossing where the red ball is clearly visible, and possibly a negative click on the blue ball. The paper notes that objects with near-identical appearance remain a failure case, and that each object is handled separately, so a correction must be given to each one that is confused.
Results
The headline results, as the authors report them. Exact figures are in the paper's tables, so read those rather than this summary.
Interactive video segmentation. Across a set of zero-shot video datasets with a few clicks per frame, SAM 2 beats the baselines that pair SAM with a video tracker, and it needs fewer interactions to reach the same accuracy.
Semi-supervised video object segmentation. Given a mask on the first frame, SAM 2 scores above the best prior methods on the standard video benchmarks, with larger encoders giving higher scores at lower speed.
Image segmentation. On the image datasets used by SAM, SAM 2 with one click is slightly more accurate than SAM and runs several times faster.
Data engine. Each data-engine phase improved accuracy on the validation set, so the model-in-the-loop annotation adds measurable gains.
Limits. The paper's own limitations section is specific. The model can fail across shot changes, in crowded scenes, after long occlusions and in very long videos. It struggles with thin, fast-moving objects, and with nearby objects of similar appearance. It tracks each object separately with no communication between objects. The data engine still relies on human annotators to check quality, which the authors list as future work. The SA-V training data, the results and the speed numbers are all reported by the authors, so independent replication is still useful.
Reading guide
- Section 1, Introduction. Needs Segment Anything for the image-only design that SAM 2 extends.
- Section 3, Task. Needs Intersection over Union to read the mask quality numbers, and the idea of a prompt as a click, box or mask.
- Section 4, Model. Needs Attention Mechanism, Multi-Head Attention and Masked Autoencoder for the Hiera encoder's pre-training. Read the memory bank paragraphs slowly.
- Section 5, Data. Read it with the data engine table. The three phases are the same loop with a stronger assistant each time. Its per-frame timing and the claim about fewer edits are the basis of the efficiency argument.
- Section 6, Zero-shot experiments. Needs Image Segmentation for the mIoU metric and Object Detection for how segmentation differs from box detection.
- Section 7 and Appendix C. The comparison to prior video methods, and the limitations, are the parts that most affect how the results should be read.
Check yourself
QuestionRecall: what is the difference between the image encoder and the memory attention step in SAM 2?Show answer
The image encoder runs once per frame and turns the frame into feature maps, with no conditioning on the past. Memory attention then conditions those features on the memory bank of past predictions, the prompted frames and the object pointers. The encoder describes what is in the frame, and memory attention describes where the tracked object was.
QuestionComputation: a training sample has focal loss 0.2, dice loss 0.5, IoU error 0.1 and presence loss 0.3. What is the total loss using the paper's weights?Show answer
Apply the weights 20, 1, 1 and 1. The focal term is 0.2 × 20 = 4.0. The dice term is 0.5, the IoU term is 0.1 and the presence term is 0.3. The total is 4.0 + 0.5 + 0.1 + 0.3 = 4.9.
QuestionWhy: why does SAM 2 keep a separate queue for prompted frames and not treat them like recent frames?Show answer
Prompted frames hold human corrections, which are more reliable than the model's own predictions. Keeping them in their own queue means a correction is not pushed out when the queue of recent frames is trimmed, so it can keep guiding the tracking after many frames have passed.
QuestionApplied: a team uses SAM 2 to cut out a moving product from a long phone video, and the product briefly leaves the frame. What should they expect, and what should they do?Show answer
The model may lose the product after it leaves and re-enter, since the paper lists long occlusions and shot changes as failure cases. Expect the mask to drift or disappear for those frames, and check the presence head's output if it is exposed. The fix the paper suggests is a refinement click on a frame where the product is visible again. Because the model tracks each object separately, a second object in the frame needs its own clicks.
Sources
- Ravi et al., SAM 2: Segment Anything in Images and Videos (arXiv:2408.00714), https://arxiv.org/abs/2408.00714
- Kirillov et al., Segment Anything (arXiv:2304.02643), https://arxiv.org/abs/2304.02643
- Ryali et al., Hiera: A Hierarchical Vision Transformer without the Bells-and-Whistles (arXiv:2306.00989), https://arxiv.org/abs/2306.00989