1. 强大的视觉理解能力
-
任意分辨率图像识别:Qwen2-VL 可以读懂不同分辨率和不同长宽比的图片,无论图像的清晰度或大小如何,都能轻松识别。这得益于其独特的 naive dynamic resolution 支持,能够将任意分辨率的图像映射成动态数量的视觉 token,保证了模型输入和图像信息的一致性,模拟了人类视觉感知的自然方式。 -
长视频理解:该模型能够理解超过 20 分钟的长视频,这在多模态模型中是一项重大的突破。通过在线流媒体能力,它可以支持高质量的视频问答、对话和内容创作等应用,为视频领域的智能化发展提供了有力的支持。
2. 多语言支持
Qwen2-VL 除了支持英语和中文外,还能理解图像视频中的多种语言文本,包括大多数欧洲语言、日语、韩语、阿拉伯语、越南语等,真正做到了面向全球用户,打破了语言的障碍,为多语言环境下的应用提供了便利。
3. 视觉智能体能力
Qwen2-VL 具备强大的视觉智能体能力,可自主操作手机和机器人等设备。借助其复杂的推理和决策能力,能够根据视觉环境和文字指令进行自动操作,实现了人工智能与现实世界的更紧密结合,为智能家居、智能机器人等领域的发展带来了新的机遇。
二、模型架构
1. 模型结构
Qwen2-VL 延续了上一代 Qwen-VL 中 ViT 加 Qwen2 的串联结构,三个不同规模的模型都采用了 600M 规模大小的 ViT,支持图像和视频统一输入。这种结构使得模型能够更好地融合视觉和语言信息,提高对多模态数据的理解能力。

2. 多模态旋转位置编码(M-ROPE)
传统的旋转位置嵌入只能捕捉一维序列的位置信息,而 Qwen2-VL 采用的 M-ROPE 将旋转位置编码分解成时间、空间(高度和宽度)三部分,使大规模语言模型能够同时捕捉和整合一维文本、二维视觉和三维视频的位置信息,赋予了模型强大的多模态处理和推理能力,能够更好地理解和建模复杂的多模态数据。

三、模型性能
1. 基准测试成绩优异
2. 高效的计算效率
在保证高性能的同时,Qwen2-VL 还具有较高的计算效率,能够在不同的硬件平台上快速运行,为大规模应用提供了可能。其量化版本的发布,进一步提高了模型的计算效率,降低了部署成本。

四、模型体验

五、模型下载
此次 Qwen2 – VL 进行了开源,其中包含两个尺寸的模型,分别是 Qwen2 – VL – 2B – Instruct 以及 Qwen2 – VL – 7B – Instruct,同时还提供了其 GPTQ 和 AWQ 的量化版本。
modelscope download --model=qwen/Qwen2-VL-7B-Instruct --local_dir ./Qwen2-VL-7B-Instruct

六、模型推理
1. 安装依赖
pip install git+https://github.com/huggingface/transformers
pip install qwen-vl-utils

2. 模型推理
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(
# "./Qwen2-VL-7B-Instruct", torch_dtype="auto", device_map="auto"
# )
import torch
model = Qwen2VLForConditionalGeneration.from_pretrained(
"./Qwen2-VL-7B-Instruct",
torch_dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
).to("cuda:0")
# 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("./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 range of 256-1280, to balance performance and cost.
# 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)
七、模型微调
1. 安装依赖
swift开源地址:https://github.com/modelscope/swift
git clone https://github.com/modelscope/swift.gitcd swiftpip install -e .[llm]pip install pyav qwen_vl_utils
2. 模型微调
# 默认会将lora_target_modules设置为llm的所有linearCUDA_VISIBLE_DEVICES=0,1,2,3 NPROC_PER_NODE=4 swift sft --model_type qwen2-vl-7b-instruct --model_id_or_path qwen/Qwen2-VL-7B-Instruct --sft_type lora --dataset coco-en-mini#20000 --deepspeed default-zero2
--dataset train.jsonl --val_dataset val.jsonl
{"query": "<image>55555", "response": "66666", "images": ["image_path"]}{"query": "eeeee<image>eeeee<image>eeeee", "response": "fffff", "history": [], "images": ["image_path1", "image_path2"]}{"query": "EEEEE", "response": "FFFFF", "history": [["query1", "response2"
CUDA_VISIBLE_DEVICES=0 swift infer --ckpt_dir output/qwen2-vl-7b-instruct/vx-xxx/checkpoint-xxx --load_dataset_config true --merge_lora true
八、结语
阿里通义千问的 Qwen2-VL 是一款具有强大功能和优异性能的视觉语言模型,它的发布为多模态技术的发展带来了新的机遇。无论是在视觉理解能力、多语言支持还是视觉智能体能力方面,Qwen2-VL 都表现出了卓越的性能,为各种应用场景的智能化发展提供了有力的支持。随着技术的不断发展和应用场景的不断拓展,相信 Qwen2-VL 将在未来发挥更加重要的作用。