Link to this section快速分割一切模型 (FastSAM)#
快速分割一切模型 (FastSAM) 是一种新型的、基于 CNN 的实时解决方案,专为“分割一切”任务而设计。该任务旨在根据各种可能的用户交互提示,分割图像中的任何对象。FastSAM 在保持竞争力的同时,显著降低了计算需求,使其成为各种视觉任务的实用选择。
Watch: Object Tracking using FastSAM with Ultralytics
Link to this section模型架构#

Link to this section概述#
FastSAM 旨在解决 Segment Anything Model (SAM) 的局限性,后者是一个对计算资源要求极高的 Transformer 模型。FastSAM 将“分割一切”任务解耦为两个连续阶段:全实例分割和提示引导选择。第一阶段使用 YOLOv8-seg 生成图像中所有实例的分割掩码。在第二阶段,它输出对应于提示的感兴趣区域。
Link to this section主要特性#
-
实时解决方案: 通过利用 CNN 的计算效率,FastSAM 为“分割一切”任务提供了实时解决方案,使其对需要快速结果的工业应用极具价值。
-
效率与性能: FastSAM 在不牺牲性能质量的前提下,显著降低了计算和资源需求。它实现了与 SAM 相当的性能,但大幅减少了计算资源,从而实现了实时应用。
-
提示引导分割: FastSAM 可以根据各种可能的用户交互提示分割图像中的任何对象,在不同场景中提供灵活性和适应性。
-
基于 YOLOv8-seg: FastSAM 基于 YOLOv8-seg,这是一种配备实例分割分支的目标检测器。这使它能够有效地生成图像中所有实例的分割掩码。
-
基准测试中的竞争结果: 在 MS COCO 的对象建议任务上,FastSAM 在单台 NVIDIA RTX 3090 上以显著快于 SAM 的速度获得了高分,展示了其效率和能力。
-
实际应用: 所提出的方法以极高的速度(比当前方法快几十倍甚至几百倍)为大量视觉任务提供了一种新的实用解决方案。
-
模型压缩的可行性: FastSAM 展示了一条路径的可行性,即通过在结构中引入人工先验,可以显著降低计算工作量,从而为通用视觉任务的大模型架构开辟了新的可能性。
Link to this section可用模型、支持的任务和操作模式#
下表列出了可用模型及其特定的预训练权重、它们支持的任务,以及它们与不同操作模式(如 推理、验证、训练 和 导出)的兼容性,支持的模式用 ✅ 表情符号表示,不支持的模式用 ❌ 表情符号表示。
| 模型类型 | 预训练权重 | 支持的任务 | 推理 | 验证 | 训练 | 导出 |
|---|---|---|---|---|---|---|
| FastSAM-s | FastSAM-s.pt | 实例分割 | ✅ | ❌ | ❌ | ✅ |
| FastSAM-x | FastSAM-x.pt | 实例分割 | ✅ | ❌ | ❌ | ✅ |
Link to this sectionFastSAM 与 YOLO 的对比#
在此,我们将 Meta 的 SAM 2 模型(包括最小的 SAM2-t 变体)与 Ultralytics 的分割模型(包括 YOLO26n-seg)进行比较:
| 模型 | 大小 (MB) | 参数量 (M) | 速度 (CPU) (ms/im) |
|---|---|---|---|
| Meta SAM-b | 375 | 93.7 | 41703 |
| Meta SAM2-b | 162 | 80.8 | 28867 |
| Meta SAM2-t | 78.1 | 38.9 | 23430 |
| MobileSAM | MobileSAM | 10.1 | 23802 |
| FastSAM-s 使用 YOLOv8 骨干网络 | 23.9 | 11.8 | 58.0 |
| Ultralytics YOLOv8n-seg | 7.1 (小 11.0 倍) | 3.4 (少 11.4 倍) | 24.8 (快 945 倍) |
| Ultralytics YOLO11n-seg | 6.2 (小 12.6 倍) | 2.9 (少 13.4 倍) | 24.3 (快 964 倍) |
| Ultralytics YOLO26n-seg | 6.7 (小 11.7 倍) | 2.7 (少 14.4 倍) | 25.2 (快 930 倍) |
此对比展示了 SAM 变体与 YOLO 分割模型在模型大小和速度上的显著差异。虽然 SAM 提供了独特的自动分割能力,但 YOLO 模型(特别是 YOLOv8n-seg、YOLO11n-seg 和 YOLO26n-seg)在尺寸更小、速度更快且计算效率更高。
SAM 速度使用 PyTorch 测量,YOLO 速度使用 ONNX Runtime 测量。测试在配备 16GB 内存的 2025 Apple M4 Air 上运行,使用 torch==2.10.0、ultralytics==8.4.31 和 onnxruntime==1.24.4。如需重现此测试:
from ultralytics import ASSETS, SAM, YOLO, FastSAM
# Profile SAM2-t, SAM2-b, SAM-b, MobileSAM
for file in ["sam_b.pt", "sam2_b.pt", "sam2_t.pt", "mobile_sam.pt"]:
model = SAM(file)
model.info()
model(ASSETS)
# Profile FastSAM-s
model = FastSAM("FastSAM-s.pt")
model.info()
model(ASSETS)
# Profile YOLO models (ONNX)
for file_name in ["yolov8n-seg.pt", "yolo11n-seg.pt", "yolo26n-seg.pt"]:
model = YOLO(file_name)
model.info()
onnx_path = model.export(format="onnx", dynamic=True)
model = YOLO(onnx_path)
model(ASSETS)Link to this section使用示例#
FastSAM 模型很容易集成到你的 Python 应用程序中。Ultralytics 提供了用户友好的 Python API 和 CLI 命令来简化开发。
Link to this section预测使用#
要在图像上执行 目标检测,请使用 predict 方法,如下所示:
from ultralytics import FastSAM
# Define an inference source
source = "path/to/bus.jpg"
# Create a FastSAM model
model = FastSAM("FastSAM-s.pt") # or FastSAM-x.pt
# Run inference on an image
everything_results = model(source, device="cpu", retina_masks=True, imgsz=1024, conf=0.4, iou=0.9)
# Run inference with bboxes prompt
results = model(source, bboxes=[439, 437, 524, 709])
# Run inference with points prompt
results = model(source, points=[[200, 200]], labels=[1])
# Run inference with texts prompt
results = model(source, texts="a photo of a dog")
# Run inference with bboxes and points and texts prompt at the same time
results = model(source, bboxes=[439, 437, 524, 709], points=[[200, 200]], labels=[1], texts="a photo of a dog")此代码片段演示了加载预训练模型并对图像进行预测的简单性。
通过这种方式,你可以对图像运行推理并一次性获取所有分割 results,然后多次运行提示推理,而无需多次运行推理。
from ultralytics.models.fastsam import FastSAMPredictor
# Create FastSAMPredictor
overrides = dict(conf=0.25, task="segment", mode="predict", model="FastSAM-s.pt", save=False, imgsz=1024)
predictor = FastSAMPredictor(overrides=overrides)
# Segment everything
everything_results = predictor("ultralytics/assets/bus.jpg")
# Prompt inference
bbox_results = predictor.prompt(everything_results, bboxes=[[200, 200, 300, 300]])
point_results = predictor.prompt(everything_results, points=[200, 200])
text_results = predictor.prompt(everything_results, texts="a photo of a dog")在上述示例中返回的所有 results 都是 Results 对象,这使得访问预测的掩码和源图像变得非常容易。
Link to this section验证使用#
对模型进行数据集验证的方法如下:
from ultralytics import FastSAM
# Create a FastSAM model
model = FastSAM("FastSAM-s.pt") # or FastSAM-x.pt
# Validate the model
results = model.val(data="coco8-seg.yaml")请注意,FastSAM 仅支持对单一类别的对象进行检测和分割。这意味着它会将所有对象识别并分割为同一类别。因此,在准备数据集时,你需要将所有对象类别 ID 转换为 0。
Link to this section跟踪使用#
要在图像上执行对象跟踪,请使用 track 方法,如下所示:
from ultralytics import FastSAM
# Create a FastSAM model
model = FastSAM("FastSAM-s.pt") # or FastSAM-x.pt
# Track with a FastSAM model on a video
results = model.track(source="path/to/video.mp4", imgsz=640)Link to this sectionFastSAM 官方使用方法#
FastSAM 也可以直接从 https://github.com/CASIA-IVA-Lab/FastSAM 仓库获取。以下是你使用 FastSAM 时通常会采取的步骤的简要概述:
Link to this section安装#
-
克隆 FastSAM 仓库:
git clone https://github.com/CASIA-IVA-Lab/FastSAM.git -
创建并激活一个 Python 3.9 的 Conda 环境:
conda create -n FastSAM python=3.9 conda activate FastSAM -
导航到克隆的仓库并安装所需的软件包:
cd FastSAM pip install -r requirements.txt -
安装 CLIP 模型:
pip install git+https://github.com/ultralytics/CLIP.git
Link to this section使用示例#
-
下载一个 模型检查点。
-
使用 FastSAM 进行推理。示例命令:
-
分割图像中的所有内容:
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg -
使用文本提示分割特定对象:
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --text_prompt "the yellow dog" -
分割 边界框 内的对象(以 xywh 格式提供框坐标):
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --box_prompt "[570,200,230,400]" -
分割特定点附近的对象:
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --point_prompt "[[520,360],[620,300]]" --point_label "[1,0]"
-
此外,你可以通过 CASIA-IVA-Lab 的 Colab 演示 试用 FastSAM。
Link to this section引用与致谢#
我们要感谢 FastSAM 作者在实时实例分割领域所做的重大贡献:
@misc{zhao2023fast,
title={Fast Segment Anything},
author={Xu Zhao and Wenchao Ding and Yongqi An and Yinglong Du and Tao Yu and Min Li and Ming Tang and Jinqiao Wang},
year={2023},
eprint={2306.12156},
archivePrefix={arXiv},
primaryClass={cs.CV}
}最初的 FastSAM 论文可以在 arXiv 上找到。作者已将其工作公开,代码库可以在 GitHub 上访问。我们感谢他们在推进该领域并让更广泛的社区能够接触到他们的工作所做的努力。
Link to this section常见问题解答#
Link to this section什么是 FastSAM,它与 SAM 有什么不同?#
FastSAM(快速分割一切模型)是一种基于实时 卷积神经网络 (CNN) 的解决方案,旨在减少计算需求,同时在对象分割任务中保持高性能。与使用更重型的基于 Transformer 架构的 Segment Anything Model (SAM) 不同,FastSAM 利用 Ultralytics YOLOv8-seg 进行高效的实例分割,分为两个阶段:全实例分割,随后是提示引导选择。
Link to this sectionFastSAM 如何实现实时分割性能?#
FastSAM 通过将分割任务解耦为使用 YOLOv8-seg 的全实例分割和提示引导选择阶段来实现实时分割。通过利用 CNN 的计算效率,FastSAM 在保持竞争性能的同时,大幅降低了计算和资源需求。这种双阶段方法使 FastSAM 能够提供快速高效的分割,适用于需要快速结果的应用。
Link to this sectionFastSAM 的实际应用是什么?#
FastSAM 适用于各种需要实时分割性能的 计算机视觉 任务。应用包括:
- 用于质量控制和保证的工业自动化
- 用于安全和监控的实时视频分析
- 用于对象检测和分割的 自动驾驶车辆
- 用于精确快速分割任务的医学影像
它处理各种用户交互提示的能力使 FastSAM 能够在不同场景中具备适应性和灵活性。
Link to this section如何在 Python 中使用 FastSAM 模型进行推理?#
要在 Python 中使用 FastSAM 进行推理,你可以遵循以下示例:
from ultralytics import FastSAM
# Define an inference source
source = "path/to/bus.jpg"
# Create a FastSAM model
model = FastSAM("FastSAM-s.pt") # or FastSAM-x.pt
# Run inference on an image
everything_results = model(source, device="cpu", retina_masks=True, imgsz=1024, conf=0.4, iou=0.9)
# Run inference with bboxes prompt
results = model(source, bboxes=[439, 437, 524, 709])
# Run inference with points prompt
results = model(source, points=[[200, 200]], labels=[1])
# Run inference with texts prompt
results = model(source, texts="a photo of a dog")
# Run inference with bboxes and points and texts prompt at the same time
results = model(source, bboxes=[439, 437, 524, 709], points=[[200, 200]], labels=[1], texts="a photo of a dog")有关推理方法的更多详细信息,请查看文档的 预测使用 部分。
Link to this sectionFastSAM 支持哪些类型的分割任务提示?#
FastSAM 支持多种用于引导分割任务的提示类型:
- 所有内容提示 (Everything Prompt):为所有可见对象生成分割。
- 边界框提示 (BBox Prompt):分割指定边界框内的对象。
- 文本提示 (Text Prompt):使用描述性文本来分割符合描述的对象。
- 点提示 (Point Prompt):分割特定用户定义点附近的对象。
这种灵活性使 FastSAM 能够适应广泛的用户交互场景,增强了其在不同应用中的实用性。有关使用这些提示的更多信息,请参阅 主要功能 部分。