StarCycle commited on
Commit
02362f3
β€’
1 Parent(s): 377d3d1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +259 -0
README.md CHANGED
@@ -1,3 +1,262 @@
1
  ---
2
  license: apache-2.0
 
 
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
+ tags:
4
+ - llava
5
+ datasets:
6
+ - liuhaotian/LLaVA-Pretrain
7
+ - liuhaotian/LLaVA-Instruct-150K
8
+ pipeline_tag: image-text-to-text
9
  ---
10
+
11
+ ## Model
12
+ llava-siglip-internlm2-1_8b-pretrain-v1 is a LLaVA checkpoint finetuned from [internlm2-1_8b](https://huggingface.co/internlm/internlm2-1_8b) and [siglip-so400m-patch14-384](https://huggingface.co/google/siglip-so400m-patch14-384) with [LLaVA-Pretrain](liuhaotian/LLaVA-Pretrain) and [LLaVA-Instruct-150K](https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K) by [Xtuner](https://github.com/InternLM/xtuner). The pretraining phase took 5.5 hours on 4 Nvidia GTX 4090 GPU (see this [intermediate checkpoint](https://huggingface.co/StarCycle/llava-siglip-internlm2-1_8b-pretrain-v2)). The finetuning phase took 16 hours on 4 Nvidia GTX 4090 GPU.
13
+
14
+ The total size of the model is around 2.2B, which is suitable for embedded applications like robotics. This model performs better than [llava-siglip-internlm2-1_8b-v1](https://huggingface.co/StarCycle/llava-siglip-internlm2-1_8b-v1) because I use the base LLM, instead of the SFT version.
15
+
16
+ I have not carefully tune the hyperparameters during training. If you have any idea to improve it, please open an issue or just send an email to [email protected]. You are welcomed!
17
+
18
+ ## Example
19
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642a298ae5f33939cf3ee600/AEw4i1rkIcUY74hFLhXLW.png)
20
+ Explain this photo in English and Chinese:
21
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642a298ae5f33939cf3ee600/TibbHJOeZeMkV3h2pinXk.png)
22
+
23
+ ## Rank
24
+ In submission...
25
+
26
+ ## Results
27
+ Model | MMBench Test (EN) | MMBench Dev (EN) | MMBench Test (CN) | MMBench Dev (CN) | CCBench Dev
28
+ ------------- | ------------- | ------------- | ------------- | ------------- | -------------
29
+ LLaVA-v1.5-7B | 67.7 | 69.2 | 61.0 | 59.7 | 28.4
30
+ LLaVA-InternLM-7B | 69.0 | 68.5 | 66.7 | 63.8 | 37.3
31
+ LLaVA-InternLM2-7B | 73.3 | 74.6 | 71.7 | 72.0 | 42.5
32
+ Bunny-3B | 69.2 | 68.6 | - | - | -
33
+ MiniCPM-V | 64.1 | 67.9 | 62.6 | 65.3 | 41.4
34
+ llava-clip-internlm2-1_8b-v1 | 63.3 | 63.1 | 63.6 | 61.7 | 35.3
35
+ llava-siglip-internlm2-1_8b-v1 | 65.7 | 63.5 | 64.5 | 62.9 | 36.3
36
+ llava-siglip-internlm2-1_8b-v2 | - | 67.1 | - | 64.1 | 37.3
37
+
38
+ The test service of MMBench wensite crashes during the special time...I will evaluate it again when the service is back to normal.
39
+
40
+ ## Installation
41
+ ```
42
+ # We need the newest version so clone from github
43
+ git clone https://github.com/huggingface/transformers/
44
+ git clone https://github.com/huggingface/peft
45
+ git clone https://github.com/InternLM/xtuner
46
+ ```
47
+ Now please replace the files in transformers and xtuner with the source code files in modified_transformers and modified_xtuner.
48
+ ```
49
+ cp -r ./modified_transformers ./transformers
50
+ cp -r ./modified_xtuner ./xtuner
51
+ ```
52
+
53
+ Then run
54
+ ```
55
+ pip install -e ./transformers
56
+ pip install -e ./peft
57
+ pip install -e ./xtuner[deepspeed]
58
+ apt install git-lfs
59
+ ```
60
+
61
+ ## Chat
62
+ ```
63
+ xtuner chat internlm/internlm2-1_8b \
64
+ --visual-encoder google/siglip-so400m-patch14-384 \
65
+ --llava ./lora_and_projectors \
66
+ --prompt-template internlm2_chat \
67
+ --image $IMAGE_PATH
68
+ ```
69
+
70
+ ## Common Errors
71
+ 1.
72
+ ```
73
+ command error: 'libGL.so.1: cannot open shared object file: No such file or directory'!
74
+ ```
75
+ You can solve it by
76
+ ```
77
+ # For Ubuntu
78
+ sudo apt-get update
79
+ sudo apt-get install libgl1-mesa-glx
80
+
81
+ # For CentOS and Fedora
82
+ sudo yum install mesa-libGL
83
+ ```
84
+
85
+ 2.
86
+ ```
87
+ Error: mkl-service + Intel(R) MKL: MKL_THREADING_LAYER=INTEL is incompatible with libgomp.so.1 library.
88
+ Try to import numpy first or set the threading layer accordingly. Set MKL_SERVICE_FORCE_INTEL to force it.
89
+ ```
90
+ You can solve it by reinstall numpy.
91
+
92
+ 3.
93
+ ```
94
+ ImportError:
95
+ InternLM2Converter requires the protobuf library but it was not found in your environment. Checkout the instructions on the
96
+ ```
97
+ You just need
98
+ ```
99
+ pip install protobuf
100
+ ```
101
+ 4.
102
+ To use tensorboard to visualize the training loss curve:
103
+ ```
104
+ pip install future tensorboard
105
+ ```
106
+
107
+ 5. If your training process is killed during data preprocessing, you can modify the `map_num_proc` in xtuner/xtuner/dataset
108
+ /huggingface.py
109
+ ```
110
+ def process(dataset,
111
+ do_dataset_tokenization=True,
112
+ tokenizer=None,
113
+ max_length=None,
114
+ dataset_map_fn=None,
115
+ template_map_fn=None,
116
+ max_dataset_length=None,
117
+ split='train',
118
+ remove_unused_columns=False,
119
+ rename_maps=[],
120
+ shuffle_before_pack=True,
121
+ pack_to_max_length=True,
122
+ use_varlen_attn=False,
123
+ input_ids_with_output=True,
124
+ with_image_token=False,
125
+ map_num_proc=32): # modify it to a smaller number, e.g., 4
126
+ ```
127
+
128
+ 6. If you fail to load the model, check whether you installed git-lfs and actually downloaded the model file.
129
+
130
+ ## Data prepration
131
+ 1. File structure
132
+
133
+ ```
134
+ # . means the llava-dinov2-internlm2-7b-v1 folder you clone
135
+ ./data/llava_data
136
+ β”œβ”€β”€ LLaVA-Pretrain
137
+ β”‚Β Β  β”œβ”€β”€ blip_laion_cc_sbu_558k.json
138
+ β”‚Β Β  β”œβ”€β”€ blip_laion_cc_sbu_558k_meta.json
139
+ β”‚Β Β  └── images
140
+ β”œβ”€β”€ LLaVA-Instruct-150K
141
+ β”‚Β Β  └── llava_v1_5_mix665k.json
142
+ └── llava_images
143
+ Β Β  β”œβ”€β”€ coco
144
+ Β Β  β”‚ └── train2017
145
+ Β Β  β”œβ”€β”€ gqa
146
+ Β Β  β”‚ └── images
147
+ Β Β  β”œβ”€β”€ ocr_vqa
148
+ Β Β  β”‚ └── images
149
+ Β Β  β”œβ”€β”€ textvqa
150
+ Β Β  β”‚ └── train_images
151
+ Β Β  └── vg
152
+ Β Β  Β Β  β”œβ”€β”€ VG_100K
153
+ Β Β  └── VG_100K_2
154
+ ```
155
+
156
+ 2. Pretrain Data
157
+
158
+ LLaVA-Pretrain
159
+
160
+ ```shell
161
+ # Make sure you have git-lfs installed (https://git-lfs.com)
162
+ git lfs install
163
+ git clone https://huggingface.co/datasets/liuhaotian/LLaVA-Pretrain --depth=1
164
+ ```
165
+
166
+ 3. Finetune Data
167
+
168
+ 3.1 Text data
169
+
170
+ LLaVA-Instruct-150K
171
+
172
+ ```shell
173
+ # Make sure you have git-lfs installed (https://git-lfs.com)
174
+ git lfs install
175
+ git clone https://huggingface.co/datasets/liuhaotian/LLaVA-Instruct-150K --depth=1
176
+ ```
177
+
178
+ 3.2 Image data
179
+
180
+ 3.2.1 COCO (coco): [train2017](http://images.cocodataset.org/zips/train2017.zip)
181
+
182
+ 3.2.2 GQA (gqa): [images](https://downloads.cs.stanford.edu/nlp/data/gqa/images.zip)
183
+
184
+ 3.2.3 OCR-VQA (ocr_vqa): [download script](https://drive.google.com/drive/folders/1_GYPY5UkUy7HIcR0zq3ZCFgeZN7BAfm_?usp=sharing)
185
+
186
+ ⚠️⚠️⚠️ Modify the name of OCR-VQA's images to keep the extension as `.jpg`!
187
+
188
+ ```shell
189
+ #!/bin/bash
190
+ ocr_vqa_path="<your-directory-path>"
191
+
192
+ find "$target_dir" -type f | while read file; do
193
+ extension="${file##*.}"
194
+ if [ "$extension" != "jpg" ]
195
+ then
196
+ cp -- "$file" "${file%.*}.jpg"
197
+ fi
198
+ done
199
+ ```
200
+
201
+ 3.2.4 TextVQA (textvqa): [train_val_images](https://dl.fbaipublicfiles.com/textvqa/images/train_val_images.zip)
202
+
203
+ 3.2.5 VisualGenome (VG): [part1](https://cs.stanford.edu/people/rak248/VG_100K_2/images.zip), [part2](https://cs.stanford.edu/people/rak248/VG_100K_2/images2.zip)
204
+
205
+ ## Cheers! Now train your own model!
206
+ 1. Alignment module pretraining
207
+ ```
208
+ # single GPU
209
+ xtuner train ./pretrain.py --deepspeed deepspeed_zero2
210
+
211
+ # multiple GPU
212
+ NPROC_PER_NODE=4 xtuner train ./pretrain.py --deepspeed deepspeed_zero2
213
+ ```
214
+
215
+ #### Remember to change the batch size and gradient accumulation parameters to fit your hardware. So your GPU_num * batch_size * gradient_accumulation is roughly equal to mine to reproduce the result.
216
+
217
+ The checkpoint and tensorboard logs are saved by default in ./work_dirs/. I only train it for 1 epoch to be same as the original LLaVA paper. Some researches also report that training for multiple epochs will make the model overfit the training dataset and perform worse in other domains.
218
+
219
+ This is my loss curve for llava-siglip-internlm2-1_8b-pretrain-v2:
220
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642a298ae5f33939cf3ee600/1vh2gsRzEFXia7zCRRIlz.png)
221
+
222
+ And the learning rate curve:
223
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642a298ae5f33939cf3ee600/CdNSJcLCv9MmYG88AJFwB.png)
224
+
225
+ 2. Instruction following fine-tuning
226
+ ```
227
+ NPROC_PER_NODE=4 xtuner train ./finetune.py --deepspeed deepspeed_zero2
228
+ ```
229
+ Here is my loss curve (the curve fluctuates strongly because the batch size is small, and I only record batch loss instead of epoch loss):
230
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642a298ae5f33939cf3ee600/DG1ac7BeaVTrfKqJHA2u8.png)
231
+
232
+ And the learning rate curve:
233
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/642a298ae5f33939cf3ee600/rbuVFeTa04qTbN5j_QF64.png)
234
+
235
+ ## Transfer the checkpoints to Huggingface safetensor format
236
+ ```
237
+ xtuner convert pth_to_hf ./finetune.py ./work_dirs/iter_xxx.pth ./my_lora_and_projector
238
+ ```
239
+ The adapter still need to be used with the internlm/internlm2-1_8b and the vision encoder. I have not tried to merge them yet but it is possible with Xtuner, see this [tutorial](https://github.com/InternLM/xtuner/blob/f63859b3d0cb39cbac709e3850f3fe01de1023aa/xtuner/configs/llava/README.md#L4).
240
+
241
+ ## MMBench Evaluation
242
+ You can first download the MMBench data:
243
+ ```
244
+ wget https://opencompass.openxlab.space/utils/VLMEval/MMBench_DEV_EN.tsv
245
+ wget https://opencompass.openxlab.space/utils/VLMEval/MMBench_TEST_EN.tsv
246
+ wget https://opencompass.openxlab.space/utils/VLMEval/MMBench_DEV_CN.tsv
247
+ wget https://opencompass.openxlab.space/utils/VLMEval/MMBench_TEST_CN.tsv
248
+ wget https://opencompass.openxlab.space/utils/VLMEval/CCBench.tsv
249
+ ```
250
+ Then run:
251
+ ```
252
+ NPROC_PER_NODE=8 xtuner mmbench internlm/internlm2-1_8b \
253
+ --visual-encoder google/siglip-so400m-patch14-384 \
254
+ --llava ./my_lora_and_projector \
255
+ --prompt-template internlm2_chat \
256
+ --data-path $MMBENCH_DATA_PATH \
257
+ --work-dir $RESULT_PATH
258
+ ```
259
+ You can also use [VLMEvalKit](https://github.com/open-compass/VLMEvalKit) to evaluate it on other benckmarks.
260
+
261
+ ## Deployment
262
+ Xtuner team is developing HF chatbot (based on Huggingface transformers) and LMDeploy chatbot (based on TurboMind). I am waiting for their final version of API.