Home

Awesome

Official repo for "Mini-Gemini: Mining the Potential of Multi-modality Vision Language Models"

<a href='https://mini-gemini.github.io/'><img src='https://img.shields.io/badge/Project-Page-Green'></a> <a href='http://103.170.5.190:7860/'><img src='https://img.shields.io/badge/Project-Demo-violet'></a> <a href='https://huggingface.co/spaces/wcy1122/MGM'><img src='https://img.shields.io/badge/šŸ¤—-Open%20In%20Spaces-blue.svg'></a> <a href='https://arxiv.org/pdf/2403.18814.pdf'><img src='https://img.shields.io/badge/Paper-Arxiv-red'></a> <a href='https://huggingface.co/collections/YanweiLi/mgm-6603c50b9b43d044171d0854'><img src='https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Models-blue'></a> <a href='https://huggingface.co/collections/YanweiLi/mgm-data-660463ea895a01d8f367624e'><img src='https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Data-green'></a>

The framework supports a series of dense and MoE Large Language Models (LLMs) from 2B to 34B with image understanding, reasoning, and generation simultaneously. We build this repo based on LLaVA.

Release

Contents

Demo

We provide some selected examples in this section. More examples can be found in our project page. Feel free to try our online demo!

<div align=center> <img width="100%" src="images/teaser.png"/> </div>

Install

Please follow the instructions below to install the required packages.

NOTE: If you want to use the 2B version, please ensure to install the latest version Transformers (>=4.38.0).

  1. Clone this repository
git clone https://github.com/dvlab-research/MGM.git
  1. Install Package
conda create -n mgm python=3.10 -y
conda activate mgm
cd MGM
pip install --upgrade pip  # enable PEP 660 support
pip install -e .
  1. Install additional packages for training cases
pip install ninja
pip install flash-attn --no-build-isolation

Model

The framework is conceptually simple: dual vision encoders are utilized to provide low-resolution visual embedding and high-resolution candidates; patch info mining is proposed to conduct patch-level mining between high-resolution regions and low-resolution visual queries; LLM is utilized to marry text with images for both comprehension and generation at the same time.

<div align=center> <img width="98%" src="images/pipeline.png"/> </div>

We provide all our fully finetuned models on Stage 1 and 2 data:

ModelLRHRBase LLMVision EncoderFinetuning DataFinetuning scheduleDownload
MGM-2B336768Gemma-2BCLIP-LMGM-Instructfull_ft-1eckpt
MGM-7B336768Vicuna-7B-v1.5CLIP-LMGM-Instructfull_ft-1eckpt
MGM-13B336768Vicuna-13B-v1.5CLIP-LMGM-Instructfull_ft-1eckpt
MGM-8B336768LLaMA-3-8B-InstructCLIP-LMGM-Instructfull_ft-1eckpt
MGM-8x7B336768Mixtral-8x7B-Instruct-v0.1CLIP-LMGM-Instructfull_ft-1eckpt
MGM-34B336768Nous-Hermes-2-Yi-34BCLIP-LMGM-Instructfull_ft-1eckpt
MGM-7B-HD6721536Vicuna-7B-v1.5CLIP-LMGM-Instructfull_ft-1eckpt
MGM-13B-HD6721536Vicuna-13B-v1.5CLIP-LMGM-Instructfull_ft-1eckpt
MGM-8B-HD6721536LLaMA-3-8B-InstructCLIP-LMGM-Instructfull_ft-1eckpt
MGM-8x7B-HD6721536Mixtral-8x7B-Instruct-v0.1CLIP-LMGM-Instructfull_ft-1eckpt
MGM-34B-HD6721536Nous-Hermes-2-Yi-34BCLIP-LMGM-Instructfull_ft-1eckpt

Here are the pretrained weights on Stage 1 data only:

ModelLRHRBase LLMVision EncoderPretrain DataFinetuning scheduleDownload
MGM-2B336768Gemma-2BCLIP-LMGM-Pretrain1eckpt
MGM-7B336768Vicuna-7B-v1.5CLIP-LMGM-Pretrain1eckpt
MGM-13B336768Vicuna-13B-v1.5CLIP-LMGM-Pretrain1eckpt
MGM-8x7B336768Mixtral-8x7B-Instruct-v0.1CLIP-LMGM-Pretrain1eckpt
MGM-34B336768Nous-Hermes-2-Yi-34BCLIP-LMGM-Pretrain1eckpt

Preparation

Dataset

We provide the processed data for the model training. For model pretraining, please download the following the training image-based data and organize them as:

-> means put the data in the local folder.

For model finetuning, please download the following the instruction data and organize them as:

-> means put the data in the local folder.

For model evaluation, please follow this link for preparation. We use some extra benchmarks for evaluation. please download the following the training image-based data and organize them as:

-> means put the data in the local folder.

Please put the pretrained data, finetuned data, and eval data in MGM-Pretrain, MGM-Finetune, and MGM-Eval subset following Structure.

For meta info, please download the following files and organize them as in Structure.

Data file nameSize
mgm_pretrain.json1.68 G
mgm_instruction.json1.79 G
mgm_generation_pure_text.json0.04 G

IMPORTANT: mgm_generation_pure_text.json is a generation-related subset. DO NOT merge it with mgm_instruction.json as it is already included in it. You may merge this file with your customized LLM/VLM SFT dataset to enable the reasoning generation ability.

Pretrained Weights

We recommend users to download the pretrained weights from the following link CLIP-Vit-L-336, OpenCLIP-ConvNeXt-L, Gemma-2b-it, Vicuna-7b-v1.5, Vicuna-13b-v1.5, Mixtral-8x7B-Instruct-v0.1, and Nous-Hermes-2-Yi-34B , and put them in model_zoo following Structure.

Structure

The folder structure should be organized as follows before training.

MGM
ā”œā”€ā”€ mgm
ā”œā”€ā”€ scripts
ā”œā”€ā”€ work_dirs
ā”‚   ā”œā”€ā”€ MGM
ā”‚   ā”‚   ā”œā”€ā”€ MGM-2B
ā”‚   ā”‚   ā”œā”€ā”€ ...
ā”œā”€ā”€ model_zoo
ā”‚   ā”œā”€ā”€ LLM
ā”‚   ā”‚   ā”œā”€ā”€ gemma
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ gemma-2b-it
ā”‚   ā”‚   ā”œā”€ā”€ vicuna
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ 7B-V1.5
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ 13B-V1.5
ā”‚   ā”‚   ā”œā”€ā”€ llama-3
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Meta-Llama-3-8B-Instruct
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Meta-Llama-3-70B-Instruct
ā”‚   ā”‚   ā”œā”€ā”€ mixtral
ā”‚   ā”‚   ā”‚   ā”œā”€ā”€ Mixtral-8x7B-Instruct-v0.1
ā”‚   ā”‚   ā”œā”€ā”€ Nous-Hermes-2-Yi-34B
ā”‚   ā”œā”€ā”€ OpenAI
ā”‚   ā”‚   ā”œā”€ā”€ clip-vit-large-patch14-336
ā”‚   ā”‚   ā”œā”€ā”€ openclip-convnext-large-d-320-laion2B-s29B-b131K-ft-soup
ā”œā”€ā”€ data
ā”‚   ā”œā”€ā”€ MGM-Pretrain
ā”‚   ā”‚   ā”œā”€ā”€ mgm_pretrain.json
ā”‚   ā”‚   ā”œā”€ā”€ images
ā”‚   ā”‚   ā”œā”€ā”€ ALLaVA-4V
ā”‚   ā”œā”€ā”€ MGM-Finetune
ā”‚   ā”‚   ā”œā”€ā”€ mgm_instruction.json
ā”‚   ā”‚   ā”œā”€ā”€ llava
ā”‚   ā”‚   ā”œā”€ā”€ coco
ā”‚   ā”‚   ā”œā”€ā”€ gqa
ā”‚   ā”‚   ā”œā”€ā”€ ocr_vqa
ā”‚   ā”‚   ā”œā”€ā”€ textvqa
ā”‚   ā”‚   ā”œā”€ā”€ vg
ā”‚   ā”‚   ā”œā”€ā”€ gpt4v-dataset
ā”‚   ā”‚   ā”œā”€ā”€ sam
ā”‚   ā”‚   ā”œā”€ā”€ share_textvqa
ā”‚   ā”‚   ā”œā”€ā”€ wikiart
ā”‚   ā”‚   ā”œā”€ā”€ web-celebrity
ā”‚   ā”‚   ā”œā”€ā”€ web-landmark
ā”‚   ā”‚   ā”œā”€ā”€ ALLaVA-4V
ā”‚   ā”‚   ā”œā”€ā”€ docvqa
ā”‚   ā”‚   ā”œā”€ā”€ chartqa
ā”‚   ā”‚   ā”œā”€ā”€ dvqa
ā”‚   ā”‚   ā”œā”€ā”€ ai2d
ā”‚   ā”œā”€ā”€ MGM-Eval
ā”‚   ā”‚   ā”œā”€ā”€ MMMU
ā”‚   ā”‚   ā”œā”€ā”€ MMB
ā”‚   ā”‚   ā”œā”€ā”€ MathVista
ā”‚   ā”‚   ā”œā”€ā”€ ...

Train

The training process consists of two stages: (1) feature alignment stage: bridge the vision and language tokens; (2) instruction tuning stage: teach the model to follow multimodal instructions.

Our models are trained on 8 A100 GPUs with 80GB memory. To train on fewer GPUs, you can reduce the per_device_train_batch_size and increase the gradient_accumulation_steps accordingly. Always keep the global batch size the same: per_device_train_batch_size x gradient_accumulation_steps x num_gpus.

Please make sure you download and organize the data following Preparation before training.

NOTE: Please set hostfile for 2 machine training and hostfile_4 for 4 machine training.

If you want to train and finetune the framework, please run the following command for MGM-7B with image size 336:

bash scripts/llama/train/stage_1_2_full_v7b_336_hr_768.sh

or for MGM-13B with image size 336:

bash scripts/llama/train/stage_1_2_full_v13b_336_hr_768.sh

Because we reuse the pre-trained projecter weights from the MGM-7B, you can directly use the MGM-7B-HD with image size 672 for stage-2 instruction tuning:

bash scripts/llama/train/stage_2_full_v7b_672_hr_1536.sh

Please find more training scripts of gemma, llama, mixtral, and yi in scripts/.

Evaluation

We perform evaluation on several image-based benchmarks. Please download the evaluation data following Preparation and organize them as in Structure.

ModelLLMRes.LinkTextVQAMMBMMEMM-VetMMMU_valMMMU_testMathVista
MGM-2BGemma-2B336ckpt56.259.81341/31231.131.729.129.4
MGM-7BVicuna-7B-v1.5336ckpt65.269.31523/31640.836.132.831.4
MGM-13BVicuna-13B-v1.5336ckpt65.968.51565/32246.038.133.537.0
MGM-8BLLaMA-3-8B-Instruct336ckpt67.672.71606/34147.338.236.3--
MGM-8x7BMixtral-8x7B-Instruct-v0.1336ckpt69.275.61639/37945.841.837.141.8
MGM-34BNous-Hermes-2-Yi-34B336ckpt70.179.61666/43953.048.743.638.9
MGM-7B-HDVicuna-7B-v1.5672ckpt68.465.81546/31941.336.832.932.2
MGM-13B-HDVicuna-13B-v1.5672ckpt70.268.61597/32050.537.335.137.0
MGM-8B-HDLLaMA-3-8B-Instruct672ckpt71.6--1532/357--37.0----
MGM-8x7B-HDMixtral-8x7B-Instruct-v0.1672ckpt71.974.71633/35653.540.037.043.1
MGM-34B-HDNous-Hermes-2-Yi-34B672ckpt74.180.61659/48259.348.044.943.3

If you want to evaluate the model on image-based benchmarks, please use the scripts in scripts/MODEL_PATH/eval. For example, run the following command for TextVQA evaluation with MGM-7B-HD:

bash scripts/llama/eval/textvqa.sh

Please find more evaluation scripts in scripts/MODEL_PATH.

CLI Inference

Chat with images without the need of Gradio interface. It also supports multiple GPUs, 4-bit and 8-bit quantized inference. With 4-bit quantization. Please make sure you have installed diffusers and PaddleOCR (only for better experience with OCR), and try this for image and generation inference:

python -m mgm.serve.cli \
    --model-path work_dirs/MGM/MGM-13B-HD \
    --image-file <path to your image>

or try this better experience with OCR (make sure you have installed PaddleOCR):

python -m mgm.serve.cli \
    --model-path work_dirs/MGM/MGM-13B-HD \
    --image-file <path to your image> \
    --ocr

or try this for inference with generation (make sure you have installed diffusers):

python -m mgm.serve.cli \
    --model-path work_dirs/MGM/MGM-13B-HD \
    --image-file <path to your image> \
    --gen

You can also try 8bit or even 4bit for efficient inference

python -m mgm.serve.cli \
    --model-path work_dirs/MGM/MGM-13B-HD \
    --image-file <path to your image> \
    --gen
    --load-8bit

Gradio Web UI

Here, we adopt the Gradio UI similar to that in LLaVA to provide a user-friendly interface for our models. To launch a Gradio demo locally, please run the following commands one by one. If you plan to launch multiple model workers to compare between different checkpoints, you only need to launch the controller and the web server ONCE.

Launch a controller

python -m mgm.serve.controller --host 0.0.0.0 --port 10000

Launch a gradio web server.

python -m mgm.serve.gradio_web_server --controller http://localhost:10000 --model-list-mode reload

You just launched the Gradio web interface. Now, you can open the web interface with the URL printed on the screen. You may notice that there is no model in the model list. Do not worry, as we have not launched any model worker yet. It will be automatically updated when you launch a model worker.

Launch a model worker

This is the actual worker that performs the inference on the GPU. Each worker is responsible for a single model specified in --model-path.

python -m mgm.serve.model_worker --host 0.0.0.0 --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path work_dirs/MGM/MGM-13B-HD

Wait until the process finishes loading the model and you see "Uvicorn running on ...". Now, refresh your Gradio web UI, and you will see the model you just launched in the model list.

You can launch as many workers as you want, and compare between different models in the same Gradio interface. Please keep the --controller the same, and modify the --port and --worker to a different port number for each worker.

python -m mgm.serve.model_worker --host 0.0.0.0 --controller http://localhost:10000 --port <different from 40000, say 40001> --worker http://localhost:<change accordingly, i.e. 40001> --model-path work_dirs/MGM/MGM-34B-HD

If you are using an Apple device with an M1 or M2 chip, you can specify the mps device by using the --device flag: --device mps.

Launch a model worker (Multiple GPUs, when GPU VRAM <= 24GB)

If the VRAM of your GPU is less than 24GB (e.g., RTX 3090, RTX 4090, etc.), you may try running it with multiple GPUs. Our latest code base will automatically try to use multiple GPUs if you have more than one GPU. You can specify which GPUs to use with CUDA_VISIBLE_DEVICES. Below is an example of running with the first two GPUs.

CUDA_VISIBLE_DEVICES=0,1 python -m mgm.serve.model_worker --host 0.0.0.0 --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path work_dirs/MGM/MGM-13B-HD

Launch a model worker (4-bit, 8-bit inference, quantized)

You can launch the model worker with quantized bits (4-bit, 8-bit), which allows you to run the inference with reduced GPU memory footprint. Note that inference with quantized bits may not be as accurate as the full-precision model. Simply append --load-4bit or --load-8bit to the model worker command that you are executing. Below is an example of running with 4-bit quantization.

python -m mgm.serve.model_worker --host 0.0.0.0 --controller http://localhost:10000 --port 40000 --worker http://localhost:40000 --model-path work_dirs/MGM/MGM-13B-HD --load-4bit

Examples

We provide some examples in this section. More examples can be found in our project page.

Hi-Resolution Understanding

<div align=center> <img width="98%" src="images/demo_und.png"/> </div>

Generation with Reasoning

<div align=center> <img width="98%" src="images/demo_gen.png"/> </div>

Citation

If you find this repo useful for your research, please consider citing the paper

@article{li2024mgm,
  title={Mini-Gemini: Mining the Potential of Multi-modality Vision Language Models},
  author={Li, Yanwei and Zhang, Yuechen and Wang, Chengyao and Zhong, Zhisheng and Chen, Yixin and Chu, Ruihang and Liu, Shaoteng and Jia, Jiaya},
  journal={arXiv:2403.18814},
  year={2023}
}

Acknowledgement

This project is not affiliated with Google LLC.

We would like to thank the following repos for their great work:

License

Code License Data License Weight License

The data and checkpoint is intended and licensed for research use only. They are also restricted to uses that follow the license agreement of LLaVA, 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.