使用transformers调用Qwen2-VL-7B-Instruct
安装
pip install transformers
pip install qwen-vl-utils
Demo
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
# default: Load the model on the available device(s)
model = Qwen2VLForConditionalGeneration.from_pretrained(
"Qwen/Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
)
# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
# model = Qwen2VLForConditionalGeneration.from_pretrained(
# "Qwen/Qwen2-VL-7B-Instruct",
# torch_dtype=torch.bfloat16,
# attn_implementation="flash_attention_2",
# device_map="auto",
# )
# default processer
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
# 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.
# min_pixels = 256*28*28
# max_pixels = 1280*28*28
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
},
{"type": "text", "text": "Describe this image."},
],
}
]
# Preparation for inference
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to("cuda")
# Inference: Generation of the output
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
图像分辨率可提高性能
min_pixels = 256 * 28 * 28
max_pixels = 1280 * 28 * 28
processor = AutoProcessor.from_pretrained(
"Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels
)
除此之外,可以直接在message修改图像尺寸或pixel
# resized_height and resized_width
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "file:///path/to/your/image.jpg",
"resized_height": 280,
"resized_width": 420,
},
{"type": "text", "text": "Describe this image."},
],
}
]
# min_pixels and max_pixels
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "file:///path/to/your/image.jpg",
"min_pixels": 50176,
"max_pixels": 50176,
},
{"type": "text", "text": "Describe this image."},
],
}
]
这里的增强逻辑是先在message里指定pixel或者尺寸,
然后使用
image_inputs, video_inputs = process_vision_info(messages)
这行代码调整输入
再使用修改后的processor处理image_inputs
展示不同处理后的图像分辨率
原始的图像分辨率
image_inputs
[<PIL.Image.Image image mode=RGB size=308x168 at 0x7F7F1673BFA0>]
不进行增强,使用默认的图像分辨率
image_inputs
[<PIL.Image.Image image mode=RGB size=308x168 at 0x7F7F1673BFA0>]
inputs['pixel_values'].shape
torch.Size([264, 1176])
仅修改processor
image_inputs
[<PIL.Image.Image image mode=RGB size=308x168 at 0x7F5EBB346F50>]
inputs['pixel_values'].shape
torch.Size([1056, 1176])
修改processor且直接修改尺寸
image_inputs
[<PIL.Image.Image image mode=RGB size=420x280 at 0x7FBBEB1BEF50>]
inputs['pixel_values'].shape
torch.Size([1120, 1176])
修改processor且直接修改pixel
image_inputs
[<PIL.Image.Image image mode=RGB size=280x168 at 0x7F5AF53E6FB0>]
inputs['pixel_values'].shape
torch.Size([1092, 1176])
可以看到第三种inputs[‘pixel_values’].shape最大