Back to Blog
Deep Learning

SAM 2: Revolutionizing Real-Time Object Segmentation in Images and Videos

Claude Directory December 29, 2025
0 views

Discover SAM 2, Meta AI's breakthrough for segmenting objects in real-time across images and videos. Trained on massive datasets, it enables precise, interactive tracking for applications in robotics, medicine, and beyond.

What Makes Real-Time Video Segmentation Possible?

Imagine pointing at a moving object in a video and instantly seeing it highlighted, frame by frame, without missing a beat. This isn't science fiction—it's the reality powered by SAM 2, Meta AI's latest advancement in computer vision. Building on the original Segment Anything Model (SAM), SAM 2 extends this capability from static images to dynamic videos, operating at real-time speeds. But how does it achieve such precision and efficiency? Let's dive into the architecture, training, and practical applications that make this possible.

From Images to Videos: The Evolution of SAM

The original SAM, released by Meta in 2023, transformed image segmentation by allowing users to "segment anything" with simple prompts like points, boxes, or text. It was trained on the massive SA-1B dataset containing over 1 billion masks across 11 million images. Developers loved its zero-shot generalization—meaning it could handle unseen objects without retraining.

SAM 2 takes this further by tackling videos. Why is video segmentation harder? Objects move, lighting changes, and occlusions occur across frames. SAM 2 addresses these challenges head-on, supporting image segmentation, video object segmentation, and even video prediction for future frames.

Key innovation? It processes videos interactively: select an object once, and it tracks it throughout. You can add or remove objects on the fly, with changes propagating seamlessly.

For hands-on exploration, check out the official SAM 2 GitHub repository, which includes notebooks, models, and inference code.

Massive Scale Training: The Foundation of Generalization

Powering SAM 2 is an enormous dataset: the SA-V dataset with 35 million masks on 5.1 million images and 34 million masks on 640,000 video frames spanning 51,000 videos. This diversity covers everyday scenes, ensuring the model generalizes to real-world chaos.

Training involved automatic mask annotation at scale, using pseudo-labeling from previous models refined by humans. The result? A model that segments anything from animals and vehicles to people and abstract shapes, even in novel videos.

To appreciate the scale:

  • Images: 5.1M with 35M masks (similar to SA-1B).
  • Videos: 640K frames with 34M masks, from 51K clips.

This data breadth enables zero-shot performance rivaling supervised methods on benchmarks like MOSE and DAVIS.

Architecture Deep Dive: Encoders, Decoders, and Memory

SAM 2's design is elegant yet powerful. At its core:

  1. Image Encoder: A Hiera model (inspired by Hierarchical Vision Transformers) pretrained with Masked Autoencoders (MAE). It extracts rich image features efficiently.

  2. Prompt Encoder: Handles inputs like points, boxes, or masks. For videos, it embeds spatial and temporal prompts.

  3. Mask Decoder: Lightweight transformer decoder that outputs segmentation masks. Identical for images and videos, ensuring consistency.

  4. Memory Bank: The secret sauce for videos. It stores compact, compressed features (128-dim embeddings) from previous frames' predicted masks. These act as "memory" for temporal propagation.

How Memory Attention Works

In video mode, the decoder attends to both current-frame image features and past memory features via memory attention. This cross-attention mechanism:

  • Queries current features against memory keys/values.
  • Enables propagation: a prompt on frame 1 influences frame 100.
  • Handles long sequences efficiently with a fixed-size memory bank (no growing with time).

Compression is key: raw features (256-dim) are projected to 128-dim and temporally averaged, keeping memory lightweight (~1% of raw size).

For streaming videos (endless or live), SAM 2 prunes old memory and adds new predictions, maintaining real-time performance.

Here's a simplified conceptual code snippet for inference (adapted from the SAM 2 repo):

import torch

from sam2.build_sam import build_sam2

from sam2.sam2_image_predictor import SAM2ImagePredictor

# Load checkpoint
checkpoint = "sam2_hiera_large.pt"
model_cfg = "sam2_hiera_l.yaml"
device = "cuda"

predictor = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint, device))

# Predict on image
predictor.set_image(image)
masks, scores, logits = predictor.predict(point_coords=points, point_labels=labels)

Extend this to videos using SAM2VideoPredictor for frame-by-frame tracking.

Blazing-Fast Performance: Real-Time on Consumer Hardware

SAM 2 isn't just accurate—it's fast:

Model SizeFPS (A100 GPU)FPS (RTX 3090)
Tiny15793
Small10458
Base7239
Large4424

On an A100, the large model hits 44 FPS—smooth for real-time apps. Smaller models run on laptops. Compared to SAM, it's 5-13x faster on videos due to optimized memory.

Benchmarks show state-of-the-art zero-shot results:

  • SA-V: 58.1 J&F (intersection over union metric).
  • Beats competitors like XMem, Cutie on DAVIS, MOSE.

Interactive Streaming: Handling Infinite Videos

Traditional trackers falter on long videos due to drift. SAM 2's streaming mode:

  • Updates memory with high-confidence predictions.
  • Prunes low-relevance or old memories.
  • Corrects drift via user corrections (add/remove prompts).

Example workflow:

  1. Prompt on initial frame.
  2. Track automatically.
  3. Click to refine (e.g., un-prompt occluded parts).
  4. Propagation updates all frames instantly.

This makes it ideal for live analysis.

Real-World Applications: Beyond Demos

SAM 2 shines in practical scenarios:

  • Robotics: Track objects for manipulation. E.g., segment a tool in a robot's camera feed for precise grasping.

  • Autonomous Driving: Identify pedestrians, vehicles in dashcam video, even under occlusion.

  • Medical Imaging: Annotate dynamic scans (e.g., ultrasound) interactively. A clinician points at a tumor; it tracks across heartbeats.

  • Video Editing: Automatic rotoscoping—select actors, isolate effects.

  • AR/VR: Real-time object masking for immersive overlays.

Consider a surgical robot: SAM 2 segments instruments and tissues in real-time, aiding AI-assisted procedures. Or in wildlife monitoring: track animals in drone footage without manual labeling.

The original SAM repo complements this for image-only tasks.

Getting Started: Build Your Own SAM 2 Pipeline

  1. Install: pip install git+https://github.com/facebookresearch/segment-anything-2

  2. Download weights: From the SAM 2 GitHub releases.

  3. Run demos: Jupyter notebooks for images/videos.

  4. Fine-tune: Possible on custom data, though zero-shot is often sufficient.

Challenges? It may struggle with extreme deformations or very crowded scenes—prompt strategically.

Future Horizons

SAM 2 democratizes video understanding, much like SAM did for images. As hardware improves, expect integration into browsers, mobiles, and edge devices. Researchers are already extending it to 3D, audio-visual segmentation.

In summary, SAM 2 answers: Can we segment anything, anywhere, in real-time? Yes—and it's open-source, ready for your projects.


<div style="text-align: center; margin-top: 2rem;"> <a href="https://www.deeplearning.ai/the-batch/locating-landmarks-on-the-fly/" target="_blank" rel="noopener noreferrer" class="view-full-resource-btn" style="display: inline-block; background-color: #f97316; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600; transition: background-color 0.2s;">View Full Resource</a> </div>
GitHub Project

Comments

More Blog

View all
Data & Analysis

Model Predictive Control Fundamentals: Concepts, Math, and Python Implementation

Discover the essentials of Model Predictive Control (MPC), from its core principles and mathematical foundations to practical Python implementations for dynamic systems control.

C
Claude Directory
2
Data & Analysis

Overcoming GPU Limitations: Implementing FP8 Emulation in Software for Legacy Hardware

Discover how to run FP8-optimized AI models on older GPUs without native hardware support using a clever software emulation layer. Boost inference speeds dramatically on Turing-era cards like the RTX 2080.

C
Claude Directory
3
Data & Analysis

Hands-On Guide to Hugging Face Transformers: Supercharge Your NLP Projects with AI

Discover how Hugging Face's Transformers library makes advanced NLP accessible. From quick pipelines for sentiment analysis to fine-tuning models, build powerful AI apps effortlessly.

C
Claude Directory
1
Data & Analysis

Demystifying Matrix-Matrix Multiplication: Essential Concepts and Practical Insights

Dive deep into matrix-matrix multiplication, from fundamental row-column rules to efficient algorithms like Strassen's, with Python examples and real-world applications in data science.

C
Claude Directory
2
Data & Analysis

Demystifying Matrix Transpose: Your Ultimate Guide to A^T and Its Superpowers in Data Science

Dive into the exciting world of matrix transpose! Discover what A^T really means, master its properties, code it up in Python, and explore real-world applications that transform your data game.

C
Claude Directory
Data & Analysis

Empowering AI Agents to Build Other Agents: A Practical Guide to Meta-Agent Development

Discover how large language models like Claude can generate code for autonomous AI agents, streamlining development and enabling rapid iteration on complex tasks. This approach turns manual coding into an automated, scalable process.

C
Claude Directory