Awesome
GPT4RoI: Instruction Tuning Large Language Model on Region-of-Interest :fire: Demo :fire:
<p align="center"> <img src="figs/demo1.gif" width="80%"> <br> <p align="center" style="font-size:1.2vw;">Single-Region Understanding</p> </p> <p align="center"> <img src="figs/demo2.gif" width="80%"> <br> <p align="center" style="font-size:1.2vw;">Multiple-Region Understanding</p> </p>Introduction
<p align="center"> <img src="figs/framework.png" width="70%"> <br> </p>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</br> (*Equal Contribution)
[Demo] [Paper] [中文介绍]
Updates
- [July 25] GPT4RoI-7B-delta-V0 has release ! :fire::fire::fire: 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 :fire::fire::fire:
Contents
Install
- Clone the
GPT4RoI
git clone https://github.com/jshilong/gpt4roi.git
cd gpt4roi
- 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
- Install the
flash-attn
package
pip install ninja
pip install flash-attn --no-build-isolation
- install the
mmcv-1.4.7
package Make sure that yournvcc -V
is consistent with cudatookit version ofpython -c "import torch;print(torch.version.cuda)
.
cd mmcv-1.4.7
MMCV_WITH_OPS=1 pip install -e .
<!-- ## Data Preparation
| Data file name | Size | original from|
| --- | --- | ---|
| [single_region_caption.json](https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K/raw/main/llava_instruct_150k.json) | 229 MB | VC, Refcocog |
| [multi_region_caption.json](https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K/raw/main/llava_instruct_80k.json) | 229 MB | flicker30k |
| [spation-instruction21k.json](https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K/raw/main/conversation_58k.json) | 126 MB | VCR |
We also use langauge-image multimodal instruction-folllowing dataset [`LLaVA-Instruct-150K`](https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K), with we detect objects with EVA-lvis model, you should download the ``eva_lvis_coco.pkl`` when you use this dataset. -->
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
- coco_imgs should contains all coco image(you can soft link them to this directory.
- 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
. - 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 \
--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
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 normalregion 1
in the conversation to refer the instance.- You should always click the
clear all
buttul and waiting the clear process finished before you start a new conversation.
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:
@article{zhang2023gpt4roi,
title={Gpt4roi: Instruction tuning large language model on region-of-interest},
author={Zhang, Shilong and Sun, Peize and Chen, Shoufa and Xiao, Min and Shao, Wenqi and Zhang, Wenwei and Chen, Kai and Luo, Ping},
journal={arXiv preprint arXiv:2307.03601},
year={2023}
}