SAM2Long: Enhancing SAM 2 for Long Video Segmentation with a Training-Free Memory Tree

Shuangrui Ding, Rui Qian, Xiaoyi Dong, Pan Zhang, Yuhang Zang, Yuhang Cao, Yuwei Guo, Dahua Lin, Jiaqi Wang

TL;DR

SAM2Long is a training-free upgrade to Meta's SAM 2 that fixes its tendency to lose track of objects during long videos or complex occlusions. By replacing SAM 2's greedy frame-by-frame choices with a constrained tree search and uncertainty-aware memory tracking, it boosts long-term segmentation accuracy by up to 5.3 points with minimal computational overhead.

What Problem Does This Paper Address?

The Segment Anything Model 2 (SAM 2) is a powerful foundation model for video object segmentation (VOS). However, it suffers from a fatal flaw in long video sequences: error accumulation.

SAM 2 processes video frames sequentially using a greedy strategy. At each frame, it predicts multiple mask proposals but only selects the single mask with the highest predicted Quality (IoU) to update its memory bank. When an object becomes occluded, disappears, or passes through a confusing background, this greedy choice becomes highly uncertain. If SAM 2 selects an incorrect or partial mask into its memory, the error is uncorrectable. This corrupted memory then misleads predictions for all subsequent frames, causing the model's tracking performance to degrade drastically over time.

Figure 1. Comparison of occlusion handling and long-term capability between SAM 2 and SAM2Long. (a) When an occlusion occurs, SAM 2 may lose track or follow the wrong object, leading to accumulated errors. In contrast, SAM2Long utilizes memory tree search to recover when the object reappears. (b) Per-frame J &F scores of the predicted masks with different timestamp are plotted on the LVOS and SA-V datasets. SAM2Long demonstrates greater resilience to long video compared to SAM 2.

Methods

To break the cycle of error accumulation, the authors introduce SAM2Long, which replaces greedy selection with a heuristic tree-search strategy inspired by classic Multiple Hypothesis Tracking (MHT).

  1. Constrained Tree Memory: Instead of maintaining a single tracking history, SAM2Long maintains a fixed number ($P$, typically 3) of parallel memory pathways (hypotheses). At each new frame, the mask decoder generates 3 candidate masks for each pathway, expanding the tree to $3P$ candidate branches.
  2. Pruning via Cumulative Scores: To keep computation under control, the tree is pruned back down to $P$ pathways at the end of each frame. The pruning is guided by a cumulative score—the accumulated logarithm of predicted IoU scores along the pathway.
  3. Uncertainty Handling: If the model is highly uncertain (i.e., the absolute occlusion confidence scores across all pathways fall below a threshold $\delta_{conf}$), SAM2Long forces the tree to select distinctly shaped masks. It achieves this by rounding predicted IoU scores and ensuring only hypotheses with unique values are preserved, preventing the tree from prematurely converging on identical, incorrect tracking paths.
  4. Object-Aware Memory Filtering & Modulation: Within each pathway, frames with poor segmentation or high occlusion are filtered out of the memory bank entirely. Furthermore, during cross-attention, the keys are dynamically weighted based on their occlusion scores, forcing the model to pay more attention to reliable, unoccluded historical frames.

Crucially, this entire setup is training-free and introduces zero external parameters. Because the heavy image encoder only needs to run once per frame, branching the lightweight mask decoder and memory module adds negligible overhead.

Datasets

The authors evaluated SAM2Long across a comprehensive suite of benchmarks:

  • Video Object Segmentation (VOS): SA-V (validation and test sets), LVOS v1, LVOS v2, MOSE, VOST, PUMaVOS, DAVIS2017, and YTVOS.
  • Video Object Tracking (VOT): LaSOT, LaSOText, and GOT-10k (evaluated by converting predicted masks back into bounding boxes).

No new datasets were introduced in this work.

Key Insights and Ideas

  • Delayed Decisions Salvage Tracking: In ambiguous situations (like a dancer being momentarily blocked by another person), it is better to say "I'm not sure, let's keep the top 3 possibilities alive" and let future frames resolve the ambiguity.
  • Forced Diversity Beats Raw Scores: When a model is confused, its top three scoring masks might just be minor variations of the same wrong object. Forcing the selection of masks with distinct IoU scores preserves structural diversity in the tracking hypotheses.
  • Exploiting the Encoder-Decoder Bottleneck: In modern vision models, the image encoder consumes the vast majority of GFlops. By restricting the tree search strictly to the lightweight mask decoder and memory attention layers, SAM2Long gains massive robustness at a fraction of the computational cost.

Key Results

SAM2Long consistently and significantly outperforms SAM 2 across all model sizes (Tiny, Small, Base+, Large) and weight versions (including the updated SAM 2.1):

  • Long-Term Benchmarks: On the challenging SA-V test set, SAM2Long-Large improves the $\mathcal{J}\&\mathcal{F}$ score by 5.3 points over standard SAM 2. On the LVOS v2 dataset, it yields a 3.5 point increase for the Small model configuration.
  • Error Resilience Over Time: As shown in the per-frame performance curves, the accuracy gap between SAM2Long and SAM 2 widens as the video progresses, validating its success in mitigating error accumulation.
  • Efficiency: Maintaining 3 pathways ($P=3$) results in only a 14% FPS slowdown, an 8% increase in GFlops, and a minor 4% increase in GPU memory usage.

Limitations and Future Work

  • Bounded by Base Capacity: Because it is a training-free wrapper, SAM2Long cannot fix fundamental representation failures of the underlying SAM 2 image encoder or mask decoder.
  • Single-Object Focus: The tree-growth and pruning logic is optimized primarily for tracking individual targets independently. Scaling this framework to handle complex, multi-object combinatorial interactions efficiently remains an open engineering challenge.

Summary updated

Abstract

The Segment Anything Model 2 (SAM 2) has emerged as a powerful foundation model for object segmentation in both images and videos, paving the way for various downstream video applications. The crucial design of SAM 2 for video segmentation is its memory module, which prompts object-aware memories from previous frames for current frame prediction. However, its greedy-selection memory design suffers from the "error accumulation" problem, where an errored or missed mask will cascade and influence the segmentation of the subsequent frames, which limits the performance of SAM 2 toward complex long-term videos. To this end, we introduce SAM2Long, an improved training-free video object segmentation strategy, which considers the segmentation uncertainty within each frame and chooses the video-level optimal results from multiple segmentation pathways in a constrained tree search manner. In practice, we maintain a fixed number of segmentation pathways throughout the video. For each frame, multiple masks are proposed based on the existing pathways, creating various candidate branches. We then select the same fixed number of branches with higher cumulative scores as the new pathways for the next frame. After processing the final frame, the pathway with the highest cumulative score is chosen as the final segmentation result. Benefiting from its heuristic search design, SAM2Long is robust toward occlusions and object reappearances, and can effectively segment and track objects for complex long-term videos. Notably, SAM2Long achieves an average improvement of 3.0 points across all 24 head-to-head comparisons, with gains of up to 5.3 points in J&F on long-term video object segmentation benchmarks such as SA-V and LVOS. The code is released at https://github.com/Mark12Ding/SAM2Long.
BibTeX
@inproceedings{ding2025sam,
  author = {Shuangrui Ding and Rui Qian and Xiaoyi Dong and Pan Zhang and Yuhang Zang and Yuhang Cao and Yuwei Guo and Dahua Lin and Jiaqi Wang},
  title = {{SAM2Long: Enhancing SAM 2 for Long Video Segmentation with a Training-Free Memory Tree}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2025},
}
Figure