Minimum Requirements for Running SAM: Complete Setup Guide for the Segment Anything Model

To run the Segment Anything Model (SAM), you need Python 3.8 or higher, PyTorch 1.7+, torchvision 0.8+, and a downloaded model checkpoint file.

The minimum requirements for running SAM are straightforward, making the model accessible for both research and production environments. Developed by Meta AI's facebookresearch/segment-anything repository, SAM requires only a small set of core dependencies to perform inference on images, with optional packages available for advanced features like ONNX export and visualization.

Core Dependencies and System Requirements

SAM's architecture is built on PyTorch, requiring specific minimum versions to ensure compatibility with the model's transformer-based image encoder and prompt encoder components.

Python Version Requirements

SAM requires Python 3.8 or higher. This version requirement ensures compatibility with the type hints and modern Python features used throughout the codebase, particularly in segment_anything/predictor.py and segment_anything/automatic_mask_generator.py.

PyTorch and Torchvision Versions

The minimum PyTorch version is 1.7 or higher, with matching torchvision 0.8+. These versions support the transformer operations and tensor handling required by SAM's vision transformer (ViT) backbone. For GPU acceleration, ensure your PyTorch installation includes CUDA support matching your NVIDIA driver version.

Required Model Checkpoints

Beyond software dependencies, SAM requires downloading pretrained model weights before inference. The repository provides three model variants:

  • ViT-H (default): sam_vit_h_4b8939.pth — highest accuracy, largest model
  • ViT-L: sam_vit_l_0b3195.pth — balanced performance
  • ViT-B: sam_vit_b_01ec64.pth — fastest inference, smallest footprint

Download these checkpoints from the SAM model repository and reference the local path when initializing the model.

Optional Dependencies for Advanced Features

While the core requirements are minimal, several optional packages extend SAM's functionality:

  • opencv-python: Required for image I/O operations in scripts/amg.py
  • pycocotools: Enables COCO-format mask export and evaluation
  • matplotlib: Powers visualization of segmentation masks
  • onnx and onnxruntime: Required for exporting the mask decoder to ONNX format using scripts/export_onnx_model.py
  • jupyter: Supports the provided notebook examples

Install these via pip if you need the specific functionality they provide.

Installation and Verification

To verify your environment meets the minimum requirements for running SAM, install the core dependencies and verify the installation:


# Install core dependencies

pip install torch>=1.7 torchvision>=0.8

# Install SAM from the repository

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

# Verify installation

python -c "from segment_anything import sam_model_registry; print('SAM installed successfully')"

Running SAM: Code Examples

Once dependencies are installed and a checkpoint is downloaded, you can run inference using several approaches implemented in the repository.

Prompt-Based Inference with SamPredictor

The SamPredictor class in segment_anything/predictor.py provides the high-level API for interactive segmentation with point or box prompts:

from segment_anything import SamPredictor, sam_model_registry
import numpy as np

# Load model

sam = sam_model_registry["vit_h"](checkpoint="sam_vit_h_4b8939.pth")
predictor = SamPredictor(sam)

# Set image (numpy array of shape HxWx3)

predictor.set_image(my_image)

# Define prompts: point coordinates and labels

point_coords = np.array([[150, 200], [400, 350]])
point_labels = np.array([1, 0])  # 1=foreground, 0=background

# Generate mask

masks, scores, logits = predictor.predict(
    point_coords=point_coords,
    point_labels=point_labels,
    multimask_output=False,
)

Automatic Mask Generation

For segmenting entire images without prompts, use SamAutomaticMaskGenerator from segment_anything/automatic_mask_generator.py:

from segment_anything import SamAutomaticMaskGenerator, sam_model_registry

sam = sam_model_registry["vit_h"](checkpoint="sam_vit_h_4b8939.pth")
mask_generator = SamAutomaticMaskGenerator(sam)

# Generate all masks for an image

masks = mask_generator.generate(my_image)

# Returns list of dicts with 'segmentation', 'area', 'bbox', 'predicted_iou'

Command-Line Usage

The repository includes scripts/amg.py for command-line mask generation without writing Python code:

python scripts/amg.py \
    --checkpoint sam_vit_h_4b8939.pth \
    --model-type vit_h \
    --input path/to/images/ \
    --output path/to/masks/

Summary

The minimum requirements for running SAM are intentionally lightweight to maximize accessibility:

  • Python 3.8+, PyTorch 1.7+, and torchvision 0.8+ constitute the core software stack
  • A downloaded model checkpoint (such as sam_vit_h_4b8939.pth) is required for inference
  • Optional packages like opencv-python, pycocotools, and onnx extend functionality for specific use cases
  • The repository provides multiple interfaces: the SamPredictor class for interactive prompts, SamAutomaticMaskGenerator for full-image segmentation, and command-line scripts for batch processing

Frequently Asked Questions

Can I run SAM without a GPU?

Yes, SAM runs on CPU-only systems meeting the minimum requirements for running SAM, though inference will be significantly slower. The PyTorch installation should be CPU-only (pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu) if you lack a CUDA-capable GPU.

Which model checkpoint should I download for basic testing?

For most users starting with SAM, download the ViT-H model (sam_vit_h_4b8939.pth). It provides the highest accuracy and is the default referenced in the segment_anything/predictor.py examples. If you need faster inference on resource-constrained devices, use the ViT-B checkpoint instead.

Do I need to install OpenCV to use SAM?

OpenCV is not part of the minimum requirements for running SAM core inference, but it is required if you use scripts/amg.py or load images from disk in the examples. The SamPredictor and SamAutomaticMaskGenerator classes themselves only require NumPy arrays as input.

How do I export SAM to ONNX format?

Exporting to ONNX requires installing the optional dependencies onnx and onnxruntime, then running scripts/export_onnx_model.py with your checkpoint path. This converts the mask decoder (not the full image encoder) to ONNX for deployment in environments without PyTorch, though you must still meet the minimum requirements for running SAM during the export process itself.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →