MiniGPT-4 and MiniGPT-v2

repository·main·Indexed 12 days ago

https://github.com/vision-cair/minigpt-4

Vision-language models designed to enhance vision-language understanding using Large Language Models (LLMs) such as Llama 2 and Vicuna. The project includes tools for VQA evaluation, local demo launching, and comprehensive guides for pre-training and finetuning using datasets like LAION, CC3M, and COCO.

Tokens
4.6K
Snippets
13
Records
29
Agent score
77%

What's inside MiniGPT-4

  1. Optimize GPU memory usage for demos

    main

    By default, LLMs are loaded in 8-bit with a beam search width of 1 to save memory. This requires approximately:

    • 13B LLM: ~23G GPU memory
    • 7B LLM: ~11.5G GPU memory

    To run the model in 16-bit (requires more powerful GPUs), set low_resource to False in the following config files:

    • MiniGPT-v2: eval_configs/minigptv2_eval.yaml
    • MiniGPT-4 (Llama2): eval_configs/minigpt4_llama2_eval.yaml
    • MiniGPT-4 (Vicuna): eval_configs/minigpt4_eval.yaml
  2. Final dataset directory structure

    main

    After completing the preparation steps, your dataset directory should follow this structure:

    . 
    ├── ${MINIGPT4_DATASET}
    │   ├── cc_sbu
    │   │   ├── convert_cc_sbu.py
    │   │   ├── download_cc_sbu.sh
    │   │   ├── ccs_synthetic_filtered_large.json
    │   │   ├── ccs_synthetic_filtered_large.tsv
    │   │   └── cc_sbu_dataset
    │   │       ├── 00000.tar
    │   │       ├── 00000.parquet
    │   │       └── ...
    │   ├── laion
    │   │   ├── convert_laion.py
    │   │   ├── download_laion.sh
    │   │   ├── laion_synthetic_filtered_large.json
    │   │   ├── laion_synthetic_filtered_large.tsv
    │   │   └── laion_dataset
    │   │       ├── 00000.tar
    │   │       ├── 00000.parquet
    │   │       └── ...
    . 
    ├── ${MINIGPT4_DATASET}
    │   ├── cc_sbu
    │   │   ├── convert_cc_sbu.py
    │   │   ├── download_cc_sbu.sh
    │   │   ├── ccs_synthetic_filtered_large.json
    │   │   ├── ccs_synthetic_filtered_large.tsv
    │   │   └── cc_sbu_dataset
    │   │       ├── 00000.tar
    │   │       ├── 00000.parquet
    │   │       └── ...
    │   ├── laion
    │   │   ├── convert_laion.py
    │   │   ├── download_laion.sh
    │   │   ├── laion_synthetic_filtered_large.json
    │   │   ├── laion_synthetic_filtered_large.tsv
    │   │   └── laion_dataset
    │   │       ├── 00000.tar
    │   │       ├── 00000.parquet
    │   │       └── ...
  3. Download pre-training dataset annotations

    main

    To prepare for pre-training, you must first download the filtered synthetic caption JSON files prepared by BLIP. Note that storing the full LAION and CC3M+CC12M+SBU datasets requires approximately 2.3TB of storage.

    Annotation Files:

    • CC3M+CC12M+SBU: ccs_synthetic_filtered_large.json
    • LAION115M: laion_synthetic_filtered_large.json
  4. Use the VQA Python API and Evaluation tools

    main

    The repository provides two main toolsets for interacting with the VQA dataset:

    Data Access and Visualization

    Located in ./PythonHelperTools, this directory contains the API to read and visualize the VQA dataset.

    • vqaTools: The core API for data reading and visualization.
    • vqaDemo.py: A demo script to show how to use the API.

    Evaluation

    Located in ./PythonEvaluationTools, this directory contains the code for evaluating model performance.

    • vqaEvaluation: The core evaluation logic.
    • vqaEvalDemo.py: A demo script for running evaluations.

    To run a demo evaluation, you can use the example results file provided in ./Results/OpenEnded_mscoco_train2014_fake_results.json.

  5. Install MiniGPT-4 and MiniGPT-v2

    main

    To set up the environment, clone the repository, create a Conda environment from the provided environment.yml, and activate it.

    git clone https://github.com/Vision-CAIR/MiniGPT-4.git
    cd MiniGPT-4
    conda env create -f environment.yml
    conda activate minigptv
  6. Prepare pre-training datasets step-by-step

    main

    Follow these steps to organize, convert, and download the datasets into the required img2dataset format.

    1. Setup dataset folders and move annotations

    Set the MINIGPT4_DATASET environment variable to your large storage path and organize the JSON files.

    2. Copy conversion and download scripts

    Move the provided scripts into their respective dataset folders so they can be executed locally within the data storage directory.

    3. Convert annotation formats

    Run the conversion scripts to transform the JSON annotations into the img2dataset format (generating .tsv files).

    4. Download datasets

    Execute the shell scripts to download the actual image data using img2dataset.

    # 1. Setup folders
    export MINIGPT4_DATASET=/YOUR/PATH/FOR/LARGE/DATASET/
    mkdir ${MINIGPT4_DATASET}/cc_sbu
    mkdir ${MINIGPT4_DATASET}/laion
    mv ccs_synthetic_filtered_large.json ${MINIGPT4_DATASET}/cc_sbu
    mv laion_synthetic_filtered_large.json ${MINIGPT4_DATASET}/laion
    
    # 2. Copy scripts
    cp convert_cc_sbu.py ${MINIGPT4_DATASET}/cc_sbu
    cp download_cc_sbu.sh ${MINIGPT4_DATASET}/cc_sbu
    cp convert_laion.py ${MINIGPT4_DATASET}/laion
    cp download_laion.sh ${MINIGPT4_DATASET}/laion
    
    # 3. Convert annotations
    cd ${MINIGPT4_DATASET}/cc_sbu
    python convert_cc_sbu.py
    cd ${MINIGPT4_DATASET}/laion
    python convert_laion.py
    
    # 4. Download data
    cd ${MINIGPT4_DATASET}/cc_sbu
    sh download_cc_sbu.sh
    cd ${MINIGPT4_DATASET}/laion
    sh download_laion.sh
  7. Set up the VQA dataset directory structure

    main

    The VQA tools expect a specific directory structure for questions, annotations, and images. Follow these steps to organize your data:

    1. Questions and Annotations

    2. Images

    Depending on the dataset type, organize images as follows:

    For Real (MS COCO) datasets: Create a directory structure: ./Images/mscoco/[train2014 | val2014 | test2015].

    For Abstract datasets: Create a directory structure: ./Images/abstract_v002/[train2015 | val2015 | test2015].

    Note: Images must be downloaded from the MS COCO website or the VQA download page as specified for each version.

  8. Download datasets for MiniGPT-v2 finetuning

    main

    To finetune MiniGPT-v2, you need to download various datasets including COCO, Visual Genome, TextCaps, RefCOCO, and others. Most datasets require both an image source and an annotation file (JSON).

    Common image sources used across multiple datasets include:

    • COCO 2014 images (used for COCO captions, COCO VQA, RefCOCO series, OKVQA, AOK-VQA, Multi-task conversation, and LLaVA).
    • Visual Genome images.
    • GQA images.
    • TextCaps images.
    • Flickr-30k images.
  9. Download and configure AOK-VQA

    main

    To download AOK-VQA annotations, use the following commands:

    export AOKVQA_DIR=YOUR_DATASET_PATH
    mkdir -p ${AOKVQA_DIR}
    curl -fsSL https://prior-datasets.s3.us-east-2.amazonaws.com/aokvqa/aokvqa_v1p0.tar.gz | tar xvz -C ${AOKVQA_DIR}

    Expected structure:

    ${MINIGPTv2_DATASET}
    └── aokvqa
        └── aokvqa_v1p0_train.json

    In minigpt4/configs/datasets/aokvqa/defaults.yaml, set:

    • image_path to the COCO 2014 image folder.
    • ann_path to the location of the AOKVQA dataset.
  10. Prepare data for MiniGPT-v2 evaluation

    main

    To evaluate MiniGPT-v2, you must download images and annotations for the target datasets. The datasets supported include OKVQA, GQA, Hateful Meme, IconQA, VizWiz, RefCOCO, RefCOCO+, and RefCOCOg.

    Ensure your directory structure follows the required pattern for the evaluation scripts to locate files correctly. For example:

    • GQA: Requires test_balanced_questions.json, testdev_balanced_questions.json, and a gqa_images folder.
    • OKVQA: Requires okvqa_test_split.json, mscoco_val2014_annotations_clean.json, and OpenEnded_mscoco_val2014_questions_clean.json.
    • RefCOCO/+/g: Requires instances.json and specific .p annotation files (e.g., refs(google).p or refs(unc).p).