Skip to content

Commit 303b735

Browse files
authored
Add Qwen2-VL infer codes
Add Qwen2-VL infer codes
2 parents 489e450 + 8883336 commit 303b735

File tree

9 files changed

+2721
-1
lines changed

9 files changed

+2721
-1
lines changed

paddlemix/examples/qwen2_vl/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Qwen2-VL
2+
3+
## 1. 模型介绍
4+
5+
[Qwen2-VL](https://qwenlm.github.io/blog/qwen2-vl/) 是大规模视觉语言模型。可以以图像、文本、检测框、视频作为输入,并以文本和检测框作为输出。
6+
本仓库提供paddle版本的Qwen2-VL-2B-Instruct和Qwen2-VL-7B-Instruct模型。
7+
8+
9+
## 2 环境准备
10+
- **python >= 3.10**
11+
- tiktoken
12+
> 注:tiktoken 要求python >= 3.8
13+
- paddlepaddle-gpu >= 2.6.1
14+
- paddlenlp >= 3.0.0
15+
16+
> 注:请确保安装了以上依赖,否则无法运行。同时,需要安装 paddlemix/external_ops 下的自定义OP, `python setup.py install`。如果安装后仍然找不到算子,需要额外设置PYTHONPATH
17+
18+
## 3 快速开始
19+
20+
### a. 单图预测
21+
```bash
22+
python paddlemix/examples/qwen2_vl/single_image_infer.py
23+
```
24+
25+
### b. 多图预测
26+
```bash
27+
python paddlemix/examples/qwen2_vl/multi_image_infer.py
28+
```
29+
30+
### c. 视频预测
31+
```bash
32+
python paddlemix/examples/qwen2_vl/video_infer.py
33+
```
34+
35+
## 参考文献
36+
```BibTeX
37+
@article{Qwen2-VL,
38+
title={Qwen2-VL},
39+
author={Qwen team},
40+
year={2024}
41+
}
42+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from paddlenlp.transformers import Qwen2Tokenizer
16+
17+
from paddlemix.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLForConditionalGeneration
18+
from paddlemix.processors.qwen2_vl_processing import (
19+
Qwen2VLImageProcessor,
20+
Qwen2VLProcessor,
21+
process_vision_info,
22+
)
23+
24+
MODEL_NAME = "Qwen/Qwen2-VL-2B-Instruct"
25+
model = Qwen2VLForConditionalGeneration.from_pretrained(MODEL_NAME, dtype="bfloat16")
26+
27+
image_processor = Qwen2VLImageProcessor.from_pretrained(MODEL_NAME)
28+
tokenizer = Qwen2Tokenizer.from_pretrained(MODEL_NAME)
29+
processor = Qwen2VLProcessor(image_processor, tokenizer)
30+
31+
# min_pixels = 256*28*28 # 200704
32+
# max_pixels = 1280*28*28 # 1003520
33+
# processor = Qwen2VLProcessor(image_processor, tokenizer, min_pixels=min_pixels, max_pixels=max_pixels)
34+
35+
36+
messages = [
37+
{
38+
"role": "user",
39+
"content": [
40+
{"type": "image", "image": "./image1.jpg"},
41+
{"type": "image", "image": "./image2.jpg"},
42+
{"type": "text", "text": "Identify the similarities between these images."},
43+
],
44+
}
45+
]
46+
47+
# Preparation for inference
48+
image_inputs, video_inputs = process_vision_info(messages)
49+
50+
question = "Identify the similarities between these images."
51+
image_pad_tokens = '<|vision_start|><|image_pad|><|vision_end|>' * len(image_inputs)
52+
text = f'<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{image_pad_tokens}{question}<|im_end|>\n<|im_start|>assistant\n'
53+
54+
inputs = processor(
55+
text=[text],
56+
images=image_inputs,
57+
videos=video_inputs,
58+
padding=True,
59+
return_tensors="pd",
60+
)
61+
62+
# Inference: Generation of the output
63+
generated_ids = model.generate(**inputs, max_new_tokens=128) # already trimmed in paddle
64+
output_text = processor.batch_decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
65+
print("output_text:\n", output_text[0])
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from paddlenlp.transformers import Qwen2Tokenizer
16+
17+
from paddlemix.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLForConditionalGeneration
18+
from paddlemix.processors.qwen2_vl_processing import (
19+
Qwen2VLImageProcessor,
20+
Qwen2VLProcessor,
21+
process_vision_info,
22+
)
23+
24+
MODEL_NAME = "Qwen/Qwen2-VL-2B-Instruct"
25+
model = Qwen2VLForConditionalGeneration.from_pretrained(MODEL_NAME, dtype="bfloat16")
26+
27+
image_processor = Qwen2VLImageProcessor.from_pretrained(MODEL_NAME)
28+
tokenizer = Qwen2Tokenizer.from_pretrained(MODEL_NAME)
29+
processor = Qwen2VLProcessor(image_processor, tokenizer)
30+
31+
# min_pixels = 256*28*28 # 200704
32+
# max_pixels = 1280*28*28 # 1003520
33+
# processor = Qwen2VLProcessor(image_processor, tokenizer, min_pixels=min_pixels, max_pixels=max_pixels)
34+
35+
36+
messages = [
37+
{
38+
"role": "user",
39+
"content": [
40+
{
41+
"type": "image",
42+
"image": "./image1.jpg",
43+
},
44+
{"type": "text", "text": "Describe this image."},
45+
],
46+
}
47+
]
48+
49+
# Preparation for inference
50+
image_inputs, video_inputs = process_vision_info(messages)
51+
52+
question = "Describe this image."
53+
image_pad_token = '<|vision_start|><|image_pad|><|vision_end|>'
54+
text = f'<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{image_pad_token}{question}<|im_end|>\n<|im_start|>assistant\n'
55+
56+
inputs = processor(
57+
text=[text],
58+
images=image_inputs,
59+
videos=video_inputs,
60+
padding=True,
61+
return_tensors="pd",
62+
)
63+
64+
# Inference: Generation of the output
65+
generated_ids = model.generate(**inputs, max_new_tokens=128) # already trimmed in paddle
66+
output_text = processor.batch_decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
67+
print("output_text:\n", output_text[0])
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from paddlenlp.transformers import Qwen2Tokenizer
16+
17+
from paddlemix.models.qwen2_vl.modeling_qwen2_vl import Qwen2VLForConditionalGeneration
18+
from paddlemix.processors.qwen2_vl_processing import (
19+
Qwen2VLImageProcessor,
20+
Qwen2VLProcessor,
21+
process_vision_info,
22+
)
23+
24+
MODEL_NAME = "Qwen/Qwen2-VL-2B-Instruct"
25+
model = Qwen2VLForConditionalGeneration.from_pretrained(MODEL_NAME, dtype="bfloat16")
26+
27+
image_processor = Qwen2VLImageProcessor.from_pretrained(MODEL_NAME)
28+
tokenizer = Qwen2Tokenizer.from_pretrained(MODEL_NAME)
29+
processor = Qwen2VLProcessor(image_processor, tokenizer)
30+
31+
# min_pixels = 256*28*28 # 200704
32+
# max_pixels = 1280*28*28 # 1003520
33+
# processor = Qwen2VLProcessor(image_processor, tokenizer, min_pixels=min_pixels, max_pixels=max_pixels)
34+
35+
36+
# # Messages containing a images list as a video and a text query
37+
# messages = [
38+
# {
39+
# "role": "user",
40+
# "content": [
41+
# {
42+
# "type": "video",
43+
# "video": [
44+
# "file:///path/to/frame1.jpg",
45+
# "file:///path/to/frame2.jpg",
46+
# "file:///path/to/frame3.jpg",
47+
# "file:///path/to/frame4.jpg",
48+
# ],
49+
# "fps": 1.0,
50+
# },
51+
# {"type": "text", "text": "Describe this video."},
52+
# ],
53+
# }
54+
# ]
55+
56+
57+
# Messages containing a video and a text query
58+
messages = [
59+
{
60+
"role": "user",
61+
"content": [
62+
{
63+
"type": "video",
64+
"video": "./video1.mp4",
65+
"max_pixels": 360 * 420,
66+
"fps": 1.0,
67+
},
68+
{"type": "text", "text": "Describe this video."},
69+
],
70+
}
71+
]
72+
73+
74+
# Preparation for inference
75+
image_inputs, video_inputs = process_vision_info(messages)
76+
77+
question = "Describe this video."
78+
video_pad_token = '<|vision_start|><|video_pad|><|vision_end|>'
79+
text = f'<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{video_pad_token}{question}<|im_end|>\n<|im_start|>assistant\n'
80+
81+
82+
inputs = processor(
83+
text=[text],
84+
images=image_inputs,
85+
videos=video_inputs,
86+
padding=True,
87+
return_tensors="pd",
88+
)
89+
90+
# Inference: Generation of the output
91+
generated_ids = model.generate(**inputs, max_new_tokens=128) # already trimmed in paddle
92+
print("generated_ids:\n", generated_ids)
93+
output_text = processor.batch_decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False)
94+
print("output_text:\n", output_text[0])

paddlemix/models/qwen2_vl/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from .configuration_qwen2_vl import Qwen2VLConfig
16+
from .modeling_qwen2_vl import (
17+
Qwen2VLForConditionalGeneration,
18+
Qwen2VLModel,
19+
Qwen2VLPreTrainedModel,
20+
)
21+
22+
__all__ = ["Qwen2VLConfig", "Qwen2VLForConditionalGeneration", "Qwen2VLModel", "Qwen2VLPreTrainedModel"]

0 commit comments

Comments
 (0)