跳转至内容

Fast Segment Anything Model (FastSAM)

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



观看: 使用 Ultralytics 的 FastSAM 进行对象跟踪

模型架构

Fast Segment Anything Model (FastSAM) 架构概述

概述

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

主要功能

  1. 实时解决方案: 通过利用 CNN 的计算效率,FastSAM 为 segment anything 任务提供了一个实时解决方案,使其对于需要快速结果的工业应用非常有价值。

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

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

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

  5. 基准测试中的竞争性结果: 在 MS COCO 上的对象提议任务中,FastSAM 在单块 NVIDIA RTX 3090 上以比 SAM 快得多的速度实现了高分,证明了其效率和能力。

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

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

可用模型、支持的任务和操作模式

此表显示了可用的模型及其特定的预训练权重、它们支持的任务以及它们与不同操作模式(如 推理验证训练导出)的兼容性,✅ 表情符号表示支持的模式,❌ 表情符号表示不支持的模式。

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

FastSAM 与 YOLO 的比较

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

模型 大小
(MB)
参数
(M)
速度 (CPU)
(ms/im)
Meta SAM-b 375 93.7 49401
Meta SAM2-b 162 80.8 31901
Meta SAM2-t 78.1 38.9 25997
MobileSAM 40.7 10.1 25381
带有 YOLOv8 主干FastSAM-s 23.7 11.8 55.9
Ultralytics YOLOv8n-seg 6.7 (小11.7倍) 3.4 (低 11.4 倍) 24.5 (快 1061 倍)
Ultralytics YOLO11n-seg 5.9 (小13.2倍) 2.9 (低 13.4 倍) 30.1 (快 864 倍)

此比较演示了 SAM 变体和 YOLO 分割模型之间模型大小和速度的显着差异。虽然 SAM 提供了独特的自动分割功能,但 YOLO 模型,特别是 YOLOv8n-seg 和 YOLO11n-seg,明显更小、更快且计算效率更高。

测试在配备 24GB 内存的 2025 款 Apple M4 Pro 上运行,使用 torch==2.6.0ultralytics==8.3.90。要重现此测试:

示例

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
for file_name in ["yolov8n-seg.pt", "yolo11n-seg.pt"]:
    model = YOLO(file_name)
    model.info()
    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")
# Load a FastSAM model and segment everything with it
yolo segment predict model=FastSAM-s.pt source=path/to/bus.jpg imgsz=640

此代码片段演示了加载预训练模型并在图像上运行预测的简易性。

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 都是 结果 对象,可以轻松访问预测的掩码和源图像。

验证用法

可以在数据集上验证模型,如下所示:

示例

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")
# Load a FastSAM model and validate it on the COCO8 example dataset at image size 640
yolo segment val model=FastSAM-s.pt data=coco8.yaml imgsz=640

请注意,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)
yolo segment track model=FastSAM-s.pt 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"
      
    • 边界框内分割对象(以 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。

引用与致谢

我们要感谢 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,它与 SAM 有何不同?

FastSAM,是 Fast Segment Anything Model 的缩写,是一种基于实时卷积神经网络 (CNN) 的解决方案,旨在减少计算需求,同时保持对象分割任务中的高性能。与使用更重的 Transformer 的架构的 Segment Anything Model (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")

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

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

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

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

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



📅 创建于 1 年前 ✏️ 更新于 2 个月前

评论