Ultralytics YOLO27:

快速分割一切模型 (FastSAM)#

快速分割一切模型 (FastSAM) 是一种新颖的实时、基于 CNN 的分割一切任务解决方案。该任务旨在根据各种可能的用户交互提示,对图像中的任意对象进行分割。FastSAM 在保持具有竞争力的性能的同时,显著降低了计算需求,因此成为各种视觉任务的实用选择。



Watch: Object Tracking using FastSAM with Ultralytics

模型架构#

快速分割一切模型 (FastSAM) 架构概览

概述#

FastSAM 旨在解决 分割一切模型 (SAM) 的局限性。后者是一种计算资源需求很高的大型 Transformer 模型。FastSAM 将分割一切任务解耦为两个连续阶段:全实例分割和提示引导选择。第一阶段使用 YOLOv8-seg 生成图像中所有实例的分割掩码。第二阶段输出与提示对应的感兴趣区域。

主要特性#

  1. 实时解决方案: 借助 CNN 的计算效率,FastSAM 为分割一切任务提供了实时解决方案,因此对于需要快速结果的工业应用非常有价值。

  2. 效率与性能: FastSAM 在不影响性能质量的情况下,显著降低了计算和资源需求。它以大幅减少的计算资源实现了与 SAM 相当的性能,从而支持实时应用。

  3. 提示引导分割: FastSAM 可以根据各种可能的用户交互提示,对图像中的任意对象进行分割,在不同场景中提供灵活性和适应性。

  4. 基于 YOLOv8-seg: FastSAM 基于 YOLOv8-seg,这是一种配备实例分割分支的目标检测器。这使其能够有效生成图像中所有实例的分割掩码。

  5. 基准测试中的竞争力结果: 在 MS COCO 的目标提议任务中,FastSAM 在单个 NVIDIA RTX 3090 上以显著更快的速度取得了高分,性能超过 SAM,展现了其效率和能力。

  6. 实际应用: 所提出的方法为大量视觉任务提供了一种全新的实用解决方案,速度非常快,比当前方法快几十倍甚至数百倍。

  7. 模型压缩可行性: FastSAM 通过向结构引入人工先验,展示了一条显著减少计算量的可行路径,从而为面向通用视觉任务的大型模型架构开辟了新的可能性。

可用模型、支持的任务和运行模式#

此表列出了可用模型及其特定的预训练权重、支持的任务,以及与不同运行模式的兼容性,例如推理验证训练导出。支持的模式以 ✅ 表示,不支持的模式以 ❌ 表示。

模型类型预训练权重支持的任务训练验证推理导出
FastSAM-sFastSAM-s.pt实例分割
FastSAM-xFastSAM-x.pt实例分割

FastSAM 与 YOLO 的比较#

在这里,我们将 FastSAM-s 与 Meta 的 SAM 变体以及包括 YOLO26n-seg 在内的 Ultralytics 分割模型进行比较:

模型大小
(MB)
参数量
(M)
速度(CPU)
(毫秒/图像)
Meta SAM-b37593.741703
Meta SAM2-b16280.828867
Meta SAM2-t78.138.923430
MobileSAM40.710.123802
使用 YOLOv8 主干网络FastSAM-s23.911.858.0
Ultralytics YOLOv8n-seg7.1 (体积缩小 11.0 倍)3.4 (参数减少 11.4 倍)24.8 (速度提升 945 倍)
Ultralytics YOLO11n-seg6.2(小 12.6 倍)2.9 (参数减少 13.4 倍)24.3(快 964 倍)
Ultralytics YOLO26n-seg6.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 RAM 的 2025 款 Apple M4 Air 上运行,使用了 torch==2.10.0ultralytics==8.4.31onnxruntime==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, nms=False)  # YOLO26 NMS-free head; no-op for YOLOv8/YOLO11
    model = YOLO(onnx_path)
    model(ASSETS)

使用示例#

FastSAM 模型易于集成到你的 Python 应用中。Ultralytics 提供易用的 Python API 和 CLI 命令,以简化开发流程。

预测用法#

要分割图像,请使用如下所示的 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")

此代码片段展示了如何轻松加载预训练模型并对图像运行预测。

FastSAMPredictor 示例

这样,你可以对图像运行推理并获取所有分割 results,只运行一次,然后多次运行提示推理,而无需多次运行推理。

from ultralytics.models.fastsam import FastSAMPredictor

# Create FastSAMPredictor
overrides = {"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 对象,可用于轻松访问预测掩码和源图像。

验证用法#

请注意,FastSAM 仅支持对单一对象类别进行检测和分割。这意味着它会将所有对象识别并分割为同一类别。因此,在准备数据集时,你需要将所有对象类别 ID 转换为 0。

可以按如下方式在数据集上验证模型:

示例
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")

跟踪用法#

要对图像执行目标跟踪,请使用下面所示的 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)

FastSAM 官方用法#

FastSAM 也可直接从 CASIA-LMC-Lab/FastSAM 仓库获取。以下是使用 FastSAM 时可能采取的典型步骤的简要概述:

安装#

  1. 克隆 FastSAM 仓库:

    git clone https://github.com/CASIA-LMC-Lab/FastSAM.git
  2. 创建并激活一个包含 Python 3.9 的 Conda 环境:

    conda create -n FastSAM python=3.9
    conda activate FastSAM
  3. 进入克隆的仓库并安装所需软件包:

    cd FastSAM
    pip install -r requirements.txt
  4. 安装 CLIP 模型:

    pip install git+https://github.com/ultralytics/CLIP.git

用法示例#

  1. 下载一个模型检查点

  2. 使用 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-LMC-Lab 的 Colab demo 尝试 FastSAM。

引用和致谢#

我们感谢 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 上访问。我们感谢他们为推动该领域发展以及让更广泛的社区能够使用其成果所付出的努力。

常见问题#

  • FastSAM 是 Fast Segment Anything Model 的简称,是一种基于实时卷积神经网络 (CNN) 的解决方案,旨在降低计算需求,同时在目标分割任务中保持高性能。与采用更大型 Transformer 架构的分割一切模型 (SAM) 不同,FastSAM 利用 Ultralytics YOLOv8-seg,通过两个阶段实现高效实例分割:全实例分割,然后进行提示引导选择。

  • FastSAM 将分割任务解耦为使用 YOLOv8-seg 的全实例分割阶段和提示引导选择阶段,从而实现实时分割。借助 CNN 的计算效率,FastSAM 在保持具有竞争力的性能的同时,显著降低了计算和资源需求。这种双阶段方法使 FastSAM 能够提供快速、高效的分割,适用于需要快速结果的应用。

  • FastSAM 适用于各种需要实时分割性能的计算机视觉任务。应用包括:

    • 用于质量控制和质量保证的工业自动化
    • 用于安全和监控的实时视频分析
    • 用于目标检测和分割的自动驾驶车辆
    • 用于精准、快速分割任务的医学影像

    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")

    有关推理方法的更多详情,请查看文档中的 预测用法 部分。

  • FastSAM 支持多种用于引导分割任务的提示类型:

    • 全体对象提示:为所有可见对象生成分割结果。
    • 边界框 (BBox) 提示:分割指定边界框内的对象。
    • 文本提示:使用描述性文本分割与描述匹配的对象。
    • 点提示:分割特定用户定义点附近的对象。

    这种灵活性使 FastSAM 能够适应各种用户交互场景,提升其在不同应用中的实用性。有关使用这些提示的更多信息,请参阅关键特性部分。

评论