快速分割一切模型 (FastSAM)

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



Watch: Object Tracking using FastSAM with Ultralytics

模型架构

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

概述

FastSAM 旨在解决 Segment Anything Model (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 的比较

在此,我们将 Meta 的 SAM 2 模型(包括最小的 SAM2-t 变体)与包括 YOLO26n-seg 在内的 Ultralytics 分割模型进行了比较:

模型大小
(MB)
参数
(M)
速度 (CPU)
(ms/im)
Meta SAM-b37593.741703
Meta SAM2-b16280.828867
Meta SAM2-t78.138.923430
MobileSAM40.710.123802
FastSAM-s 使用 YOLOv8 骨干网络23.911.858.0
Ultralytics YOLOv8n-seg7.1 (11.0x 更小)3.4 (11.4x 更少)24.8 (945x 更快)
Ultralytics YOLO11n-seg6.2 (12.6x 更小)2.9 (13.4x 更少)24.3 (964x 更快)
Ultralytics YOLO26n-seg6.7 (11.7x 更小)2.7 (14.4x 更少)25.2 (930x 更快)

此对比展示了 SAM 变体与 YOLO 分割模型之间在模型大小和速度上的巨大差异。虽然 SAM 提供了独特的自动分割能力,但 YOLO 模型,尤其是 YOLOv8n-seg、YOLO11n-seg 和 YOLO26n-seg,在体积上明显更小、速度更快,计算效率也更高。

SAM 速度使用 PyTorch 测量,YOLO 速度使用 ONNX Runtime 测量。测试在 2025 年的 Apple M4 Air(16GB 内存)上运行,使用 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)
    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 = 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 对象,它们允许轻松访问预测的掩码和源图像。

验证用法

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

示例
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。

跟踪用法

要在图像上执行对象跟踪,请使用如下所示的 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 也可直接从 https://github.com/CASIA-IVA-Lab/FastSAM 仓库获取。以下是你使用 FastSAM 时通常会采取的步骤概述:

安装

  1. 克隆 FastSAM 仓库:

    git clone https://github.com/CASIA-IVA-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"
    • 分割 bounding box 内的对象(以 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 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 上访问。我们感谢他们在推动该领域发展并使广大社区能够使用其工作方面所做的努力。

常见问题 (FAQ)

什么是 FastSAM,它与 SAM 有何不同?

FastSAM 是“快速分割一切模型”的缩写,是一种基于卷积神经网络 (CNN) 的实时解决方案,旨在降低计算需求的同时,在对象分割任务中保持高性能。与使用更重型基于 Transformer 架构的分割一切模型 (SAM) 不同,FastSAM 利用 Ultralytics YOLOv8-seg 进行高效的实例分割,分为两个阶段:全实例分割,随后是提示引导的选择。

FastSAM 如何实现实时分割性能?

FastSAM 通过将分割任务拆解为使用 YOLOv8-seg 进行全实例分割和提示引导选择阶段来实现实时分割。通过利用 CNN 的计算效率,FastSAM 在保持竞争性性能的同时,大幅降低了计算和资源需求。这种双阶段方法使 FastSAM 能够提供适合需要快速结果的应用的快速高效的分割。

FastSAM 有哪些实际应用?

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

  • 用于质量控制和保证的工业自动化
  • 用于安防监控的实时视频分析
  • 用于对象检测和分割的自动驾驶车辆
  • 用于精确快速分割任务的医学成像

其处理各种用户交互提示的能力使 FastSAM 能够在不同场景下具有适应性和灵活性。

如何在 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")

有关推理方法的更多详细信息,请查看文档的 Predict Usage 部分。

FastSAM 支持哪些类型的分割任务提示?

FastSAM 支持多种提示类型以指导分割任务:

  • Everything 提示:为所有可见对象生成分割。
  • Bounding Box (BBox) 提示:分割指定 bounding box 内的对象。
  • 文本提示:使用描述性文本来分割与描述匹配的对象。
  • 点提示:分割特定用户定义点附近的对象。

这种灵活性使 FastSAM 能够适应广泛的用户交互场景,增强了其在不同应用中的实用性。有关使用这些提示的更多信息,请参阅 Key Features 部分。

评论