🚀 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,展示了全面的性能提升。
✨ 主要特性
LLM2CLIP性能
⚠️ 重要提示
請注意,論文中呈現的所有結果均使用PyTorch權重進行評估。使用Hugging Face (hf)模型時,性能可能會有所不同。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
Huggingface版本 - 圖像嵌入
from PIL import Image
from transformers import AutoModel
from transformers import CLIPImageProcessor
import torch
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
image_path = "CLIP.png"
model_name_or_path = "microsoft/LLM2CLIP-Openai-L-14-224"
processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14")
model = AutoModel.from_pretrained(
model_name_or_path,
torch_dtype=torch.bfloat16,
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)
Huggingface版本 - 檢索
from PIL import Image
from transformers import AutoModel, AutoConfig, AutoTokenizer
from transformers import CLIPImageProcessor
import torch
from llm2vec import LLM2Vec
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14")
model_name_or_path = "microsoft/LLM2CLIP-Openai-L-14-224"
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)
📚 詳細文檔
模型詳情
屬性 |
詳情 |
模型類型 |
視覺基礎模型,特徵主幹 |
預訓練數據集 |
CC3M、CC12M、YFCC15M和Recap-DataComp-1B(30M子集) |
📄 許可證
本項目採用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},
}