Views
No views yet


| Benchmark | InternVL2-2B | MiniCPM-V 2.0 | Qwen2-VL-2B |
|---|---|---|---|
| DocVQAtest | 86.9 | - | 90.1 |
| InfoVQAtest | 58.9 | - | 65.5 |
| ChartQAtest | 76.2 | - | 73.5 |
| TextVQAval | 73.4 | - | 79.7 |
| OCRBench | 781 | 605 | 794 |
| MTVQA | - | - | 20.0 |
| MMMUval | 36.3 | 38.2 | 41.1 |
| RealWorldQA | 57.3 | 55.8 | 62.9 |
| MMEsum | 1876.8 | 1808.6 | 1872.0 |
| MMBench-ENtest | 73.2 | 69.1 | 74.9 |
| MMBench-CNtest | 70.9 | 66.5 | 73.5 |
| MMBench-V1.1test | 69.6 | 65.8 | 72.2 |
| MMT-Benchtest | - | - | 54.5 |
| MMStar | 49.8 | 39.1 | 48.0 |
| MMVetGPT-4-Turbo | 39.7 | 41.0 | 49.5 |
| HallBenchavg | 38.0 | 36.1 | 41.7 |
| MathVistatestmini | 46.0 | 39.8 | 43.0 |
| MathVision | - | - | 12.4 |
| Benchmark | Qwen2-VL-2B |
|---|---|
| MVBench | 63.2 |
| PerceptionTesttest | 53.9 |
| EgoSchematest | 54.9 |
| Video-MMEwo/w subs | 55.6/60.4 |
pip install git+https://github.com/huggingface/transformers, or you might encounter the following error:KeyError: 'qwen2_vl'pip install qwen-vl-utilstransformers and qwen_vl_utils:1from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
2from qwen_vl_utils import process_vision_info
3
4# default: Load the model on the available device(s)
5model = Qwen2VLForConditionalGeneration.from_pretrained(
6 "Qwen/Qwen2-VL-2B-Instruct", torch_dtype="auto", device_map="auto"
7)
8
9# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
10# model = Qwen2VLForConditionalGeneration.from_pretrained(
11# "Qwen/Qwen2-VL-2B-Instruct",
12# torch_dtype=torch.bfloat16,
13# attn_implementation="flash_attention_2",
14# device_map="auto",
15# )
16
17# default processer
18processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
19
20# The default range for the number of visual tokens per image in the model is 4-16384. You can set min_pixels and max_pixels according to your needs, such as a token count range of 256-1280, to balance speed and memory usage.
21# min_pixels = 256*28*28
22# max_pixels = 1280*28*28
23# processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
24
25messages = [
26 {
27 "role": "user",
28 "content": [
29 {
30 "type": "image",
31 "image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
32 },
33 {"type": "text", "text": "Describe this image."},
34 ],
35 }
36]
37
38# Preparation for inference
39text = processor.apply_chat_template(
40 messages, tokenize=False, add_generation_prompt=True
41)
42image_inputs, video_inputs = process_vision_info(messages)
43inputs = processor(
44 text=[text],
45 images=image_inputs,
46 videos=video_inputs,
47 padding=True,
48 return_tensors="pt",
49)
50inputs = inputs.to("cuda")
51
52# Inference: Generation of the output
53generated_ids = model.generate(**inputs, max_new_tokens=128)
54generated_ids_trimmed = [
55 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
56]
57output_text = processor.batch_decode(
58 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
59)
60print(output_text)1from PIL import Image
2import requests
3import torch
4from torchvision import io
5from typing import Dict
6from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
7
8# Load the model in half-precision on the available device(s)
9model = Qwen2VLForConditionalGeneration.from_pretrained(
10 "Qwen/Qwen2-VL-2B-Instruct", torch_dtype="auto", device_map="auto"
11)
12processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
13
14# Image
15url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
16image = Image.open(requests.get(url, stream=True).raw)
17
18conversation = [
19 {
20 "role": "user",
21 "content": [
22 {
23 "type": "image",
24 },
25 {"type": "text", "text": "Describe this image."},
26 ],
27 }
28]
29
30
31# Preprocess the inputs
32text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
33# Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
34
35inputs = processor(
36 text=[text_prompt], images=[image], padding=True, return_tensors="pt"
37)
38inputs = inputs.to("cuda")
39
40# Inference: Generation of the output
41output_ids = model.generate(**inputs, max_new_tokens=128)
42generated_ids = [
43 output_ids[len(input_ids) :]
44 for input_ids, output_ids in zip(inputs.input_ids, output_ids)
45]
46output_text = processor.batch_decode(
47 generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
48)
49print(output_text)1# Messages containing multiple images and a text query
2messages = [
3 {
4 "role": "user",
5 "content": [
6 {"type": "image", "image": "file:///path/to/image1.jpg"},
7 {"type": "image", "image": "file:///path/to/image2.jpg"},
8 {"type": "text", "text": "Identify the similarities between these images."},
9 ],
10 }
11]
12
13# Preparation for inference
14text = processor.apply_chat_template(
15 messages, tokenize=False, add_generation_prompt=True
16)
17image_inputs, video_inputs = process_vision_info(messages)
18inputs = processor(
19 text=[text],
20 images=image_inputs,
21 videos=video_inputs,
22 padding=True,
23 return_tensors="pt",
24)
25inputs = inputs.to("cuda")
26
27# Inference
28generated_ids = model.generate(**inputs, max_new_tokens=128)
29generated_ids_trimmed = [
30 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
31]
32output_text = processor.batch_decode(
33 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
34)
35print(output_text)1# Messages containing a images list as a video and a text query
2messages = [
3 {
4 "role": "user",
5 "content": [
6 {
7 "type": "video",
8 "video": [
9 "file:///path/to/frame1.jpg",
10 "file:///path/to/frame2.jpg",
11 "file:///path/to/frame3.jpg",
12 "file:///path/to/frame4.jpg",
13 ],
14 "fps": 1.0,
15 },
16 {"type": "text", "text": "Describe this video."},
17 ],
18 }
19]
20# Messages containing a video and a text query
21messages = [
22 {
23 "role": "user",
24 "content": [
25 {
26 "type": "video",
27 "video": "file:///path/to/video1.mp4",
28 "max_pixels": 360 * 420,
29 "fps": 1.0,
30 },
31 {"type": "text", "text": "Describe this video."},
32 ],
33 }
34]
35
36# Preparation for inference
37text = processor.apply_chat_template(
38 messages, tokenize=False, add_generation_prompt=True
39)
40image_inputs, video_inputs = process_vision_info(messages)
41inputs = processor(
42 text=[text],
43 images=image_inputs,
44 videos=video_inputs,
45 padding=True,
46 return_tensors="pt",
47)
48inputs = inputs.to("cuda")
49
50# Inference
51generated_ids = model.generate(**inputs, max_new_tokens=128)
52generated_ids_trimmed = [
53 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
54]
55output_text = processor.batch_decode(
56 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
57)
58print(output_text)1# Sample messages for batch inference
2messages1 = [
3 {
4 "role": "user",
5 "content": [
6 {"type": "image", "image": "file:///path/to/image1.jpg"},
7 {"type": "image", "image": "file:///path/to/image2.jpg"},
8 {"type": "text", "text": "What are the common elements in these pictures?"},
9 ],
10 }
11]
12messages2 = [
13 {"role": "system", "content": "You are a helpful assistant."},
14 {"role": "user", "content": "Who are you?"},
15]
16# Combine messages for batch processing
17messages = [messages1, messages1]
18
19# Preparation for batch inference
20texts = [
21 processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
22 for msg in messages
23]
24image_inputs, video_inputs = process_vision_info(messages)
25inputs = processor(
26 text=texts,
27 images=image_inputs,
28 videos=video_inputs,
29 padding=True,
30 return_tensors="pt",
31)
32inputs = inputs.to("cuda")
33
34# Batch Inference
35generated_ids = model.generate(**inputs, max_new_tokens=128)
36generated_ids_trimmed = [
37 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
38]
39output_texts = processor.batch_decode(
40 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
41)
42print(output_texts)1# You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
2## Local file path
3messages = [
4 {
5 "role": "user",
6 "content": [
7 {"type": "image", "image": "file:///path/to/your/image.jpg"},
8 {"type": "text", "text": "Describe this image."},
9 ],
10 }
11]
12## Image URL
13messages = [
14 {
15 "role": "user",
16 "content": [
17 {"type": "image", "image": "http://path/to/your/image.jpg"},
18 {"type": "text", "text": "Describe this image."},
19 ],
20 }
21]
22## Base64 encoded image
23messages = [
24 {
25 "role": "user",
26 "content": [
27 {"type": "image", "image": "data:image;base64,/9j/..."},
28 {"type": "text", "text": "Describe this image."},
29 ],
30 }
31]1min_pixels = 256 * 28 * 28
2max_pixels = 1280 * 28 * 28
3processor = AutoProcessor.from_pretrained(
4 "Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels
5)resized_height and resized_width. These values will be rounded to the nearest multiple of 28.1# min_pixels and max_pixels
2messages = [
3 {
4 "role": "user",
5 "content": [
6 {
7 "type": "image",
8 "image": "file:///path/to/your/image.jpg",
9 "resized_height": 280,
10 "resized_width": 420,
11 },
12 {"type": "text", "text": "Describe this image."},
13 ],
14 }
15]
16# resized_height and resized_width
17messages = [
18 {
19 "role": "user",
20 "content": [
21 {
22 "type": "image",
23 "image": "file:///path/to/your/image.jpg",
24 "min_pixels": 50176,
25 "max_pixels": 50176,
26 },
27 {"type": "text", "text": "Describe this image."},
28 ],
29 }
30]@article{Qwen2-VL,
title={Qwen2-VL},
author={Qwen team},
year={2024}
}
@article{Qwen-VL,
title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
author={Bai, Jinze and Bai, Shuai and Yang, Shusheng and Wang, Shijie and Tan, Sinan and Wang, Peng and Lin, Junyang and Zhou, Chang and Zhou, Jingren},
journal={arXiv preprint arXiv:2308.12966},
year={2023}
}