MobileCLIP: 快速图像-文本模型的多元强化训练
1. 项目介绍
MobileCLIP 是一种通过多元强化训练实现的快速图像-文本模型。该项目是 CVPR 2024 研究论文 "MobileCLIP: Fast Image-Text Models through Multi-Modal Reinforced Training" 的官方实现。MobileCLIP 模型在各种任务中表现出色,包括零样本图像分类,且在速度和大小上都有优势。
2. 项目快速启动
首先,需要创建一个 Python 环境,并安装必要的依赖:
conda create -n clipenv python=3.10
conda activate clipenv
pip install -e .
为了下载预训练的检查点,执行以下命令:
source get_pretrained_models.sh
以下是使用 MobileCLIP 模型的示例代码:
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)
3. 应用案例和最佳实践
-
iOS 应用案例:项目中提供了一个 iOS 应用,用于展示模型在移动设备上的实时零样本图像分类性能。可以在
ios_app
目录中找到相关代码。 -
最佳实践:为了在 OpenCLIP 中使用 MobileCLIP 模型,需要先设置环境,并按照以下步骤操作:
conda create -n clipenv python=3.10
conda activate clipenv
pip install git+https://github.com/mlfoundations/open_clip
pip install git+https://github.com/huggingface/pytorch-image-models
然后在 OpenCLIP 中运行推理,具体示例可以参考 OpenCLIP 仓库中的示例。
4. 典型生态项目
目前,MobileCLIP 支持的变体有 MobileCLIP-S0、MobileCLIP-S1、MobileCLIP-S2 和 MobileCLIP-B。这些变体在 ImageNet-1k 数据集上的零样本性能和在不同数据集上的平均性能都有详细的评估结果。具体的性能数据和比较可以在项目的 eval
目录中找到相关脚本进行复现。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考