LLM2CLIP
repository·main·Indexed 20 days ago
https://github.com/microsoft/llm2clipA framework that leverages Large Language Models (LLMs) as textual teachers to enhance CLIP's visual encoder. Using a Caption-to-Caption Contrastive Learning strategy, LLM2CLIP enables CLIP-style models to handle longer text inputs and complex, dense captions. The training process involves a two-stage approach consisting of Masked Next Token Prediction (MNTP) and Supervised SimCSE Loss, utilizing mixed datasets of pure text and image-captions.
What's inside LLM2CLIP
- LLM2CLIP is a framework that uses Large Language Models (LLMs) as textual teachers to enhance CLIP's visual encoder. It addresses common CLIP limitations such as short context windows (77 tokens) and weak text comprehension by using a Caption-to-Caption Contrastive Learning strategy. This approach improves text-image alignment, especially for long, dense, and complex captions, and provides better open-world knowledge for multimodal feature alignment.
Understand the LLM2CLIP training process
mainThe training process for LLM2CLIP follows a two-stage approach to integrate Large Language Models (LLMs) with CLIP:
- Masked Next Token Prediction (MNTP): An initial stage used to prepare the model.
- Supervised SimCSE Loss: The primary stage where supervised SimCSE loss is applied to make different captions of the same image positive samples for each other, while captions of different images serve as negative samples.
Note: While MNTP is used for optimal performance, it has been observed that removing it does not significantly affect the final conclusions or results.
Dataset Strategy: To maintain the LLM's original distribution and mitigate caption bias, the training process mixes and shuffles two types of datasets:
- Pure text datasets (e.g., Wikitext-103)
- Image-caption datasets (e.g., CC-3M)
Batches are sampled normally from this mixed and shuffled pool during training.
Understand the LLM2CLIP fine-tuning process
mainLLM2CLIP uses a two-stage fine-tuning process to align the LLM with the CLIP vision encoder:
- Stage 1: Masked Next Token Prediction (MNTP): An initial stage used to prepare the model. While it has little impact on final results if removed, it is recommended for optimal performance.
- Stage 2: Supervised SimCSE Loss: The core stage where different captions of the same image are treated as positive samples, and captions of different images are treated as negative samples. This allows the LLM to provide meaningful supervisory signals to the image.
Dataset Strategy: The training mixes pure text datasets (e.g.,
Wikitext-103) with image-caption datasets (e.g.,CC-3M). The pure text data helps retain the LLM's original distribution and mitigates biases introduced by captions. The datasets are mixed, shuffled, and sampled normally during training.LLM2CLIP integration with Diffusion Models
mainLLM2CLIP can be integrated into image-text generative models (like Stable Diffusion) to enable more complex and precise control over text-to-image generation.
Initial experiments indicate that using the LLM2CLIP Llama 3 model performs significantly better than a standard Llama 3 model when integrated with Stable Diffusion 3.
Understanding LLM gradient freezing in LLM2CLIP
mainIn the LLM2CLIP training methodology, the LLM gradients are typically kept frozen (or only specific layers are opened based on GPU memory constraints).
Why the LLM is frozen:
- Batch Size: CLIP training is highly dependent on large batch sizes. Opening the LLM for full fine-tuning significantly increases memory requirements, which would force a reduction in batch size, potentially harming performance.
- Alignment Goal: The primary objective is to align the visual model with the correct textual modality provided by the LLM, rather than changing the LLM itself.
Users attempting to replace the original text encoder with a tuned Llama 3 model should refer to the provided fine-tuned weights rather than attempting to manually reconfigure standard CLIP text encoder parameters in the configuration files.
Train using Supervised learning with CC3M
mainTo perform supervised training using CC3M, follow these steps:
- Data Preparation:
- Prepare the E5 data used in
llm2vec. - Prepare the CC3M dataset in CSV format (containing
short_captionandlong_caption).
- Prepare the E5 data used in
- Configuration: Add the LoRA weights (pretrained during the MNTP phase) into the
train_configs/supervised/MetaLlama3_cc3m.jsonconfiguration file. - Execution: Run the supervised training script.
Note: You will need a Hugging Face token (
HF_TOKEN) for authentication.HF_TOKEN=xxxx accelerate launch --config_file ./ac_zero2.yaml run_supervised.py train_configs/supervised/MetaLlama3_cc3m.json- Data Preparation:
Install LLM2CLIP
mainFollow the instructions in the💻 Installation Guidesection of the README to set up the environment and install the necessary dependencies for LLM2CLIP.Install llm2vec and dependencies
mainTo set up the environment for LLM2CLIP (which builds on
llm2vec), create a Conda environment with Python 3.10 and install the required packages. Note that a specific version ofaccelerateis required to ensure compatibility with the provided scripts.conda create -n llm2vec python=3.10 -y conda activate llm2vec pip install llm2vec pip install flash-attn --no-build-isolation pip install deepspeed pip install accelerate==0.34.2Train using MNTP with CC3M
mainTo perform Masked Next Token Prediction (MNTP) training using the CC3M dataset, follow these steps:
- Data Preparation: Prepare the CC3M dataset in CSV format. The CSV must contain two specific keys:
short_captionandlong_caption. - Configuration: Update the path to your
cc3m.csvfile within theMetaLlama3_cc3m.jsonconfiguration file. - Execution: Run the training script using
accelerate launchwith the provided configuration.
Note: You will need a Hugging Face token (
HF_TOKEN) for authentication.cd llm2vec HF_TOKEN=xxxx accelerate launch --config_file ./ac_zero2.yaml run_mntp.py train_configs/mntp/MetaLlama3_cc3m.json- Data Preparation: Prepare the CC3M dataset in CSV format. The CSV must contain two specific keys:
Use pre-tuned LLM2CLIP models for retrieval and training
mainTo avoid manual configuration of text encoders, you can use pre-tuned models available on Hugging Face:
- For retrieval experiments: Use the caption contrastive fine-tuned version of Llama3-8B-CC:
microsoft/LLM2CLIP-Llama-3-8B-Instruct-CC-Finetuned. - For training your own CLIP models: Use the adapter and projector parameters available in the OpenAI ViT-L repository:
microsoft/LLM2CLIP-Openai-L-14-336.
Detailed retrieval testing methods can be found in the respective model cards on Hugging Face.
# Llama 3-8B-Instruct-CC-Finetuned (for retrieval) https://huggingface.co/microsoft/LLM2CLIP-Llama-3-8B-Instruct-CC-Finetuned # OpenAI ViT-L (for adapter/projector parameters) https://huggingface.co/microsoft/LLM2CLIP-Openai-L-14-336- For retrieval experiments: Use the caption contrastive fine-tuned version of Llama3-8B-CC:
Use fine-tuned Llama 3 models for retrieval and training
mainTo assist with retrieval experiments or to train your own CLIP models, you can use the caption contrastive fine-tuned version of Llama 3.
- For retrieval experiments: Use the
LLM2CLIP-Llama-3-8B-Instruct-CC-Finetunedmodel available on Hugging Face. - For adapter and projector parameters: Use the parameters provided in the
LLM2CLIP-Openai-L-14-336repository on Hugging Face.
Note: If you are using the EVA series of models, be aware that precision mismatches during Hugging Face conversion have been reported and are being addressed in progressive updates.
- For retrieval experiments: Use the
Prepare evaluation datasets and embeddings
mainTo set up the environment for evaluation, run the setup script to prepare the datasets and then execute the embedding extraction script.
bash setup_eval_datasets.sh python extract_eval_embedding.py