{"id":22331,"library":"sam2","title":"SAM 2 (Segment Anything 2)","description":"SAM 2 is the second generation of the Segment Anything Model, supporting both image and video segmentation. Version 1.1.0 introduces new model variants (e.g., sam2.1_hiera_tiny) and improvements. Install from PyPI; requires Python >=3.10.","status":"active","version":"1.1.0","language":"python","source_language":"en","source_url":"https://github.com/JinsuaFeito-dev/segment-anything-2","tags":["segmentation","sam","meta","image-segmentation","video-segmentation","deep-learning"],"install":[{"cmd":"pip install sam2","lang":"bash","label":"PyPI install"}],"dependencies":[{"reason":"Core dependency for model inference","package":"torch","optional":false},{"reason":"Required for image processing","package":"torchvision","optional":false},{"reason":"Used for video handling","package":"opencv-python","optional":true}],"imports":[{"note":"build_sam2 is inside the build_sam module","wrong":"from sam2 import build_sam2","symbol":"build_sam2","correct":"from sam2.build_sam import build_sam2"},{"note":"SamPredictor is in the predictor module","wrong":"from sam2 import SamPredictor","symbol":"SamPredictor","correct":"from sam2.predictor import SamPredictor"},{"note":"Full path changed in 1.1.0","wrong":"from sam2 import SAM2ImagePredictor","symbol":"SAM2ImagePredictor","correct":"from sam2.sam2_image_predictor import SAM2ImagePredictor"}],"quickstart":{"code":"import torch\nfrom sam2.build_sam import build_sam2\nfrom sam2.sam2_image_predictor import SAM2ImagePredictor\n\ncheckpoint = \"sam2.1_hiera_tiny.pt\"\nmodel_cfg = \"sam2.1_hiera_t.yaml\"\n\npredictor = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint))\n\nimport numpy as np\nimage = np.random.rand(1024, 1024, 3).astype(np.uint8)\npredictor.set_image(image)\n\ninput_point = np.array([[500, 375]])\ninput_label = np.array([1])\nmask, _, _ = predictor.predict(point_coords=input_point, point_labels=input_label)\nprint(mask.shape)","lang":"python","description":"Load a SAM 2 model, set an image, and predict a mask from a point prompt."},"warnings":[{"fix":"Update import to `from sam2.sam2_image_predictor import SAM2ImagePredictor`.","message":"In 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.","severity":"breaking","affected_versions":"<=1.0.0 -> 1.1.0"},{"fix":"Always download the corresponding config and checkpoint from the GitHub releases.","message":"The 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.","severity":"gotcha","affected_versions":"all"},{"fix":"Pass the config YAML path: build_sam2('path/to/config.yaml', checkpoint_path).","message":"The `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.","severity":"deprecated","affected_versions":">=1.1.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Use `from sam2.sam2_image_predictor import SAM2ImagePredictor` instead.","cause":"The import path changed in sam2 1.1.0; 'predictor' module no longer exists.","error":"ModuleNotFoundError: No module named 'sam2.predictor'"},{"fix":"Ensure you download the correct pair from the official GitHub releases.","cause":"Mismatch between model config (e.g., sam2.1_hiera_t.yaml) and checkpoint (e.g., sam2.1_hiera_tiny.pt).","error":"RuntimeError: The checkpoint file does not match the model configuration."},{"fix":"Update both checkpoint and config to version 1.1.0. Use `sam2.1_hiera_tiny.pt` with `sam2.1_hiera_t.yaml`.","cause":"Using an older checkpoint with new code or vice versa, or incorrect config path.","error":"KeyError: 'image_encoder'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}