| name | image size | MMMU (val) | MMMU (test) | MathVista (testmini) | MMB (test) | MMB−CN (test) | MMVP | MME | ScienceQA (image) | POPE | TextVQA (val) | SEEDv1 (image) | VizWiz (test) | GQA (test) |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| GPT−4V* | unknown | 56.8 | 55.7 | 49.9 | 77.0 | 74.4 | 38.7 | 1409/517 | - | - | 78.0 | 71.6 | - | - |
| Gemini Ultra* | unknown | 59.4 | - | 53.0 | - | - | - | - | - | - | 82.3 | - | - | - |
| Gemini Pro* | unknown | 47.9 | - | 45.2 | 73.6 | 74.3 | 40.7 | 1497/437 | - | - | 74.6 | 70.7 | - | - |
| Qwen−VL−Plus* | unknown | 45.2 | 40.8 | 43.3 | 67.0 | 70.7 | - | 1681/502 | - | - | 78.9 | 65.7 | - | - |
| Qwen−VL−Max* | unknown | 51.4 | 46.8 | 51.0 | 77.6 | 75.7 | - | - | - | - | 79.5 | - | - | - |
| LLaVA−NEXT−34B | 672x672 | 51.1 | 44.7 | 46.5 | 79.3 | 79.0 | - | 1631/397 | 81.8 | 87.7 | 69.5 | 75.9 | 63.8 | 67.1 |
| InternVL−Chat −V1-2 | 448x448 | 51.6 | 46.2 | 47.7 | 82.2 | 81.2 | 56.7 | 1687/489 | 83.3 | 88.0 | 72.5 | 75.6 | 60.0 | 64.0 |
| Hyperparameter | Trainable Param | Global Batch Size | Learning rate | Epochs | Max length | Weight decay |
|---|---|---|---|---|---|---|
| InternVL−Chat −V1-2 | 40B (full model) | 512 | 1e-5 | 1 | 2048 | 0.05 |
transformers.Please use transformers>=4.37.2 to ensure the model works normally.
1import torch
2from transformers import AutoTokenizer, AutoModel
3path = "OpenGVLab/InternVL-Chat-V1-2"
4model = AutoModel.from_pretrained(
5 path,
6 torch_dtype=torch.bfloat16,
7 low_cpu_mem_usage=True,
8 use_flash_attn=True,
9 trust_remote_code=True).eval().cuda()1import torch
2from transformers import AutoTokenizer, AutoModel
3path = "OpenGVLab/InternVL-Chat-V1-2"
4model = AutoModel.from_pretrained(
5 path,
6 torch_dtype=torch.bfloat16,
7 load_in_8bit=True,
8 low_cpu_mem_usage=True,
9 use_flash_attn=True,
10 trust_remote_code=True).eval()⚠️ Warning: Due to significant quantization errors with BNB 4-bit quantization on InternViT-6B, the model may produce nonsensical outputs and fail to understand images. Therefore, please avoid using BNB 4-bit quantization.
1import math
2import torch
3from transformers import AutoTokenizer, AutoModel
4
5def split_model(model_name):
6 device_map = {}
7 world_size = torch.cuda.device_count()
8 num_layers = {'InternVL-Chat-V1-2': 60, 'InternVL-Chat-V1-2-Plus': 60}[model_name]
9 # Since the first GPU will be used for ViT, treat it as half a GPU.
10 num_layers_per_gpu = math.ceil(num_layers / (world_size - 0.5))
11 num_layers_per_gpu = [num_layers_per_gpu] * world_size
12 num_layers_per_gpu[0] = math.ceil(num_layers_per_gpu[0] * 0.5)
13 layer_cnt = 0
14 for i, num_layer in enumerate(num_layers_per_gpu):
15 for j in range(num_layer):
16 device_map[f'language_model.model.layers.{layer_cnt}'] = i
17 layer_cnt += 1
18 device_map['vision_model'] = 0
19 device_map['mlp1'] = 0
20 device_map['language_model.model.tok_embeddings'] = 0
21 device_map['language_model.model.embed_tokens'] = 0
22 device_map['language_model.output'] = 0
23 device_map['language_model.model.norm'] = 0
24 device_map['language_model.model.rotary_emb'] = 0
25 device_map['language_model.lm_head'] = 0
26 device_map[f'language_model.model.layers.{num_layers - 1}'] = 0
27
28 return device_map
29
30path = "OpenGVLab/InternVL-Chat-V1-2"
31device_map = split_model('InternVL-Chat-V1-2')
32model = AutoModel.from_pretrained(
33 path,
34 torch_dtype=torch.bfloat16,
35 low_cpu_mem_usage=True,
36 use_flash_attn=True,
37 trust_remote_code=True,
38 device_map=device_map).eval()1from transformers import AutoTokenizer, AutoModel
2import torch
3
4path = "OpenGVLab/InternVL-Chat-V1-2"
5model = AutoModel.from_pretrained(
6 path,
7 torch_dtype=torch.bfloat16,
8 low_cpu_mem_usage=True,
9 use_flash_attn=True,
10 trust_remote_code=True).eval().cuda()
11tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
12
13generation_config = dict(max_new_tokens=1024, do_sample=True)
14question = 'Hello, who are you?'
15response, history = model.chat(tokenizer, None, question, generation_config, history=None, return_history=True)
16print(f'User: {question}')
17print(f'Assistant: {response}')
18
19question = 'Can you tell me a story?'
20response, history = model.chat(tokenizer, None, question, generation_config, history=history, return_history=True)
21print(f'User: {question}')
22print(f'Assistant: {response}')1from transformers import AutoTokenizer, AutoModel, CLIPImageProcessor
2from PIL import Image
3import torch
4
5path = "OpenGVLab/InternVL-Chat-V1-2"
6model = AutoModel.from_pretrained(
7 path,
8 torch_dtype=torch.bfloat16,
9 low_cpu_mem_usage=True,
10 use_flash_attn=True,
11 trust_remote_code=True).eval().cuda()
12tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
13
14image_processor = CLIPImageProcessor.from_pretrained(path)
15image = Image.open('./examples/image2.jpg').resize((448, 448))
16pixel_values = image_processor(images=image, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
17
18generation_config = dict(max_new_tokens=1024, do_sample=True)
19question = '<image>\nPlease describe the image shortly.'
20response = model.chat(tokenizer, pixel_values, question, generation_config)
21print(f'User: {question}')
22print(f'Assistant: {response}')1from transformers import AutoTokenizer, AutoModel, CLIPImageProcessor
2from PIL import Image
3import torch
4
5path = "OpenGVLab/InternVL-Chat-V1-2"
6model = AutoModel.from_pretrained(
7 path,
8 torch_dtype=torch.bfloat16,
9 low_cpu_mem_usage=True,
10 use_flash_attn=True,
11 trust_remote_code=True).eval().cuda()
12tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
13
14image_processor = CLIPImageProcessor.from_pretrained(path)
15image = Image.open('./examples/image2.jpg').resize((448, 448))
16pixel_values = image_processor(images=image, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
17
18generation_config = dict(max_new_tokens=1024, do_sample=True)
19question = '<image>\nPlease describe the image in detail.'
20response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=None, return_history=True)
21print(f'User: {question}')
22print(f'Assistant: {response}')
23
24question = 'Please write a poem according to the image.'
25response, history = model.chat(tokenizer, pixel_values, question, generation_config, history=history, return_history=True)
26print(f'User: {question}')
27print(f'Assistant: {response}')⚠️️ Warning: Please note that for this model, we support multi-image chat in the interface, but the results are not very good due to the lack of training with multi-image data.
1from transformers import AutoTokenizer, AutoModel, CLIPImageProcessor
2from PIL import Image
3import torch
4
5path = "OpenGVLab/InternVL-Chat-V1-2"
6model = AutoModel.from_pretrained(
7 path,
8 torch_dtype=torch.bfloat16,
9 low_cpu_mem_usage=True,
10 use_flash_attn=True,
11 trust_remote_code=True).eval().cuda()
12tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
13
14image_processor = CLIPImageProcessor.from_pretrained(path)
15image1 = Image.open('./examples/image1.jpg').resize((448, 448))
16pixel_values1 = image_processor(images=image1, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
17image2 = Image.open('./examples/image2.jpg').resize((448, 448))
18pixel_values2 = image_processor(images=image2, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
19pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
20
21generation_config = dict(max_new_tokens=1024, do_sample=True)
22question = '<image>\nDescribe the two images in detail.'
23response, history = model.chat(tokenizer, pixel_values, question, generation_config,
24 history=None, return_history=True)
25print(f'User: {question}')
26print(f'Assistant: {response}')
27
28question = 'What are the similarities and differences between these two images.'
29response, history = model.chat(tokenizer, pixel_values, question, generation_config,
30 history=history, return_history=True)
31print(f'User: {question}')
32print(f'Assistant: {response}')⚠️️ Warning: Please note that for this model, we support multi-image chat in the interface, but the results are not very good due to the lack of training with multi-image data.
1from transformers import AutoTokenizer, AutoModel, CLIPImageProcessor
2from PIL import Image
3import torch
4
5path = "OpenGVLab/InternVL-Chat-V1-2"
6model = AutoModel.from_pretrained(
7 path,
8 torch_dtype=torch.bfloat16,
9 low_cpu_mem_usage=True,
10 use_flash_attn=True,
11 trust_remote_code=True).eval().cuda()
12tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
13
14image_processor = CLIPImageProcessor.from_pretrained(path)
15image1 = Image.open('./examples/image1.jpg').resize((448, 448))
16pixel_values1 = image_processor(images=image1, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
17image2 = Image.open('./examples/image2.jpg').resize((448, 448))
18pixel_values2 = image_processor(images=image2, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
19pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
20num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
21
22generation_config = dict(max_new_tokens=1024, do_sample=True)
23question = 'Image-1: <image>\nImage-2: <image>\nDescribe the two images in detail.'
24response, history = model.chat(tokenizer, pixel_values, question, generation_config,
25 num_patches_list=num_patches_list, history=None, return_history=True)
26print(f'User: {question}')
27print(f'Assistant: {response}')
28
29question = 'What are the similarities and differences between these two images.'
30response, history = model.chat(tokenizer, pixel_values, question, generation_config,
31 num_patches_list=num_patches_list, history=history, return_history=True)
32print(f'User: {question}')
33print(f'Assistant: {response}')1from transformers import AutoTokenizer, AutoModel, CLIPImageProcessor
2from PIL import Image
3import torch
4
5path = "OpenGVLab/InternVL-Chat-V1-2"
6model = AutoModel.from_pretrained(
7 path,
8 torch_dtype=torch.bfloat16,
9 low_cpu_mem_usage=True,
10 use_flash_attn=True,
11 trust_remote_code=True).eval().cuda()
12tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
13
14image_processor = CLIPImageProcessor.from_pretrained(path)
15image1 = Image.open('./examples/image1.jpg').resize((448, 448))
16pixel_values1 = image_processor(images=image1, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
17image2 = Image.open('./examples/image2.jpg').resize((448, 448))
18pixel_values2 = image_processor(images=image2, return_tensors='pt').pixel_values.to(torch.bfloat16).cuda()
19pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
20num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
21
22generation_config = dict(max_new_tokens=1024, do_sample=True)
23questions = ['<image>\nDescribe the image in detail.'] * len(num_patches_list)
24responses = model.batch_chat(tokenizer, pixel_values,
25 num_patches_list=num_patches_list,
26 questions=questions,
27 generation_config=generation_config)
28for question, response in zip(questions, responses):
29 print(f'User: {question}')
30 print(f'Assistant: {response}')⚠️️ Warning: Please note that for this model, we support video chat in the interface, but the results are not very good due to the lack of training with video data.
1from transformers import AutoTokenizer, AutoModel, CLIPImageProcessor
2from decord import VideoReader, cpu
3from PIL import Image
4import numpy as np
5import torch
6
7
8def get_index(bound, fps, max_frame, first_idx=0, num_segments=32):
9 if bound:
10 start, end = bound[0], bound[1]
11 else:
12 start, end = -100000, 100000
13 start_idx = max(first_idx, round(start * fps))
14 end_idx = min(round(end * fps), max_frame)
15 seg_size = float(end_idx - start_idx) / num_segments
16 frame_indices = np.array([
17 int(start_idx + (seg_size / 2) + np.round(seg_size * idx))
18 for idx in range(num_segments)
19 ])
20 return frame_indices
21
22def load_video(video_path, bound=None, num_segments=32):
23 vr = VideoReader(video_path, ctx=cpu(0), num_threads=1)
24 max_frame = len(vr) - 1
25 fps = float(vr.get_avg_fps())
26
27 pixel_values_list, num_patches_list = [], []
28 image_processor = CLIPImageProcessor.from_pretrained(path)
29 frame_indices = get_index(bound, fps, max_frame, first_idx=0, num_segments=num_segments)
30 for frame_index in frame_indices:
31 img = Image.fromarray(vr[frame_index].asnumpy()).convert('RGB').resize((448, 448))
32 pixel_values = image_processor(images=img, return_tensors='pt').pixel_values
33 num_patches_list.append(pixel_values.shape[0])
34 pixel_values_list.append(pixel_values)
35 pixel_values = torch.cat(pixel_values_list)
36 return pixel_values, num_patches_list
37
38
39path = "OpenGVLab/InternVL-Chat-V1-2"
40model = AutoModel.from_pretrained(
41 path,
42 torch_dtype=torch.bfloat16,
43 low_cpu_mem_usage=True,
44 use_flash_attn=True,
45 trust_remote_code=True).eval().cuda()
46tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
47
48generation_config = dict(max_new_tokens=1024, do_sample=True)
49
50video_path = './examples/red-panda.mp4'
51pixel_values, num_patches_list = load_video(video_path, num_segments=8)
52pixel_values = pixel_values.to(torch.bfloat16).cuda()
53video_prefix = ''.join([f'Frame{i+1}: <image>\n' for i in range(len(num_patches_list))])
54question = video_prefix + 'What is the red panda doing?'
55# Frame1: <image>\nFrame2: <image>\n...\nFrame8: <image>\n{question}
56response, history = model.chat(tokenizer, pixel_values, question, generation_config,
57 num_patches_list=num_patches_list, history=None, return_history=True)
58print(f'User: {question}')
59print(f'Assistant: {response}')
60
61question = 'Describe this video in detail.'
62response, history = model.chat(tokenizer, pixel_values, question, generation_config,
63 num_patches_list=num_patches_list, history=history, return_history=True)
64print(f'User: {question}')
65print(f'Assistant: {response}')1from transformers import TextIteratorStreamer
2from threading import Thread
3
4# Initialize the streamer
5streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=10)
6# Define the generation configuration
7generation_config = dict(max_new_tokens=1024, do_sample=False, streamer=streamer)
8# Start the model chat in a separate thread
9thread = Thread(target=model.chat, kwargs=dict(
10 tokenizer=tokenizer, pixel_values=pixel_values, question=question,
11 history=None, return_history=False, generation_config=generation_config,
12))
13thread.start()
14
15# Initialize an empty string to store the generated text
16generated_text = ''
17# Loop through the streamer to get the new text as it is generated
18for new_text in streamer:
19 if new_text == model.conv_template.sep:
20 break
21 generated_text += new_text
22 print(new_text, end='', flush=True) # Print each new chunk of generated text on the same line1@article{chen2024expanding,
2 title={Expanding Performance Boundaries of Open-Source Multimodal Models with Model, Data, and Test-Time Scaling},
3 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},
4 journal={arXiv preprint arXiv:2412.05271},
5 year={2024}
6}
7@article{gao2024mini,
8 title={Mini-internvl: A flexible-transfer pocket multimodal model with 5\% parameters and 90\% performance},
9 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},
10 journal={arXiv preprint arXiv:2410.16261},
11 year={2024}
12}
13@article{chen2024far,
14 title={How Far Are We to GPT-4V? Closing the Gap to Commercial Multimodal Models with Open-Source Suites},
15 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},
16 journal={arXiv preprint arXiv:2404.16821},
17 year={2024}
18}
19@inproceedings{chen2024internvl,
20 title={Internvl: Scaling up vision foundation models and aligning for generic visual-linguistic tasks},
21 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},
22 booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
23 pages={24185--24198},
24 year={2024}
25}