
Mobile Segment Anything (MobileSAM)
MobileSAM 是一款紧凑、高效的图像分割模型,专为移动和边缘设备打造。它旨在将 Meta 的 Segment Anything Model (SAM) 的强大功能带入计算资源有限的环境,在保持与原始 SAM 流水线兼容的同时提供近乎即时的分割效果。无论你是开发实时应用程序还是进行轻量级部署,MobileSAM 都能以远低于前代模型的尺寸和速度需求提供出色的分割结果。
Watch: How to Run Inference with MobileSAM using Ultralytics | Step-by-Step Guide 🎉
MobileSAM 已被广泛应用于多个项目,包括 Grounding-SAM、AnyLabeling 以及 Segment Anything in 3D。
MobileSAM 使用 10 万张图像数据集(原始图像的 1%)在单张 GPU 上不到一天内训练完成。训练代码将在未来发布。
可用模型、支持的任务和操作模式
下表列出了可用的 MobileSAM 模型、其预训练权重、支持的任务以及与不同操作模式的兼容性,例如 推理、验证、训练 和 导出。支持的模式由 ✅ 指示,不支持的模式由 ❌ 指示。
| 模型类型 | 预训练权重 | 支持的任务 | 推理 | 验证 | 训练 | 导出 |
|---|---|---|---|---|---|---|
| MobileSAM | mobile_sam.pt | 实例分割 | ✅ | ❌ | ❌ | ❌ |
MobileSAM 与 YOLO 的对比
以下对比突出了 Meta 的 SAM 变体、MobileSAM 以及包含 YOLO26n-seg 在内的 Ultralytics 分割模型之间的差异:
| 模型 | 大小 (MB) | 参数 (M) | 速度 (CPU) (ms/im) |
|---|---|---|---|
| Meta SAM-b | 375 | 93.7 | 41703 |
| Meta SAM2-b | 162 | 80.8 | 28867 |
| Meta SAM2-t | 78.1 | 38.9 | 23430 |
| MobileSAM | 40.7 | 10.1 | 23802 |
| 使用 YOLOv8 骨干网络 的 FastSAM-s | 23.9 | 11.8 | 58.0 |
| Ultralytics YOLOv8n-seg | 7.1 (11.0x 更小) | 3.4 (11.4x 更少) | 24.8 (945x 更快) |
| Ultralytics YOLO11n-seg | 6.2 (12.6x 更小) | 2.9 (13.4x 更少) | 24.3 (964x 更快) |
| Ultralytics YOLO26n-seg | 6.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 RAM) 上进行,使用 torch==2.10.0、ultralytics==8.4.31 和 onnxruntime==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)从 SAM 适配到 MobileSAM
MobileSAM 保留了与原始 SAM 相同的流水线,包括预处理、后处理和所有接口。这意味着你可以以极小的改动将工作流从 SAM 迁移到 MobileSAM。
关键区别在于图像编码器:MobileSAM 将原始的 ViT-H 编码器(6.32 亿参数)替换为更小的 Tiny-ViT 编码器(500 万参数)。在单张 GPU 上,MobileSAM 处理一张图像大约需要 12 毫秒(编码器 8 毫秒,掩码解码器 4 毫秒)。
基于 ViT 的图像编码器对比
| 图像编码器 | 原始 SAM | MobileSAM |
|---|---|---|
| 参数量 | 6.11 亿 | 500 万 |
| 速度 | 452 毫秒 | 8 毫秒 |
提示引导掩码解码器
| 掩码解码器 | 原始 SAM | MobileSAM |
|---|---|---|
| 参数量 | 387.6 万 | 387.6 万 |
| 速度 | 4 毫秒 | 4 毫秒 |
完整流水线对比
| 完整流水线 (编码+解码) | 原始 SAM | MobileSAM |
|---|---|---|
| 参数量 | 6.15 亿 | 966 万 |
| 速度 | 456 毫秒 | 12 毫秒 |
MobileSAM 和原始 SAM 的性能表现如下图所示,使用了点和框两种提示方式。


MobileSAM 的尺寸约为 FastSAM 的 1/7,速度则快 5 倍。更多详细信息,请访问 MobileSAM 项目页面。
在 Ultralytics 中测试 MobileSAM
正如原始 SAM 一样,Ultralytics 提供了一个简单的接口来测试 MobileSAM,支持点和框提示。
模型下载
从 Ultralytics 资源库 下载 MobileSAM 预训练权重。
点提示
from ultralytics import SAM
# Load the model
model = SAM("mobile_sam.pt")
# Predict a segment based on a single point prompt
model.predict("ultralytics/assets/zidane.jpg", points=[900, 370], labels=[1])
# Predict multiple segments based on multiple points prompt
model.predict("ultralytics/assets/zidane.jpg", points=[[400, 370], [900, 370]], labels=[1, 1])
# Predict a segment based on multiple points prompt per object
model.predict("ultralytics/assets/zidane.jpg", points=[[[400, 370], [900, 370]]], labels=[[1, 1]])
# Predict a segment using both positive and negative prompts.
model.predict("ultralytics/assets/zidane.jpg", points=[[[400, 370], [900, 370]]], labels=[[1, 0]])框提示
from ultralytics import SAM
# Load the model
model = SAM("mobile_sam.pt")
# Predict a segment based on a single point prompt
model.predict("ultralytics/assets/zidane.jpg", points=[900, 370], labels=[1])
# Predict multiple segments based on multiple points prompt
model.predict("ultralytics/assets/zidane.jpg", points=[[400, 370], [900, 370]], labels=[1, 1])
# Predict a segment based on multiple points prompt per object
model.predict("ultralytics/assets/zidane.jpg", points=[[[400, 370], [900, 370]]], labels=[[1, 1]])
# Predict a segment using both positive and negative prompts.
model.predict("ultralytics/assets/zidane.jpg", points=[[[400, 370], [900, 370]]], labels=[[1, 0]])MobileSAM 和 SAM 共享相同的 API。更多使用细节,请参阅 SAM 文档。
使用检测模型自动构建分割数据集
要使用 Ultralytics 框架自动 标注你的数据集,请按照如下所示使用 auto_annotate 函数:
from ultralytics.data.annotator import auto_annotate
auto_annotate(data="path/to/images", det_model="yolo26x.pt", sam_model="mobile_sam.pt")| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
data | str | 必需 | 包含用于标注或分割的目标图像的目录路径。 |
det_model | str | 'yolo26x.pt' | 用于初始对象检测的 YOLO 检测模型路径。 |
sam_model | str | 'sam_b.pt' | 用于分割的 SAM 模型路径(支持 SAM、SAM2 变体和 MobileSAM 模型)。 |
device | str | '' | 计算设备(例如 'cuda:0'、'cpu',或 '' 用于自动设备检测)。 |
conf | float | 0.25 | 用于过滤弱检测的 YOLO 检测置信度阈值。 |
iou | float | 0.45 | 用于非极大值抑制(NMS)以过滤重叠框的 IoU 阈值。 |
imgsz | int | 640 | 调整图像大小的输入尺寸(必须是 32 的倍数)。 |
max_det | int | 300 | 每张图像的最大检测数量,用于提高内存效率。 |
classes | list[int] | None | 要检测的类索引列表(例如 [0, 1] 代表人和自行车)。 |
output_dir | str | None | 标注的保存目录(默认相对于数据路径为 './labels')。 |
引文与致谢
如果 MobileSAM 对你的研究或开发有所帮助,请考虑引用以下论文:
@article{mobile_sam,
title={Faster Segment Anything: Towards Lightweight SAM for Mobile Applications},
author={Zhang, Chaoning and Han, Dongshen and Qiao, Yu and Kim, Jung Uk and Bae, Sung Ho and Lee, Seungkyu and Hong, Choong Seon},
journal={arXiv preprint arXiv:2306.14289},
year={2023}
}常见问题 (FAQ)
什么是 MobileSAM,它与原始 SAM 模型有何不同?
MobileSAM 是一款轻量级、快速的 图像分割 模型,专为移动和边缘应用优化。它保持了与原始 SAM 相同的流水线,但用紧凑的 Tiny-ViT 编码器(500 万参数)替换了大型 ViT-H 编码器(6.32 亿参数)。这使得 MobileSAM 的体积比原始 SAM 小约 5 倍,速度快约 7 倍,处理单张图像约为 12 毫秒,而 SAM 为 456 毫秒。更多关于 MobileSAM 实现的信息,请访问 MobileSAM GitHub 仓库。
我该如何使用 Ultralytics 测试 MobileSAM?
在 Ultralytics 中测试 MobileSAM 非常直观。你可以使用点和框提示来预测分割区域。例如,使用点提示:
from ultralytics import SAM
# Load the model
model = SAM("mobile_sam.pt")
# Predict a segment based on a point prompt
model.predict("ultralytics/assets/zidane.jpg", points=[900, 370], labels=[1])更多详细信息,请参阅 在 Ultralytics 中测试 MobileSAM 一节。
为什么我应该在移动应用中使用 MobileSAM?
由于其轻量级设计和极快的推理速度,MobileSAM 是移动和边缘应用的理想选择。与原始 SAM 相比,MobileSAM 体积小约 5 倍,速度快 7 倍,非常适合在计算资源有限的设备上进行实时分割。其高效性使移动设备能够实现 实时图像分割 且没有明显的延迟。此外,MobileSAM 支持针对移动性能优化的 推理模式。
MobileSAM 是如何训练的,训练代码是否可用?
MobileSAM 使用 10 万张图像数据集(原始图像的 1%)在单张 GPU 上不到一天内训练完成。虽然训练代码将在未来发布,但你目前可以从 MobileSAM GitHub 仓库 获取预训练权重和实现细节。
MobileSAM 的主要用例是什么?
MobileSAM 专为移动和边缘环境下的快速高效图像分割而设计。主要用例包括:
- 移动应用程序的 实时 目标检测和分割
- 计算受限设备上的 低延迟图像处理
- 集成到 AI 驱动的移动应用程序中,用于增强现实 (AR)、分析等
有关用例和性能的更多详情,请参阅 从 SAM 适配到 MobileSAM 以及 关于 MobileSAM 应用的 Ultralytics 博客。