[ PROMPT_NODE_22764 ]
segment-anything-model
[ SKILL_DOCUMENTATION ]
# Segment Anything Model (SAM)
使用 Meta AI 的 Segment Anything Model 进行零样本图像分割的综合指南。
## 何时使用 SAM
**在以下情况使用 SAM:**
- 需要在无需特定任务训练的情况下分割图像中的任何对象
- 构建带有点/框提示词的交互式标注工具
- 为其他视觉模型生成训练数据
- 需要零样本迁移到新的图像领域
- 构建对象检测/分割工作流
- 处理医学、卫星或特定领域的图像
**主要特性:**
- **零样本分割**:无需微调即可在任何图像领域工作
- **灵活的提示词**:点、边界框或先前的掩码
- **自动分割**:自动生成所有对象掩码
- **高质量**:在 1100 万张图像的 11 亿个掩码上进行训练
- **多种模型尺寸**:ViT-B(最快)、ViT-L、ViT-H(最准确)
- **ONNX 导出**:可在浏览器和边缘设备上部署
**建议使用替代方案:**
- **YOLO/Detectron2**:用于带类别的实时对象检测
- **Mask2Former**:用于带类别的语义/全景分割
- **GroundingDINO + SAM**:用于文本提示词分割
- **SAM 2**:用于视频分割任务
## 快速开始
### 安装
bash
# 从 GitHub 安装
pip install git+https://github.com/facebookresearch/segment-anything.git
# 可选依赖
pip install opencv-python pycocotools matplotlib
# 或使用 HuggingFace transformers
pip install transformers
### 下载检查点 (Checkpoints)
bash
# ViT-H (最大,最准确) - 2.4GB
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
# ViT-L (中等) - 1.2GB
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth
# ViT-B (最小,最快) - 375MB
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
### 使用 SamPredictor 的基本用法
python
import numpy as np
from segment_anything import sam_model_registry, SamPredictor
# 加载模型
sam = sam_model_registry["vit_h"](checkpoint="sam_vit_h_4b8939.pth")
sam.to(device="cuda")
# 创建预测器
predictor = SamPredictor(sam)
# 设置图像(仅计算一次嵌入)
image = cv2.imread("image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
predictor.set_image(image)
# 使用点提示词进行预测
input_point = np.array([[500, 375]]) # (x, y) 坐标
input_label = np.array([1]) # 1 = 前景, 0 = 背景
masks, scores, logits = predictor.predict(
point_coords=input_point,
po