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 硬體上。
🎯 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 演示程式,皆採用寬鬆的授權。
相關
- 專案
- 專案
- 專案
- 專案