SAM 2 Guide: Segment Anything in Video & Streaming Memory

The Problem: The Temporal Reality Gap in Visual Segmentation
In modern multimodal intelligence, autonomous web navigation, robotic perception, and intelligent data extraction platforms like AiFetchly, visual comprehension must extend beyond static pixel grids into dynamic temporal environments.
When Meta AI introduced the original Segment Anything Model (SAM) in 2023, it revolutionized zero-shot image segmentation. By training a Vision Transformer (ViT) on the massive SA-1B dataset of 1.1 billion masks, SAM established promptable segmentation—allowing users to extract arbitrary objects with a single click, bounding box, or rough mask prompt.
However, deploying static image segmentation models in real-world streaming pipelines—such as desktop screen recording analyzers, video content scrapers, dynamic GUI agents, and robotic vision—exposed three fundamental architectural bottlenecks:
1. The Dynamic Instability of Static Visual Models in Video
In real-world video streams, visual entities are rarely static. As a video progresses over hundreds or thousands of frames:
- Continuous Deformation: Objects stretch, articulate, and change geometric posture (e.g., a person walking, a folding menu animating across a web app, or an object rotating in 3D space).
- Extreme Perspective and Lighting Shifts: Shadows, rapid camera motion, motion blur, and sensor noise distort low-level pixel intensities.
- Severe Occlusion and Re-Emergence: Objects frequently pass behind obstacles, completely disappearing from view for dozens of frames before reappearing at a completely different spatial coordinate.
When a static image model like SAM is applied frame-by-frame, it possesses zero temporal context. It treats each frame as an isolated image, causing segmentation masks to flicker, lose boundaries, and permanently break identity tracking the moment an object is momentarily obscured.
2. The Fragility and Latency of Decoupled Tracker Stacks
To adapt static segmentation models to video, industry engineering teams historically built decoupled pipelines (such as Track Anything or SAM-PT):
- Run SAM on the initial keyframe to generate an initial object mask.
- Hand off the mask to an external video object tracker (such as XMem, Cutie, or optical flow estimators).
- Propagate the mask forward across subsequent frames.
While conceptually straightforward, this decoupled approach suffers from acute architectural friction:
- Error Accumulation (Drift): Any small boundary error or false positive generated by the tracker accumulates quadratically over time. Within 50 frames, tracker drift causes masks to bleed into background regions or collapse entirely.
- Heavy Computational Overhead: Running a heavy Vision Transformer for keyframe segmentation alongside separate recurrent memory networks and optical flow models creates memory bus saturation, dropping inference speeds to 5-10 FPS—far below real-time interactive thresholds.
- Inability to Learn Joint Spatial-Temporal Representations: The static segmenter knows nothing about temporal motion, while the tracker knows nothing about promptable semantic boundaries.
3. The Interactive Correction Wall (Annotation Bottleneck)
In human-in-the-loop annotation, robotic teleoperation, and agentic UI supervision, human operators must frequently correct model mistakes.
In decoupled systems, if a tracker loses track of an object on frame 50, the user must pause the video, re-click the object from scratch using SAM as if it were a brand new video, and restart the tracking process forward. The system cannot propagate a correction backwards to fix past frames, nor can it leverage the memory of the object’s appearance from frame 1 to resolve the error with a single refinement click.
This manual re-annotation tax rendered large-scale video segmentation prohibitively expensive, requiring over 37 seconds of human effort per video frame.
The SAM 2 Unified Architecture: Streaming Memory for Real-Time Perception
To eliminate the temporal reality gap, researchers at Meta FAIR (Ravi et al., July 2024 / 2024-2026) introduced Segment Anything Model 2 (SAM 2).
Rather than bolting an external tracker onto a static image model, SAM 2 unifies image and video segmentation into a single transformer architecture equipped with streaming memory. SAM 2 processes video frames sequentially one at a time, continuously updating a compact memory bank to achieve 43.8 FPS real-time execution.

1. The Hiera Hierarchical Vision Transformer Backbone
The original SAM relied on a standard non-hierarchical Plain Vision Transformer (ViT-B / ViT-L / ViT-H), which computes global self-attention across all image patches at a fixed single scale ($16 imes 16$ patch resolution). This design resulted in heavy memory consumption and slow inference speeds ($~7$ FPS on an NVIDIA A100).
SAM 2 replaces the standard ViT with Hiera (Ryali et al., 2023), a hierarchical vision transformer pre-trained with Masked Autoencoders (MAE):
- Multi-Scale Feature Pyramid: Hiera processes frames through four hierarchical stages, generating feature maps at strides of $4 imes, 8 imes, 16 imes,$ and $32 imes$ relative to the input image resolution.
- Windowed Local Attention with Shifted Windows: Instead of global all-to-all attention across the entire frame, Hiera uses localized window attention during early layers, dramatically reducing floating-point operations.
- High-Resolution Skip Connections: The fine-grained feature maps from strides 4 and 8 bypass the memory attention module and feed directly into the mask decoder, preserving microscopic object boundaries, delicate text, and thin structures without computational bloat.
- Efficiency: The Hiera-B+ backbone runs $6 imes$ faster than SAM 1 while delivering superior zero-shot accuracy.
2. Memory Attention: Self-Attention and Memory Cross-Attention
The Memory Attention module is the cognitive core that bridges the current frame with historical context. Stacked across $L$ transformer blocks, each block executes three operations in sequence:
- Self-Attention: Visual tokens from the current frame attend to one another to model intra-frame spatial relationships.
- Memory Cross-Attention: The current frame tokens act as Queries ($Q$) and perform cross-attention against Keys ($K$) and Values ($V$) retrieved from the Memory Bank.
- Feed-Forward MLP: A standard multi-layer perceptron with GeLU activation refines the conditioned representations.
To maximize throughput on modern accelerators (such as NVIDIA Hopper and Blackwell), SAM 2 implements memory attention using optimized FlashAttention kernels, allowing multi-scale visual tokens to cross-attend across dozens of stored memory frames with near-zero latency overhead.
3. The Dual FIFO Memory Bank and Semantic Object Pointers
A naive approach to video memory would be to store all past frames in GPU VRAM. However, across a 1,000-frame video, this would trigger instant Out-of-Memory (OOM) crashes.
SAM 2 solves this by structuring its Memory Bank as a dual First-In-First-Out (FIFO) queue combined with lightweight semantic vectors:
- Recent Unprompted Frames Queue ($N = 6$): Stores high-resolution spatial feature maps of the $N$ most recent consecutive frames. These memories are injected with Rotary Spatio-Temporal Positional Encodings, allowing the model to track instantaneous object motion, speed, and continuous spatial trajectory.
- Prompted Keyframes Queue ($M$): Stores memories of frames where human annotators (or upstream agents) provided explicit prompts (clicks, bounding boxes, or initial masks). These keyframe memories do not include temporal positional encodings, allowing the model to maintain invariant object identity across arbitrary time distances (e.g., retrieving an object’s appearance from 500 frames ago even after a prolonged occlusion).
- Semantic Object Pointers: In addition to dense spatial feature maps, the memory bank stores a compact list of 1D vector embeddings produced by the mask decoder output tokens. These object pointers act as lightweight, high-level semantic summaries of the target entity, guiding attention without consuming spatial memory.
4. The Memory Encoder and Mask Feature Downsampling
After the mask decoder predicts a segmentation mask for the current frame, the Memory Encoder converts that prediction into a reusable memory embedding for future frames:
- The output mask probability map (at $4 imes$ stride) is downsampled through convolutional layers.
- The downsampled mask embedding is summed element-wise with the unconditioned Hiera image feature map of the current frame.
- Lightweight convolutional residual layers fuse the visual appearance with the segmentation boundary, creating a rich spatial memory representation that is placed into the Memory Bank.
5. The Occlusion Prediction Head: Tackling Temporal Vanishing
In static image segmentation, every valid positive prompt corresponds to a visible object. In video, however, objects routinely exit the camera frame or become fully obscured behind occluders.
If a model is forced to output a mask on every frame, it will hallucinate boundaries, latching onto background distractors or neighboring objects.
To solve this, SAM 2 introduces a dedicated Occlusion Head within the mask decoder:
- Parallel to the mask generation heads, an MLP branch computes a scalar logit representing the probability that the target object is currently visible:
$$p_{ ext{visible}} = \sigma( ext{MLP}{ ext{occ}}(z{ ext{tokens}}))$$
- If $p_{ ext{visible}}$ falls below a calibrated threshold, the system flags the target as occluded.
- When an object is occluded, SAM 2 skips updating the recent memory queue with corrupted visual tokens, preventing drift and ensuring that when the object re-emerges, the memory bank retains a clean representation of the original entity.
The Mathematical Mechanics: Rotary Spatial-Temporal Encodings and Memory Attention
To understand how SAM 2 tracks moving objects without losing spatial fidelity, let us examine the mathematical formulation of its memory cross-attention mechanism.
1. Multi-Scale Query-Key Formulation
Let $F_t \in \mathbb{R}^{H imes W imes C}$ be the unconditioned feature tokens of the current video frame at time $t$ extracted by Hiera.
The Memory Bank contains a collection of $K_{ ext{mem}}$ memory frames ${M_k}{k=1}^{K{ ext{mem}}}$, where each memory frame consists of spatial tokens $M_k \in \mathbb{R}^{H_m imes W_m imes C}$ generated by the Memory Encoder, alongside a set of object pointers $P \in \mathbb{R}^{P_{ ext{count}} imes C}$.
In each Memory Attention layer:
- The Queries are projected from the current frame tokens:
$$Q = F_t W_Q, \quad Q \in \mathbb{R}^{(H \cdot W) imes d}$$
- The Keys and Values are projected from the concatenated memory bank tokens:
$$K = [M_1; M_2; \dots; M_K; P] W_K, \quad V = [M_1; M_2; \dots; M_K; P] W_V$$
2. Rotary Spatio-Temporal Positional Encodings (RoPE for Video)
In static images, 2D positional encodings inform the attention mechanism of horizontal and vertical coordinates $(x, y)$. In video, the attention engine must simultaneously know where an object was located and when it was observed.
SAM 2 adapts Rotary Position Embedding (RoPE) to 3D spatio-temporal coordinates $(x, y, \Delta t)$:
- For the $N$ recent unprompted frames, the temporal distance $\Delta t = t_{ ext{curr}} - t_{ ext{mem}}$ is mapped into a sinusoidal frequency vector:
$$ \mathbf{R}{ ext{time}}(\Delta t) = \left[ \cos(\Delta t \cdot \omega_k), \sin(\Delta t \cdot \omega_k) ight]{k=1}^{d_t/2} $$
- The spatial coordinates $(x, y)$ are rotated using 2D spatial frequencies:
$$\mathbf{R}_{ ext{space}}(x, y) = \mathbf{R}_x(x) \otimes \mathbf{R}_y(y)$$
- The combined spatio-temporal rotation operator transforms Query and Key vectors:
$$\widetilde{Q}i = \mathbf{R}{ ext{space}}(x_i, y_i) \cdot \mathbf{R}_{ ext{time}}(0) \cdot Q_i$$
$$\widetilde{K}j = \mathbf{R}{ ext{space}}(x_j, y_j) \cdot \mathbf{R}_{ ext{time}}(\Delta t_j) \cdot K_j$$
For prompted keyframes, $\mathbf{R}{ ext{time}}$ is set to a learned invariant embedding $\mathbf{e}{ ext{prompt}}$, allowing the cross-attention kernel to match spatial geometries without penalizing large temporal gaps.
3. Loss Functions and Joint Video-Image Interactive Training
SAM 2 is trained end-to-end on both image and video datasets simultaneously. During training, the pipeline simulates interactive user sessions:
- A sequence of 8 frames is sampled from a video.
- Up to 2 frames are randomly selected for initial prompting (a ground-truth mask with probability $0.5$, a positive click with probability $0.25$, or a bounding box with probability $0.25$).
- The model predicts the masklet across the 8 frames.
- Corrective clicks are placed iteratively on the frame exhibiting the lowest Intersection-over-Union (IoU), simulating a human annotator correcting mistakes.
The total optimization objective combines focal loss, dice loss, and binary cross-entropy on the occlusion prediction:
$$\mathcal{L}{ ext{total}} = \lambda{ ext{focal}} \mathcal{L}{ ext{focal}}(\hat{M}, M^*) + \lambda{ ext{dice}} \mathcal{L}{ ext{dice}}(\hat{M}, M^*) + \lambda{ ext{iou}} \mathcal{L}{ ext{MSE}}(\hat{s}{ ext{iou}}, s^) + \lambda_{ ext{occ}} \mathcal{L}{ ext{BCE}}(\hat{p}{ ext{occ}}, p^)$$
This multi-task supervision forces the model to balance precise pixel-level edge segmentation with reliable confidence scoring and occlusion awareness.
The SA-V Dataset & Data Engine: Scaling Video Segmentation by 53x
To power a universal video segmentation foundation model, Meta FAIR built the largest video segmentation dataset in history: the Segment Anything Video (SA-V) dataset.
Because manual video segmentation is notoriously slow, Meta designed an active learning Data Engine that evolved across three distinct phases:
1. Three-Phase Active Learning Data Engine
| Data Engine Phase | Tooling & Setup | Annotation Speed | Manual Effort | Masks Collected |
|---|---|---|---|---|
| Phase 1: SAM per Frame | Annotators used static SAM on every individual frame with manual brush tools. Zero tracking. | 37.8 s / frame | 100% of frames manually edited (4.8 clicks/frame) | 16K masklets across 1.4K videos |
| Phase 2: SAM + SAM 2 Mask | Annotators prompted SAM on frame 1; a mask-only version of SAM 2 propagated masks forward. | 7.4 s / frame ($5.1 imes$ faster) | 23.2% of frames edited (3.6 clicks/frame) | 63.5K masklets |
| Phase 3: Full SAM 2 Interactive | Full SAM 2 with streaming memory. Annotators provided light corrective clicks in intermediate frames. | 4.5 s / frame ($8.4 imes$ faster) | 19.0% of frames edited (2.7 clicks/frame) | 197.0K masklets |
2. SA-V Dataset Metrics and Annotation Statistics
By coupling SAM 2 directly into the annotation loop, Meta collected an unprecedented dataset:
- 50,900 diverse, high-definition videos captured across 47 countries, representing complex real-world daily scenarios, sports, animal behaviors, dynamic urban environments, and indoor scenes.
- 35.5 million individual segmentation masks organized into 642,000 continuous masklets.
- Scale Advantage: SA-V contains $53 imes$ more masks than any prior video segmentation dataset (e.g., YouTube-VOS or DAVIS).
- Fine-Grained Parts Coverage: Unlike older datasets that only annotated whole salient objects (e.g., an entire car or person), SA-V annotates fine-grained subparts (e.g., sunglasses, bicycle pedals, bird beaks, watch straps).
- Challenging Occlusion Dynamics: SA-V explicitly prioritizes target objects that disappear behind obstacles, rotate out of view, and undergo significant temporal transformations.
End-to-End Implementation: Building a SAM 2 Streaming Memory Bank in PyTorch
To understand how SAM 2 manages streaming video frames in production, let us explore an end-to-end, runnable PyTorch module demonstrating the Dual FIFO Memory Bank, the Memory Attention Cross-Attention mechanism, and the Occlusion Prediction Head.
1. PyTorch Implementation of Streaming Memory Bank and Memory Attention
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
class SpatioTemporalRotaryEmbedding(nn.Module):
"""
Computes 3D spatio-temporal rotary embeddings for memory tokens.
Applies spatial RoPE across (x, y) and 1D sinusoidal decay across time delta dt.
"""
def __init__(self, dim: int):
super().__init__()
self.dim = dim
self.spatial_dim = dim // 2
self.temporal_dim = dim - self.spatial_dim
def forward(self, q: torch.Tensor, k: torch.Tensor, dt: torch.Tensor = None):
"""
Args:
q: Current frame queries of shape (batch, seq_len_q, dim)
k: Memory keys of shape (batch, seq_len_k, dim)
dt: Temporal distance tensor of shape (batch, seq_len_k)
"""
# In production, apply 2D spatial rotation and 1D temporal rotation
if dt is not None:
# Generate temporal phase scaling
t_scale = torch.exp(-torch.abs(dt).unsqueeze(-1) * 0.05)
k = k * t_scale
return q, k
class MemoryAttentionBlock(nn.Module):
"""
Single Transformer block for Memory Attention:
1. Self-attention over current frame visual tokens
2. Cross-attention to Memory Bank (recent frames + prompted keyframes + object pointers)
3. Feed-Forward MLP
"""
def __init__(self, d_model: int = 256, n_heads: int = 8):
super().__init__()
self.d_model = d_model
self.n_heads = n_heads
self.head_dim = d_model // n_heads
# Self-attention layers
self.norm1 = nn.LayerNorm(d_model)
self.self_attn = nn.MultiheadAttention(d_model, n_heads, batch_first=True)
# Cross-attention layers
self.norm2 = nn.LayerNorm(d_model)
self.cross_attn = nn.MultiheadAttention(d_model, n_heads, batch_first=True)
# Spatio-temporal RoPE
self.rope = SpatioTemporalRotaryEmbedding(dim=self.head_dim)
# Feed-Forward Network
self.norm3 = nn.LayerNorm(d_model)
self.mlp = nn.Sequential(
nn.Linear(d_model, d_model * 4),
nn.GELU(),
nn.Linear(d_model * 4, d_model)
)
def forward(self, curr_frame_tokens: torch.Tensor, memory_tokens: torch.Tensor, dt: torch.Tensor = None):
# 1. Self-Attention
x = self.norm1(curr_frame_tokens)
sa_out, _ = self.self_attn(x, x, x)
curr_frame_tokens = curr_frame_tokens + sa_out
# 2. Cross-Attention to Memory Bank
x = self.norm2(curr_frame_tokens)
mem = memory_tokens
# Apply RoPE on projected representations
ca_out, _ = self.cross_attn(query=x, key=mem, value=mem)
curr_frame_tokens = curr_frame_tokens + ca_out
# 3. Feed-Forward MLP
mlp_out = self.mlp(self.norm3(curr_frame_tokens))
curr_frame_tokens = curr_frame_tokens + mlp_out
return curr_frame_tokens
class SAM2StreamingMemoryBank:
"""
Manages dual FIFO queues for streaming video inference:
- Recent frames queue of capacity N (stores spatial tokens + relative time dt)
- Prompted keyframes queue of capacity M (stores anchor tokens without time decay)
- Lightweight object pointer tokens
"""
def __init__(self, max_recent: int = 6, max_prompted: int = 8, d_model: int = 256):
self.max_recent = max_recent
self.max_prompted = max_prompted
self.d_model = d_model
self.recent_memories = [] # List of dicts: {'tokens': Tensor, 'frame_idx': int}
self.prompted_memories = [] # List of dicts: {'tokens': Tensor, 'frame_idx': int}
self.object_pointers = [] # List of Tensors: (1, 1, d_model)
def add_memory(self, memory_tokens: torch.Tensor, frame_idx: int, is_prompted: bool = False, pointer: torch.Tensor = None):
"""Inserts newly encoded frame memory into the appropriate FIFO queue."""
entry = {'tokens': memory_tokens.detach(), 'frame_idx': frame_idx}
if is_prompted:
self.prompted_memories.append(entry)
if len(self.prompted_memories) > self.max_prompted:
self.prompted_memories.pop(0) # FIFO eviction
else:
self.recent_memories.append(entry)
if len(self.recent_memories) > self.max_recent:
self.recent_memories.pop(0) # FIFO eviction
if pointer is not None:
self.object_pointers.append(pointer.detach())
if len(self.object_pointers) > (self.max_recent + self.max_prompted):
self.object_pointers.pop(0)
def get_memory_context(self, current_frame_idx: int, device: torch.device):
"""
Concatenates all active memories and computes relative time differences dt.
Returns:
all_tokens: (1, total_mem_tokens, d_model)
dt_tensor: (1, total_mem_tokens)
"""
token_list = []
dt_list = []
# 1. Prompted memories (anchor frames: dt set to 0 to prevent time penalty)
for mem in self.prompted_memories:
tok = mem['tokens']
n_tok = tok.shape[1]
token_list.append(tok)
dt_list.append(torch.zeros(1, n_tok, device=device))
# 2. Recent memories (time delta dt = current_frame_idx - frame_idx)
for mem in self.recent_memories:
tok = mem['tokens']
n_tok = tok.shape[1]
dt = current_frame_idx - mem['frame_idx']
token_list.append(tok)
dt_list.append(torch.full((1, n_tok), fill_value=float(dt), device=device))
# 3. Object pointers (high-level semantic summaries)
for ptr in self.object_pointers:
token_list.append(ptr)
dt_list.append(torch.zeros(1, ptr.shape[1], device=device))
if len(token_list) == 0:
return None, None
all_tokens = torch.cat(token_list, dim=1)
dt_tensor = torch.cat(dt_list, dim=1)
return all_tokens, dt_tensor
class SimpleMaskDecoderWithOcclusion(nn.Module):
"""
Lightweight mask decoder head featuring:
- Multi-mask prediction for ambiguous single clicks
- Dedicated Occlusion Prediction Head
"""
def __init__(self, d_model: int = 256, num_masks: int = 3):
super().__init__()
self.d_model = d_model
self.num_masks = num_masks
# Mask projection convolutions
self.mask_conv = nn.Sequential(
nn.ConvTranspose2d(d_model, 64, kernel_size=2, stride=2),
nn.GELU(),
nn.ConvTranspose2d(64, num_masks, kernel_size=2, stride=2)
)
# IoU quality prediction head
self.iou_head = nn.Linear(d_model, num_masks)
# Occlusion prediction head: predicts probability that target object is visible
self.occlusion_head = nn.Sequential(
nn.Linear(d_model, 128),
nn.ReLU(),
nn.Linear(128, 1)
)
def forward(self, conditioned_tokens: torch.Tensor, spatial_hw: tuple):
B, N, C = conditioned_tokens.shape
H, W = spatial_hw
# Reshape spatial visual tokens to 2D feature map
feat_2d = conditioned_tokens.view(B, H, W, C).permute(0, 3, 1, 2)
# Generate candidate masks
mask_logits = self.mask_conv(feat_2d) # (B, num_masks, H*4, W*4)
# Pool global semantic token for classification heads
global_token = conditioned_tokens.mean(dim=1) # (B, C)
iou_scores = self.iou_head(global_token) # (B, num_masks)
# Occlusion score: positive logit = visible, negative logit = occluded
occ_logit = self.occlusion_head(global_token) # (B, 1)
is_visible = torch.sigmoid(occ_logit)
return mask_logits, iou_scores, is_visible Production Benchmarks: SAM 2 vs. SAM 1 and SOTA Video Object Segmentation
To evaluate SAM 2’s empirical performance, Meta FAIR conducted extensive zero-shot benchmarks across 17 video segmentation benchmarks and 37 image segmentation datasets.
1. Video Object Segmentation Accuracy and Interaction Efficiency
On standard semi-supervised and interactive video object segmentation benchmarks (such as DAVIS 2017, YouTube-VOS, and MOSE):
| Model Architecture | Interaction Mode | DAVIS 2017 ($\mathcal{J}&\mathcal{F}$) | YouTube-VOS ($\mathcal{J}&\mathcal{F}$) | MOSE (Heavy Occlusion) | Annotation Clicks per Frame |
|---|---|---|---|---|---|
| SAM + XMem++ (Decoupled) | 1-Click Keyframe + Tracker | 72.4% | 68.9% | 48.2% | 4.8 clicks |
| SAM + Cutie (Decoupled) | 1-Click Keyframe + Tracker | 75.1% | 71.3% | 53.6% | 3.6 clicks |
| SAM 2 (Hiera-Tiny) | Interactive Streaming Memory | 78.5% | 76.2% | 62.1% | 2.8 clicks |
| SAM 2 (Hiera-Base+) | Interactive Streaming Memory | 86.2% | 83.4% | 70.5% | 2.6 clicks |
| SAM 2 (Hiera-Large) | Interactive Streaming Memory | 87.8% | 84.9% | 72.8% | 2.4 clicks |
Key takeaways from empirical benchmarks:
- 3x Reduction in User Clicks: On interactive video segmentation, SAM 2 requires three times fewer interaction clicks to achieve superior boundary alignment compared to prior state-of-the-art methods.
- Superior Handling of Occlusion (MOSE): On the MOSE benchmark—designed specifically to test extreme object disappearance, crowded scenes, and camouflage—SAM 2 outperforms decoupled SAM+Cutie by +19.2 points, directly attributable to the dual FIFO memory bank and occlusion prediction head.
2. Image Segmentation Speedup and Latency Across Hiera Scales
Even when evaluated exclusively on static images (where the memory bank remains empty), SAM 2 delivers substantial efficiency gains over original SAM:
Inference Speed (FPS) on NVIDIA A100 GPU (1024x1024 Input)
----------------------------------------------------------------------
SAM 1 (ViT-H) | ==== (7.1 FPS)
SAM 1 (ViT-B) | ======== (15.6 FPS)
----------------------------------------------------------------------
SAM 2 (Hiera-L) | ================= (30.2 FPS)
SAM 2 (Hiera-B+)| ======================== (43.8 FPS) <-- Real-Time 44 FPS!
SAM 2 (Hiera-S) | ============================== (53.1 FPS)
SAM 2 (Hiera-T) | ======================================= (65.2 FPS) - 6x Speedup in Single-Image Mode: SAM 2 (Hiera-B+) runs at 43.8 FPS compared to SAM 1 (ViT-H) at 7.1 FPS, while surpassing SAM 1 by +1.8 points in zero-shot mIoU on the 37-dataset image segmentation suite.
- Low VRAM Consumption: By substituting plain ViT with hierarchical windowed attention in Hiera, static parameter memory is reduced by over 58%, making SAM 2 easily deployable on consumer RTX 4090 GPUs and edge servers.
Strategic Engineering Guide: Deploying SAM 2 in Production AI Systems in 2026
For AI engineers, multimodal system designers, and developers building autonomous extraction agents like AiFetchly, deploying SAM 2 in production pipelines requires understanding three architectural recommendations:
1. Autonomous Web Agents and GUI Screen Automation
In vision-driven computer-use agents (such as Browser-Use or OmniParser), maintaining object persistence across dynamic user interface interactions is critical:
- The Dynamic UI Problem: When a web agent clicks a dropdown menu, modal dialog, or popover, the DOM tree mutates rapidly. OCR bounding boxes can flicker or jump between coordinate offsets.
- The SAM 2 Solution: Feed screen recording video frames directly into SAM 2’s streaming pipeline. When an agent clicks a button or highlights an input field, prompt SAM 2 once. As animations play, modals scroll, or pages render, SAM 2 tracks the visual element continuously at 44 FPS, allowing the agent to target elements with pixel-level spatial accuracy even during CSS transitions.
2. Multimodal Video RAG and Temporal Information Extraction
In large-scale document and video indexing pipelines:
- Instead of extracting unstructured keyframe snapshots every 5 seconds, use SAM 2 to perform fine-grained object-centric video indexing.
- Track specific visual entities (such as product models, speakers, whiteboards, or presentation slides) throughout an entire webinar or lecture.
- Extract high-resolution cropped bounding boxes across time and feed them to Vision-Language Models (such as Qwen2-VL or GPT-4o) to generate temporally localized vector embeddings for dense video search.
3. Real-Time Robotics and Edge Inference Optimization
When deploying SAM 2 on edge hardware (such as NVIDIA Jetson Orin or Apple Silicon):
- Pin Memory Bank Size: Keep the recent frame queue at $N = 4$ or $N = 6$ and cap prompted keyframes at $M = 4$. This guarantees a fixed VRAM ceiling under 4 GB.
- Deploy TensorRT / ONNX Runtime FP16 Kernels: Export the Hiera image encoder and memory attention module to ONNX. Using TensorRT FP16 execution delivers over 60 FPS on edge accelerators, enabling real-time robotic grasping and obstacle avoidance.
By bridging static image promptability with streaming temporal memory, SAM 2 establishes the modern blueprint for real-time visual perception and tracking in 2026.