跳至内容

快速分段 Anything Model (FastSAM)

Fast Segment Anything Model (FastSAM) 是一种基于 CNN 的新型实时解决方案,可用于 Segment Anything 任务。该任务旨在根据各种可能的用户交互提示分割图像中的任何物体。FastSAM 在保持极具竞争力的性能的同时大幅降低了计算需求,使其成为各种视觉任务的实用选择。



观看: 使用FastSAM 进行物体跟踪Ultralytics

模型架构

快速分段 Anything Model (FastSAM) 架构概述

概述

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

主要功能

  1. 实时解决方案:通过利用 CNN 的计算效率,FastSAM 可为任何细分任务提供实时解决方案,这使其在需要快速结果的工业应用中具有重要价值。

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

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

  4. 基于YOLOv8-seg FastSAM 基于YOLOv8-seg,这是一个配备实例分割分支的对象检测器。这样,它就能有效地生成图像中所有实例的分割掩码。

  5. 基准测试的竞争结果:在 MS COCO 的对象建议任务中,FastSAM 以明显快于单个 RTX 3090 的速度获得了高分,这证明了它的效率和能力。 SAM在单个NVIDIA RTX 3090 上取得的高分,证明了其效率和能力。

  6. 实际应用:所提出的方法为大量视觉任务提供了全新的实用解决方案,而且速度非常快,是现有方法的数十倍或数百倍。

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

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

本表介绍了可用模型及其特定的预训练权重、支持的任务以及与推理验证训练导出等不同操作模式的兼容性,支持的模式用 ✅ 符号表示,不支持的模式用 ❌ 符号表示。

型号 预训练重量 支持的任务 推论 验证 培训 出口
FastSAM-s FastSAM-s.pt 实例分割
FastSAM-x FastSAM-x.pt 实例分割

使用示例

FastSAM 模型可轻松集成到您的Python 应用程序中。Ultralytics 提供用户友好的Python API 和CLI 命令,以简化开发。

预测使用情况

要在图像上执行对象检测,请使用 predict 方法,如下图所示:

示例

from ultralytics import FastSAM
from ultralytics.models.fastsam import FastSAMPrompt

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

# Prepare a Prompt Process object
prompt_process = FastSAMPrompt(source, everything_results, device="cpu")

# Everything prompt
results = prompt_process.everything_prompt()

# Bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
results = prompt_process.box_prompt(bbox=[200, 200, 300, 300])

# Text prompt
results = prompt_process.text_prompt(text="a photo of a dog")

# Point prompt
# points default [[0,0]] [[x1,y1],[x2,y2]]
# point_label default [0] [1,0] 0:background, 1:foreground
results = prompt_process.point_prompt(points=[[200, 200]], pointlabel=[1])
prompt_process.plot(annotations=results, output="./")
# Load a FastSAM model and segment everything with it
yolo segment predict model=FastSAM-s.pt source=path/to/bus.jpg imgsz=640

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

备注

所有返回的 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/file.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]"
      

此外,您还可以通过Colab 演示HuggingFace 网页演示试用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是一种基于实时卷积神经网络(CNN)的解决方案,旨在降低计算需求,同时保持对象分割任务的高性能。与使用重型变换器架构的 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
from ultralytics.models.fastsam import FastSAMPrompt

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

# Prepare a Prompt Process object
prompt_process = FastSAMPrompt(source, everything_results, device="cpu")

# Everything prompt
ann = prompt_process.everything_prompt()

# Bounding box prompt
ann = prompt_process.box_prompt(bbox=[200, 200, 300, 300])

# Text prompt
ann = prompt_process.text_prompt(text="a photo of a dog")

# Point prompt
ann = prompt_process.point_prompt(points=[[200, 200]], pointlabel=[1])
prompt_process.plot(annotations=ann, output="./")

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

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

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

  • 一切提示:生成所有可见物体的分割。
  • 边界框 (BBox) 提示:在指定的边界框内分割对象。
  • 文本提示:使用描述性文本来分割与描述相符的对象。
  • 点提示:对用户定义的特定点附近的对象进行分割。

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



创建于 2023-11-12,更新于 2024-07-18
作者:Laughing-q(2),glenn-jocher(13),RizwanMunawar(2),berry-ding(1)

评论