zwgao commited on
Commit
958a29c
1 Parent(s): 2bfacc6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +277 -3
README.md CHANGED
@@ -1,3 +1,277 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ pipeline_tag: image-text-to-text
4
+ library_name: transformers
5
+ base_model:
6
+ - OpenGVLab/InternVL2-1B
7
+ base_model_relation: merge
8
+ language:
9
+ - multilingual
10
+ tags:
11
+ - internvl
12
+ - custom_code
13
+ ---
14
+
15
+ # Mini-InternVL2-DA-RS
16
+
17
+ [\[📂 GitHub\]](https://github.com/OpenGVLab/InternVL) [\[🆕 Blog\]](https://internvl.github.io/blog/) [\[📜 Mini-InternVL\]](https://arxiv.org/abs/2410.16261) [\[📜 InternVL 1.0\]](https://arxiv.org/abs/2312.14238) [\[📜 InternVL 1.5\]](https://arxiv.org/abs/2404.16821) [\[📜 InternVL 2.5\]](https://huggingface.co/papers/2412.05271)
18
+
19
+ [\[🗨️ InternVL Chat Demo\]](https://internvl.opengvlab.com/) [\[🤗 HF Demo\]](https://huggingface.co/spaces/OpenGVLab/InternVL) [\[🚀 Quick Start\]](#quick-start) [\[📖 中文解读\]](https://zhuanlan.zhihu.com/p/706547971) [\[📖 Documents\]](https://internvl.readthedocs.io/en/latest/internvl2.0/domain_adaptation.html#data-preparation)
20
+
21
+
22
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/64564b0e4a7ffb7d5a47f412/Qp9tEtBAjbq39bJZ7od4A.png)
23
+
24
+ ## Introduction
25
+
26
+ We release the adaptation models for the specific domains: autonomous driving, medical images, and remote sensing.
27
+
28
+ These models are built upon Mini-InternVL and fine-tuned using a unified adaptation framework, achieving good performance on tasks in specific domains.
29
+
30
+ ![image/png](https://cdn-uploads.huggingface.co/production/uploads/64564b0e4a7ffb7d5a47f412/rlz4XL8DFWXplvp0Yx4lg.png)
31
+
32
+ <table>
33
+ <tr>
34
+ <th>Model Name</th>
35
+ <th>HF Link</th>
36
+ <th>Note</th>
37
+ </tr>
38
+ <tr>
39
+ <td>Mini-InternVL2-DA-Drivelm</td>
40
+ <td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-Drivelm">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-Drivelm">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-Drivelm">🤗4B</a></td>
41
+ <td> Adaptation for <a href="https://github.com/OpenDriveLab/DriveLM/tree/main/challenge"> CVPR 2024 Autonomous Driving Challenge </a></td>
42
+ </tr>
43
+ <tr>
44
+ <td>Mini-InternVL2-DA-BDD</td>
45
+ <td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-BDD">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-BDD">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-BDD">🤗4B</a></td>
46
+ <td> Fine-tuning with data constructed by <a href="https://tonyxuqaq.github.io/projects/DriveGPT4/"> DriveGPT4 </a></td>
47
+ </tr>
48
+ <tr>
49
+ <td>Mini-InternVL2-DA-RS</td>
50
+ <td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-RS">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-RS">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-RS">🤗4B</a></td>
51
+ <td> Adaptation for remote sensing domain </td>
52
+ </tr>
53
+ <tr>
54
+ <td>Mini-InternVL2-DA-Medical</td>
55
+ <td><a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-1B-DA-Medical">🤗1B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-2B-DA-Medical">🤗2B</a> / <a href="https://huggingface.co/OpenGVLab/Mini-InternVL2-4B-DA-Medical">🤗4B</a></td>
56
+ <td> Fine-tuning using our <a href="https://huggingface.co/datasets/OpenGVLab/InternVL-Domain-Adaptation-Data/blob/main/train_meta/internvl_1_2_finetune_medical.json">medical data</a>.</td>
57
+ </tr>
58
+ </table>
59
+
60
+ The script for evaluation is in the [document](https://internvl.readthedocs.io/en/latest/internvl2.0/domain_adaptation.html#id3).
61
+
62
+ ## Training datasets
63
+
64
+ - General domain dataset:
65
+
66
+ ShareGPT4V, AllSeeingV2, LLaVA-Instruct-ZH, DVQA, ChartQA, AI2D, DocVQA, GeoQA+, SynthDoG-EN
67
+
68
+ - Medicalt dataset:
69
+
70
+ PMC-OA, MedICaT, Open-i, MedPix, Quilt-1M, RP3D, MIMIC-CXR, Retina Image Bank and others
71
+
72
+ ## Quick Start
73
+
74
+ We provide an example code to run `Mini-InternVL2-1B` using `transformers`.
75
+
76
+ > Please use transformers>=4.37.2 to ensure the model works normally.
77
+
78
+
79
+ ```python
80
+ import numpy as np
81
+ import torch
82
+ import torchvision.transforms as T
83
+ from decord import VideoReader, cpu
84
+ from PIL import Image
85
+ from torchvision.transforms.functional import InterpolationMode
86
+ from transformers import AutoModel, AutoTokenizer
87
+
88
+ IMAGENET_MEAN = (0.485, 0.456, 0.406)
89
+ IMAGENET_STD = (0.229, 0.224, 0.225)
90
+
91
+ def build_transform(input_size):
92
+ MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
93
+ transform = T.Compose([
94
+ T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
95
+ T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
96
+ T.ToTensor(),
97
+ T.Normalize(mean=MEAN, std=STD)
98
+ ])
99
+ return transform
100
+
101
+ def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
102
+ best_ratio_diff = float('inf')
103
+ best_ratio = (1, 1)
104
+ area = width * height
105
+ for ratio in target_ratios:
106
+ target_aspect_ratio = ratio[0] / ratio[1]
107
+ ratio_diff = abs(aspect_ratio - target_aspect_ratio)
108
+ if ratio_diff < best_ratio_diff:
109
+ best_ratio_diff = ratio_diff
110
+ best_ratio = ratio
111
+ elif ratio_diff == best_ratio_diff:
112
+ if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
113
+ best_ratio = ratio
114
+ return best_ratio
115
+
116
+ def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
117
+ orig_width, orig_height = image.size
118
+ aspect_ratio = orig_width / orig_height
119
+
120
+ # calculate the existing image aspect ratio
121
+ target_ratios = set(
122
+ (i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
123
+ i * j <= max_num and i * j >= min_num)
124
+ target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
125
+
126
+ # find the closest aspect ratio to the target
127
+ target_aspect_ratio = find_closest_aspect_ratio(
128
+ aspect_ratio, target_ratios, orig_width, orig_height, image_size)
129
+
130
+ # calculate the target width and height
131
+ target_width = image_size * target_aspect_ratio[0]
132
+ target_height = image_size * target_aspect_ratio[1]
133
+ blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
134
+
135
+ # resize the image
136
+ resized_img = image.resize((target_width, target_height))
137
+ processed_images = []
138
+ for i in range(blocks):
139
+ box = (
140
+ (i % (target_width // image_size)) * image_size,
141
+ (i // (target_width // image_size)) * image_size,
142
+ ((i % (target_width // image_size)) + 1) * image_size,
143
+ ((i // (target_width // image_size)) + 1) * image_size
144
+ )
145
+ # split the image
146
+ split_img = resized_img.crop(box)
147
+ processed_images.append(split_img)
148
+ assert len(processed_images) == blocks
149
+ if use_thumbnail and len(processed_images) != 1:
150
+ thumbnail_img = image.resize((image_size, image_size))
151
+ processed_images.append(thumbnail_img)
152
+ return processed_images
153
+
154
+ def load_image(image_file, input_size=448, max_num=12):
155
+ image = Image.open(image_file).convert('RGB')
156
+ transform = build_transform(input_size=input_size)
157
+ images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
158
+ pixel_values = [transform(image) for image in images]
159
+ pixel_values = torch.stack(pixel_values)
160
+ return pixel_values
161
+
162
+ # If you want to load a model using multiple GPUs, please refer to the `Multiple GPUs` section.
163
+ path = 'OpenGVLab/Mini-InternVL2-1B-DA-Medical'
164
+ model = AutoModel.from_pretrained(
165
+ path,
166
+ torch_dtype=torch.bfloat16,
167
+ low_cpu_mem_usage=True,
168
+ use_flash_attn=True,
169
+ trust_remote_code=True).eval().cuda()
170
+ tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
171
+
172
+ # set the max number of tiles in `max_num`
173
+ pixel_values = load_image('path/to/image.jpg', max_num=12).to(torch.bfloat16).cuda()
174
+ generation_config = dict(max_new_tokens=1024, do_sample=True)
175
+
176
+ # pure-text conversation (纯文本对话)
177
+ question = 'Hello, who are you?'
178
+ response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
179
+ print(f'User: {question}\nAssistant: {response}')
180
+
181
+ question = 'Can you tell me a story?'
182
+ response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
183
+ print(f'User: {question}\nAssistant: {response}')
184
+
185
+ # single-image single-round conversation (单图单轮对话)
186
+ question = '<image>\nPlease describe the image shortly.'
187
+ response = model.chat(tokenizer, pixel_values, question, generation_config)
188
+ print(f'User: {question}\nAssistant: {response}')
189
+
190
+ # single-image multi-round conversation (单图多轮对话)
191
+ question = '<image>\nPlease describe the image in detail.'
192
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
193
+ print(f'User: {question}\nAssistant: {response}')
194
+
195
+ question = 'Please write a poem according to the image.'
196
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
197
+ print(f'User: {question}\nAssistant: {response}')
198
+
199
+ # multi-image multi-round conversation, combined images (多图多轮对话,拼接图像)
200
+ pixel_values1 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
201
+ pixel_values2 = load_image('path/to/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
202
+ pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
203
+
204
+ question = '<image>\nDescribe the two images in detail.'
205
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config,
206
+ history=None, return_history=True)
207
+ print(f'User: {question}\nAssistant: {response}')
208
+
209
+ question = 'What are the similarities and differences between these two images.'
210
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config,
211
+ history=history, return_history=True)
212
+ print(f'User: {question}\nAssistant: {response}')
213
+
214
+ # multi-image multi-round conversation, separate images (多图多轮对话,独立图像)
215
+ pixel_values1 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
216
+ pixel_values2 = load_image('path/to/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
217
+ pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
218
+ num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
219
+
220
+ question = 'Image-1: <image>\nImage-2: <image>\nDescribe the two images in detail.'
221
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config,
222
+ num_patches_list=num_patches_list,
223
+ history=None, return_history=True)
224
+ print(f'User: {question}\nAssistant: {response}')
225
+
226
+ question = 'What are the similarities and differences between these two images.'
227
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config,
228
+ num_patches_list=num_patches_list,
229
+ history=history, return_history=True)
230
+ print(f'User: {question}\nAssistant: {response}')
231
+
232
+ # batch inference, single image per sample (单图批处理)
233
+ pixel_values1 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
234
+ pixel_values2 = load_image('path/to/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
235
+ num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
236
+ pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
237
+
238
+ questions = ['<image>\nDescribe the image in detail.'] * len(num_patches_list)
239
+ responses = model.batch_chat(tokenizer, pixel_values,
240
+ num_patches_list=num_patches_list,
241
+ questions=questions,
242
+ generation_config=generation_config)
243
+ for question, response in zip(questions, responses):
244
+ print(f'User: {question}\nAssistant: {response}')
245
+
246
+ ```
247
+ ## Citation
248
+
249
+ If you find this project useful in your research, please consider citing:
250
+
251
+ ```BibTeX
252
+ @article{gao2024mini,
253
+ title={Mini-internvl: A flexible-transfer pocket multimodal model with 5\% parameters and 90\% performance},
254
+ author={Gao, Zhangwei and Chen, Zhe and Cui, Erfei and Ren, Yiming and Wang, Weiyun and Zhu, Jinguo and Tian, Hao and Ye, Shenglong and He, Junjun and Zhu, Xizhou and others},
255
+ journal={arXiv preprint arXiv:2410.16261},
256
+ year={2024}
257
+ }
258
+ @article{chen2024expanding,
259
+ title={Expanding Performance Boundaries of Open-Source Multimodal Models with Model, Data, and Test-Time Scaling},
260
+ author={Chen, Zhe and Wang, Weiyun and Cao, Yue and Liu, Yangzhou and Gao, Zhangwei and Cui, Erfei and Zhu, Jinguo and Ye, Shenglong and Tian, Hao and Liu, Zhaoyang and others},
261
+ journal={arXiv preprint arXiv:2412.05271},
262
+ year={2024}
263
+ }
264
+ @article{chen2024far,
265
+ title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
266
+ author={Chen, Zhe and Wang, Weiyun and Tian, Hao and Ye, Shenglong and Gao, Zhangwei and Cui, Erfei and Tong, Wenwen and Hu, Kongzhi and Luo, Jiapeng and Ma, Zheng and others},
267
+ journal={arXiv preprint arXiv:2404.16821},
268
+ year={2024}
269
+ }
270
+ @inproceedings{chen2024internvl,
271
+ title={Internvl: Scaling up vision foundation models and aligning for generic visual-linguistic tasks},
272
+ author={Chen, Zhe and Wu, Jiannan and Wang, Wenhai and Su, Weijie and Chen, Guo and Xing, Sen and Zhong, Muyan and Zhang, Qinglong and Zhu, Xizhou and Lu, Lewei and others},
273
+ booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
274
+ pages={24185--24198},
275
+ year={2024}
276
+ }
277
+ ```