阿里通义千问开源Qwen2.5系列模型:Qwen2-VL-72B媲美GPT-4

Qwen2.5系列模型及Qwen2-VL新功能介绍

通义千问团队宣布,继Qwen2发布三个月后,Qwen家族的最新成员——Qwen2.5系列语言模型正式开源。这标志着可能是历史上最大规模的开源发布之一,包括了通用语言模型Qwen2.5,以及专门针对编程和数学领域的Qwen2.5-Coder和Qwen2.5-Math模型。

Qwen2.5系列模型在最新的大规模数据集上进行了预训练,数据集包含高达18T tokens,相较于Qwen2,新模型在知识获取、编程能力和数学能力方面均有显著提升。模型支持长文本处理,能够生成最多8K tokens的内容,并保持了对29种以上语言的支持。

在这里插入图片描述
在这里插入图片描述
新模型在指令执行、长文本生成、结构化数据理解以及生成结构化输出方面取得了显著改进。特别是在编程和数学领域,Qwen2.5-Coder和Qwen2.5-Math模型在专业数据集上进行了训练,展现了更强的专业领域能力。

Qwen2-VL 有哪些新功能?

主要增强功能:

  • SoTA 可理解各种分辨率和比例的图像:Qwen2-VL 在视觉理解基准测试(包括 MathVista、DocVQA、RealWorldQA、MTVQA 等)中取得了一流的性能。

  • 可理解 20 分钟以上的视频:Qwen2-VL 可理解 20 分钟以上的视频,用于基于视频的高质量问题解答、对话、内容创建等。

  • 可操作手机、机器人等的代理:Qwen2-VL 具有复杂的推理和决策能力,可与手机、机器人等设备集成,根据视觉环境和文本指令进行自动操作。

  • 多语言支持:为了服务全球用户,除了英文和中文,Qwen2-VL 现在还支持理解图像中的不同语言文本,包括大多数欧洲语言、日语、韩语、阿拉伯语、越南语等。

模型架构更新:

  • 自然动态分辨率:与以往不同的是,Qwen2-VL 可以处理任意图像分辨率,并将其映射为动态的视觉标记数,从而提供更接近人类的视觉处理体验。

    在这里插入图片描述

  • 多模态旋转位置嵌入(M-ROPE):将位置嵌入分解为多个部分,以捕捉一维文本、二维视觉和三维视频位置信息,从而增强其多模态处理能力。 在这里插入图片描述

图像基准

BenchmarkPrevious SoTA
(Open-source LVLM)
Claude-3.5 SonnetGPT-4oQwen2-VL-72B
MMMUval58.368.369.164.5
DocVQAtest94.195.292.896.5
InfoVQAtest82.0--84.5
ChartQAtest88.490.885.788.3
TextVQAval84.4--85.5
OCRBench852788736877
MTVQA17.325.727.830.9
VCRen easy84.6763.8591.5591.93
VCRzh easy22.091.014.8765.37
RealWorldQA72.260.175.477.8
MMEsum2414.71920.02328.72482.7
MMBench-ENtest86.579.783.486.5
MMBench-CNtest86.380.782.186.6
MMBench-V1.1test85.578.582.285.9
MMT-Benchtest63.4-65.571.7
MMStar67.162.263.968.3
MMVetGPT-4-Turbo65.766.069.174.0
HallBenchavg55.249.955.058.1
MathVistatestmini67.567.763.870.5
MathVision16.97-30.425.9

视频基准

BenchmarkPrevious SoTA
(Open-source LVLM)
Gemini 1.5-ProGPT-4oQwen2-VL-72B
MVBench69.6--73.6
PerceptionTesttest66.9--68.0
EgoSchematest62.063.272.277.9
Video-MME
(wo/w subs)
66.3/69.675.0/81.371.9/77.271.2/77.8

智能体基准

BenchmarkMetricPrevious SoTAGPT-4oQwen2-VL-72B
GeneralFnCall[1]TM-90.293.1
EM-50.053.2
GameNumber LineSR89.4[2]91.5100.0
BlackJackSR40.2[2]34.542.6
EZPointSR50.0[2]85.5100.0
Point24SR2.6[2]3.04.5
AndroidAITZTM83.0[3]70.089.6
EM47.7[3]35.372.1
AI2THORALFREDvalid-unseenSR67.7[4]-67.8
GC75.3[4]-75.8
VLNR2Rvalid-unseenSR79.043.7[5]51.7
REVERIEvalid-unseenSR61.031.6[5]31.0

SR、GC、TM 和 EM 是成功率、目标条件成功率、类型匹配和精确匹配的简称。SAM[6] 支持 ALFRED。

  1. Qwen 团队自编函数调用基准测试
  2. 通过强化学习微调作为决策代理的大型视觉语言模型
  3. 动物园中的 Android:图形用户界面代理的行动思维链
  4. ThinkBot:利用思维链推理进行嵌入式指令跟踪
  5. MapGPT:地图引导提示与视觉语言导航的自适应路径规划
  6. 任意分段

多语言基准

ModelsAR DE FR IT JA KO RU TH VI AVG
Qwen2-VL-72B20.7 36.5 44.1 42.8 21.6 37.4 15.6 17.7 41.6 30.9
GPT-4o20.2 34.2 41.2 32.7 20.0 33.9 11.5 22.5 34.2 27.8
Claude3 Opus15.1 33.4 40.6 34.4 19.4 27.2 13.0 19.5 29.1 25.7
Gemini Ultra14.7 32.3 40.0 31.8 12.3 17.2 11.8 20.3 28.6 23.2

Quickstart

我们提供了一个工具包,帮助您更方便地处理各种类型的视觉输入。其中包括 base64、URL 以及交错图片和视频。您可以使用以下命令安装它:

pip install qwen-vl-utils

下面我们将展示一个代码片段,告诉您如何使用transformersqwen_vl_utils 来使用聊天模型:

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-72B-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-72B-Instruct",
#     torch_dtype=torch.bfloat16,
#     attn_implementation="flash_attention_2",
#     device_map="auto",
# )

# default processer
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-72B-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-72B-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)

无 qwen_vl_utils:

from PIL import Image
import requests
import torch
from torchvision import io
from typing import Dict
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor

# Load the model in half-precision on the available device(s)
model = Qwen2VLForConditionalGeneration.from_pretrained(
    "Qwen/Qwen2-VL-72B-Instruct", torch_dtype="auto", device_map="auto"
)
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-72B-Instruct")

# Image
url = "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg"
image = Image.open(requests.get(url, stream=True).raw)

conversation = [
    {
        "role": "user",
        "content": [
            {
                "type": "image",
            },
            {"type": "text", "text": "Describe this image."},
        ],
    }
]


# Preprocess the inputs
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
# 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'

inputs = processor(
    text=[text_prompt], images=[image], padding=True, return_tensors="pt"
)
inputs = inputs.to("cuda")

# Inference: Generation of the output
output_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids = [
    output_ids[len(input_ids) :]
    for input_ids, output_ids in zip(inputs.input_ids, output_ids)
]
output_text = processor.batch_decode(
    generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
)
print(output_text)

多图像推理

# Messages containing multiple images and a text query
messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": "file:///path/to/image1.jpg"},
            {"type": "image", "image": "file:///path/to/image2.jpg"},
            {"type": "text", "text": "Identify the similarities between these images."},
        ],
    }
]

# 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
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)

视频推理

# Messages containing a images list as a video and a text query
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "video",
                "video": [
                    "file:///path/to/frame1.jpg",
                    "file:///path/to/frame2.jpg",
                    "file:///path/to/frame3.jpg",
                    "file:///path/to/frame4.jpg",
                ],
                "fps": 1.0,
            },
            {"type": "text", "text": "Describe this video."},
        ],
    }
]
# Messages containing a video and a text query
messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "video",
                "video": "file:///path/to/video1.mp4",
                "max_pixels": 360 * 420,
                "fps": 1.0,
            },
            {"type": "text", "text": "Describe this video."},
        ],
    }
]

# 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
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)

批量推理

# Sample messages for batch inference
messages1 = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": "file:///path/to/image1.jpg"},
            {"type": "image", "image": "file:///path/to/image2.jpg"},
            {"type": "text", "text": "What are the common elements in these pictures?"},
        ],
    }
]
messages2 = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who are you?"},
]
# Combine messages for batch processing
messages = [messages1, messages1]

# Preparation for batch inference
texts = [
    processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
    for msg in messages
]
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=texts,
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt",
)
inputs = inputs.to("cuda")

# Batch Inference
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_texts = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_texts)

更多使用技巧

对于输入图片,我们支持本地文件、base64 和 URL。对于视频,我们目前只支持本地文件。

# You can directly insert a local file path, a URL, or a base64-encoded image into the position where you want in the text.
## Local file path
messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": "file:///path/to/your/image.jpg"},
            {"type": "text", "text": "Describe this image."},
        ],
    }
]
## Image URL
messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": "http://path/to/your/image.jpg"},
            {"type": "text", "text": "Describe this image."},
        ],
    }
]
## Base64 encoded image
messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "image": "data:image;base64,/9j/..."},
            {"type": "text", "text": "Describe this image."},
        ],
    }
]

提升性能的图像分辨率

该模型支持多种分辨率输入。默认情况下,它使用本机分辨率进行输入,但更高的分辨率会以更多计算量为代价提高性能。用户可以设置最小和最大像素数,以达到最佳配置,满足自己的需求,例如令牌数范围为 256-1280,以平衡速度和内存使用。

min_pixels = 256 * 28 * 28
max_pixels = 1280 * 28 * 28
processor = AutoProcessor.from_pretrained(
    "Qwen/Qwen2-VL-72B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels
)

此外,我们还提供了两种方法,可对模型输入的图像尺寸进行精细控制:

定义 min_pixels 和 max_pixels:图像将在 min_pixels 和 max_pixels 的范围内调整大小,以保持其纵横比。

指定精确尺寸:直接设置 resized_height 和 resized_width。这些值将四舍五入为最接近的 28 的倍数。
# min_pixels and max_pixels
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."},
        ],
    }
]
# resized_height and resized_width
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."},
        ],
    }
]

Qwen 2.5 VL
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen2.5-72B-Instruct"

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)

prompt = "Give me a short introduction to large language model."
messages = [
    {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=512
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]

response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]

局限性

虽然 Qwen2-VL 适用于多种视觉任务,但了解其局限性也同样重要。以下是一些已知的限制:

  • 缺乏音频支持:当前模式无法理解视频中的音频信息。
  • 数据时效性:我们的图像数据集更新至 2023 年 6 月,此后的信息可能无法覆盖。
  • 个人和知识产权 (IP) 的限制:该模型识别特定个人或知识产权的能力有限,可能无法全面覆盖所有知名人士或品牌。
  • 处理复杂指令的能力有限:面对复杂的多步骤指令,模型的理解和执行能力需要加强。
  • 计数精度不足:特别是在复杂场景中,物体计数的准确性不高,需要进一步改进。
  • 空间推理能力较弱:特别是在三维空间中,模型对物体位置关系的推理能力不足,难以精确判断物体的相对位置。

这些限制是模型优化和改进的持续方向,我们致力于不断提高模型的性能和应用范围。

### 部署阿里Qwen2.5-VL-3B-Instruct 的指南 部署阿里Qwen2.5-VL-3B-Instruct 模型可以通过多种方式实现,包括本地部署、Docker 容器化部署以及使用云服务 API。以下是关于这些方法的具体说明: #### 1. **本地部署** 在本地环境中部署 Qwen2.5-VL-3B-Instruct 模型需要准备以下内容: - 确保安装了必要的依赖库,例如 PyTorch 或 TensorFlow。 - 下载模型权重文件 `Qwen2.5-VL-3B-Instruct` 并将其存储在本地目录中[^1]。 - 使用 Python 脚本加载模型并进行推理。以下是一个简单的代码示例: ```python from transformers import AutoTokenizer, AutoModelForCausalLM # 加载 tokenizer 和模型 tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct") model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-VL-3B-Instruct") # 输入文本 input_text = "请生成一段关于人工智能的描述:" inputs = tokenizer(input_text, return_tensors="pt") # 模型推理 outputs = model.generate(**inputs, max_length=100) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` #### 2. **Docker 容器化部署** 如果希望通过 Docker 进行容器化部署,可以参考以下步骤: - 拉取官方提供的 Docker 镜像或自行构建镜像[^2]。 - 使用以下命令运行容器: ```bash docker pull registry.openanolis.cn/openanolis/pytorch-amd:1.13.1-23-zendnn4.1 docker run -d -it --net host -v $HOME:/root qwen-vl-chat-container ``` - 在容器内部加载模型权重并启动服务。 #### 3. **云服务调用** 对于不想进行本地安装的用户,可以直接通过阿里云百炼平台的 API 调用 Qwen2.5-VL-3B-Instruct 模型[^2]。具体步骤如下: - 注册阿里云账号并开通百炼平台服务。 - 获取 API 密钥并根据文档发送请求。 - 请求示例(使用 curl 命令): ```bash curl -X POST https://api.bailian.aliyun.com/invoke \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -d '{ "model": "Qwen2.5-VL-3B-Instruct", "prompt": "请生成一段关于人工智能的描述:", "max_tokens": 100 }' ``` #### 4. **离线环境部署** 在离线环境中部署 Qwen2.5-VL-3B-Instruct 模型时,可以参考以下步骤[^3]: -模型权重文件上传到服务器,并确保路径正确。 - 修改配置文件以适配离线环境。 - 示例代码片段: ```python import torch from transformers import AutoTokenizer, AutoModelForCausalLM # 加载本地模型 model_path = "/path/to/Qwen2.5-VL-3B-Instruct" tokenizer = AutoTokenizer.from_pretrained(model_path) model = AutoModelForCausalLM.from_pretrained(model_path) # 推理过程 input_text = "请生成一段关于人工智能的描述:" inputs = tokenizer(input_text, return_tensors="pt") outputs = model.generate(**inputs, max_length=100) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` ### 注意事项 - 确保硬件资源充足,尤其是 GPU 内存[^4]。 - 如果使用 Docker 部署,请注意挂载正确的数据卷。 - 对于云服务调用,需关注费用和 Token 使用情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值