TopGit
GitHub Repo Review

Segment Anything Model (SAM): SAM segmentation model

facebookresearch/segment-anything
STopGit review image for facebookresearch/segment-anything
Review by Topgit.dev for facebookresearch/segment-anything, with GitHub repository stats and README context.
Quick verdict

Segment Anything Model (SAM) is Meta AI's zero-shot image segmentation model, trained to turn a point or box prompt into an object mask without any task-specific fine-tuning. Reach for it when you need one-shot masks inside a larger pipeline; skip it if you need real-time video tracking, since that's what SAM 2 handles, not this original release.

Stars
★ 54.9k
Forks
⑂ 6.4k
Language
Jupyter Notebook
License
Apache-2.0
Topic
Updated
Sep 2024
Homepage
GitHub

What is the Segment Anything Model?

Segment Anything Model (SAM) is Meta AI's image segmentation model that turns a point, box, or full-image sweep into pixel-accurate object masks. SamPredictor takes one prompt and returns a mask in a single forward pass, while SamAutomaticMaskGenerator masks every object in an image with no prompt needed. It ships as inference code and three pretrained ViT checkpoints, not a training recipe.

Key capabilities: prompts and automatic mask generation

  • Two ways to get masks: SamPredictor for a single point/box prompt, SamAutomaticMaskGenerator for every object in the image at once.
  • Three ViT backbone checkpoints — vit_h (default), vit_l, and vit_b — swap the model_type string to trade accuracy for speed.
  • Trained on a dataset of 11 million images and 1.1 billion masks, which is what gives it strong zero-shot performance on unfamiliar images.
  • Mask decoder exports to ONNX, so the exported model can run outside PyTorch — including in a browser, per the demo/ React app.
  • CLI entry point (scripts/amg.py) for batch-generating masks over a folder of images without writing Python.
  • Output masks come as COCO RLE segmentation plus bbox, area, predicted_iou, and stability_score fields, ready for downstream COCO tooling.
How this repository's GitHub stars have grown over time. Source: star-history.com.View the star history

Where SAM fits in computer vision pipelines

  • Pulling object masks for a labeling pipeline instead of hand-drawing polygons.
  • Prepping training data for a downstream detector or segmentation model that needs masks it doesn't have to hand-annotate.
  • Interactive image editors where a user clicks a point and the app needs a mask back immediately (this is what the ONNX web demo shows).
  • Batch-masking a folder of images from the command line via scripts/amg.py, without writing any Python.

Installing SAM and its dependencies

Requires python>=3.8, pytorch>=1.7, and torchvision>=0.8 — install PyTorch/TorchVision with CUDA support first, since the README calls that strongly recommended. Then either `pip install git+https://github.com/facebookresearch/segment-anything.git` or clone the repo and run `pip install -e .`. For mask post-processing, COCO-format export, the example notebooks, and ONNX export, also install `pip install opencv-python pycocotools matplotlib onnxruntime onnx` — none of that is bundled by default. You still need to separately download a model checkpoint (a vit_h/vit_l/vit_b .pth file) before anything runs; the pip package ships code only, not weights.

Running SAM: prompts, automatic masks, and CLI

Point-and-box prompting goes through SamPredictor: load a checkpoint with `sam_model_registry["<model_type>"](checkpoint=...)`, call `predictor.set_image(<your_image>)` once, then `predictor.predict(<input_prompts>)` per prompt. For unattended, whole-image masking, hand the same registry-loaded model to `SamAutomaticMaskGenerator(sam)` and call `.generate(<your_image>)` — no prompts required. The same job also runs from a terminal: `python scripts/amg.py --checkpoint <path> --model-type <model_type> --input <image_or_folder> --output <path>`. To get an ONNX file for browser or edge use, run `python scripts/export_onnx_model.py --checkpoint <path> --model-type <model_type> --output <path>`; the README recommends the latest stable PyTorch for that export step specifically.

Why SAM became a standard in segmentation

  • One model covers both prompted and fully-automatic segmentation — you don't maintain two separate model paths for the two modes.
  • Apache-2.0 on the code and model, so there's no copyleft or field-of-use restriction blocking commercial use of SAM itself.
  • Three checkpoint sizes let you pick vit_b for speed or vit_h for quality without switching architectures.
  • ONNX export path is real, not aspirational — the demo/ folder ships a working browser app built on it.

Limitations of the original SAM release

  • The SA-1B dataset itself carries a separate 'SA-1B Dataset Research License' you have to accept before downloading — it isn't the same Apache-2.0 grant that covers the code.
  • This is the original, image-only SAM. For video, the README points you to the separate SAM 2 project instead.
  • No training code is included — you get inference plus checkpoints, not a way to fine-tune SAM on your own data from this repo.
  • Model checkpoints (ViT-H being the largest) aren't small; the README doesn't publish file sizes, so budget GPU and download capacity before committing to vit_h.
View on GitHub

How SAM compares to other segmentation approaches

SAM 2 (facebookresearch/segment-anything-2) — Meta's own successor, extending the same idea to video with streaming memory; use it instead if the input isn't static images.supervision — a computer-vision utility library that wraps SAM's mask output for annotation and pipeline glue rather than replacing the model itself.Detectron2 — Meta's older instance-segmentation library; a fit if you want to train a detector from scratch instead of prompting a frozen foundation model.opencv (classic contour/watershed segmentation) — no prompting or zero-shot generalization, but far lighter weight for simple, well-defined shapes.

Frequently asked questions about SAM

Is the Segment Anything Model free to use commercially?

Segment Anything Model (SAM) code and weights are released under the Apache 2.0 license, so commercial use is permitted; only the separate SA-1B dataset carries its own research-use license terms.

What ViT backbone sizes does SAM offer (ViT-H, ViT-L, ViT-B)?

Segment Anything Model (SAM) ships three checkpoint sizes — vit_h (the default, highest-quality), vit_l, and vit_b — all loaded through the same sam_model_registry call by swapping the model_type string.

What prompt types does SAM accept for mask generation?

Segment Anything Model (SAM) accepts point and box prompts through SamPredictor for a single targeted mask, or no prompt at all via SamAutomaticMaskGenerator, which masks every object it finds in the image.

What is the SA-1B dataset that SAM was trained on?

SA-1B is the dataset Segment Anything Model (SAM) was trained on — a collection of 11 million images and 1.1 billion masks, downloadable separately under its own SA-1B Dataset Research License.

How does SAM 2 differ from the original Segment Anything Model?

SAM 2 extends the original Segment Anything Model to video, treating a video as a sequence of frames and adding streaming memory for real-time processing; it lives in a separate facebookresearch/segment-anything-2 repository, not this one.

Can SAM be exported to ONNX for web or edge deployment?

Segment Anything Model (SAM)'s mask decoder exports to ONNX via scripts/export_onnx_model.py, and the demo/ folder includes a React app that runs the exported model in-browser with multithreading.

Who should try it — and who should skip

Try it if you're building a labeling or annotation pipeline and want masks without hand-drawing polygons, or you're prototyping a click-to-mask feature and can run inference on a GPU. Skip it if your images already suit simple thresholding or contour detection — SAM is overkill there — or if you actually need video, in which case go straight to SAM 2 instead of stretching this repo to do it.

Source & attribution

Facts and code examples sourced from the facebookresearch/segment-anything GitHub repository README.

GitHub data · last synced Aug 14, 2026Reviewed by Henry
Back to TopGit

Curious whether segment-anything is right for you?

Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about segment-anything.

GitHub