README.md 12 KB
Newer Older
ShuoZhang2003's avatar
ShuoZhang2003 committed
1
<p align="left">
ShuoZhang2003's avatar
ShuoZhang2003 committed
2
        English</a>&nbsp | &nbsp<a href="README_cn.md">中文</a>&nbsp
ShuoZhang2003's avatar
ShuoZhang2003 committed
3
4
</p>
<br><br>
lvskiller's avatar
readme  
lvskiller committed
5

ShuoZhang2003's avatar
ShuoZhang2003 committed
6
# Monkey: Image Resolution and Text Label Are Important Things for Large Multi-modal Models
Melos's avatar
Melos committed
7

Yuliang Liu's avatar
Yuliang Liu committed
8
9
<br>
<p align="center">
Yuliang Liu's avatar
Yuliang Liu committed
10
    <img src="images/Logo-Monkey2.gif" width="300"/>
Yuliang Liu's avatar
Yuliang Liu committed
11
12
<p>
<br>
lvskiller's avatar
readme  
lvskiller committed
13
14

<div align="center">
Yuliang Liu's avatar
Yuliang Liu committed
15
Zhang Li*, Biao Yang*, Qiang Liu, Zhiyin Ma, Shuo Zhang, Jingxu Yang, Yabo Sun, Yuliang Liu†, Xiang Bai†
lvskiller's avatar
readme  
lvskiller committed
16
17
18
19
</div>
<div align="center">
<strong>Huazhong University of Science and Technology, Kingsoft</strong>
</div>
Yuliang Liu's avatar
Yuliang Liu committed
20
<p align="center">
echo840's avatar
echo840 committed
21
<a href="https://arxiv.org/abs/2311.06607">Paper</a>&nbsp&nbsp | &nbsp&nbsp<a href="http://vlrlab-monkey.xyz:7681/">Demo_chat</a>&nbsp&nbsp | &nbsp&nbsp<a href="http://huggingface.co/datasets/echo840/Detailed_Caption">Detailed Caption</a>&nbsp&nbsp | &nbsp&nbsp<a href="http://huggingface.co/echo840/Monkey">Model Weight</a>&nbsp&nbsp  |  <a href="https://www.wisemodel.cn/models/HUST-VLRLab/Monkey/">Model Weight in wisemodel</a>&nbsp&nbsp| <a href="https://wisemodel.cn/space/gradio/huakeMonkey">Demo in wisemodel</a>&nbsp&nbsp
lvskiller's avatar
lvskiller committed
22
<!--     | &nbsp&nbsp<a href="Monkey Model">Monkey Models</a>&nbsp | &nbsp <a href="http://huggingface.co/echo840/Monkey">Tutorial</a> -->
Yuliang Liu's avatar
Yuliang Liu committed
23
</p>
lvskiller's avatar
lvskiller committed
24

Melos's avatar
Melos committed
25
-----
Yuliang Liu's avatar
Yuliang Liu committed
26
  
Yuliang Liu's avatar
Yuliang Liu committed
27
**Monkey** brings a training-efficient approach to effectively improve the input resolution capacity up to 896 x 1344 pixels without pretraining from the start. To bridge the gap between simple text labels and high input resolution, we propose a multi-level description generation method, which automatically provides rich information that can guide the model to learn the contextual association between scenes and objects. With the synergy of these two designs, our model achieved excellent results on multiple benchmarks. By comparing our model with various LMMs, including GPT4V, our model demonstrates promising performance in image captioning by paying attention to textual information and capturing fine details within the images; its improved input resolution also enables remarkable performance in document images with dense text. 
Yuliang Liu's avatar
Yuliang Liu committed
28
    
Yuliang Liu's avatar
Yuliang Liu committed
29
## News 
Yuliang Liu's avatar
Yuliang Liu committed
30
* ```2024.4.5  ``` 🚀 Monkey is nominated as CVPR 2024 Highlight.
Yuliang Liu's avatar
Yuliang Liu committed
31
* ```2024.3.8  ``` 🚀 We introduce [TextMonkey](https://arxiv.org/abs/2403.04473), using only public document data, with a ([Demo](http://vlrlab-monkey.xyz:7684/)) available and code forthcoming.
Yuliang Liu's avatar
Yuliang Liu committed
32
33
34
35
36
37
* ```2024.2.27 ``` 🚀 Monkey is accepted by CVPR 2024. The [paper](https://arxiv.org/abs/2311.06607) has been carefully updated according to the valuable comments.
* ```2024.1.3  ``` 🚀 Release the basic data generation pipeline. [Data Generation](./data_generation)
* ```2023.12.21``` 🚀 The JSON file used for Monkey training is provided.
* ```2023.12.16``` 🚀 Monkey can be trained using 8 NVIDIA 3090 GPUs. See subsection [train](#Train) for details.
* ```2023.11.25``` 🚀 Monkey-chat demo is released. 
* ```2023.11.06``` 🚀 Monkey [paper](https://arxiv.org/abs/2311.06607) is released.
Yuliang Liu's avatar
Yuliang Liu committed
38

lvskiller's avatar
lvskiller committed
39

lvskiller's avatar
readme  
lvskiller committed
40
## Spotlights
echo840's avatar
echo840 committed
41
- **Contextual associations.** We introduce a multilevel description generation method that improves the model’s ability to grasp the relationships among multiple targets and more effectively utilize common knowledge in generating text descriptions.
Melos's avatar
Melos committed
42
- **Support resolution up to 1344 x 896.** Surpassing the standard 448 x 448 resolution typically employed for LMMs, this significant increase in resolution augments the ability to discern and understand unnoticeable or tightly clustered objects and dense text. 
echo840's avatar
echo840 committed
43
- **Enhanced general performance.** We carried out testing across 18 diverse datasets, leading to a very competitive performance by our Monkey model in tasks such as Image Captioning, General Visual Question Answering, Scene Text-centric Visual Question Answering, and Document-oriented Visual Question Answering. In particular, during qualitative evaluations centered on dense text question answering, Monkey has shown promising results, comparing with GPT4V
lvskiller's avatar
readme  
lvskiller committed
44

ShuoZhang2003's avatar
ShuoZhang2003 committed
45

lvskiller's avatar
lvskiller committed
46
47
48
49
50
51
52
53
54
55
56
## Environment

```python
conda create -n monkey python=3.9
conda activate monkey
git clone https://github.com/Yuliang-Liu/Monkey.git
cd ./Monkey
pip install -r requirements.txt
```


Yuliang Liu's avatar
Yuliang Liu committed
57
## Demo
lvskiller's avatar
readme  
lvskiller committed
58

ShuoZhang2003's avatar
ShuoZhang2003 committed
59
Demo is fast and easy to use. Simply uploading an image from your desktop or phone, or capture one directly. 
echo840's avatar
echo840 committed
60
[Demo_chat](http://vlrlab-monkey.xyz:7681) is also launched as an upgraded version of the original demo to deliver an enhanced interactive experience.
ShuoZhang2003's avatar
ShuoZhang2003 committed
61
62

Before 14/11/2023, we have observed that for some random pictures Monkey can achieve more accurate results than GPT4V.  
Yuliang Liu's avatar
Yuliang Liu committed
63
64
<br>
<p align="center">
Yuliang Liu's avatar
Yuliang Liu committed
65
    <img src="images/demo_gpt4v_compare4.png" width="900"/>
Yuliang Liu's avatar
Yuliang Liu committed
66
67
68
<p>
<br>

Yuliang Liu's avatar
Yuliang Liu committed
69
70
71
72
73
74
75
Before 31/1/2024, Monkey-chat achieved the fifth rank in the Multimodal Model category on [OpenCompass](https://opencompass.org.cn/home). 
<br>
<p align="center">
    <img src="images/Monkey-rank.png" width="900"/>
<p>
<br>
	
ShuoZhang2003's avatar
ShuoZhang2003 committed
76
We also provide the source code and the model weight for the original demo, allowing you to customize certain parameters for a more unique experience. The specific operations are as follows:
lvskiller's avatar
lvskiller committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 1. Make sure you have configured the [environment](#environment).
 2. You can choose to use the demo offline or online:
- **Offline:** 
	- Download the [Model Weight](http://huggingface.co/echo840/Monkey). 
	- Modify `DEFAULT_CKPT_PATH="pathto/Monkey"` in the `demo.py` file to your model weight path. 
	- Run the demo using the following command: 
	```
	python demo.py
	```
- **Online:** 
	- Run the demo and download model weights online with the following command: 
	```
	python demo.py -c echo840/Monkey 
	```
ShuoZhang2003's avatar
ShuoZhang2003 committed
91

lvskiller's avatar
lvskiller committed
92
93
## Dataset

ShuoZhang2003's avatar
ShuoZhang2003 committed
94
95
The json file used for Monkey training can be downloaded at [Link](https://drive.google.com/file/d/18z_uQTe8Jq61V5rgHtxOt85uKBodbvw1/view?usp=sharing).

Yuliang Liu's avatar
Yuliang Liu committed
96
The data from our multi-level description generation method is now open-sourced and available for download at [Link](https://huggingface.co/datasets/echo840/Detailed_Caption). Examples:
lvskiller's avatar
lvskiller committed
97

Yuliang Liu's avatar
Yuliang Liu committed
98
99
100
101
102
<br>
<p align="center">
    <img src="images/detailed_caption.png" width="1000"/>
<p>
<br>
echo840's avatar
echo840 committed
103
	
echo840's avatar
echo840 committed
104
105
106
107
You can download train images from [Train](https://pan.baidu.com/s/1svSjXTxWpI-3boALgSeLlw). Extraction code: 4hdh

You can download test images and jsonls from [Test](https://pan.baidu.com/s/1ABrQKeE9QBeKvtGzXfM8Eg). Extraction code: 5h71

echo840's avatar
echo840 committed
108
The images are from CC3M, COCO Caption, TextCaps, VQAV2, OKVQA, GQA, ScienceQA, VizWiz, TextVQA, OCRVQA, ESTVQA, STVQA, AI2D and DUE_Benchmark. When using the data, it is necessary to comply with the protocols of the original dataset.
ShuoZhang2003's avatar
ShuoZhang2003 committed
109

lvskiller's avatar
lvskiller committed
110
111
112
113
114
## Evaluate

We offer evaluation code for 14 Visual Question Answering (VQA) datasets in the `evaluate_vqa.py` file, facilitating a quick verification of results.  The specific operations are as follows:

 1. Make sure you have configured the [environment](#environment).
echo840's avatar
echo840 committed
115
 2. Modify `sys.path.append("pathto/Monkey")`  to the project path.
lvskiller's avatar
lvskiller committed
116
117
118
119
120
121
122
123
124
125
126
 3. Prepare the datasets required for evaluation. 
 4. Run the evaluation code.

 Take ESTVQA as an example:
 - Prepare data according to the following directory structure:
```
├── data
|	├── estvqa
|		├── test_image
|			├── {image_path0}
|			├── {image_path1}
ShuoZhang2003's avatar
ShuoZhang2003 committed
127
128
|				  ·
|				  ·
Melos's avatar
Melos committed
129
|	├── estvqa.jsonl
lvskiller's avatar
lvskiller committed
130
131
132
133
134
135
136
137
138
```
 - Example of the format of each line of the annotated `.jsonl` file:
```
{"image": "data/estvqa/test_image/011364.jpg", "question": "What is this store?", "answer": "pizzeria", "question_id": 0}
```
 - Modify the dictionary `ds_collections`:
```
ds_collections = {
	'estvqa_test': {
Melos's avatar
Melos committed
139
140
141
		'test': 'data/estvqa/estvqa.jsonl',
		'metric': 'anls',
		'max_new_tokens': 100,
lvskiller's avatar
lvskiller committed
142
143
144
145
146
147
148
149
150
151
152
153
154
	},
	...
}
```
 - Run the following command:
```
bash eval/eval.sh 'EVAL_PTH' 'SAVE_NAME'
```

## Train

We also offer Monkey's model definition and training code, which you can explore above. You can execute the training code through executing `finetune_ds_debug.sh`.

ShuoZhang2003's avatar
ShuoZhang2003 committed
155
156
The json file used for Monkey training can be downloaded at [Link](https://drive.google.com/file/d/18z_uQTe8Jq61V5rgHtxOt85uKBodbvw1/view?usp=sharing).

lvskiller's avatar
lvskiller committed
157
158
**ATTENTION:** Specify the path to your training data, which should be a json file consisting of a list of conversations.

echo840's avatar
echo840 committed
159
Inspired by Qwen-VL, we freeze the Large Language Model (LLM) and introduce LoRA into four linear layers ```"c_attn", "attn.c_proj", "w1", "w2"``` for training. This step makes it possible to train Monkey using 8 NVIDIA 3090 GPUs. The specific implementation code is in ```modeling_qwen_nvdia3090.py```.
ShuoZhang2003's avatar
ShuoZhang2003 committed
160

echo840's avatar
echo840 committed
161
 - Add LoRA: You need to replace the contents of ```modeling_qwen.py``` with the contents of ```modeling_qwen_nvdia3090.py```.
ShuoZhang2003's avatar
ShuoZhang2003 committed
162
 - Freeze LLM: You need to freeze other modules except LoRA and Resampler modules in ```finetune_multitask.py```.
ShuoZhang2003's avatar
ShuoZhang2003 committed
163

lvskiller's avatar
lvskiller committed
164

lz's avatar
lz committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
## Inference

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "echo840/Monkey"
model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map='cuda', trust_remote_code=True).eval()
tokenizer = AutoTokenizer.from_pretrained(checkpoint, trust_remote_code=True)
tokenizer.padding_side = 'left'
tokenizer.pad_token_id = tokenizer.eod_id
img_path = ""
question = ""
query = f'<img>{img_path}</img> {question} Answer: ' #VQA
# query = f'<img>{img_path}</img> Generate the detailed caption in English: ' #detailed caption

input_ids = tokenizer(query, return_tensors='pt', padding='longest')
attention_mask = input_ids.attention_mask
input_ids = input_ids.input_ids

pred = model.generate(
            input_ids=input_ids.cuda(),
            attention_mask=attention_mask.cuda(),
            do_sample=False,
            num_beams=1,
echo840's avatar
echo840 committed
188
            max_new_tokens=512,
lz's avatar
lz committed
189
190
191
192
193
194
195
196
197
198
199
200
            min_new_tokens=1,
            length_penalty=1,
            num_return_sequences=1,
            output_hidden_states=True,
            use_cache=True,
            pad_token_id=tokenizer.eod_id,
            eos_token_id=tokenizer.eod_id,
            )
response = tokenizer.decode(pred[0][input_ids.size(1):].cpu(), skip_special_tokens=True).strip()
print(response)
```

Yuliang Liu's avatar
Yuliang Liu committed
201
## Performance
lvskiller's avatar
lvskiller committed
202

Yuliang Liu's avatar
Yuliang Liu committed
203
<br>
Yuliang Liu's avatar
Yuliang Liu committed
204

Yuliang Liu's avatar
Yuliang Liu committed
205
<p align="center">
ShuoZhang2003's avatar
ShuoZhang2003 committed
206
    <img src="images/radar_1.png" width="800"/>
Yuliang Liu's avatar
Yuliang Liu committed
207
208
<p>
<br>
Yuliang Liu's avatar
Yuliang Liu committed
209
210


lvskiller's avatar
readme  
lvskiller committed
211
212
## Cases

Yuliang Liu's avatar
Yuliang Liu committed
213
Our model can accurately describe the details in the image.
lvskiller's avatar
readme  
lvskiller committed
214

Yuliang Liu's avatar
Yuliang Liu committed
215
216
217
218
219
<br>
<p align="center">
    <img src="images/caption_1.png" width="700"/>
<p>
<br>
lvskiller's avatar
lvskiller committed
220

ShuoZhang2003's avatar
ShuoZhang2003 committed
221
222
223
224
225
226
227
Our model performs particularly well in dense text question answering tasks. For example, in the dense text of item labels, Monkey can accurately answer various information about the item, and its performance is very impressive compared to other LMMs including GPT4V.

<br>
<p align="center">
    <img src="images/dense_text_1.png" width="700"/>
<p>
<br>
lvskiller's avatar
readme  
lvskiller committed
228

Yuliang Liu's avatar
Yuliang Liu committed
229
230
<br>
<p align="center">
ShuoZhang2003's avatar
ShuoZhang2003 committed
231
    <img src="images/dense_text_2.png" width="700"/>
Yuliang Liu's avatar
Yuliang Liu committed
232
233
<p>
<br>
lvskiller's avatar
readme  
lvskiller committed
234

ShuoZhang2003's avatar
ShuoZhang2003 committed
235
Monkey also performs equally well in daily life scenes. It can complete various Q&A and caption tasks and describe various details in the image in detail, even the inconspicuous watermark.
lvskiller's avatar
readme  
lvskiller committed
236

Yuliang Liu's avatar
Yuliang Liu committed
237
238
<br>
<p align="center">
ShuoZhang2003's avatar
ShuoZhang2003 committed
239
    <img src="images/qa_caption.png" width="700"/>
Yuliang Liu's avatar
Yuliang Liu committed
240
241
<p>
<br>
lvskiller's avatar
readme  
lvskiller committed
242

Yuliang Liu's avatar
Yuliang Liu committed
243
We qualitatively compare with existing LMMs including GPT4V, Qwen-vl, etc, which shows inspiring results. One can have a try using the provided demo. 
lvskiller's avatar
readme  
lvskiller committed
244

Yuliang Liu's avatar
Yuliang Liu committed
245
246
247
248
249
<br>
<p align="center">
    <img src="images/compare.png" width="800"/>
<p>
<br>
lvskiller's avatar
lvskiller committed
250

ShuoZhang2003's avatar
ShuoZhang2003 committed
251

Yuliang Liu's avatar
Yuliang Liu committed
252
253
254
255
## Citing Monkey
If you wish to refer to the baseline results published here, please use the following BibTeX entries:

```BibTeX
Yuliang Liu's avatar
Yuliang Liu committed
256
@article{li2023monkey,
Yuliang Liu's avatar
Yuliang Liu committed
257
  title={Monkey: Image Resolution and Text Label Are Important Things for Large Multi-modal Models},
Yuliang Liu's avatar
Yuliang Liu committed
258
  author={Li, Zhang and Yang, Biao and Liu, Qiang and Ma, Zhiyin and Zhang, Shuo and Yang, Jingxu and Sun, Yabo and Liu, Yuliang and Bai, Xiang},
Yuliang Liu's avatar
Yuliang Liu committed
259
260
261
262
263
  journal={arXiv preprint arXiv:2311.06607},
  year={2023}
}
```

lvskiller's avatar
readme  
lvskiller committed
264
265
## Acknowledgement

Melos's avatar
Melos committed
266
[Qwen-VL](https://github.com/QwenLM/Qwen-VL.git): the codebase we built upon. Thanks for the authors of Qwen for providing the framework.
lvskiller's avatar
readme  
lvskiller committed
267

Yuliang Liu's avatar
Yuliang Liu committed
268

Yuliang Liu's avatar
Yuliang Liu committed
269
## Copyright
Yuliang Liu's avatar
Yuliang Liu committed
270
We welcome suggestions to help us improve the Monkey. For any query, please contact Dr. Yuliang Liu: ylliu@hust.edu.cn. If you find something interesting, please also feel free to share with us through email or open an issue. Thanks!