This is the official repository of
The repository contains code for inference, training, and evaluation of MobileCLIP models trained on DataCompDR datasets and MobileCLIP2 models trained on DFNDR dataset.
For code to generate multi-modal reinforced datasets at large scale see ml-mobileclip-dr repository.
MobileCLIP2-S4 matches the accuracy of SigLIP-SO400M/14 with 2x fewer parameters and surpasses DFN ViT-L/14 at 2.5x lower latency measured on iPhone12 Pro Max.MobileCLIP-S3/S4 are our new architectures trained on MobileCLIP’s training dataset, DataCompDR-1B (dashed lines).MobileCLIP-S0 obtains similar zero-shot performance as OpenAI's ViT-B/16 model while being 4.8x faster and 2.8x smaller.MobileCLIP-S2 obtains better avg zero-shot performance than SigLIP's ViT-B/16 model while being 2.3x faster and 2.1x smaller, and trained with 3x less seen samples.MobileCLIP-B (LT) attains zero-shot ImageNet performance of 77.2% which is significantly better than recent works like DFN and SigLIP with similar architectures or even OpenAI's ViT-L/14@336.
conda create -n clipenv python=3.10
conda activate clipenv
pip install -e .
Our models are now natively supported in OpenCLIP. To use MobileCLIP models in OpenCLIP, setup your environment as shown below,
conda create -n clipenv python=3.10
conda activate clipenv
# Clone OpenCLIP repository, add MobileCLIP2 models, and install
git clone https://github.com/mlfoundations/open_clip.git
pushd open_clip
git apply ../mobileclip2/open_clip_inference_only.patch
cp -r ../mobileclip2/* ./src/open_clip/
pip install -e .
popd
pip install git+https://github.com/huggingface/pytorch-image-models
To download pretrained checkpoints follow the code snippet below
# MobileCLIP2
for model in S0 S2 B S3 L-14 S4
do
hf download apple/MobileCLIP2-$model
done
# MobileCLIP
for model in S0 S1 S2 B B-LT S3 L-14 S4
do
hf download apple/MobileCLIP-$model
done
To run inference, see example below,
import torch
import 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_kwargs = {}
if not (model_name == "MobileCLIP2-S3" or model_name == "MobileCLIP2-S4" or model_name.endswith("L-14")):
model_kwargs = {"image_mean": (0, 0, 0), "image_std": (1, 1, 1)}
model, _, preprocess = open_clip.create_model_and_transforms(model_name, pretrained=model_path, **model_kwargs)
tokenizer = open_clip.get_tokenizer(model_name)
# Model needs to be in eval mode for inference because of batchnorm layers unlike ViTs
model.eval()
# For inference/model exporting purposes, please reparameterize first
model = reparameterize_model(model)
image = preprocess(Image.open("docs/fig_accuracy_latency.png").convert("RGB")).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)
The following variants are directly available on OpenCLIP by passing the corresponding pretrained argument and not specifying the image-mean/image-std:
'MobileCLIP-S1', pretrained='datacompdr'
'MobileCLIP-S2', pretrained='datacompdr'
'MobileCLIP-B', pretrained='datacompdr'
'MobileCLIP-B', pretrained='datacompdr_lt'
'MobileCLIP2-B', pretrained='dfndr2b'
'MobileCLIP2-S0', pretrained='dfndr2b'
'MobileCLIP2-S2', pretrained='dfndr2b'
'MobileCLIP2-S3', pretrained='dfndr2b'
'MobileCLIP2-S4', pretrained='dfndr2b'
'MobileCLIP2-L-14', pretrained='dfndr2b'
To models from the official repo, follow the code snippet below
import torch
from PIL import Image
import mobileclip
model, _, preprocess = mobileclip.create_model_and_transforms('mobileclip_s0', pretrained='/path/to/mobileclip_s0.pt')
tokenizer = mobileclip.get_tokenizer('mobileclip_s0')
image = preprocess(Image.open("docs/fig_accuracy_latency.png").convert('RGB')).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)
For an example of loading the data from HuggingFace see hf_dataset_example.py.
Please find the detailed evaluation results here. To reproduce results, we provide script to perform zero-shot evaluation on ImageNet-1k dataset. To evaluate on all the 38 datasets, please follow instructions in datacomp.
# Run evaluation with single GPU
python eval/zeroshot_imagenet.py --model-arch mobileclip_s0 --model-path /path/to/mobileclip_s0.pt
Please refer to Open CLIP Results to compare with other models.
| Model | # Seen Samples (B) | # Params (M) (img + txt) | Latency (ms) (img + txt) | IN-1k Zero-Shot Top-1 Acc. (%) | Avg. Perf. (%) on 38 datasets |
|---|---|---|---|---|---|
| MobileCLIP2-S0 | 13 | 11.4 + 63.4 | 1.5 + 3.3 | 71.5 | 59.7 |
| MobileCLIP2-S2 | 13 | 35.7 + 63.4 | 3.6 + 3.3 | 77.2 | 64.1 |
| MobileCLIP2-B | 13 | 86.3 + 63.4 | 10.4 + 3.3 | 79.4 | 65.8 |
| MobileCLIP2-S3 | 13 | 125.1 + 123.6 | 8.0 + 6.6 | 80.7 | 66.8 |
| MobileCLIP2-L/14 | 13 | 304.3 + 123.6 | 57.9 + 6.6 | 81.9 | 67.8 |
| MobileCLIP2-S4 | 13 | 321.6 + 123.6 | 19.6 + 6.6 | 81.9 | 67.5 |
| MobileCLIP-S0 | 13 | 11.4 + 42.4 | 1.5 + 1.6 | 67.8 | 58.1 |
| MobileCLIP-S1 | 13 | 21.5 + 63.4 | 2.5 + 3.3 | 72.6 | 61.3 |
| MobileCLIP-S2 | 13 | 35.7 + 63.4 | 3.6 + 3.3 | 74.4 | 63.7 |
| MobileCLIP-B | 13 | 86.3 + 63.4 | 10.4 + 3.3 | 76.8 | 65.2 |
| MobileCLIP-B (LT) | 36 | 86.3 + 63.4 | 10.4 + 3.3 | 77.2 | 65.8 |
| MobileCLIP-S3 | 13 | 125.1 + 123.6 | 8.0 + 6.6 | 78.3 | 66.3 |
| MobileCLIP-L/14 | 13 | 304.3 + 123.6 | 57.9 + 6.6 | 79.5 | 66.9 |
| MobileCLIP-S4 | 13 | 321.6 + 123.6 | 19.6 + 6.6 | 79.4 | 68.1 |
Note: MobileCLIP-B(LT) is trained for 300k iterations with constant learning rate schedule and 300k iterations with cosine learning rate schedule.
We release CoCa models used to generate synthetic captions in DFNDR-2B on
HuggingFace. These models can be downloaded using the following instructions.
We recommend using mscoco38k_s12m_context77 for a balanced performance.
# context=77 models
for model in \
mscoco38k_s12m_context77 \
gbc1m-short_context77 \
docci_s12m_context77 \
dci-short_s12m_context77 \
dci-complete_s12m_context77 \
dci-extended_s12m_context77 \
recap-coco-30k_s12m_context77 \
do
hf download apple/mobileclip2_coca_dfn2b_s13b_$model
done
# Context=256 models. These models have a higher chance of generating repeated output
for model in \
docci_s12m_context256 \
dci-complete_s12m_context256 \
dci-extended_s12m_context256 \
do
hf download apple/mobileclip2_coca_dfn2b_s13b_$model
done
# Base CoCa-DFN2B model that can be used for fine-tuning new CoCa models on
# high-quality datasets
hf download apple/mobileclip2_coca_dfn2b_s13b_context77
The following is a usage example. For models length with context lengths 256, copy config.json to src/open_clip/model_configs/coca_ViT-L-14-context$len.json and change the model name in below example to coca_ViT-L-14-context$len.
import torch
import open_clip
from PIL import Image
model, _, preprocess = open_clip.create_model_and_transforms('coca_ViT-L-14', pretrained='/path/to/mobileclip2_coca.pt')
model.eval()
image = preprocess(Image.open("docs/fig_accuracy_latency.png").convert('RGB')).unsqueeze(0)
with torch.no_grad(), torch.cuda.amp.autocast():
syn_text = model.generate(
image,
generation_type="top_p",
top_p=0.9,
fixed_output_length=True
)[0]
syn_text = open_clip.decode(syn_text).split("<end_of_text>")[0].split("<start_of_text>")[-1].split(".")[0].rstrip()
print("Caption:", syn_text)
This software and accompanying data and models have been released under the following licenses:
Our codebase is built using multiple opensource contributions, please see ACKNOWLEDGEMENTS for more details.
If you found this code useful, please cite the following papers:
@article{faghri2025mobileclip2,
title={Mobile{CLIP}2: Improving Multi-Modal Reinforced Training},
author={Fartash Faghri and Pavan Kumar Anasosalu Vasu and Cem Koc and
Vaishaal Shankar and Alexander T Toshev and Oncel Tuzel and Hadi
Pouransari},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2025},
url={https://openreview.net/forum?id=WeF9zolng8},
note={Featured Certification}
}
@InProceedings{vasu2024mobileclip,
author = {Vasu, Pavan Kumar Anasosalu and Pouransari, Hadi and Faghri, Fartash and Vemulapalli, Raviteja and Tuzel, Oncel},
title = {MobileCLIP: Fast Image-Text Models through Multi-Modal Reinforced Training},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {June},
year = {2024},
}
15 commits
3 commits
Python
69.4%
Swift
26.9%
Shell
3.7%
This is the official repository of
The repository contains code for inference, training, and evaluation of MobileCLIP models trained on DataCompDR datasets and MobileCLIP2 models trained on DFNDR dataset.
For code to generate multi-modal reinforced datasets at large scale see ml-mobileclip-dr repository.
MobileCLIP2-S4 matches the accuracy of SigLIP-SO400M/14 with 2x fewer parameters and surpasses DFN ViT-L/14 at 2.5x lower latency measured on iPhone12 Pro Max.MobileCLIP-S3/S4 are our new architectures trained on MobileCLIP’s training dataset, DataCompDR-1B (dashed lines).MobileCLIP-S0 obtains similar zero-shot performance as OpenAI's ViT-B/16 model while being 4.8x faster and 2.8x smaller.MobileCLIP-S2 obtains better avg zero-shot performance than SigLIP's ViT-B/16 model while being 2.3x faster and 2.1x smaller, and trained with 3x less seen samples.MobileCLIP-B (LT) attains zero-shot ImageNet performance of 77.2% which is significantly better than recent works like DFN and SigLIP with similar architectures or even OpenAI's ViT-L/14@336.
conda create -n clipenv python=3.10
conda activate clipenv
pip install -e .
Our models are now natively supported in OpenCLIP. To use MobileCLIP models in OpenCLIP, setup your environment as shown below,
conda create -n clipenv python=3.10
conda activate clipenv
# Clone OpenCLIP repository, add MobileCLIP2 models, and install
git clone https://github.com/mlfoundations/open_clip.git
pushd open_clip
git apply ../mobileclip2/open_clip_inference_only.patch
cp -r ../mobileclip2/* ./src/open_clip/
pip install -e .
popd
pip install git+https://github.com/huggingface/pytorch-image-models
To download pretrained checkpoints follow the code snippet below
# MobileCLIP2
for model in S0 S2 B S3 L-14 S4
do
hf download apple/MobileCLIP2-$model
done
# MobileCLIP
for model in S0 S1 S2 B B-LT S3 L-14 S4
do
hf download apple/MobileCLIP-$model
done
To run inference, see example below,
import torch
import 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_kwargs = {}
if not (model_name == "MobileCLIP2-S3" or model_name == "MobileCLIP2-S4" or model_name.endswith("L-14")):
model_kwargs = {"image_mean": (0, 0, 0), "image_std": (1, 1, 1)}
model, _, preprocess = open_clip.create_model_and_transforms(model_name, pretrained=model_path, **model_kwargs)
tokenizer = open_clip.get_tokenizer(model_name)
# Model needs to be in eval mode for inference because of batchnorm layers unlike ViTs
model.eval()
# For inference/model exporting purposes, please reparameterize first
model = reparameterize_model(model)
image = preprocess(Image.open("docs/fig_accuracy_latency.png").convert("RGB")).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)
The following variants are directly available on OpenCLIP by passing the corresponding pretrained argument and not specifying the image-mean/image-std:
'MobileCLIP-S1', pretrained='datacompdr'
'MobileCLIP-S2', pretrained='datacompdr'
'MobileCLIP-B', pretrained='datacompdr'
'MobileCLIP-B', pretrained='datacompdr_lt'
'MobileCLIP2-B', pretrained='dfndr2b'
'MobileCLIP2-S0', pretrained='dfndr2b'
'MobileCLIP2-S2', pretrained='dfndr2b'
'MobileCLIP2-S3', pretrained='dfndr2b'
'MobileCLIP2-S4', pretrained='dfndr2b'
'MobileCLIP2-L-14', pretrained='dfndr2b'
To models from the official repo, follow the code snippet below
import torch
from PIL import Image
import mobileclip
model, _, preprocess = mobileclip.create_model_and_transforms('mobileclip_s0', pretrained='/path/to/mobileclip_s0.pt')
tokenizer = mobileclip.get_tokenizer('mobileclip_s0')
image = preprocess(Image.open("docs/fig_accuracy_latency.png").convert('RGB')).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)
For an example of loading the data from HuggingFace see hf_dataset_example.py.
Please find the detailed evaluation results here. To reproduce results, we provide script to perform zero-shot evaluation on ImageNet-1k dataset. To evaluate on all the 38 datasets, please follow instructions in datacomp.
# Run evaluation with single GPU
python eval/zeroshot_imagenet.py --model-arch mobileclip_s0 --model-path /path/to/mobileclip_s0.pt
Please refer to Open CLIP Results to compare with other models.
| Model | # Seen Samples (B) | # Params (M) (img + txt) | Latency (ms) (img + txt) | IN-1k Zero-Shot Top-1 Acc. (%) | Avg. Perf. (%) on 38 datasets |
|---|---|---|---|---|---|
| MobileCLIP2-S0 | 13 | 11.4 + 63.4 | 1.5 + 3.3 | 71.5 | 59.7 |
| MobileCLIP2-S2 | 13 | 35.7 + 63.4 | 3.6 + 3.3 | 77.2 | 64.1 |
| MobileCLIP2-B | 13 | 86.3 + 63.4 | 10.4 + 3.3 | 79.4 | 65.8 |
| MobileCLIP2-S3 | 13 | 125.1 + 123.6 | 8.0 + 6.6 | 80.7 | 66.8 |
| MobileCLIP2-L/14 | 13 | 304.3 + 123.6 | 57.9 + 6.6 | 81.9 | 67.8 |
| MobileCLIP2-S4 | 13 | 321.6 + 123.6 | 19.6 + 6.6 | 81.9 | 67.5 |
| MobileCLIP-S0 | 13 | 11.4 + 42.4 | 1.5 + 1.6 | 67.8 | 58.1 |
| MobileCLIP-S1 | 13 | 21.5 + 63.4 | 2.5 + 3.3 | 72.6 | 61.3 |
| MobileCLIP-S2 | 13 | 35.7 + 63.4 | 3.6 + 3.3 | 74.4 | 63.7 |
| MobileCLIP-B | 13 | 86.3 + 63.4 | 10.4 + 3.3 | 76.8 | 65.2 |
| MobileCLIP-B (LT) | 36 | 86.3 + 63.4 | 10.4 + 3.3 | 77.2 | 65.8 |
| MobileCLIP-S3 | 13 | 125.1 + 123.6 | 8.0 + 6.6 | 78.3 | 66.3 |
| MobileCLIP-L/14 | 13 | 304.3 + 123.6 | 57.9 + 6.6 | 79.5 | 66.9 |
| MobileCLIP-S4 | 13 | 321.6 + 123.6 | 19.6 + 6.6 | 79.4 | 68.1 |
Note: MobileCLIP-B(LT) is trained for 300k iterations with constant learning rate schedule and 300k iterations with cosine learning rate schedule.
We release CoCa models used to generate synthetic captions in DFNDR-2B on
HuggingFace. These models can be downloaded using the following instructions.
We recommend using mscoco38k_s12m_context77 for a balanced performance.
# context=77 models
for model in \
mscoco38k_s12m_context77 \
gbc1m-short_context77 \
docci_s12m_context77 \
dci-short_s12m_context77 \
dci-complete_s12m_context77 \
dci-extended_s12m_context77 \
recap-coco-30k_s12m_context77 \
do
hf download apple/mobileclip2_coca_dfn2b_s13b_$model
done
# Context=256 models. These models have a higher chance of generating repeated output
for model in \
docci_s12m_context256 \
dci-complete_s12m_context256 \
dci-extended_s12m_context256 \
do
hf download apple/mobileclip2_coca_dfn2b_s13b_$model
done
# Base CoCa-DFN2B model that can be used for fine-tuning new CoCa models on
# high-quality datasets
hf download apple/mobileclip2_coca_dfn2b_s13b_context77
The following is a usage example. For models length with context lengths 256, copy config.json to src/open_clip/model_configs/coca_ViT-L-14-context$len.json and change the model name in below example to coca_ViT-L-14-context$len.
import torch
import open_clip
from PIL import Image
model, _, preprocess = open_clip.create_model_and_transforms('coca_ViT-L-14', pretrained='/path/to/mobileclip2_coca.pt')
model.eval()
image = preprocess(Image.open("docs/fig_accuracy_latency.png").convert('RGB')).unsqueeze(0)
with torch.no_grad(), torch.cuda.amp.autocast():
syn_text = model.generate(
image,
generation_type="top_p",
top_p=0.9,
fixed_output_length=True
)[0]
syn_text = open_clip.decode(syn_text).split("<end_of_text>")[0].split("<start_of_text>")[-1].split(".")[0].rstrip()
print("Caption:", syn_text)
This software and accompanying data and models have been released under the following licenses:
Our codebase is built using multiple opensource contributions, please see ACKNOWLEDGEMENTS for more details.
If you found this code useful, please cite the following papers:
@article{faghri2025mobileclip2,
title={Mobile{CLIP}2: Improving Multi-Modal Reinforced Training},
author={Fartash Faghri and Pavan Kumar Anasosalu Vasu and Cem Koc and
Vaishaal Shankar and Alexander T Toshev and Oncel Tuzel and Hadi
Pouransari},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2025},
url={https://openreview.net/forum?id=WeF9zolng8},
note={Featured Certification}
}
@InProceedings{vasu2024mobileclip,
author = {Vasu, Pavan Kumar Anasosalu and Pouransari, Hadi and Faghri, Fartash and Vemulapalli, Raviteja and Tuzel, Oncel},
title = {MobileCLIP: Fast Image-Text Models through Multi-Modal Reinforced Training},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {June},
year = {2024},
}
15 commits
3 commits
Python
69.4%
Swift
26.9%
Shell
3.7%