apple-aiml-research/ml-mobileclip
This repository contains the official implementation of the research papers, "MobileCLIP" CVPR 2024 and "MobileCLIP2" TMLR August 2025
📦 What is MobileCLIP?
MobileCLIP (以及其更新版本 MobileCLIP2) 是轻量、快速的图文模型,旨在为计算能力有限的设备 (例如智能手机) 提供零样本分类与描述功能。它们通过多模态强化训练方案进行训练,该方案将大规模图文数据 (DataCompDR, DFNDR) 与合成描述由 CoCa 模型生成。其目标是在使用比传统 CLIP 模型少 2-5 倍参数且延迟更低的情况下,达到与重量级 CLIP 模型相当的准确度,特别是在 iPhone hardware 硬件上。
🎯 Key Features & Highlights (from the README)
| Feature | Detail |
|---|---||
| Model families | MobileCLIP-S0…S4, MobileCLIP-B, MobileCLIP-L/14 以及较新的 MobileCLIP2-S0…S4, MobileCLIP2-B, MobileCLIP2-L/14 |
| Speed / size | 最小变体 (MobileCLIP-S0) 比 OpenAI 的 ViT-B/16 快 4.8 倍且小 2.8 倍,同时保持相似的零样本性能。MobileCLIP2-S4 在 iPhone 12 Pro Max 上运行速度约为 20 ms (图文处理),提供与大型模型相当的准确度。 |
| Accuracy | MobileCLIP-B (LT) 在 ImageNet-1k 零样本测试中达到 77.2% top-1;MobileCLIP2-S4 在仅见过 13B 样本的情况下达到 81.9%。 |
| Training data | 使用 DataCompDR-1B 数据集 (用于 MobileCLIP) 与 DFNDR 数据集 (用于 MobileCLIP2) 进行训练。 |
| Open-source tooling | 提供推理、训练与评估脚本。模型也通过一个小的补丁与 OpenCLIP 兼容。 |
| Mobile demo | 一个 iOS app (ios_app/) 展示了设备上的实时零样本分类。 |
| Model releases | 预训练权重托管于 HuggingFace (例如 apple/MobileCLIP2-S0)。 |
🚀 Getting Started (quick-start)
- Create a conda environment
conda create -n clipenv python=3.10 conda activate clipenv pip install -e . - Download a checkpoint (example for the smallest MobileCLIP2 model)
hf download apple/MobileCLIP2-S0 # stores the .pt file locally - Run a one-liner inference
这会打印出三个文本提示的概率值。import torch, open_clip from PIL import Image from mobileclip.modules.common.mobileone import reparameterize_model model_name = "MobileCLIP2-S0" model_path = "/path/to/MobileCLIP2-S0.pt" model, _, preprocess = open_clip.create_model_and_transforms( model_name, pretrained=model_path) tokenizer = open_clip.get_tokenizer(model_name) model.eval() model = reparameterize_model(model) # required for batch-norm based models img = preprocess(Image.open("docs/fig_accuracy_latency.png").convert("RGB")).unsqueeze(0) txt = tokenizer(["a diagram", "a dog", "a cat"]) with torch.no_grad(), torch.cuda.amp.autocast(): img_feat = model.encode_image(img) txt_feat = model.encode_text(txt) img_feat = img_feat / img_feat.norm(dim=-1, keepdim=True) txt_feat = txt_feat / txt_feat.norm(dim=-1, keepdim=True) probs = (100.0 * img_feat @ txt_feat.T).softmax(dim=-1) print("Label probs:", probs)
📚 Training & Evaluation
- Training – 仓库包含一个
training/文件夹,其中包含使用 OpenCLIP 代码库来训练 MobileCLIP/2 模型模型的脚本。README 指向一个单独的仓库 (ml-mobileclip-dr) 用于生成大规模的多模态强化数据集。 - Evaluation – 零样本 ImageNet-1k 评估可以运行如下:
若要进行完整的 38 个数据集基准测试,请遵循python eval/zeroshot_imagenet.py --model-arch mobileclip_s0 \ --model-path /path/to/mobileclip_s0.ptdatacomp仓库中的说明。
📱 iOS Demo
ios_app/ 目录包含一个极简的 iOS 应用程序,可加载 MobileCLIP 模型并在相机帧上进行实时分类。这展示了模型的设备端延迟表现。
📦 Model Zoo (selected checkpoints)
| Model | Params (M) | Latency (ms) img+txt | ImageNet-1k Zero-Shot |
|---|---|---|---|
| MobileCLIP2-S0 | 11.4 + 63.4 | 1.5 + 3.3 | 71.5 |
| MobileCLIP2-S2 | 35.7 + 63.4 | 3.6 + 3.3 | 77.2 |
| MobileCLIP2-B | 86.3 + 63.4 | 10.4 + 3.3 | 79.4 |
| MobileCLIP2-S4 | 321.6 + 123.6 | 19.6 + 6.6 | 81.9 |
| MobileCLIP-B (LT) | 86.3 + 63.4 | 10.4 + 3.3 | 77.2 |
所有权重均可从 README 中链接的 HuggingFace 集合中下载。
🧩 CoCa Caption Models
此仓库也提供 CoCa 模型,用于生成 DFNDR-2B 数据集的合成描述。它们可以像 CLIP 模型一样通过 OpenCLIP 加载,并提供了一个用于描述生成的简短示例。
📜 License
- Code – MIT License
- Model weights – Apple ML Research Model Terms of Use (一种宽松的仅限研究用途的授权)
- Data – CC-BY-NC-ND 4.0 (非商业, 无衍生作品)
📖 Citation
如果在研究中使用 MobileCLIP 或 MobileCLIP2,请引用 README 中列出的两个论文 (CVPR 2024 和 TMLR 2025)。
TL;DR
MobileCLIP 是一系列轻量、快速的 CLIP 视觉语言模型,专为移动端优化。该仓库提供现成的预训练权重、训练脚本 (通过 OpenCLIP)、评估工具和 iOS 演示程序,均采用宽松的授权。
相关
- 项目
- 项目
- 项目
- 项目