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

引子

2024年8月30号,阿里推出Qwen2-VL,开源了2B/7B模型,处理任意分辨率图像无需分割成块。之前写了一篇Qwen-VL的博客,感兴趣的童鞋请移步(Qwen-VL环境搭建&推理测试-CSDN博客),这么小的模型,显然我的机器是跑的起来的,OK,那就让我们开始吧。

一、模型介绍

Qwen2-VL 的一项关键架构改进是实现了动态分辨率支持(Naive Dynamic Resolution support)。与上一代模型 Qwen-VL 不同,Qwen2-VL 可以处理任意分辨率的图像,而无需将其分割成块,从而确保模型输入与图像固有信息之间的一致性。这种方法更接近地模仿人类的视觉感知,使模型能够处理任何清晰度或大小的图像。另一个关键架构增强是 Multimodal Rotary Position Embedding(M-ROPE)。通过将 original rotary embedding 分解为代表时间和空间(高度和宽度)信息的三个部分,M-ROPE 使 LLM 能够同时捕获和集成 1D 文本、2D 视觉和 3D 视频位置信息。这使 LLM 能够充当多模态处理器和推理器。

二、环境搭建

1、模型下载

https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct

2、环境安装

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

git clone GitHub - huggingface/transformers: 🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

cd transformers

pip install .

pip install qwen-vl-utils -i Simple Index

pip install accelerate==0.26.0 -i Simple Index

三、推理测试

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"
# )
model = Qwen2VLForConditionalGeneration.from_pretrained(
    "models", 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")
processor = AutoProcessor.from_pretrained("models")
# 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)

python test.py

在面对资源有限的环境时,Lora微调技术为我们提供了一种高效的模型微调方法,特别适合于边缘计算设备。首先,你需要熟悉Qwen-VL模型的基本结构和工作原理,该模型在视觉语言任务中表现出色,例如在视觉问答或图像字幕生成等应用上。为了进行Lora微调,你可以利用已有的《Qwen-VL模型深度Lora微调实践指南与源码》资源,其中包含了详细的步骤和源码实现,帮助你从零开始搭建环境并进行模型微调。 参考资源链接:[Qwen-VL模型深度Lora微调实践指南与源码](https://wenku.csdn.net/doc/5e1jpjg9dt) 在微调之前,确保你的环境配置符合要求,通常需要安装TensorFlow或PyTorch等深度学习框架,并选择合适版本的GPU服务器进行配置。之后,通过低秩分解权重来更新模型参数,这不仅能够减少计算资源的消耗,也能降低对内存的需求。整个微调过程需要你对模型的权重进行适度的调整,以便更好地适应新的任务。 在微调完成后,你应该评估模型在目标任务上的表现,这可能涉及到在特定的数据集上进行测试。一旦微调效果达到预期,就可以将模型部署到边缘计算设备上。部署过程中,可能需要进行模型压缩和推理加速,确保模型在有限的资源下依然可以高效运行。在此过程中,你可能需要使用一些模型优化工具,例如TensorRT或者ONNX,以提升模型运行效率和减少资源占用。 最后,针对模型部署后的监控和维护工作也是不可忽视的。确保模型在实际运行中稳定可靠,并且符合相关的安全和隐私保护要求。通过以上步骤,你将能够完成从模型微调到部署的全流程,并在边缘计算环境中实现Qwen-VL模型的实际应用。 为了进一步提高你的技能和了解更深入的实践知识,建议在《Qwen-VL模型深度Lora微调实践指南与源码》的基础上,深入学习更多关于边缘计算、模型压缩以及部署优化的内容。 参考资源链接:[Qwen-VL模型深度Lora微调实践指南与源码](https://wenku.csdn.net/doc/5e1jpjg9dt)
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

要养家的程序猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值