SAM 2: Segment Anything in Images and Videos
- Publication
- ICLR 2025Conference
- First submitted to arXiv
- arXiv:2408.00714
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{segmentationsam22024,
title = {{SAM 2: Segment Anything in Images and Videos}},
author = {Nikhila Ravi and Valentin Gabeur and Yuan-Ting Hu and Ronghang Hu and Chaitanya Ryali and Tengyu Ma and Haitham Khedr and Roman Rädle and Chloe Rolland and Laura Gustafson and Eric Mintun and Junting Pan and Kalyan Vasudev Alwala and Nicolas Carion and Chao-Yuan Wu and Ross Girshick and Piotr Dollár and Christoph Feichtenhofer},
year = {2024},
eprint = {2408.00714},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2408.00714}
}
Soon after SAM came out, people started using it on video. The most common approach was to click the object with SAM on the first frame and then hand it to a video tracker, which passed the mask along frame by frame.
The problems with this surfaced quickly. The tracker can't necessarily hold on to every kind of object, and SAM isn't as handy on blurry, low-resolution video frames as it is on photos. Worst of all is what happens once the track is lost. You can only click again with SAM on the frame where it was lost, the tracker starts over from scratch, and everything accumulated before is thrown away.
In August 2024, Meta FAIR released SAM 2. It moves SAM into video and treats an image as a video with just one frame, handling both with the same model. This time, too, it came with the model, a demo and a new dataset.
Defining the Problem
Video segmentation brings several headaches that image segmentation doesn't have. Objects move, deform, get occluded and change appearance with the lighting; video quality is usually worse than a photo's, with shake and blur; and there are a great many frames, every one of which has to be processed.
The authors define the task as Promptable Visual Segmentation (PVS). You can give a prompt on any frame of the video, whether positive points, negative points, a box or a mask. The model has to return the mask for that frame immediately and then propagate it through the whole video to get a spatio-temporal mask, which the paper calls a masklet. Afterwards you can add more prompts on any frame to correct the masklet.

This figure shows the user experience PVS is after. A few clicks on frame 1 send the tongue mask on its way, but by frame 3 it has lost track. With SAM plus a tracker, you'd have to click several times again on frame 3, starting from scratch as on frame 1; SAM 2 recovers it with a single extra point on frame 3, because it remembers what the tongue looked like earlier.
The common setting of semi-supervised video object segmentation (semi-supervised VOS), where an annotated mask is given on the first frame and the model has to track it through the whole video, is just a special case of PVS.
Solving the Problem
Model

Remove the memory attention in the middle and the memory modules on the right, and the remaining image encoder, prompt encoder and mask decoder are SAM. What SAM 2 adds is that each frame's features, before entering the decoder, first read through "what has been seen of this object before."
Let's follow a video through it.
Image encoder. Frames come in one at a time and are processed as a stream, each encoded only once. The encoder has been changed to Hiera, a hierarchical ViT, also pretrained with MAE, that can output features at several resolutions. The stride-16 and stride-32 features are fused by an FPN into the frame's image embedding; the high-resolution stride-4 and stride-8 features bypass the memory modules and go straight into the mask decoder's upsampling layers to help draw finer boundaries.
Memory attention. The frame's image embedding then passes through 4 Transformer layers. Each layer first does self-attention, then cross-attention over the contents of the memory bank, and finally an MLP. If the memory bank is empty, as with a single image or the first frame of a video, this step effectively does nothing, and SAM 2 reduces to SAM.
Mask decoder. It's almost the same as SAM's, a two-way Transformer that outputs several masks when a single point is ambiguous. In video, ambiguity can persist across several frames; if the user gives no further prompts, only the mask with the highest predicted IoU for the current frame is passed on.
The decoder has one new output. When SAM handles a photo and is given a positive point, the object is certainly in the picture; in video, the object may be occluded or may leave the frame. So SAM 2 adds an occlusion prediction head that decides whether the target is actually present in this frame.
Memory encoder. Once the frame has been predicted, the output mask is downsampled with convolutions, added element-wise to the raw frame embedding from the image encoder, and fused through a few lightweight convolution layers to become this frame's memory. The memory has only 64 channels.
Memory bank. The memory bank holds two first-in, first-out queues, one storing the memories of the most recent frames (6 by default) and the other storing up to frames the user has prompted. In the traditional VOS setting, for example, the first frame's annotated mask always stays in the memory bank, plus the most recent 6 frames.
Besides these spatial memories, the memory bank also stores a list of object pointers. The token the mask decoder uses to output each frame's mask is itself a high-level summary of the object in that frame, and SAM 2 stores it as a lightweight vector. Memory attention reads both the spatial memories and these pointers.
Temporal information is added only to the memories of the most recent frames, so the model can sense the object's short-term motion; prompted frames don't get it. The authors' reasoning is that prompted frames are sparse during training, and at inference they may come from temporal distances very different from those seen in training, so adding it would hurt generalization.
Back to the puppy's tongue. By frame 3, the dog has turned its head and the tongue hangs down, with an angle and appearance unlike the first two frames, and SAM 2 loses it. The user adds a point on frame 3, which makes it a prompted frame and puts it into the memory bank's prompt queue; every later frame can read it, and the tongue stays tracked from then on.
Training
Images and videos are trained together. Each step takes an 8-frame clip and gives prompts on at most 2 randomly chosen frames, and during training corrective clicks are added probabilistically based on the annotations and the model's predictions, simulating user interaction. The initial prompt is an annotated mask with probability one half, a single positive point with probability one quarter and a box with probability one quarter.
Data Engine
SAM's data engine was run again on video, again in three phases, which differ in how much the model helps.
Phase 1: SAM frame by frame. Videos are sampled at 6 frames per second, and annotators label every frame from scratch with SAM plus a brush and eraser, with no tracking at all. It averages 37.8 seconds per frame, which is slow, but every frame is of high quality. This phase collected 1.4K videos and 16K masklets; SA-V's validation and test sets were also annotated this way, to avoid an evaluation bias toward SAM 2.
Phase 2: SAM plus a mask-only SAM 2. The first frame is labeled with SAM, and then a SAM 2 that accepts only mask prompts propagates it to the other frames. Wherever a frame goes wrong, it is relabeled from scratch with SAM on that frame and propagated again. Time drops to 7.4 seconds per frame, and 63.5K masklets were collected.
Phase 3: full SAM 2. Intermediate frames only need an occasional click to correct them, not labeling from scratch. Time drops to 4.5 seconds per frame, 8.4 times faster than Phase 1, and 197.0K masklets were collected.
After each masklet is finished, it goes to another group of annotators for verification; unsatisfactory ones are sent back for correction, and those whose target is itself ill-defined are discarded outright. In addition, the authors laid a grid of points on the first frame to have SAM 2 generate masklets automatically. Those that pass verification are added to the dataset, and those that fail are handed to Phase 3 for manual correction, which also exposes the model's weak spots.
Table 1 of the paper compares the three phases in a controlled experiment:
| Phase | Time per frame | Frames edited | Clicks per clicked frame | Aligned with Phase 1 (IoU>0.75) |
|---|---|---|---|---|
| Phase 1 | 37.8 s | 100% | 4.80 | – |
| Phase 2 | 7.4 s | 23.25% | 3.61 | 86.4% |
| Phase 3 | 4.5 s | 19.04% | 2.68 | 89.1% |
Phase 1 has the best quality, so it serves as the reference answer. Phase 3 is both fast and the closest to the reference.
The final SA-V dataset has 50.9K videos averaging 14 seconds, 54% indoor and 46% outdoor. Of its 642.6K masklets, 190.9K were annotated manually and 451.7K generated automatically, for 35.5 million masks in total, 53 times the largest video object segmentation dataset at the time (15 times counting only the manual annotations). Of the manually annotated masklets, 42.5% disappear in some frame and reappear later, so occlusion is quite frequent.
The data from every phase of the data engine was useful (Table 2 of the paper). With the number of training steps fixed, on the SA-V validation set, using only existing VOS datasets plus SA-1B gives 50.0; adding the data of the three phases in turn gives 53.0, 58.8 and 62.5, and adding the automatic masklets gives 63.2.
Discussion
The metric for video is . is the IoU of the mask and is the F-score of the boundary; the two are averaged, and higher is better.
Video Segmentation
The comparison is "SAM plus a tracker," with the trackers being XMem++ and Cutie, the two strongest VOS models at the time. On 17 video datasets never seen in training, with different prompts on the first frame (Table 4 of the paper):
| Method | 1 point | 3 points | 5 points | Box | GT mask |
|---|---|---|---|---|---|
| SAM + XMem++ | 56.9 | 68.4 | 70.6 | 67.6 | 72.7 |
| SAM + Cutie | 56.7 | 70.1 | 72.2 | 69.4 | 74.1 |
| SAM 2 | 64.7 | 75.3 | 77.6 | 74.4 | 79.3 |
The last column hands the annotated mask directly to XMem++ and Cutie, which is exactly how they were designed to be used, and SAM 2 is still more than 5 points higher. The gap is widest with a single point, about 8 points. From the multi-frame interaction experiments, the authors conclude that SAM 2 reaches better accuracy with fewer than a third of the interactions.
On the traditional semi-supervised VOS benchmarks (Table 6 of the paper), the Hiera-B+ version gets 76.6 on the MOSE validation set, against 71.7 for the previous best, Cutie-base+; on DAVIS 2017 it's 90.2 versus 88.1. The biggest gap is on SA-V itself, where earlier methods mostly stall in the low 60s and SAM 2 gets 76.8. As for speed, on a single A100 with batch size 1, Hiera-B+ runs at 43.8 frames per second and Hiera-L at 30.2, both within real-time range.
Image Segmentation
Taking SAM 2 back to SAM's 23 datasets for single-point segmentation (Table 5 of the paper):
| Model | Training data | SA-23 avg. mIoU (1 point) | FPS (A100) |
|---|---|---|---|
| SAM | SA-1B | 58.1 | 21.7 |
| SAM 2 | SA-1B | 58.9 | 130.1 |
| SAM 2 | SA-1B + video | 61.9 | 130.1 |
Trained only on SA-1B, SAM 2 is already a little better than SAM, at 6 times the speed. The authors credit this mainly to Hiera, an image encoder that is smaller than SAM's ViT-H but more effective. Training with video data added brings another 3 points on SA-23; on another 14 video datasets (evaluated as single images), it jumps from 56.6 to 69.6.
Which Designs Help
The appendix ablations hold a few more results (Tables 9 and 11 of the paper):
- Resolution. Raising the input from 512 to 1024 takes MOSE from 73.0 to 77.0, while speed drops to about a fifth.
- Number of frames. Going from 4 to 10 frames per training clip takes MOSE from 71.1 to 74.5.
- Image encoder. Going from S to L improves both image and video results; enlarging memory attention helps only on video.
- Object pointer. The average over 9 zero-shot datasets is unchanged, but the SA-V validation set goes from 64.5 to 68.3, and the long-video LVOSv2 from 67.0 to 71.6. When objects disappear and reappear, or the video is long, this list of high-level summaries is especially useful.
- GRU. Many VOS methods use a GRU to integrate memory; the authors tried it, and it didn't help. Storing memories directly in the memory bank is both simpler and faster.
Limitations
The authors list several failure cases. It fails at shot changes; in crowded scenes, under long occlusions or in very long videos, it may lose track or mix objects up; very thin, fast-moving objects are hard to track; and it's easily confused when identical-looking objects are nearby, the paper's example being a pile of identical juggling balls. The authors think adding more explicit motion modeling might help.
Another limitation lies in the architecture. SAM 2 can track several objects at once, but each object is processed separately, sharing only the per-frame image embedding, with no information exchanged between objects.
Conclusion
Compared with SAM, SAM 2 adds only memory. Memory attention, the memory encoder and the memory bank sit between the encoder and the decoder; each frame first reads the past few frames, the frames the user prompted and a list of object pointers before going to the decoder, and without memory it is just SAM. On video it gets better results with fewer clicks; on photos it's a little more accurate than SAM and 6 times faster.
When you download weights, watch the version. A footnote on page 2 of the paper states that every number in the paper comes from SAM 2.1 on GitHub.
Faced with things that look exactly alike, like the pile of juggling balls in the paper's own example, it still mixes them up. When it loses track, don't start over; add a point on that frame and it'll remember.