Views
No views yet


SAM2VideoPredictor to better handle multi-object trackingtorch.compile of the entire SAM 2 model on videos, which can be turned on by setting vos_optimized=True in build_sam2_video_predictor, leading to a major speedup for VOS inference.SAM2VideoPredictor to support independent per-object inference, allowing us to relax the assumption of prompting for multi-object tracking and adding new objects after tracking starts.RELEASE_NOTES.md for full details.pip uninstall SAM-2, pull the latest code from this repo (with git pull), and then reinstall the repo following Installation below.training/README.md on how to get started.demo/README.md for details.python>=3.10, as well as torch>=2.5.1 and torchvision>=0.20.1. Please follow the instructions here to install both PyTorch and TorchVision dependencies. You can install SAM 2 on a GPU machine using:1git clone https://github.com/facebookresearch/sam2.git && cd sam2
2
3pip install -e .jupyter and matplotlib are required and can be installed by:pip install -e ".[notebooks]"pip following https://pytorch.org/. If you have a PyTorch version lower than 2.5.1 in your current environment, the installation command above will try to upgrade it to the latest PyTorch version using pip.nvcc compiler. If it isn't already available on your machine, please install the CUDA toolkits with a version that matches your PyTorch CUDA version.Failed to build the SAM 2 CUDA extension during installation, you can ignore it and still use SAM 2 (some post-processing functionality may be limited, but it doesn't affect the results in most cases).INSTALL.md for FAQs on potential issues and solutions.1cd checkpoints && \
2./download_ckpts.sh && \
3cd ..SAM2ImagePredictor class has an easy interface for image prompting.1import torch
2from sam2.build_sam import build_sam2
3from sam2.sam2_image_predictor import SAM2ImagePredictor
4
5checkpoint = "./checkpoints/sam2.1_hiera_large.pt"
6model_cfg = "configs/sam2.1/sam2.1_hiera_l.yaml"
7predictor = SAM2ImagePredictor(build_sam2(model_cfg, checkpoint))
8
9with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
10 predictor.set_image(<your_image>)
11 masks, _, _ = predictor.predict(<input_prompts>)1import torch
2from sam2.build_sam import build_sam2_video_predictor
3
4checkpoint = "./checkpoints/sam2.1_hiera_large.pt"
5model_cfg = "configs/sam2.1/sam2.1_hiera_l.yaml"
6predictor = build_sam2_video_predictor(model_cfg, checkpoint)
7
8with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
9 state = predictor.init_state(<your_video>)
10
11 # add new prompts and instantly get the output on the same frame
12 frame_idx, object_ids, masks = predictor.add_new_points_or_box(state, <your_prompts>):
13
14 # propagate the prompts to get masklets throughout the video
15 for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
16 ...pip install huggingface_hub).1import torch
2from sam2.sam2_image_predictor import SAM2ImagePredictor
3
4predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-large")
5
6with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
7 predictor.set_image(<your_image>)
8 masks, _, _ = predictor.predict(<input_prompts>)1import torch
2from sam2.sam2_video_predictor import SAM2VideoPredictor
3
4predictor = SAM2VideoPredictor.from_pretrained("facebook/sam2-hiera-large")
5
6with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
7 state = predictor.init_state(<your_video>)
8
9 # add new prompts and instantly get the output on the same frame
10 frame_idx, object_ids, masks = predictor.add_new_points_or_box(state, <your_prompts>):
11
12 # propagate the prompts to get masklets throughout the video
13 for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
14 ...| Model | Size (M) | Speed (FPS) | SA-V test (J&F) | MOSE val (J&F) | LVOS v2 (J&F) |
|---|---|---|---|---|---|
| sam2.1_hiera_tiny (config, checkpoint) | 38.9 | 91.2 | 76.5 | 71.8 | 77.3 |
| sam2.1_hiera_small (config, checkpoint) | 46 | 84.8 | 76.6 | 73.5 | 78.3 |
| sam2.1_hiera_base_plus (config, checkpoint) | 80.8 | 64.1 | 78.2 | 73.7 | 78.2 |
| sam2.1_hiera_large (config, checkpoint) | 224.4 | 39.5 | 79.5 | 74.6 | 80.6 |
| Model | Size (M) | Speed (FPS) | SA-V test (J&F) | MOSE val (J&F) | LVOS v2 (J&F) |
|---|---|---|---|---|---|
| sam2_hiera_tiny (config, checkpoint) | 38.9 | 91.5 | 75.0 | 70.9 | 75.3 |
| sam2_hiera_small (config, checkpoint) | 46 | 85.6 | 74.9 | 71.5 | 76.4 |
| sam2_hiera_base_plus (config, checkpoint) | 80.8 | 64.8 | 74.7 | 72.8 | 75.8 |
| sam2_hiera_large (config, checkpoint) | 224.4 | 39.7 | 76.0 | 74.6 | 79.8 |
torch 2.5.1, cuda 12.4. See benchmark.py for an example on benchmarking (compiling all the model components). Compiling only the image encoder can be more flexible and also provide (a smaller) speed-up (set compile_image_encoder: True in the config).cc_torch (with its license in LICENSE_cctorch) as an optional post-processing step for the mask predictions.1@article{ravi2024sam2,
2 title={SAM 2: Segment Anything in Images and Videos},
3 author={Ravi, Nikhila and Gabeur, Valentin and Hu, Yuan-Ting and Hu, Ronghang and Ryali, Chaitanya and Ma, Tengyu and Khedr, Haitham and R{\"a}dle, Roman and Rolland, Chloe and Gustafson, Laura and Mintun, Eric and Pan, Junting and Alwala, Kalyan Vasudev and Carion, Nicolas and Wu, Chao-Yuan and Girshick, Ross and Doll{\'a}r, Piotr and Feichtenhofer, Christoph},
4 journal={arXiv preprint arXiv:2408.00714},
5 url={https://arxiv.org/abs/2408.00714},
6 year={2024}
7}