Apple 的 MobileCLIP:开源移动视觉模型
Apple 的 MobileCLIP – 开源移动视觉模型
Apple 在 2024‑2025 年发布了 MobileCLIP 和 MobileCLIP2 库,为在 iPhone 硬件上流畅运行的高精度图像‑文本模型提供支持。GitHub 仓库 ml‑mobileclip 以 完整实现——从训练流水线到端到端 iOS 应用——的形式发售,使其成为想在移动设备上构建视觉‑语言功能的开发者的理想案例研究。
MobileCLIP 是什么?
MobileCLIP 是一系列轻量级多模态模型,使用 MobileOne 体系结构进行 CLIP 的改造。它在保持参数量和延迟最小化的同时,实现了零样本的最先进性能。
- 变体:S0(小 0)到 S4(小 4)、B(标准)和 L‑14(大型)。更新的 MobileCLIP2 通过改进训练稳定性、使用更大的数据集以及新增 S3 / S4 对,进一步提升性能。
- 训练数据:MobileCLIP 采用 DataCompDR‑1B(1 B 图像‑文本对),MobileCLIP2 则使用 DFNDR‑2B,并可选用 CoCa 模型生成的合成标题。
- 性能:在 ImageNet‑1k 零样本 top‑1 上,MobileCLIP‑S0 与 ViT‑B/16 的性能相当,同时速度是前者的 4.8 倍,体积是其 2.8 倍。MobileCLIP2‑S4 在 iPhone 12 Pro Max 上实现 81.9% 的准确率,延迟仅 19.6 ms。
仓库亮点
| 章节 | 内容简介 |
|---|---|
mobileclip/ |
核心模型代码(MobileOne、Transformer 头部、可高效推理的重参化)。 |
training/ |
通过命令行脚本、分布式训练工具进行自定义数据集微调的流水线。 |
eval/ |
零样本评估脚本(ImageNet、38 数据集基准)。 |
ios_app/ |
Swift 项目展示在 iPhone 上实时零样本分类的示例。 |
docs/ |
精度‑延迟图、架构图、许可证信息。 |
所有模块均可通过 pip install -e . 安装,并与流行 OpenCLIP 框架顺畅集成。
快速入门:使用 OpenCLIP 推理
# 1️⃣ 创建虚拟环境并安装库
conda create -n clipenv python=3.10 -y
conda activate clipenv
pip install -e .
import torch, open_clip
from PIL import Image
from mobileclip.modules.common.mobileone import reparameterize_model
model_name = "MobileCLIP2-S0"
# 如果想使用 HuggingFace 上的预训练模型
# hf download apple/MobileCLIP2-S0 --> 本地检查点
model, _, preprocess = open_clip.create_model_and_transforms(
model_name,
pretrained="/path/to/mobileclip2_s0.pt",
image_mean=(0,0,0), image_std=(1,1,1)
)
model.eval()
model = reparameterize_model(model) # 加速部署
image = preprocess(Image.open("path/to/image.jpg").convert("RGB")).unsqueeze(0)
text = open_clip.get_tokenizer(model_name)(["a dog", "a cat", "a tree"]) # 句子列表
with torch.no_grad():
i_f = model.encode_image(image)
t_f = model.encode_text(text)
i_f /= i_f.norm(dim=-1, keepdim=True)
t_f /= t_f.norm(dim=-1, keepdim=True)
probs = torch.softmax(100.0 * i_f @ t_f.T, dim=-1)
print("label probs:", probs)
reparameterize_model() 会把 batch‑norm 层折叠成单个变换,使 MobileCLIP 成为纯 Transformer,更快地在 GPU/CPU 上运行。
零样本评估
# 运行预构建的 ImageNet‑1k 脚本
python eval/zeroshot_imagenet.py \
--model-arch mobileclip_s0 \
--model-path /path/to/mobileclip_s0.pt
脚本输出 top‑1 准确率、延迟(ms)、已见样本数以及跨 38 个数据集的综合指标。
运行 iOS 演示
仓库附带可用 Xcode 15+ 构建的 iOS 应用。步骤如下:
- 克隆仓库并定位
ios_app文件夹。 - 打开
AppleMobileCLIP.xcodeproj。 - 从 HuggingFace 或本地路径拉取预训练
mobileclip_s0.pt。 - 将文件添加到 Xcode 项目下的 Resources。
- 点击 Run – 应用会加载模型、从摄像头捕获并实时分类帧。
此演示展示了 MobileCLIP2‑S0 在 iPhone 12 Pro Max 上的运行效果,图像‑文本推理时间约 2 ms。
扩展与微调
- 自定义数据集 – 把 HuggingFace 样式的数据集放到
training/并修改train.py。 - 新架构 – Fork MobileOne,添加自己的
head并进行重参化。 - 量化 – 使用 OpenCLIP‑Q 或 PyTorch 量化工具进一步压缩延迟。
社区鼓励开启 issue 请求新功能,例如 TensorRT 导出或 Android 支持。
上下文中的性能
| 模型 | 参数量 | 延迟(ms) | ImageNet‑1k Top‑1 | 38 数据集平均精度 |
|---|---|---|---|---|
| MobileCLIP‑S0 | 11.4 + 63.4 | 1.5 + 3.3 | 67.8 % | 58.1 % |
| MobileCLIP2‑S0 | 11.4 + 63.4 | 1.5 + 3.3 | 71.5 % | 59.7 % |
| MobileCLIP2‑S2 | 35.7 + 63.4 | 3.6 + 3.3 | 77.2 % | 64.1 % |
| MobileCLIP2‑L‑14 | 304.3 + 123.6 | 57.9 + 6.6 | 81.9 % | 67.8 % |
这些数据表明,MobileCLIP2 在常见硬件上可达到与大型 ViT 模型相匹敌的性能,并且速度更快。
最终思考
Apple 的 MobileCLIP 库为在手机上运行的视觉‑语言任务提供了 完整、生产就绪 的堆栈。通过发布源代码、预训练检查点、评估脚本以及 iOS 演示,仓库让研究人员和应用开发者能够快速实验、微调并发布新服务。无论你是在构建通过文本提示检测对象的 AR 滤镜,还是将零样本分类推理推到边缘,MobileCLIP 都为你提供所需的工具与性能。
欲了解更多信息,请访问官方 GitHub 仓库,阅读相应的研究论文,并在 HuggingFace 上探索预训练模型。