Views
No views yet


| Ovis MLLMs | ViT | LLM | Model Weights | Demo |
|---|---|---|---|---|
| Ovis2-1B | aimv2-large-patch14-448 | Qwen2.5-0.5B-Instruct | Huggingface | Space |
| Ovis2-2B | aimv2-large-patch14-448 | Qwen2.5-1.5B-Instruct | Huggingface | Space |
| Ovis2-4B | aimv2-huge-patch14-448 | Qwen2.5-3B-Instruct | Huggingface | Space |
| Ovis2-8B | aimv2-huge-patch14-448 | Qwen2.5-7B-Instruct | Huggingface | Space |
| Ovis2-16B | aimv2-huge-patch14-448 | Qwen2.5-14B-Instruct | Huggingface | Space |
| Ovis2-34B | aimv2-1B-patch14-448 | Qwen2.5-32B-Instruct | Huggingface | - |

| Benchmark | Qwen2.5-VL-3B | SAIL-VL-2B | InternVL2.5-2B-MPO | Ovis1.6-3B | InternVL2.5-1B-MPO | Ovis2-1B | Ovis2-2B |
|---|---|---|---|---|---|---|---|
| MMBench-V1.1test | 77.1 | 73.6 | 70.7 | 74.1 | 65.8 | 68.4 | 76.9 |
| MMStar | 56.5 | 56.5 | 54.9 | 52.0 | 49.5 | 52.1 | 56.7 |
| MMMUval | 51.4 | 44.1 | 44.6 | 46.7 | 40.3 | 36.1 | 45.6 |
| MathVistatestmini | 60.1 | 62.8 | 53.4 | 58.9 | 47.7 | 59.4 | 64.1 |
| HallusionBench | 48.7 | 45.9 | 40.7 | 43.8 | 34.8 | 45.2 | 50.2 |
| AI2D | 81.4 | 77.4 | 75.1 | 77.8 | 68.5 | 76.4 | 82.7 |
| OCRBench | 83.1 | 83.1 | 83.8 | 80.1 | 84.3 | 89.0 | 87.3 |
| MMVet | 63.2 | 44.2 | 64.2 | 57.6 | 47.2 | 50.0 | 58.3 |
| MMBenchtest | 78.6 | 77 | 72.8 | 76.6 | 67.9 | 70.2 | 78.9 |
| MMT-Benchval | 60.8 | 57.1 | 54.4 | 59.2 | 50.8 | 55.5 | 61.7 |
| RealWorldQA | 66.5 | 62 | 61.3 | 66.7 | 57 | 63.9 | 66.0 |
| BLINK | 48.4 | 46.4 | 43.8 | 43.8 | 41 | 44.0 | 47.9 |
| QBench | 74.4 | 72.8 | 69.8 | 75.8 | 63.3 | 71.3 | 76.2 |
| ABench | 75.5 | 74.5 | 71.1 | 75.2 | 67.5 | 71.3 | 76.6 |
| MTVQA | 24.9 | 20.2 | 22.6 | 21.1 | 21.7 | 23.7 | 25.6 |
| Benchmark | Qwen2.5-VL-3B | InternVL2.5-2B | InternVL2.5-1B | Ovis2-1B | Ovis2-2B |
|---|---|---|---|---|---|
| VideoMME(wo/w-subs) | 61.5/67.6 | 51.9 / 54.1 | 50.3 / 52.3 | 48.6/49.5 | 57.2/60.8 |
| MVBench | 67.0 | 68.8 | 64.3 | 60.32 | 64.9 |
| MLVU(M-Avg/G-Avg) | 68.2/- | 61.4/- | 57.3/- | 58.5/3.66 | 68.6/3.86 |
| MMBench-Video | 1.63 | 1.44 | 1.36 | 1.26 | 1.57 |
| TempCompass | 64.4 | - | - | 51.43 | 62.64 |
1pip install torch==2.4.0 transformers==4.46.2 numpy==1.25.0 pillow==10.3.0
2pip install flash-attn==2.7.0.post2 --no-build-isolation1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM
4
5# load model
6model = AutoModelForCausalLM.from_pretrained("AIDC-AI/Ovis2-2B",
7 torch_dtype=torch.bfloat16,
8 multimodal_max_length=32768,
9 trust_remote_code=True).cuda()
10text_tokenizer = model.get_text_tokenizer()
11visual_tokenizer = model.get_visual_tokenizer()
12
13# single-image input
14image_path = '/data/images/example_1.jpg'
15images = [Image.open(image_path)]
16max_partition = 9
17text = 'Describe the image.'
18query = f'<image>\n{text}'
19
20## cot-style input
21# cot_suffix = "Provide a step-by-step solution to the problem, and conclude with 'the answer is' followed by the final solution."
22# image_path = '/data/images/example_1.jpg'
23# images = [Image.open(image_path)]
24# max_partition = 9
25# text = "What's the area of the shape?"
26# query = f'<image>\n{text}\n{cot_suffix}'
27
28## multiple-images input
29# image_paths = [
30# '/data/images/example_1.jpg',
31# '/data/images/example_2.jpg',
32# '/data/images/example_3.jpg'
33# ]
34# images = [Image.open(image_path) for image_path in image_paths]
35# max_partition = 4
36# text = 'Describe each image.'
37# query = '\n'.join([f'Image {i+1}: <image>' for i in range(len(images))]) + '\n' + text
38
39## video input (require `pip install moviepy==1.0.3`)
40# from moviepy.editor import VideoFileClip
41# video_path = '/data/videos/example_1.mp4'
42# num_frames = 12
43# max_partition = 1
44# text = 'Describe the video.'
45# with VideoFileClip(video_path) as clip:
46# total_frames = int(clip.fps * clip.duration)
47# if total_frames <= num_frames:
48# sampled_indices = range(total_frames)
49# else:
50# stride = total_frames / num_frames
51# sampled_indices = [min(total_frames - 1, int((stride * i + stride * (i + 1)) / 2)) for i in range(num_frames)]
52# frames = [clip.get_frame(index / clip.fps) for index in sampled_indices]
53# frames = [Image.fromarray(frame, mode='RGB') for frame in frames]
54# images = frames
55# query = '\n'.join(['<image>'] * len(images)) + '\n' + text
56
57## text-only input
58# images = []
59# max_partition = None
60# text = 'Hello'
61# query = text
62
63# format conversation
64prompt, input_ids, pixel_values = model.preprocess_inputs(query, images, max_partition=max_partition)
65attention_mask = torch.ne(input_ids, text_tokenizer.pad_token_id)
66input_ids = input_ids.unsqueeze(0).to(device=model.device)
67attention_mask = attention_mask.unsqueeze(0).to(device=model.device)
68if pixel_values is not None:
69 pixel_values = pixel_values.to(dtype=visual_tokenizer.dtype, device=visual_tokenizer.device)
70pixel_values = [pixel_values]
71
72# generate output
73with torch.inference_mode():
74 gen_kwargs = dict(
75 max_new_tokens=1024,
76 do_sample=False,
77 top_p=None,
78 top_k=None,
79 temperature=None,
80 repetition_penalty=None,
81 eos_token_id=model.generation_config.eos_token_id,
82 pad_token_id=text_tokenizer.pad_token_id,
83 use_cache=True
84 )
85 output_ids = model.generate(input_ids, pixel_values=pixel_values, attention_mask=attention_mask, **gen_kwargs)[0]
86 output = text_tokenizer.decode(output_ids, skip_special_tokens=True)
87 print(f'Output:\n{output}')1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM
4
5# load model
6model = AutoModelForCausalLM.from_pretrained("AIDC-AI/Ovis2-2B",
7 torch_dtype=torch.bfloat16,
8 multimodal_max_length=32768,
9 trust_remote_code=True).cuda()
10text_tokenizer = model.get_text_tokenizer()
11visual_tokenizer = model.get_visual_tokenizer()
12
13# preprocess inputs
14batch_inputs = [
15 ('/data/images/example_1.jpg', 'What colors dominate the image?'),
16 ('/data/images/example_2.jpg', 'What objects are depicted in this image?'),
17 ('/data/images/example_3.jpg', 'Is there any text in the image?')
18]
19
20batch_input_ids = []
21batch_attention_mask = []
22batch_pixel_values = []
23
24for image_path, text in batch_inputs:
25 image = Image.open(image_path)
26 query = f'<image>\n{text}'
27 prompt, input_ids, pixel_values = model.preprocess_inputs(query, [image], max_partition=9)
28 attention_mask = torch.ne(input_ids, text_tokenizer.pad_token_id)
29 batch_input_ids.append(input_ids.to(device=model.device))
30 batch_attention_mask.append(attention_mask.to(device=model.device))
31 batch_pixel_values.append(pixel_values.to(dtype=visual_tokenizer.dtype, device=visual_tokenizer.device))
32
33batch_input_ids = torch.nn.utils.rnn.pad_sequence([i.flip(dims=[0]) for i in batch_input_ids], batch_first=True,
34 padding_value=0.0).flip(dims=[1])
35batch_input_ids = batch_input_ids[:, -model.config.multimodal_max_length:]
36batch_attention_mask = torch.nn.utils.rnn.pad_sequence([i.flip(dims=[0]) for i in batch_attention_mask],
37 batch_first=True, padding_value=False).flip(dims=[1])
38batch_attention_mask = batch_attention_mask[:, -model.config.multimodal_max_length:]
39
40# generate outputs
41with torch.inference_mode():
42 gen_kwargs = dict(
43 max_new_tokens=1024,
44 do_sample=False,
45 top_p=None,
46 top_k=None,
47 temperature=None,
48 repetition_penalty=None,
49 eos_token_id=model.generation_config.eos_token_id,
50 pad_token_id=text_tokenizer.pad_token_id,
51 use_cache=True
52 )
53 output_ids = model.generate(batch_input_ids, pixel_values=batch_pixel_values, attention_mask=batch_attention_mask,
54 **gen_kwargs)
55
56for i in range(len(batch_inputs)):
57 output = text_tokenizer.decode(output_ids[i], skip_special_tokens=True)
58 print(f'Output {i + 1}:\n{output}\n')@article{lu2024ovis,
title={Ovis: Structural Embedding Alignment for Multimodal Large Language Model},
author={Shiyin Lu and Yang Li and Qing-Guo Chen and Zhao Xu and Weihua Luo and Kaifu Zhang and Han-Jia Ye},
year={2024},
journal={arXiv:2405.20797}
}