Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
build_sam2
✓ from sam2.build_sam import build_sam2
✗ from sam2 import build_sam2
build_sam2 is inside the build_sam module
SamPredictor
✓ from sam2.predictor import SamPredictor
✗ from sam2 import SamPredictor
SamPredictor is in the predictor module
SAM2ImagePredictor
✓ from sam2.sam2_image_predictor import SAM2ImagePredictor
✗ from sam2 import SAM2ImagePredictor
Full path changed in 1.1.0
Load a SAM 2 model, set an image, and predict a mask from a point prompt.
import torch
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
checkpoint = "sam2.1_hiera_tiny.pt"
model_cfg = "sam2.1_hiera_t.yaml"
predictor = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint))
import numpy as np
image = np.random.rand(1024, 1024, 3).astype(np.uint8)
predictor.set_image(image)
input_point = np.array([[500, 375]])
input_label = np.array([1])
mask, _, _ = predictor.predict(point_coords=input_point, point_labels=input_label)
print(mask.shape)
Debug
Known issues
breakingIn version 1.1.0, the import for SAM2ImagePredictor changed from `sam2.predictor` to `sam2.sam2_image_predictor`. Old code using `from sam2.predictor import SAM2ImagePredictor` will break.fixUpdate import to `from sam2.sam2_image_predictor import SAM2ImagePredictor`.
affects: <=1.0.0 -> 1.1.0
gotchaThe model checkpoint filename must match the config file. For example, `sam2.1_hiera_tiny.pt` pairs with `sam2.1_hiera_t.yaml`. Mismatch causes silent errors or wrong behavior.fixAlways download the corresponding config and checkpoint from the GitHub releases.
affects: all
deprecatedThe `build_sam2` function signature changed to accept config file path instead of model type string. Old code passing model type (e.g., 'sam2_hiera_l') is deprecated.fixPass the config YAML path: build_sam2('path/to/config.yaml', checkpoint_path). affects: >=1.1.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'sam2.predictor'
The import path changed in sam2 1.1.0; 'predictor' module no longer exists.
fixUse `from sam2.sam2_image_predictor import SAM2ImagePredictor` instead.
RuntimeError: The checkpoint file does not match the model configuration.
Mismatch between model config (e.g., sam2.1_hiera_t.yaml) and checkpoint (e.g., sam2.1_hiera_tiny.pt).
fixEnsure you download the correct pair from the official GitHub releases.
KeyError: 'image_encoder'
Using an older checkpoint with new code or vice versa, or incorrect config path.
fixUpdate both checkpoint and config to version 1.1.0. Use `sam2.1_hiera_tiny.pt` with `sam2.1_hiera_t.yaml`.
Upgrade
Version history
1.1.0latest on PyPI · released Dec 21, 2024
Audit
Dependencies
torchrequiredCore dependency for model inference
torchvisionrequiredRequired for image processing
opencv-pythonoptionalUsed for video handling