# SAM 2

> Meta's open-source foundation model and code for promptable visual segmentation and tracking in images and videos.

SAM 2 (Segment Anything Model 2) comes from AI at Meta, FAIR. The repository provides inference code, downloadable model checkpoints, example notebooks, training code, and a web demo for promptable segmentation in images and videos. It is released under the Apache 2.0 license.

## What It Is

SAM 2 is a foundation model for promptable visual segmentation. It extends the original SAM to video by treating an image as a video with a single frame. The model uses a transformer architecture with streaming memory for real-time video processing. The repository also links the SA-V video segmentation dataset, which the authors collected with a model-in-the-loop data engine.

## How It Works

Users install the Python package, download a checkpoint (SAM 2.1 tiny, small, base plus, or large), and run it through Python APIs.

- **Images:** `SAM2ImagePredictor` accepts prompts and returns masks. Automatic mask generation is also supported.
- **Videos:** the video predictor takes click or box prompts, refines them, and propagates masklets through the video. It can track multiple objects and keeps an inference state for each video.
- **Hugging Face:** models can be loaded from Hugging Face.

## Training, Demo, and Recent Updates

The repository includes code to train or fine-tune SAM 2 on custom image and video datasets. It also includes frontend and backend code for a locally deployable web demo. The December 2024 update added `torch.compile` support for the full model on videos, enabled with `vos_optimized=True`, and a reworked `SAM2VideoPredictor` that supports independent per-object inference and adding objects after tracking starts. The SAM 2.1 release in September 2024 brought improved checkpoints.

## Setup Requirements

The code requires Python 3.10 or later, torch 2.5.1 or later, and torchvision 0.20.1 or later. It targets GPU machines, and installation compiles a custom CUDA kernel with nvcc. The README recommends WSL with Ubuntu on Windows.

## Features
- Promptable segmentation in images and videos
- Click and box prompts with refinement
- Multi-object video tracking with masklets
- Automatic mask generation on images
- SAM 2.1 checkpoints in tiny, small, base plus, and large sizes
- Training and fine-tuning code
- Locally deployable web demo
- Hugging Face model loading
- torch.compile full-model optimization for video inference

## Integrations
PyTorch, Hugging Face, Jupyter, Google Colab

## Platforms
LINUX, WINDOWS, DEVELOPER_SDK

## Pricing
Open Source

## Version
SAM 2.1

## Links
- Website: https://github.com/facebookresearch/sam2
- Repository: https://github.com/facebookresearch/sam2
- EveryDev.ai: https://www.everydev.ai/tools/sam-2
