/GPT4RoI

GPT4RoI: Instruction Tuning Large Language Model on Region-of-Interest

Primary LanguagePythonOtherNOASSERTION

GPT4RoI: Instruction Tuning Large Language Model on Region-of-Interest πŸ”₯ Demo πŸ”₯


Single-Region Understanding


Multiple-Region Understanding

Introduction


GPT4RoI: Instruction Tuning Large Language Model on Region-of-Interest
Shilong Zhang*, Peize Sun*, Shoufa Chen*, Min Xiao, Wenqi Shao ,Wenwei Zhang, Kai Chen, Ping Luo
(*Equal Contribution)

Updates

  • [July 25] GPT4RoI-7B-delta-V0 has release ! πŸ”₯πŸ”₯πŸ”₯ You need to combine our delta with the original LLaMA weights follow the GPT4RoI Weights section.
  • [July 7] All training and inference code has been released, you can try demo here πŸ”₯πŸ”₯πŸ”₯

Contents

Install

  1. Clone the GPT4RoI
git clone https://github.com/jshilong/gpt4roi.git
cd gpt4roi
  1. Create the env
conda create -n gpt4roi python=3.10 -y
conda activate gpt4roi
pip install --upgrade pip  # enable PEP 660 support
pip install setuptools_scm
pip install --no-cache-dir  -e .
# please use conda re-install the torch, pip may loss some runtime lib
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia 
  1. Install the flash-attn package
pip install ninja
pip install flash-attn --no-build-isolation
  1. install the mmcv-1.4.7 package Make sure that your nvcc -V is consistent with cudatookit version of python -c "import torch;print(torch.version.cuda).
cd mmcv-1.4.7
MMCV_WITH_OPS=1 pip install -e .

Data

Our dataset includes RefCOCO, RefCOCO+, RefCOCOg, Visual Genome, Flickr30K entities, and the VCR dataset. We are sincerely grateful to the creators of these datasets, especially for the VCR dataset, for their forward-thinking in creating these dataset.

The dataset section of this repository may appear somewhat messy, especially the VCR part(still finishing), which may cause GPT4RoI not be very user-friendly. We are currently working on formulating the datasets into a unified format and will be accompanying them with stronger models. Please stay tuned for updates.

You can download the corresponding dataset from the official website and organize it as follows. Afterwards, you can modify the gpt4roi/configs/dataset_config.json file to select the specific dataset you want to use:

GPT4RoI
β”œβ”€β”€ data
β”‚   β”œβ”€β”€ coco_det
β”‚   β”‚   β”œβ”€β”€ annotations
β”‚   β”‚   β”‚      β”œβ”€β”€instances_train2017.json
β”‚   β”‚   β”œβ”€β”€ train2017/
β”‚   β”œβ”€β”€ mdetr_annotations
β”‚   β”‚          β”œβ”€β”€finetune_refcoco_train.json
β”‚   β”‚          β”œβ”€β”€finetune_refcoco+_train.json
β”‚   β”‚          β”œβ”€β”€finetune_refcocog_train.json
β”‚   β”‚          β”œβ”€β”€final_flickr_mergedGT_train.json
β”‚   β”œβ”€β”€ coco_imgs/
β”‚   β”œβ”€β”€ flickr30k-images/
β”‚   β”œβ”€β”€ visual_genome
β”‚   β”‚          β”œβ”€β”€train.json
β”‚   β”‚          β”œβ”€β”€vg_all/
β”‚   β”œβ”€β”€ llava
β”‚   β”‚   β”œβ”€β”€ llava_instruct_150k.json
β”‚   β”‚   β”œβ”€β”€ llava_150k_bbox_pred_results.pkl
β”‚   β”œβ”€β”€ vcr
β”‚   β”‚   β”œβ”€β”€ train.jsonl
β”‚   β”‚   β”œβ”€β”€ vcr1images/

NOTE

  1. coco_imgs should contains all coco image(you can soft link them to this directory.
  2. We use Visual_Genome_Dataset_V1.2, available for download from OpenDataLab. Ensure to download the train.json, you should create a soft link for all VG images to the directory vg_all.
  3. llava_150k_bbox_pred_results.pkl contains the detection predicted results with EVA-02-DET. We appreciate their work.

Weights

Due to the licensing restrictions of LLaMA, the delta weights GPT4RoI-7B is produced from LLaMA-7B. To acquire the GPT4RoI weights, you need to combine our delta with the original LLaMA weights.

Step1. Download the original LLaMA-7B weights

The original LLaMA weights are available for download. Use the following commands:

git lfs install
git clone https://huggingface.co/decapoda-research/llama-7b-hf ./llama-7b 

Alternatively, access the webpage to download the file.

Step2. Download the delta weights of GPT4RoI-7B

The delta weights for GPT4RoI-7B can be downloaded using the following commands:

git lfs install
git clone https://huggingface.co/shilongz/GPT4RoI-7B-delta-V0 ./GPT4RoI-7B-delta

You can also directly download the file from this webpage.

Step3. Apply the delta weights to the original LLaMA-7B weights

Apply the delta weights to the original LLaMA-7B weights. Note that this conversion command requires approximately 30 GB of CPU RAM.

export PYTHONPATH=`pwd`:$PYTHONPATH
python3 -m scripts.apply_delta.py \
    --base ./llama-7b \
    --target ./GPT4RoI-7B \
    --delta ./GPT4RoI-7B-delta

Training

GPT4RoI is trained on 8 A100 with the following code.

STAGE 1

Vicuna-v0, an instruction-tuned chatbot, is the base model for this setup. In order to prepare it, first download the delta weights available here. To obtain the original weights, follow the instructions provided here to integrate these delta weights into LLaMA-7B.

Ensure to download the following projector weight file: LLaVA-7b-pretrain-projector-v0-CC3M-595K-original_caption.bin.

Additionally, you have the flexibility to choose from different versions of Vicuna (such as the 13B version or llama v2 chatbot) and the corresponding projector weights from LLaVA to meet your specific requirements effectively. exp/stage1 is the work directory.

bash train_stage1.sh exp/stage1
# Resume training in stage1
# bash train_stage1.sh exp/stage1

STAGE 2

exp/stage2 is the work directory. and you should give the work directory of stage1 so we can load the corresponding weight as pretrain model.

# At the beginning of stage2
bash train_stage2.sh exp/stage2 exp/stage1
# Resume training in stage2
# bash train_stage2.sh exp/stage2 

Gradio

Please install Gradio Box first.

python gpt4roi/app.py

NOTES

  1. prompt format in GPT4RoI You should always use <region1>, <region2>... to refer the new bounding box in the image when you first draw them. Then you can use normal region 1 in the conversation to refer the instance.
  2. You should always click the clear all buttul and waiting the clear process finished before you start a new conversation.


Multiple Rounds of Dialogue

Acknowledge

  • LLaVA: The codebase we built upon.
  • Vicuna: The LLM we used.
  • VCR: We get strong region reasoning ability from this forward thinking dataset.

If you find GPT4RoI useful for your your research and applications, please cite using this BibTeX:

@misc{zhang2023gpt4roi,
      title={GPT4RoI: Instruction Tuning Large Language Model on Region-of-Interest}, 
      author={Shilong Zhang and Peize Sun and Shoufa Chen and Min Xiao and Wenqi Shao and Wenwei Zhang and Kai Chen and Ping Luo},
      year={2023},
      eprint={2307.03601},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}