🚀 LLM2CLIP:通过大语言模型拓展CLIP的能力边界
LLM2CLIP是一种新颖的方法,借助大语言模型(LLMs)的强大能力,释放CLIP的潜力。通过在字幕空间中使用对比学习对LLM进行微调,将其文本能力提取到输出嵌入中,显著提高输出层的文本区分能力。该方法还设计了高效的训练过程,让微调后的LLM作为CLIP视觉编码器的强大“教师”,能纳入更长、更复杂的字幕,突破了传统CLIP文本编码器的上下文窗口和能力限制。实验表明,该方法在跨模态任务中带来了显著改进。
🚀 快速开始
在本文中,我们提出了LLM2CLIP,这是一种新颖的方法,它借助大语言模型(LLMs)的强大能力来释放CLIP的潜力。通过在字幕空间中使用对比学习对LLM进行微调,我们将其文本能力提取到输出嵌入中,显著提高了输出层的文本区分能力。然后,我们设计了一个高效的训练过程,其中微调后的LLM作为CLIP视觉编码器的强大“教师”。由于有了LLM,我们现在可以纳入更长、更复杂的字幕,而不受传统CLIP文本编码器的上下文窗口和能力限制。我们的实验表明,这种方法在跨模态任务中带来了显著的改进。我们的方法直接将之前的SOTA EVA02模型在长文本和短文本检索任务上的性能提高了16.5%,将仅在英语数据上训练的CLIP模型转变为最先进的跨语言模型。此外,当与Llava 1.5等模型集成到多模态训练中时,它在几乎所有基准测试中都始终优于CLIP,展示了全面的性能提升。
✨ 主要特性
- 拓展CLIP能力:借助大语言模型释放CLIP潜力,提高输出层文本区分能力。
- 突破限制:可纳入更长、更复杂的字幕,不受传统CLIP文本编码器限制。
- 性能提升显著:在跨模态任务、长文本和短文本检索任务等方面带来显著性能提升。
- 跨语言能力:将仅在英语数据上训练的CLIP模型转变为跨语言模型。
📦 安装指南
文档中未提及具体安装步骤,跳过此章节。
💻 使用示例
基础用法
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 = ["a diagram", "a dog", "a cat"]
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("Label probs:", text_probs)
📚 详细文档
LLM2CLIP性能
**需要注意的是,论文中呈现的所有结果均使用PyTorch权重进行评估。使用Hugging Face (hf) 模型时,性能可能会有所不同。**
模型详情
属性 |
详情 |
模型类型 |
视觉基础模型,特征骨干网络 |
预训练数据集 |
CC3M、CC12M、YFCC15M和Recap-DataComp-1B(30M子集) |
📄 许可证
本项目采用Apache-2.0许可证。
📚 引用信息
@misc{huang2024llm2clippowerfullanguagemodel,
title={LLM2CLIP: Powerful Language Model Unlock Richer Visual Representation},
author={Weiquan Huang and Aoqi Wu and Yifan Yang and Xufang Luo and Yuqing Yang and Liang Hu and Qi Dai and Xiyang Dai and Dongdong Chen and Chong Luo and Lili Qiu},
year={2024},
eprint={2411.04997},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2411.04997},
}