Segment Anything
lastmod 2025-11-24created 2023-04-0625 edits
Meta's AI model that extracts any object from an image
The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Features
- The first foundation model for "image segmentation"
- Identifying which object each pixel belongs to
- Applicable to new image domains (underwater photos, cell microscopy, etc.) without additional training
Segment Anything Model 2 (SAM 2)
Segment Anything Model 3 (SAM 3)
Projects
- Magic Copy - Chrome extension using the Segment Anything Model
- Edit Anything - Uses Meta's Segment-Anything Model to recognize every part of an image, and edit and regenerate each one
- Inpaint Anything - Image inpainting using Segment Anything
- FoodSAM - SAM for food images
- SAM.cpp - Meta's Segment Anything Model implemented in pure C/C++
- Language Segment-Anything - SAM with text prompt
- GroundingDINO
- GroundingSAM (GroundedSAM; Grounded SAM; Grounding-SAM; Grounding SAM; Grounded-Segment-Anything; Grounded Segment Anything)
- Semantic-SAM - Segment and Recognize Anything at Any Granularity
- FastSAM - Fast Segment Anything Model
- MobileSAM - This is the official code for MobileSAM project that makes SAM lightweight for mobile applications and beyond!
- Segment Anything Web - a Hugging Face Space by Xenova - SAM that can be demoed directly on the Web at Korean: 허깅 페이스
- SegmentAnything-TensorRT - Running SAM on TensorRT
- Track-Anything
- Caption-Anything
- SegmentAnythingin3D (Segment Anything in 3D; SA3D)
- AnyLabeling
Predict example
The code requires python>=3.8, as well as pytorch>=1.7 and torchvision>=0.8.
Install Segment Anything:
The following optional dependencies are necessary for mask post-processing, saving masks in COCO format, the example notebooks, and exporting the model in ONNX format. jupyter is also required to run the example notebooks.
Download Model Checkpoints:
Three model versions of the model are available with different backbone sizes. These models can be instantiated by running
from segment_anything import sam_model_registry
sam = sam_model_registry["<model_type>"](checkpoint="<path/to/checkpoint>")
Click the links below to download the checkpoint for the corresponding model type.
-
defaultorvit_h: ViT-H SAM model - most parameters -
vit_l: ViT-L SAM model - medium -
vit_b: ViT-B SAM model - most lightweight
from segment_anything import SamPredictor, sam_model_registry
sam = sam_model_registry["<model_type>"](checkpoint="<path/to/checkpoint>")
predictor = SamPredictor(sam)
predictor.set_image(<your_image>)
masks, _, _ = predictor.predict(<input_prompts>)
Example code
# -*- coding: utf-8 -*-
# curl -O https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
from segment_anything import SamPredictor, sam_model_registry
import cv2
import os
import numpy as np
from datetime import datetime
use_gpu = False
sam = sam_model_registry["default"](checkpoint="./sam_vit_h_4b8939.pth")
if use_gpu:
sam.to(device="cuda")
predictor = SamPredictor(sam)
files = (
"mpv-shot0001.jpg",
"mpv-shot0002.jpg",
"mpv-shot0003.jpg",
"mpv-shot0004.jpg",
"mpv-shot0005.jpg",
"mpv-shot0006.jpg",
"mpv-shot0007.jpg",
"mpv-shot0008.jpg",
"mpv-shot0009.jpg",
"mpv-shot0010.jpg",
"mpv-shot0011.jpg",
"mpv-shot0012.jpg",
"mpv-shot0013.jpg",
"mpv-shot0014.jpg",
"mpv-shot0015.jpg",
"mpv-shot0016.jpg",
)
for filename in files:
file_prefix, ext = os.path.splitext(filename)
input_point = np.array([[1950, 378]])
input_label = np.array([1])
img = cv2.imread(filename)
begin = datetime.now()
predictor.set_image(img)
masks, scores, logits = predictor.predict(
point_coords=input_point,
point_labels=input_label,
multimask_output=True,
)
duration = (datetime.now() - begin).total_seconds()
print(f"Predict duration: {duration:.02f}s")
for i in range(masks.shape[0]):
mask_filename = file_prefix + f"-{i}.png"
cv2.imwrite(mask_filename, masks[0] * 255)
print(f"Save mask image: {mask_filename}")
On an NVIDIA GeForce RTX 3070 Ti, the Predict duration is about 0.72s.
Internal test results
- NVIDIA GeForce RTX 3070 Ti
- 3840x2160 (4K Original) image - takes about 0.7s