跳至内容

快速分段 Anything Model (FastSAM)

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

快速分段 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 以明显快于单个英伟达™(NVIDIA®)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
ann = prompt_process.everything_prompt()

# Bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
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
# points default [[0,0]] [[x1,y1],[x2,y2]]
# point_label default [0] [1,0] 0:background, 1:foreground
ann = prompt_process.point_prompt(points=[[200, 200]], pointlabel=[1])
prompt_process.plot(annotations=ann, output='./')
# Load a FastSAM model and segment everything with it
yolo segment predict model=FastSAM-s.pt source=path/to/bus.jpg imgsz=640

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

阀门使用

在数据集上验证模型的方法如下:

示例

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
  1. 使用Python 3.9 创建并激活 Conda 环境:
conda create -n FastSAM python=3.9
conda activate FastSAM
  1. 导航至克隆的软件源并安装所需的软件包:
cd FastSAM
pip install -r requirements.txt
  1. 安装 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 上访问。我们感谢他们为推动这一领域的发展和让更多人了解他们的工作所做的努力。



创建于 2023-11-12,更新于 2024-05-01
作者:RizwanMunawar(1)、glenn-jocher(8)、Laughing-q(1)、berry-ding(1)

评论