Visual instruction tuning towards large language and vision models with GPT-4 level capabilities.
[Project Page] [Paper] [Demo] [Data] [Model]
Visual Instruction Tuning
Haotian Liu*, Chunyuan Li*, Qingyang Wu, Yong Jae Lee (*Equal Contribution)
- 🔥 We released LLaVA: Large Language and Vision Assistant. We propose visual instruction tuning, towards building large language and vision models with GPT-4 level capabilities. Checkout the paper and demo.
Usage and License Notices: The data, code and checkpoint is intended and licensed for research use only. They are also restricted to uses that follow the license agreement of LLaMA, Vicuna and GPT-4. The dataset is CC BY NC 4.0 (allowing only non-commercial use) and models trained using the dataset should not be used outside of research purposes.
Data file name | Size |
---|---|
conversation_58k.json | 126 MB |
detail_23k.json | 20.5 MB |
complex_reasoning_77k.json | 79.6 MB |
To download our langauge-image multimodal instruction-folllowing dataset LLaVA-Instruct-150K
, please run the following script:
sh download_data.sh
The pretraining dataset used in this release is a subset of CC-3M dataset, filtered with a more balanced concept coverage distribution. Please see here for a detailed description on the dataset structure and how to download the images.
If you already have CC-3M dataset on your disk, the image names follow this format: GCC_train_000000000.jpg
. You may edit the image
field correspondingly if necessary.
Data | Chat File | Meta Data | Size |
---|---|---|---|
CC-3M Pretrain 595K | chat.json | metadata.json | 211 MB |
- Clone this repository and navigate to LLaVA folder
git clone https://github.com/haotian-liu/LLaVA.git
cd LLaVA
- Install Package
conda create -n llava python=3.10 -y
conda activate llava
pip install --upgrade pip # enable PEP 660 support
pip install -e .
NOTE: In this research preview, we used a modified version of huggingface/transformers library to support multimodal models and the LLaMA tokenizer. Make sure that you create a new Conda environment and you are using the correct transformers library from https://github.com/haotian-liu/transformers_llava.
You may try running the following command to make sure the version is correct.
pip install git+https://github.com/haotian-liu/transformers_llava.git@988b6abb3b7da9a5cbb5051e994706f7f88c2565
- Install additional packages for training cases
pip install ninja
pip install flash-attn
We release LLaVA weights as delta weights to comply with the LLaMA model license. You can add our delta to the original LLaMA weights to obtain the LLaVA weights.
Instructions:
- Get the original LLaMA weights in the huggingface format by following the instructions here.
- Use the following scripts to get LLaVA weights by applying our delta. It will automatically download delta weights from our Hugging Face account.
This conversion command needs around 60 GB of CPU RAM.
python3 -m llava.model.apply_delta \
--base /path/to/llama-13b \
--target /output/path/to/LLaVA-13B-v0 \
--delta liuhaotian/LLaVA-13b-delta-v0
Coming soon.
The initial release is pretrained on LLaVA-filtered CC3M 595K with 1 epoch. The pretrained weights are released here.
You may perform instruction tuning on our pretrained checkpoints, by using our visual instruction tuning data following the instructions here.
python -m llava.serve.controller --host 0.0.0.0 --port 10000
python -m llava.serve.model_worker --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path ./checkpoints/LLaVA-13B-v0 --multi-modal
Wait until the process finishes loading the model and you see "Uvicorn running on ...".
If your the VRAM of your GPU is less than 24GB (e.g., RTX 3090, RTX 4090, etc.), you may try running it with multiple GPUs.
If you install our repo before 4/20/23, you may need to reinstall our fork of transformers
: pip install git+https://github.com/haotian-liu/transformers_llava.git@988b6abb3b7da9a5cbb5051e994706f7f88c2565
.
python -m llava.serve.model_worker --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path ./checkpoints/LLaVA-13B-v0 --multi-modal --num-gpus 2
Wait until the process finishes loading the model and you see "Uvicorn running on ...".
python -m llava.serve.test_message --model-name LLaVA-13B-v0 --controller http://localhost:10000
python -m llava.serve.gradio_web_server --controller http://localhost:10000
Our GPT-assisted evaluation pipeline for multimodal modeling is provided for a comprehensive understanding of the capabilities of vision-language models. Please see our paper for more details.
- Generate LLaVA responses
python model_vqa.py \
--model-name ./checkpoints/LLaVA-13B-v0 \
--question-file \
playground/data/coco2014_val_qa_eval/qa90_questions.jsonl \
--image-folder \
/path/to/coco2014_val \
--answers-file \
/path/to/answer-file.jsonl
- Evaluate the generated responses
OPENAI_API_KEY="sk-***********************************" python eval_gpt_review_visual.py \
--question playground/data/coco2014_val_qa_eval/qa90_questions.jsonl \
--context table/caps_boxes_coco2014_val_80.jsonl \
--answer-list \
/path/to/answer-file-1.jsonl \
/path/to/answer-file-2.jsonl \
--rule table/rule.json \
--output /path/to/review.json
- Summarize the evaluation results
python summarize_gpt_review.py
Please see ScienceQA repo for setting up the dataset. You may either use the official ScienceQA evaluation script or our script to evaluate the model.
The current version of LLaVA is fine-tuned from a Vicuna-13B model. We use approximately 600K filtered CC3M in feature alignment pretraining and 150K GPT-generated multimodal instruction-following data in finetuning. For detailed description of the data generation pipeline, please refer see our paper.
We are working on a more capable model that is pretrained with the data at a larger scale. Stay tuned!
We release all three types of multimodal instruction-following data. The use of these data is subject to OpenAI TOS.
We fine-tune the model using the code from FastChat. We use a similar set of hyperparameters as Vicuna in finetuning. Both hyperparameters used in pretraining and finetuning are provided below.
- Pretraining
Hyperparameter | Global Batch Size | Learning rate | Epochs | Max length | Weight decay |
---|---|---|---|---|---|
LLaVA-13B | 128 | 2e-3 | 1 | 2048 | 0 |
- Finetuning
Hyperparameter | Global Batch Size | Learning rate | Epochs | Max length | Weight decay |
---|---|---|---|---|---|
LLaVA-13B | 32 | 2e-5 | 3 | 2048 | 0 |
LLaVA is trained on 8 A100 GPUs with 80GB memory with the following code. To train on fewer GPUs, you can reduce the per_device_train_batch_size
and increase the gradient_accumulation_steps
accordingly to keep the global batch size the same.
- Pretraining
torchrun --nnodes=1 --nproc_per_node=8 --master_port=25001 \
llava/train/train_mem.py \
--model_name_or_path ./checkpoints/llama-vicuna-13b \
--data_path /path/to/cc3m_595k.json \
--image_folder /path/to/cc3m_595k \
--vision_tower openai/clip-vit-large-patch14 \
--tune_mm_mlp_adapter True \
--mm_vision_select_layer -2 \
--mm_use_im_start_end \
--bf16 True \
--output_dir ./checkpoints/llava-13b-pretrain \
--num_train_epochs 1 \
--per_device_train_batch_size 16 \
--per_device_eval_batch_size 4 \
--gradient_accumulation_steps 1 \
--evaluation_strategy "no" \
--save_strategy "steps" \
--save_steps 2400 \
--save_total_limit 1 \
--learning_rate 2e-3 \
--weight_decay 0. \
--warmup_ratio 0.03 \
--lr_scheduler_type "cosine" \
--logging_steps 1 \
--tf32 True \
--model_max_length 2048 \
--gradient_checkpointing True \
--lazy_preprocess True \
--report_to wandb
- Extract projector features
python scripts/extract_mm_projector.py \
--model_name_or_path ./checkpoints/llava-13b-pretrain \
--output ./checkpoints/mm_projector/llava-13b-pretrain.bin
- Finetuning
torchrun --nnodes=1 --nproc_per_node=8 --master_port=25001 \
llava/train/train_mem.py \
--model_name_or_path /path/to/llama-vicuna-13b \
--data_path /path/to/llava_instruct_150k.json \
--image_folder /Data/haotian/coco/train2017 \
--vision_tower openai/clip-vit-large-patch14 \
--pretrain_mm_mlp_adapter ./checkpoints/mm_projector/llava-13b-pretrain.bin \
--mm_vision_select_layer -2 \
--mm_use_im_start_end True \
--bf16 True \
--output_dir ./checkpoints \
--num_train_epochs 3 \
--per_device_train_batch_size 4 \
--per_device_eval_batch_size 4 \
--gradient_accumulation_steps 1 \
--evaluation_strategy "no" \
--save_strategy "steps" \
--save_steps 5000 \
--save_total_limit 3 \
--learning_rate 2e-5 \
--weight_decay 0. \
--warmup_ratio 0.03 \
--lr_scheduler_type "cosine" \
--logging_steps 1 \
--tf32 True \
--fsdp "full_shard auto_wrap" \
--fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \
--model_max_length 2048 \
--gradient_checkpointing True \
--lazy_preprocess True \
--report_to wandb
- Vicuna: the codebase we built upon, and our base model Vicuna-13B that has the amazing language capabilities!
If you find LLaVA useful for your your research and applications, please cite using this BibTeX:
@misc{liu2023llava,
title={Visual Instruction Tuning},
author={Liu, Haotian and Li, Chunyuan and Wu, Qingyang and Lee, Yong Jae},
publisher={arXiv:2304.08485},
year={2023},
}
For future project ideas, checkout the Grounded-Segment-Anything to detect, segment, and generate anything by marrying Grounding DINO and Segment-Anything.