radna commited on
Commit
0703a82
1 Parent(s): 5c0cb68

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +329 -397
README.md CHANGED
@@ -1,397 +1,329 @@
1
- ---
2
- license: mit
3
- pipeline_tag: visual-question-answering
4
- ---
5
-
6
- # InternVL2-2B
7
-
8
- [\[📂 GitHub\]](https://github.com/OpenGVLab/InternVL) [\[🆕 Blog\]](https://internvl.github.io/blog/) [\[📜 InternVL 1.0 Paper\]](https://arxiv.org/abs/2312.14238) [\[📜 InternVL 1.5 Report\]](https://arxiv.org/abs/2404.16821)
9
-
10
- [\[🗨️ Chat Demo\]](https://internvl.opengvlab.com/) [\[🤗 HF Demo\]](https://huggingface.co/spaces/OpenGVLab/InternVL) [\[🚀 Quick Start\]](#quick-start) [\[📖 中文解读\]](https://zhuanlan.zhihu.com/p/675877376)
11
-
12
- ## Introduction
13
-
14
- We are excited to announce the release of InternVL 2.0, the latest addition to the InternVL series of multimodal large language models. InternVL 2.0 features a variety of instruction-tuned models, ranging from 2 billion to 108 billion parameters. This repository contains the instruction-tuned InternVL2-2B model.
15
-
16
- Compared to the state-of-the-art open-source multimodal large language models, InternVL 2.0 surpasses most open-source models. It demonstrates competitive performance on par with proprietary commercial models across various capabilities, including document and chart comprehension, infographics QA, scene text understanding and OCR tasks, scientific and mathematical problem solving, as well as cultural understanding and integrated multimodal capabilities.
17
-
18
- InternVL 2.0 is trained with an 8k context window and utilizes training data consisting of long texts, multiple images, and videos, significantly improving its ability to handle these types of inputs compared to InternVL 1.5. For more details, please refer to our blog and GitHub.
19
-
20
- ## Model Details
21
-
22
- InternVL 2.0 is a multimodal large language model series, featuring models of various sizes. For each size, we release instruction-tuned models optimized for multimodal tasks. InternVL2-2B consists of [InternViT-300M-448px](https://huggingface.co/OpenGVLab/InternViT-300M-448px), an MLP projector, and [internlm2-chat-1_8b](https://huggingface.co/internlm/internlm2-chat-1_8b).
23
-
24
- ## Performance
25
-
26
- | Benchmark | PaliGemma-3B | Phi-3-Vision | Mini-InternVL-2B-1.5 | InternVL2-2B |
27
- | :--------------------------: | :----------: | :----------: | :------------------: | :----------: |
28
- | Model Size | 2.9B | 4.2B | 2.2B | 2.2B |
29
- | | | | | |
30
- | DocVQA<sub>test</sub> | - | - | 85.0 | 86.9 |
31
- | ChartQA<sub>test</sub> | - | 81.4 | 74.8 | 76.2 |
32
- | InfoVQA<sub>test</sub> | - | - | 55.4 | 58.9 |
33
- | TextVQA<sub>val</sub> | 68.1 | 70.9 | 70.5 | 73.4 |
34
- | OCRBench | 614 | 639 | 654 | 784 |
35
- | MME<sub>sum</sub> | 1686.1 | 1508.0 | 1901.5 | 1876.8 |
36
- | RealWorldQA | 55.2 | 58.8 | 57.9 | 57.3 |
37
- | AI2D<sub>test</sub> | 68.3 | 76.7 | 69.8 | 74.1 |
38
- | MMMU<sub>val</sub> | 34.9 | 40.4 | 34.6 | 34.3 |
39
- | MMBench-EN<sub>test</sub> | 71.0 | 73.6 | 70.9 | 73.2 |
40
- | MMBench-CN<sub>test</sub> | 63.6 | - | 66.2 | 70.9 |
41
- | CCBench<sub>dev</sub> | 29.6 | 24.1 | 63.5 | 74.7 |
42
- | MMVet<sub>GPT-4-0613</sub> | - | - | 39.3 | 44.6 |
43
- | MMVet<sub>GPT-4-Turbo</sub> | 33.1 | 44.1 | 35.5 | 39.5 |
44
- | SEED-Image | 69.6 | 70.9 | 69.8 | 71.6 |
45
- | HallBench<sub>avg</sub> | 32.2 | 39.0 | 37.5 | 37.9 |
46
- | MathVista<sub>testmini</sub> | 28.7 | 44.5 | 41.1 | 46.3 |
47
-
48
- - We simultaneously use InternVL and VLMEvalKit repositories for model evaluation. Specifically, the results reported for DocVQA, ChartQA, InfoVQA, TextVQA, MME, AI2D, MMBench, CCBench, MMVet, and SEED-Image were tested using the InternVL repository. MMMU, OCRBench, RealWorldQA, HallBench, and MathVista were evaluated using the VLMEvalKit.
49
-
50
- - Please note that evaluating the same model using different testing toolkits like InternVL and VLMEvalKit can result in slight differences, which is normal. Updates to code versions and variations in environment and hardware can also cause minor discrepancies in results.
51
-
52
- - It is important to mention that the MMVet scores we report are evaluated using GPT-4-0613 as the judge model. Different versions of GPT-4 can lead to significant variations in the scores for this dataset. For instance, using GPT-4-Turbo would result in significantly lower scores.
53
-
54
- Limitations: Although we have made efforts to ensure the safety of the model during the training process and to encourage the model to generate text that complies with ethical and legal requirements, the model may still produce unexpected outputs due to its size and probabilistic generation paradigm. For example, the generated responses may contain biases, discrimination, or other harmful content. Please do not propagate such content. We are not responsible for any consequences resulting from the dissemination of harmful information.
55
-
56
- ## Quick Start
57
-
58
- We provide an example code to run InternVL2-2B using `transformers`.
59
-
60
- > Please use transformers==4.37.2 to ensure the model works normally.
61
-
62
- ```python
63
- import torch
64
- import torchvision.transforms as T
65
- from PIL import Image
66
- from torchvision.transforms.functional import InterpolationMode
67
- from transformers import AutoModel, AutoTokenizer
68
-
69
- IMAGENET_MEAN = (0.485, 0.456, 0.406)
70
- IMAGENET_STD = (0.229, 0.224, 0.225)
71
-
72
-
73
- def build_transform(input_size):
74
- MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
75
- transform = T.Compose([
76
- T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
77
- T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
78
- T.ToTensor(),
79
- T.Normalize(mean=MEAN, std=STD)
80
- ])
81
- return transform
82
-
83
-
84
- def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
85
- best_ratio_diff = float('inf')
86
- best_ratio = (1, 1)
87
- area = width * height
88
- for ratio in target_ratios:
89
- target_aspect_ratio = ratio[0] / ratio[1]
90
- ratio_diff = abs(aspect_ratio - target_aspect_ratio)
91
- if ratio_diff < best_ratio_diff:
92
- best_ratio_diff = ratio_diff
93
- best_ratio = ratio
94
- elif ratio_diff == best_ratio_diff:
95
- if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
96
- best_ratio = ratio
97
- return best_ratio
98
-
99
-
100
- def dynamic_preprocess(image, min_num=1, max_num=6, image_size=448, use_thumbnail=False):
101
- orig_width, orig_height = image.size
102
- aspect_ratio = orig_width / orig_height
103
-
104
- # calculate the existing image aspect ratio
105
- target_ratios = set(
106
- (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
107
- i * j <= max_num and i * j >= min_num)
108
- target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
109
-
110
- # find the closest aspect ratio to the target
111
- target_aspect_ratio = find_closest_aspect_ratio(
112
- aspect_ratio, target_ratios, orig_width, orig_height, image_size)
113
-
114
- # calculate the target width and height
115
- target_width = image_size * target_aspect_ratio[0]
116
- target_height = image_size * target_aspect_ratio[1]
117
- blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
118
-
119
- # resize the image
120
- resized_img = image.resize((target_width, target_height))
121
- processed_images = []
122
- for i in range(blocks):
123
- box = (
124
- (i % (target_width // image_size)) * image_size,
125
- (i // (target_width // image_size)) * image_size,
126
- ((i % (target_width // image_size)) + 1) * image_size,
127
- ((i // (target_width // image_size)) + 1) * image_size
128
- )
129
- # split the image
130
- split_img = resized_img.crop(box)
131
- processed_images.append(split_img)
132
- assert len(processed_images) == blocks
133
- if use_thumbnail and len(processed_images) != 1:
134
- thumbnail_img = image.resize((image_size, image_size))
135
- processed_images.append(thumbnail_img)
136
- return processed_images
137
-
138
-
139
- def load_image(image_file, input_size=448, max_num=6):
140
- image = Image.open(image_file).convert('RGB')
141
- transform = build_transform(input_size=input_size)
142
- images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
143
- pixel_values = [transform(image) for image in images]
144
- pixel_values = torch.stack(pixel_values)
145
- return pixel_values
146
-
147
-
148
- path = 'OpenGVLab/InternVL2-2B'
149
- model = AutoModel.from_pretrained(
150
- path,
151
- torch_dtype=torch.bfloat16,
152
- low_cpu_mem_usage=True,
153
- trust_remote_code=True).eval().cuda()
154
-
155
- tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
156
- # set the max number of tiles in `max_num`
157
- pixel_values = load_image('./examples/image1.jpg', max_num=6).to(torch.bfloat16).cuda()
158
-
159
- generation_config = dict(
160
- num_beams=1,
161
- max_new_tokens=1024,
162
- do_sample=False,
163
- )
164
-
165
- # pure-text conversation (纯文本对话)
166
- question = 'Hello, who are you?'
167
- response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
168
- print(f'User: {question}')
169
- print(f'Assistant: {response}')
170
-
171
- question = 'Can you tell me a story?'
172
- response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
173
- print(f'User: {question}')
174
- print(f'Assistant: {response}')
175
-
176
- # single-image single-round conversation (单图单轮对话)
177
- question = '<image>\nPlease describe the image shortly.'
178
- response = model.chat(tokenizer, pixel_values, question, generation_config)
179
- print(f'User: {question}')
180
- print(f'Assistant: {response}')
181
-
182
- # single-image multi-round conversation (单图多轮对话)
183
- question = '<image>\nPlease describe the image in detail.'
184
- response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
185
- print(f'User: {question}')
186
- print(f'Assistant: {response}')
187
-
188
- question = 'Please write a poem according to the image.'
189
- response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
190
- print(f'User: {question}')
191
- print(f'Assistant: {response}')
192
-
193
- # multi-image multi-round conversation (多图多轮对话)
194
- pixel_values1 = load_image('./examples/image1.jpg', max_num=6).to(torch.bfloat16).cuda()
195
- pixel_values2 = load_image('./examples/image2.jpg', max_num=6).to(torch.bfloat16).cuda()
196
- pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
197
- num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
198
-
199
- question = 'Image-1: <image>\nImage-2: <image>\nDescribe the two images in detail.'
200
- response, history = model.chat(tokenizer, pixel_values, question, generation_config,
201
- num_patches_list=num_patches_list,
202
- history=None, return_history=True)
203
- print(f'User: {question}')
204
- print(f'Assistant: {response}')
205
-
206
- question = 'What are the similarities and differences between these two images.'
207
- response, history = model.chat(tokenizer, pixel_values, question, generation_config,
208
- num_patches_list=num_patches_list,
209
- history=history, return_history=True)
210
- print(f'User: {question}')
211
- print(f'Assistant: {response}')
212
-
213
- # batch inference, single image per sample (单图批处理)
214
- pixel_values1 = load_image('./examples/image1.jpg', max_num=6).to(torch.bfloat16).cuda()
215
- pixel_values2 = load_image('./examples/image2.jpg', max_num=6).to(torch.bfloat16).cuda()
216
- num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
217
- pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
218
-
219
- questions = ['<image>\nDescribe the image in detail.'] * len(num_patches_list)
220
- responses = model.batch_chat(tokenizer, pixel_values,
221
- num_patches_list=num_patches_list,
222
- questions=questions,
223
- generation_config=generation_config)
224
- for question, response in zip(questions, responses):
225
- print(f'User: {question}')
226
- print(f'Assistant: {response}')
227
-
228
- # video multi-round conversation (视频多轮对话)
229
- def get_index(bound, fps, max_frame, first_idx=0, num_segments=32):
230
- if bound:
231
- start, end = bound[0], bound[1]
232
- else:
233
- start, end = -100000, 100000
234
- start_idx = max(first_idx, round(start * fps))
235
- end_idx = min(round(end * fps), max_frame)
236
- seg_size = float(end_idx - start_idx) / num_segments
237
- frame_indices = np.array([
238
- int(start_idx + (seg_size / 2) + np.round(seg_size * idx))
239
- for idx in range(num_segments)
240
- ])
241
- return frame_indices
242
-
243
- def load_video(video_path, bound=None, input_size=448, max_num=1, num_segments=32):
244
- vr = VideoReader(video_path, ctx=cpu(0), num_threads=1)
245
- max_frame = len(vr) - 1
246
- fps = float(vr.get_avg_fps())
247
-
248
- pixel_values_list, num_patches_list = [], []
249
- transform = build_transform(input_size=input_size)
250
- frame_indices = get_index(bound, fps, max_frame, first_idx=0, num_segments=num_segments)
251
- for frame_index in frame_indices:
252
- img = Image.fromarray(vr[frame_index].asnumpy()).convert('RGB')
253
- img = dynamic_preprocess(img, image_size=input_size, use_thumbnail=True, max_num=max_num)
254
- pixel_values = [transform(tile) for tile in img]
255
- pixel_values = torch.stack(pixel_values)
256
- num_patches_list.append(pixel_values.shape[0])
257
- pixel_values_list.append(pixel_values)
258
- pixel_values = torch.cat(pixel_values_list)
259
- return pixel_values, num_patches_list
260
-
261
-
262
- video_path = './examples/red-panda.mp4'
263
- # pixel_values, num_patches_list = load_video(video_path, num_segments=32, max_num=1)
264
- pixel_values, num_patches_list = load_video(video_path, num_segments=8, max_num=2)
265
- pixel_values = pixel_values.to(torch.bfloat16).cuda()
266
- video_prefix = '\n'.join([f'Frame{i+1}:<image>' for i in range(len(num_patches_list))])
267
- question = video_prefix + 'What is the red panda doing?'
268
- # Frame1:<image>\nFrame2:<image>\n...\nFrame31:<image>\n{question}
269
- response, history = model.chat(tokenizer, pixel_values, question, generation_config,
270
- num_patches_list=num_patches_list,
271
- history=None, return_history=True)
272
- print(f'User: {question}')
273
- print(f'Assistant: {response}')
274
-
275
- question = 'Describe this video in detail.'
276
- response, history = model.chat(tokenizer, pixel_values, question, generation_config,
277
- num_patches_list=num_patches_list,
278
- history=history, return_history=True)
279
- print(f'User: {question}')
280
- print(f'Assistant: {response}')
281
- ```
282
-
283
- ## Deployment
284
-
285
- ### LMDeploy
286
-
287
- LMDeploy is a toolkit for compressing, deploying, and serving LLM, developed by the MMRazor and MMDeploy teams.
288
-
289
- ```sh
290
- pip install lmdeploy
291
- ```
292
-
293
- You can run batch inference locally with the following python code:
294
-
295
- ```python
296
- from lmdeploy.vl import load_image
297
- from lmdeploy import ChatTemplateConfig, pipeline
298
-
299
- model = 'OpenGVLab/InternVL2-2B'
300
- system_prompt = '我是书生·万象,英文名是InternVL,是由上海人工智能实验室及多家合作单位联合开发的多模态基础模型。人工智能实验室致力于原始技术创新,开源开放,共享共创,推动科技进步和产业发展。'
301
- image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
302
- chat_template_config = ChatTemplateConfig('internlm2-chat')
303
- chat_template_config.meta_instruction = system_prompt
304
- pipe = pipeline(model, chat_template_config=chat_template_config)
305
- response = pipe(('describe this image', image))
306
- print(response)
307
- ```
308
-
309
- ## License
310
-
311
- This project is released under the MIT license, while InternLM is licensed under the Apache-2.0 license.
312
-
313
- ## Citation
314
-
315
- If you find this project useful in your research, please consider citing:
316
-
317
- ```BibTeX
318
- @article{chen2023internvl,
319
- title={InternVL: Scaling up Vision Foundation Models and Aligning for Generic Visual-Linguistic Tasks},
320
- 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 Li, Bin and Luo, Ping and Lu, Tong and Qiao, Yu and Dai, Jifeng},
321
- journal={arXiv preprint arXiv:2312.14238},
322
- year={2023}
323
- }
324
- @article{chen2024far,
325
- title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
326
- 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},
327
- journal={arXiv preprint arXiv:2404.16821},
328
- year={2024}
329
- }
330
- ```
331
-
332
- ## 简介
333
-
334
- 我们很高兴宣布 InternVL 2.0 的发布,这是 InternVL 系列多模态大语言模型的最新版本。InternVL 2.0 提供了多种指令微调的模型,参数从 20 亿到 1080 亿不等。此仓库包含经过指令微调的 InternVL2-2B 模型。
335
-
336
- 与最先进的开源多模态大语言模型相比,InternVL 2.0 超越了大多数开源模型。它在各种能力上表现出与闭源商业模型相媲美的竞争力,包括文档和图表理解、信息图表问答、场景文本理解和 OCR 任务、科学和数学问题解决,以及文化理解和综合多模态能力。
337
-
338
- InternVL 2.0 使用 8k 上下文窗口进行训练,训练数据包含长文本、多图和视频数据,与 InternVL 1.5 相比,其处理这些类型输入的能力显著提高。更多详细信息,请参阅我们的博客和 GitHub。
339
-
340
- ## 模型细节
341
-
342
- InternVL 2.0 是一个多模态大语言模型系列,包含各种规模的模型。对于每个规模的模型,我们都会发布针对多模态任务优化的指令微调模型。InternVL2-2B 包含 [InternViT-300M-448px](https://huggingface.co/OpenGVLab/InternViT-300M-448px)、一个 MLP 投影器和 [internlm2-chat-1_8b](https://huggingface.co/internlm/internlm2-chat-1_8b)。
343
-
344
- ## 性能测试
345
-
346
- | 评测数据集 | PaliGemma-3B | Phi-3-Vision | Mini-InternVL-2B-1.5 | InternVL2-2B |
347
- | :--------------------------: | :----------: | :----------: | :------------------: | :----------: |
348
- | 模型大小 | 2.9B | 4.2B | 2.2B | 2.2B |
349
- | | | | | |
350
- | DocVQA<sub>test</sub> | - | - | 85.0 | 86.9 |
351
- | ChartQA<sub>test</sub> | - | 81.4 | 74.8 | 76.2 |
352
- | InfoVQA<sub>test</sub> | - | - | 55.4 | 58.9 |
353
- | TextVQA<sub>val</sub> | 68.1 | 70.9 | 70.5 | 73.4 |
354
- | OCRBench | 614 | 639 | 654 | 784 |
355
- | MME<sub>sum</sub> | 1686.1 | 1508.0 | 1901.5 | 1876.8 |
356
- | RealWorldQA | 55.2 | 58.8 | 57.9 | 57.3 |
357
- | AI2D<sub>test</sub> | 68.3 | 76.7 | 69.8 | 74.1 |
358
- | MMMU<sub>val</sub> | 34.9 | 40.4 | 34.6 | 34.3 |
359
- | MMBench-EN<sub>test</sub> | 71.0 | 73.6 | 70.9 | 73.2 |
360
- | MMBench-CN<sub>test</sub> | 63.6 | - | 66.2 | 70.9 |
361
- | CCBench<sub>dev</sub> | 29.6 | 24.1 | 63.5 | 74.7 |
362
- | MMVet<sub>GPT-4-0613</sub> | - | - | 39.3 | 44.6 |
363
- | MMVet<sub>GPT-4-Turbo</sub> | 33.1 | 44.1 | 35.5 | 39.5 |
364
- | SEED-Image | 69.6 | 70.9 | 69.8 | 71.6 |
365
- | HallBench<sub>avg</sub> | 32.2 | 39.0 | 37.5 | 37.9 |
366
- | MathVista<sub>testmini</sub> | 28.7 | 44.5 | 41.1 | 46.3 |
367
-
368
- - 我们同时使用 InternVL 和 VLMEvalKit 仓库进行模型评估。具体来说,DocVQA、ChartQA、InfoVQA、TextVQA、MME、AI2D、MMBench、CCBench、MMVet 和 SEED-Image 的结果是使用 InternVL 仓库测试的。MMMU、OCRBench、RealWorldQA、HallBench 和 MathVista 是使用 VLMEvalKit 进行评估的。
369
-
370
- - 请注意,使用不同的测试工具包(如 InternVL 和 VLMEvalKit)评估同一模型可能会导致细微差异,这是正常的。代码版本的更新、环境和硬件的变化也可能导致结果的微小差异。
371
-
372
- - 需要提到的是,我们报告的 MMVet 分数是使用 GPT-4-0613 作为评判模型评估的。不同版本的 GPT-4 会导致该数据集分数的显著变化。例如,使用 GPT-4-Turbo 会导致分数显著降低。
373
-
374
- 限制:尽管在训练过程中我们非常注重模型的安全性,尽力促使模型输出符合伦理和法律要求的文本,但受限于模型大小以及概率生成范式,模型可能会产生各种不符合预期的输出,例如回复内容包含偏见、歧视等有害内容,请勿传播这些内容。由于传播不良信息导致的任何后果,本项目不承担责任。
375
-
376
- ## 开源许可证
377
-
378
- 该项目采用 MIT 许可证发布,而 InternLM 则采用 Apache-2.0 许可证。
379
-
380
- ## 引用
381
-
382
- 如果您发现此项目对您的研究有用,可以考虑引用我们的论文:
383
-
384
- ```BibTeX
385
- @article{chen2023internvl,
386
- title={InternVL: Scaling up Vision Foundation Models and Aligning for Generic Visual-Linguistic Tasks},
387
- 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 Li, Bin and Luo, Ping and Lu, Tong and Qiao, Yu and Dai, Jifeng},
388
- journal={arXiv preprint arXiv:2312.14238},
389
- year={2023}
390
- }
391
- @article{chen2024far,
392
- title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
393
- 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},
394
- journal={arXiv preprint arXiv:2404.16821},
395
- year={2024}
396
- }
397
- ```
 
1
+ ---
2
+ license: mit
3
+ pipeline_tag: visual-question-answering
4
+ ---
5
+
6
+ # InternVL2-2B
7
+
8
+ [\[📂 GitHub\]](https://github.com/OpenGVLab/InternVL) [\[🆕 Blog\]](https://internvl.github.io/blog/) [\[📜 InternVL 1.0 Paper\]](https://arxiv.org/abs/2312.14238) [\[📜 InternVL 1.5 Report\]](https://arxiv.org/abs/2404.16821)
9
+
10
+ [\[🗨️ Chat Demo\]](https://internvl.opengvlab.com/) [\[🤗 HF Demo\]](https://huggingface.co/spaces/OpenGVLab/InternVL) [\[🚀 Quick Start\]](#quick-start) [\[📖 中文解读\]](https://zhuanlan.zhihu.com/p/675877376)
11
+
12
+ ## Introduction
13
+
14
+ We are excited to announce the release of InternVL 2.0, the latest addition to the InternVL series of multimodal large language models. InternVL 2.0 features a variety of instruction-tuned models, ranging from 2 billion to 108 billion parameters. This repository contains the instruction-tuned InternVL2-2B model.
15
+
16
+ Compared to the state-of-the-art open-source multimodal large language models, InternVL 2.0 surpasses most open-source models. It demonstrates competitive performance on par with proprietary commercial models across various capabilities, including document and chart comprehension, infographics QA, scene text understanding and OCR tasks, scientific and mathematical problem solving, as well as cultural understanding and integrated multimodal capabilities.
17
+
18
+ InternVL 2.0 is trained with an 8k context window and utilizes training data consisting of long texts, multiple images, and videos, significantly improving its ability to handle these types of inputs compared to InternVL 1.5. For more details, please refer to our blog and GitHub.
19
+
20
+ ## Model Details
21
+
22
+ InternVL 2.0 is a multimodal large language model series, featuring models of various sizes. For each size, we release instruction-tuned models optimized for multimodal tasks. InternVL2-2B consists of [InternViT-300M-448px](https://huggingface.co/OpenGVLab/InternViT-300M-448px), an MLP projector, and [internlm2-chat-1_8b](https://huggingface.co/internlm/internlm2-chat-1_8b).
23
+
24
+ ## Performance
25
+
26
+ | Benchmark | PaliGemma-3B | Phi-3-Vision | Mini-InternVL-2B-1.5 | InternVL2-2B |
27
+ | :--------------------------: | :----------: | :----------: | :------------------: | :----------: |
28
+ | Model Size | 2.9B | 4.2B | 2.2B | 2.2B |
29
+ | | | | | |
30
+ | DocVQA<sub>test</sub> | - | - | 85.0 | 86.9 |
31
+ | ChartQA<sub>test</sub> | - | 81.4 | 74.8 | 76.2 |
32
+ | InfoVQA<sub>test</sub> | - | - | 55.4 | 58.9 |
33
+ | TextVQA<sub>val</sub> | 68.1 | 70.9 | 70.5 | 73.4 |
34
+ | OCRBench | 614 | 639 | 654 | 784 |
35
+ | MME<sub>sum</sub> | 1686.1 | 1508.0 | 1901.5 | 1876.8 |
36
+ | RealWorldQA | 55.2 | 58.8 | 57.9 | 57.3 |
37
+ | AI2D<sub>test</sub> | 68.3 | 76.7 | 69.8 | 74.1 |
38
+ | MMMU<sub>val</sub> | 34.9 | 40.4 | 34.6 | 34.3 |
39
+ | MMBench-EN<sub>test</sub> | 71.0 | 73.6 | 70.9 | 73.2 |
40
+ | MMBench-CN<sub>test</sub> | 63.6 | - | 66.2 | 70.9 |
41
+ | CCBench<sub>dev</sub> | 29.6 | 24.1 | 63.5 | 74.7 |
42
+ | MMVet<sub>GPT-4-0613</sub> | - | - | 39.3 | 44.6 |
43
+ | MMVet<sub>GPT-4-Turbo</sub> | 33.1 | 44.1 | 35.5 | 39.5 |
44
+ | SEED-Image | 69.6 | 70.9 | 69.8 | 71.6 |
45
+ | HallBench<sub>avg</sub> | 32.2 | 39.0 | 37.5 | 37.9 |
46
+ | MathVista<sub>testmini</sub> | 28.7 | 44.5 | 41.1 | 46.3 |
47
+
48
+ - We simultaneously use InternVL and VLMEvalKit repositories for model evaluation. Specifically, the results reported for DocVQA, ChartQA, InfoVQA, TextVQA, MME, AI2D, MMBench, CCBench, MMVet, and SEED-Image were tested using the InternVL repository. MMMU, OCRBench, RealWorldQA, HallBench, and MathVista were evaluated using the VLMEvalKit.
49
+
50
+ - Please note that evaluating the same model using different testing toolkits like InternVL and VLMEvalKit can result in slight differences, which is normal. Updates to code versions and variations in environment and hardware can also cause minor discrepancies in results.
51
+
52
+ - It is important to mention that the MMVet scores we report are evaluated using GPT-4-0613 as the judge model. Different versions of GPT-4 can lead to significant variations in the scores for this dataset. For instance, using GPT-4-Turbo would result in significantly lower scores.
53
+
54
+ Limitations: Although we have made efforts to ensure the safety of the model during the training process and to encourage the model to generate text that complies with ethical and legal requirements, the model may still produce unexpected outputs due to its size and probabilistic generation paradigm. For example, the generated responses may contain biases, discrimination, or other harmful content. Please do not propagate such content. We are not responsible for any consequences resulting from the dissemination of harmful information.
55
+
56
+ ## Quick Start
57
+
58
+ We provide an example code to run InternVL2-2B using `transformers`.
59
+
60
+ > Please use transformers==4.37.2 to ensure the model works normally.
61
+
62
+ ```python
63
+ import torch
64
+ import torchvision.transforms as T
65
+ from PIL import Image
66
+ from torchvision.transforms.functional import InterpolationMode
67
+ from transformers import AutoModel, AutoTokenizer
68
+
69
+ IMAGENET_MEAN = (0.485, 0.456, 0.406)
70
+ IMAGENET_STD = (0.229, 0.224, 0.225)
71
+
72
+
73
+ def build_transform(input_size):
74
+ MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
75
+ transform = T.Compose([
76
+ T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
77
+ T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
78
+ T.ToTensor(),
79
+ T.Normalize(mean=MEAN, std=STD)
80
+ ])
81
+ return transform
82
+
83
+
84
+ def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
85
+ best_ratio_diff = float('inf')
86
+ best_ratio = (1, 1)
87
+ area = width * height
88
+ for ratio in target_ratios:
89
+ target_aspect_ratio = ratio[0] / ratio[1]
90
+ ratio_diff = abs(aspect_ratio - target_aspect_ratio)
91
+ if ratio_diff < best_ratio_diff:
92
+ best_ratio_diff = ratio_diff
93
+ best_ratio = ratio
94
+ elif ratio_diff == best_ratio_diff:
95
+ if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
96
+ best_ratio = ratio
97
+ return best_ratio
98
+
99
+
100
+ def dynamic_preprocess(image, min_num=1, max_num=6, image_size=448, use_thumbnail=False):
101
+ orig_width, orig_height = image.size
102
+ aspect_ratio = orig_width / orig_height
103
+
104
+ # calculate the existing image aspect ratio
105
+ target_ratios = set(
106
+ (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
107
+ i * j <= max_num and i * j >= min_num)
108
+ target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
109
+
110
+ # find the closest aspect ratio to the target
111
+ target_aspect_ratio = find_closest_aspect_ratio(
112
+ aspect_ratio, target_ratios, orig_width, orig_height, image_size)
113
+
114
+ # calculate the target width and height
115
+ target_width = image_size * target_aspect_ratio[0]
116
+ target_height = image_size * target_aspect_ratio[1]
117
+ blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
118
+
119
+ # resize the image
120
+ resized_img = image.resize((target_width, target_height))
121
+ processed_images = []
122
+ for i in range(blocks):
123
+ box = (
124
+ (i % (target_width // image_size)) * image_size,
125
+ (i // (target_width // image_size)) * image_size,
126
+ ((i % (target_width // image_size)) + 1) * image_size,
127
+ ((i // (target_width // image_size)) + 1) * image_size
128
+ )
129
+ # split the image
130
+ split_img = resized_img.crop(box)
131
+ processed_images.append(split_img)
132
+ assert len(processed_images) == blocks
133
+ if use_thumbnail and len(processed_images) != 1:
134
+ thumbnail_img = image.resize((image_size, image_size))
135
+ processed_images.append(thumbnail_img)
136
+ return processed_images
137
+
138
+
139
+ def load_image(image_file, input_size=448, max_num=6):
140
+ image = Image.open(image_file).convert('RGB')
141
+ transform = build_transform(input_size=input_size)
142
+ images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
143
+ pixel_values = [transform(image) for image in images]
144
+ pixel_values = torch.stack(pixel_values)
145
+ return pixel_values
146
+
147
+
148
+ path = 'radna/Triton-InternVL2-2B'
149
+ model = AutoModel.from_pretrained(
150
+ path,
151
+ torch_dtype=torch.bfloat16,
152
+ low_cpu_mem_usage=True,
153
+ trust_remote_code=True).eval().cuda()
154
+
155
+ tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True)
156
+ # set the max number of tiles in `max_num`
157
+ pixel_values = load_image('./examples/image1.jpg', max_num=6).to(torch.bfloat16).cuda()
158
+
159
+ generation_config = dict(
160
+ num_beams=1,
161
+ max_new_tokens=1024,
162
+ do_sample=False,
163
+ )
164
+
165
+ # pure-text conversation (纯文本对话)
166
+ question = 'Hello, who are you?'
167
+ response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
168
+ print(f'User: {question}')
169
+ print(f'Assistant: {response}')
170
+
171
+ question = 'Can you tell me a story?'
172
+ response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
173
+ print(f'User: {question}')
174
+ print(f'Assistant: {response}')
175
+
176
+ # single-image single-round conversation (单图单轮对话)
177
+ question = '<image>\nPlease describe the image shortly.'
178
+ response = model.chat(tokenizer, pixel_values, question, generation_config)
179
+ print(f'User: {question}')
180
+ print(f'Assistant: {response}')
181
+
182
+ # single-image multi-round conversation (单图多轮对话)
183
+ question = '<image>\nPlease describe the image in detail.'
184
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
185
+ print(f'User: {question}')
186
+ print(f'Assistant: {response}')
187
+
188
+ question = 'Please write a poem according to the image.'
189
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
190
+ print(f'User: {question}')
191
+ print(f'Assistant: {response}')
192
+
193
+
194
+ video_path = './examples/red-panda.mp4'
195
+ # pixel_values, num_patches_list = load_video(video_path, num_segments=32, max_num=1)
196
+ pixel_values, num_patches_list = load_video(video_path, num_segments=8, max_num=2)
197
+ pixel_values = pixel_values.to(torch.bfloat16).cuda()
198
+ video_prefix = '\n'.join([f'Frame{i+1}:<image>' for i in range(len(num_patches_list))])
199
+ question = video_prefix + 'What is the red panda doing?'
200
+ # Frame1:<image>\nFrame2:<image>\n...\nFrame31:<image>\n{question}
201
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config,
202
+ num_patches_list=num_patches_list,
203
+ history=None, return_history=True)
204
+ print(f'User: {question}')
205
+ print(f'Assistant: {response}')
206
+
207
+ question = 'Describe this video in detail.'
208
+ response, history = model.chat(tokenizer, pixel_values, question, generation_config,
209
+ num_patches_list=num_patches_list,
210
+ history=history, return_history=True)
211
+ print(f'User: {question}')
212
+ print(f'Assistant: {response}')
213
+ ```
214
+
215
+ ## Deployment
216
+
217
+ ### LMDeploy
218
+
219
+ LMDeploy is a toolkit for compressing, deploying, and serving LLM, developed by the MMRazor and MMDeploy teams.
220
+
221
+ ```sh
222
+ pip install lmdeploy
223
+ ```
224
+
225
+ You can run batch inference locally with the following python code:
226
+
227
+ ```python
228
+ from lmdeploy.vl import load_image
229
+ from lmdeploy import ChatTemplateConfig, pipeline
230
+
231
+ model = 'OpenGVLab/InternVL2-2B'
232
+ system_prompt = '我是书生·万象,英文名是InternVL,是由上海人工智能实验室及多家合作单位联合开发的多模态基础模型。人工智能实验室致力于原始技术创新,开源开放,共享共创,推动科技进步和产业发展。'
233
+ image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg')
234
+ chat_template_config = ChatTemplateConfig('internlm2-chat')
235
+ chat_template_config.meta_instruction = system_prompt
236
+ pipe = pipeline(model, chat_template_config=chat_template_config)
237
+ response = pipe(('describe this image', image))
238
+ print(response)
239
+ ```
240
+
241
+ ## License
242
+
243
+ This project is released under the MIT license, while InternLM is licensed under the Apache-2.0 license.
244
+
245
+ ## Citation
246
+
247
+ If you find this project useful in your research, please consider citing:
248
+
249
+ ```BibTeX
250
+ @article{chen2023internvl,
251
+ title={InternVL: Scaling up Vision Foundation Models and Aligning for Generic Visual-Linguistic Tasks},
252
+ 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 Li, Bin and Luo, Ping and Lu, Tong and Qiao, Yu and Dai, Jifeng},
253
+ journal={arXiv preprint arXiv:2312.14238},
254
+ year={2023}
255
+ }
256
+ @article{chen2024far,
257
+ title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
258
+ 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},
259
+ journal={arXiv preprint arXiv:2404.16821},
260
+ year={2024}
261
+ }
262
+ ```
263
+
264
+ ## 简介
265
+
266
+ 我们很高兴宣布 InternVL 2.0 的发布,这是 InternVL 系列多模态大语言模型的最新版本。InternVL 2.0 提供了多种指令微调的模型,参数从 20 亿到 1080 亿不等。此仓库包含经过指令微调的 InternVL2-2B 模型。
267
+
268
+ 与最先进的开源多模态大语言模型相比,InternVL 2.0 超越了大多数开源模型。它在各种能力上表现出与闭源商业模型相媲美的竞争力,包括文档和图表理解、信息图表问答、场景文本理解和 OCR 任务、科学和数学问题解决,以及文化理解和综合多模态能力。
269
+
270
+ InternVL 2.0 使用 8k 上下文窗口进行训练,训练数据包含长文本、多图和视频数据,与 InternVL 1.5 相比,其处理这些类型输入的能力显著提高。更多详细信息,请参阅我们的博客和 GitHub。
271
+
272
+ ## 模型细节
273
+
274
+ InternVL 2.0 是一个多模态大语言模型系列,包含各种规模的模型。对于每个规模的模型,我们都会发布针对多模态任务优化的指令微调模型。InternVL2-2B 包含 [InternViT-300M-448px](https://huggingface.co/OpenGVLab/InternViT-300M-448px)、一个 MLP 投影器和 [internlm2-chat-1_8b](https://huggingface.co/internlm/internlm2-chat-1_8b)。
275
+
276
+ ## 性能测试
277
+
278
+ | 评测数据集 | PaliGemma-3B | Phi-3-Vision | Mini-InternVL-2B-1.5 | InternVL2-2B |
279
+ | :--------------------------: | :----------: | :----------: | :------------------: | :----------: |
280
+ | 模型大小 | 2.9B | 4.2B | 2.2B | 2.2B |
281
+ | | | | | |
282
+ | DocVQA<sub>test</sub> | - | - | 85.0 | 86.9 |
283
+ | ChartQA<sub>test</sub> | - | 81.4 | 74.8 | 76.2 |
284
+ | InfoVQA<sub>test</sub> | - | - | 55.4 | 58.9 |
285
+ | TextVQA<sub>val</sub> | 68.1 | 70.9 | 70.5 | 73.4 |
286
+ | OCRBench | 614 | 639 | 654 | 784 |
287
+ | MME<sub>sum</sub> | 1686.1 | 1508.0 | 1901.5 | 1876.8 |
288
+ | RealWorldQA | 55.2 | 58.8 | 57.9 | 57.3 |
289
+ | AI2D<sub>test</sub> | 68.3 | 76.7 | 69.8 | 74.1 |
290
+ | MMMU<sub>val</sub> | 34.9 | 40.4 | 34.6 | 34.3 |
291
+ | MMBench-EN<sub>test</sub> | 71.0 | 73.6 | 70.9 | 73.2 |
292
+ | MMBench-CN<sub>test</sub> | 63.6 | - | 66.2 | 70.9 |
293
+ | CCBench<sub>dev</sub> | 29.6 | 24.1 | 63.5 | 74.7 |
294
+ | MMVet<sub>GPT-4-0613</sub> | - | - | 39.3 | 44.6 |
295
+ | MMVet<sub>GPT-4-Turbo</sub> | 33.1 | 44.1 | 35.5 | 39.5 |
296
+ | SEED-Image | 69.6 | 70.9 | 69.8 | 71.6 |
297
+ | HallBench<sub>avg</sub> | 32.2 | 39.0 | 37.5 | 37.9 |
298
+ | MathVista<sub>testmini</sub> | 28.7 | 44.5 | 41.1 | 46.3 |
299
+
300
+ - 我们同时使用 InternVL 和 VLMEvalKit 仓库进行模型评估。具体来说,DocVQA、ChartQA、InfoVQA、TextVQA、MME、AI2D、MMBench、CCBench、MMVet 和 SEED-Image 的结果是使用 InternVL 仓库测试的。MMMU、OCRBench、RealWorldQA、HallBench 和 MathVista 是使用 VLMEvalKit 进行评估的。
301
+
302
+ - 请注意,使用不同的测试工具包(如 InternVL 和 VLMEvalKit)评估同一模型可能会导致细微差异,这是正常的。代码版本的更新、环境和硬件的变化也可能导致结果的微小差异。
303
+
304
+ - 需要提到的是,我们报告的 MMVet 分数是使用 GPT-4-0613 作为评判模型评估的。不同版本的 GPT-4 会导致该数据集分数的显著变化。例如,使用 GPT-4-Turbo 会导致分数显著降低。
305
+
306
+ 限制:尽管在训练过程中我们非常注重模型的安全性,尽力促使模型输出符合伦理和法律要求的文本,但受限于模型大小以及概率生成范式,模型可能会产生各种不符合预期的输出,例如回复内容包含偏见、歧视等有害内容,请勿传播这些内容。由于传播不良信息导致的任何后果,本项目不承担责任。
307
+
308
+ ## 开源许可证
309
+
310
+ 该项目采用 MIT 许可证发布,而 InternLM 则采用 Apache-2.0 许可证。
311
+
312
+ ## 引用
313
+
314
+ 如果您发现此项目对您的研究有用,可以考虑引用我们的论文:
315
+
316
+ ```BibTeX
317
+ @article{chen2023internvl,
318
+ title={InternVL: Scaling up Vision Foundation Models and Aligning for Generic Visual-Linguistic Tasks},
319
+ 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 Li, Bin and Luo, Ping and Lu, Tong and Qiao, Yu and Dai, Jifeng},
320
+ journal={arXiv preprint arXiv:2312.14238},
321
+ year={2023}
322
+ }
323
+ @article{chen2024far,
324
+ title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
325
+ 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},
326
+ journal={arXiv preprint arXiv:2404.16821},
327
+ year={2024}
328
+ }
329
+ ```