🚀 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,展示了全面的性能提升。
⚠️ 重要提示
论文中呈现的所有结果均使用PyTorch权重进行评估。使用Hugging Face (hf) 模型时,性能可能会有所不同。
✨ 主要特性
- 通过微调大语言模型,提升CLIP输出层的文本区分能力。
- 突破原始CLIP文本编码器的上下文窗口和能力限制,可纳入更长、更复杂的字幕。
- 在跨模态任务中显著提升性能,如将EVA02模型在长文本和短文本检索任务上的性能提高16.5%。
- 可将仅在英语数据上训练的CLIP模型转变为先进的跨语言模型。
- 在与Llava 1.5等模型集成的多模态训练中,全面优于CLIP。
📚 详细文档
模型信息
属性 |
详情 |
模型类型 |
视觉基础模型,特征骨干网络 |
预训练数据集 |
CC3M、CC12M、YFCC15M和Recap - DataComp - 1B(30M子集) |
作者信息
Weiquan Huang1*、Aoqi Wu1*、Yifan Yang2†、Xufang Luo2、Yuqing Yang2、Liang Hu1、Qi Dai2、Xiyang Dai2、Dongdong Chen2、Chong Luo2、Lili Qiu2
1同济大学,2微软公司
*同等贡献
† 通讯作者邮箱:yifanyang@microsoft.com
相关链接
💻 使用示例
基础用法
图像嵌入
from PIL import Image
from transformers import AutoModel
from transformers import CLIPImageProcessor
import torch
image_path = "CLIP.png"
model_name_or_path = "LLM2CLIP-Openai-B-16"
processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch16")
model = AutoModel.from_pretrained(
model_name_or_path,
torch_dtype=torch.float16,
trust_remote_code=True).to('cuda').eval()
image = Image.open(image_path)
input_pixels = processor(images=image, return_tensors="pt").pixel_values.to('cuda')
with torch.no_grad(), torch.cuda.amp.autocast():
outputs = model.get_image_features(input_pixels)
高级用法
检索
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from PIL import Image
import torch
from llm2vec import LLM2Vec
from transformers import AutoModel, AutoConfig, AutoTokenizer
from transformers import CLIPImageProcessor
processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-base-patch16")
model_name_or_path = "microsoft/LLM2CLIP-Openai-B-16"
model = AutoModel.from_pretrained(
model_name_or_path,
torch_dtype=torch.bfloat16,
trust_remote_code=True).to('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)
captions = ["a diagram", "a dog", "a cat"]
image_path = "CLIP.png"
image = Image.open(image_path)
input_pixels = processor(images=image, return_tensors="pt").pixel_values.to('cuda')
text_features = l2v.encode(captions, convert_to_tensor=True).to('cuda')
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.get_image_features(input_pixels)
text_features = model.get_text_features(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)
📄 许可证
本项目采用Apache-2.0许可证。
📖 BibTeX引用
@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},
}