Qwen2.5-VL环境搭建&推理测试

引子

2025年春节期间,阿里推出Qwen2.5-VL,开源了3B、7B、72B模型,,能够处理图像和文本的联合任务,如图像描述生成、视觉问答(VQA)、图文匹配等。。之前写了一篇Qwen2-VL的博客,感兴趣的童鞋请移步(Qwen2-VL环境搭建&推理测试-CSDN博客),如果对华为卡部署感兴趣的童鞋请移步(Qwen2-VL华为卡300i duo环境搭建&推理测试_ollama支持华为300i吗-CSDN博客),7B模型,显然我的机器是跑的起来的,OK,那就让我们开始吧。

一、模型介绍

Qwen2.5-VL模型不仅在对话、指令跟随、数学、代码等能力上有所提高,还支持坐标、json等返回格式、支持更长(1小时)的视频理解、更细粒度的时间感知、更全面的知识解析能力、具备更强的agent能力来操作手机和电脑。Agent和实时视频交互能力,看了官方几个视频,感觉处理相关任务效果还不错,具体等模型下完之后,体验再评价。

Qwen2.5-VL模型,在视觉编码器部分是原生训练的支持动态分辨率的ViT。同时在空间维度引入大量检测框和点等坐标,让模型理解空间的尺寸;

在时间维度引入动态FPS和绝对时间编码,使mRoPE的ids与时间快慢进行对齐,让模型理解时间的流速。

二、环境搭建

1、模型下载

modelscope download --model Qwen/Qwen2.5-VL-7B-Instruct --local_dir ./

2、环境安装

docker run -it --rm --gpus=all -v /datas/work/zzq:/workspace pytorch/pytorch:2.4.0-cuda12.4-cudnn9-devel bash

git clone https://github.com/QwenLM/Qwen2.5-VL.git

cd /workspace/Qwen2.5-VL/Qwen2.5-VL-main

pip install -r requirements_web_demo.txt -i Simple Index

三、推理测试

ffrom transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info

# default: Load the model on the available device(s)
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
    "Qwen/Qwen2.5-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 = Qwen2_5_VLForConditionalGeneration.from_pretrained(
#     "Qwen/Qwen2.5-VL-7B-Instruct",
#     torch_dtype=torch.bfloat16,
#     attn_implementation="flash_attention_2",
#     device_map="auto",
# )

# default processor
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-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.5-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(model.device)

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

### 设置和使用Qwen2.5-VL模型进行本地推理 #### 准备工作环境 为了在本地环境中成功运行Qwen2.5-VL模型,需先确保计算机满足最低硬件需求并已安装必要的软件包。对于Windows操作系统而言,在开始之前应当确认Python版本兼容性,并创建虚拟环境来隔离项目依赖项。 #### 安装必要组件 进入Qwen2.5-VL项目的根目录后执行命令以安装所需的库文件: ```bash cd Qwen2.5-VL pip install -r requirements_web_demo.txt ``` 此操作会依据`requirements_web_demo.txt`列表下载并配置所有必需的第三方模块[^3]。 #### 加载预训练模型 完成上述准备工作之后,可以通过加载官方发布的预训练权重来进行初始化。通常情况下,这一步骤涉及指定模型架构参数以及对应的checkpoint位置。具体实现方式取决于所使用的编程接口或框架文档说明。 #### 进行推理测试 当一切准备就绪时,可以编写简单的脚本来调用API函数处理输入数据(如图片),进而获取预测结果。下面给出一段基于PyTorch框架下的伪代码示例用于演示目的: ```python from transformers import AutoModelForVision2Seq, AutoProcessor processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL") model = AutoModelForVision2Seq.from_pretrained("Qwen/Qwen2.5-VL") def predict(image_path): image = Image.open(image_path).convert('RGB') inputs = processor(images=image, return_tensors="pt") with torch.no_grad(): outputs = model.generate(**inputs) generated_text = processor.decode(outputs[0], skip_special_tokens=True) return generated_text ``` 这段代码展示了如何利用Hugging Face Transformers库快速搭建起一个多模态应用原型,其中包含了从读取图像到最终输出描述文字的过程[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

要养家的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值