facebookresearch /segment-anything

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.

★ 55,119⑂ 6,403👁 334⑃ 134● 719▤ 0◈ 0
Jupyter NotebookApache-2.0Created 2023-03-23
55,119
stars
+7
6,403
forks
334
watchers
134
prs
719
issues
0
releases
0
discussions

README

Latest updates -- SAM 2: Segment Anything in Images and Videos

Please check out our new release on Segment Anything Model 2 (SAM 2).

 SAM 2 code: https://github.com/facebookresearch/segment-anything-2
 SAM 2 demo: https://sam2.metademolab.com/
 SAM 2 paper: https://arxiv.org/abs/2408.00714

 !SAM 2 architecture

Segment Anything Model 2 (SAM 2) is a foundation model towards solving promptable visual segmentation in images and videos. We extend SAM to video by considering images as a video with a single frame. The model design is a simple transformer architecture with streaming memory for real-time video processing. We build a model-in-the-loop data engine, which improves model and data via user interaction, to collect our SA-V dataset, the largest video segmentation dataset to date. SAM 2 trained on our data provides strong performance across a wide range of tasks and visual domains.

 Segment Anything

Meta AI Research, FAIR

Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alex Berg, Wan-Yen Lo, Piotr Dollar, Ross Girshick

[Paper] [Project] [Demo] [Dataset] [Blog] [BibTeX]

!SAM design

The Segment Anything Model (SAM) produces high quality object masks from input prompts such as points or boxes, and it can be used to generate masks for all objects in an image. It has been trained on a dataset of 11 million images and 1.1 billion masks, and has strong zero-shot performance on a variety of segmentation tasks.


  
   


 Installation

The code requires python=3.8, as well as pytorch=1.7 and torchvision=0.8. Please follow the instructions here to install both PyTorch and TorchVision dependencies. Installing both PyTorch and TorchVision with CUDA support is strongly recommended.

Install Segment Anything:


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


or clone the repository locally and install with


git clone [email protected]:facebookresearch/segment-anything.git
cd segment-anything; pip install -e .


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.


pip install opencv-python pycocotools matplotlib onnxruntime onnx


 Getting Started

First download a model checkpoint. Then the model can be used in just a few lines to get masks from a given prompt:


from segment_anything import SamPredictor, sam_model_registry
sam = sam_model_registry""
predictor = SamPredictor(sam)
predictor.set_image()
masks, _, _ = predictor.predict()


or generate masks for an entire image:


from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
sam = sam_model_registry""
mask_generator = SamAutomaticMaskGenerator(sam)
masks = mask_generator.generate()


Additionally, masks can be generated for images from the command line:


python scripts/amg.py --checkpoint  --model-type  --inp

Top issues

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON