🚀 SAM 2:图像和视频中的任意分割模型
SAM 2 是由 FAIR 开发的基础模型,旨在解决图像和视频中的可提示视觉分割问题。它能够根据用户的提示,在图像和视频中实现灵活的分割任务。更多信息请参考 SAM 2 论文。
官方代码已在这个 仓库 中公开。
🚀 快速开始
本项目提供了在图像和视频中进行分割预测的功能,以下是具体的使用方法。
💻 使用示例
基础用法
图像预测
import torch
from sam2.sam2_image_predictor import SAM2ImagePredictor
predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2-hiera-base-plus")
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
predictor.set_image(<your_image>)
masks, _, _ = predictor.predict(<input_prompts>)
视频预测
import torch
from sam2.sam2_video_predictor import SAM2VideoPredictor
predictor = SAM2VideoPredictor.from_pretrained("facebook/sam2-hiera-base-plus")
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
state = predictor.init_state(<your_video>)
frame_idx, object_ids, masks = predictor.add_new_points_or_box(state, <your_prompts>):
for frame_idx, object_ids, masks in predictor.propagate_in_video(state):
...
更多详细信息请参考 演示笔记本。
引用
如果您想引用该论文、模型或软件,请使用以下 BibTeX 格式:
@article{ravi2024sam2,
title={SAM 2: Segment Anything in Images and Videos},
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},
journal={arXiv preprint arXiv:2408.00714},
url={https://arxiv.org/abs/2408.00714},
year={2024}
}
📄 许可证
本项目采用 Apache-2.0 许可证。
属性 |
详情 |
模型类型 |
图像和视频分割模型 |
库名称 |
sam2 |
许可证 |
Apache-2.0 |
任务类型 |
掩码生成 |