license: apache-2.0
pipeline_tag: zero-shot-image-classification
黄伟权
1*, 吴傲奇
1*, 杨一帆
2†, 罗旭芳
2, 杨雨晴
2, 胡亮
1, 戴琪
2, 戴熙阳
2, 陈栋栋
2, 罗翀
2, 邱莉莉
2
1同济大学, 2微软公司
*同等贡献
† 通讯作者: yifanyang@microsoft.com
[📂 GitHub] [🆕 博客] [📜 LLM2CLIP]
本文提出LLM2CLIP这一创新方法,通过利用大语言模型(LLM)释放CLIP的潜力。我们通过在字幕空间采用对比学习微调LLM,将其文本能力提取到输出嵌入中,显著提升了输出层的文本判别能力。随后设计了一个高效训练流程,让微调后的LLM作为CLIP视觉编码器的强力教师。得益于LLM的引入,我们现在可以整合更长更复杂的字幕描述,突破原始CLIP文本编码器的上下文窗口和能力限制。实验表明,该方法在跨模态任务中带来显著提升。我们的方法直接将此前SOTA模型EVA02在长文本和短文本检索任务上的性能提升了16.5%,将仅用英文数据训练的CLIP模型转变为最先进的跨语言模型。此外,当与Llava 1.5等多模态模型结合时,在几乎所有基准测试中都持续超越CLIP,展现出全面的性能提升。
LLM2CLIP性能表现
**请注意:论文中所有结果均基于PyTorch权重评估,使用Hugging Face(hf)模型时性能可能存在差异。**
模型详情
- 模型类型: 视觉基础模型,特征骨干网络
- 预训练数据集: CC3M、CC12M、YFCC15M及Recap-DataComp-1B(3000万子集)
使用指南
PyTorch版本
前往GitHub
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from transformers import AutoModel, AutoConfig, AutoTokenizer
from eva_clip import create_model_and_transforms
from llm2vec import LLM2Vec
from PIL import Image
import torch
model, _, preprocess_val = create_model_and_transforms('EVA02-CLIP-L-14-336', force_custom_clip=True)
ckpt = torch.load('LLM2CLIP-EVA02-L-14-336.pt')
model.load_state_dict(ckpt)
model = model.cuda().eval()
llm_model_name = 'microsoft/LLM2CLIP-Llama-3-8B-Instruct-CC-Finetuned'
config = AutoConfig.from_pretrained(
llm_model_name, trust_remote_code=True
)
llm_model = AutoModel.from_pretrained(llm_model_name, torch_dtype=torch.bfloat16, config=config, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(llm_model_name)
llm_model.config._name_or_path = 'meta-llama/Meta-Llama-3-8B-Instruct'
l2v = LLM2Vec(llm_model, tokenizer, pooling_mode="mean", max_length=512, doc_max_length=512)
image_path = "CLIP.png"
captions = ["示意图", "狗", "猫"]
image = preprocess_val(Image.open(image_path)).cuda().unsqueeze(dim=0)
text_features = l2v.encode(captions, convert_to_tensor=True).to('cuda')
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text_features)
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("标签概率:", text_probs)
引用文献
@misc{huang2024llm2clippowerfullanguagemodel,
title={LLM2CLIP:大语言模型解锁更丰富的视觉表征},
author={黄伟权 and 吴傲奇 and 杨一帆 and 罗旭芳 and 杨雨晴 and 胡亮 and 戴琪 and 戴熙阳 and 陈栋栋 and 罗翀 and 邱莉莉},
year={2024},
eprint={2411.04997},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2411.04997},
}