Qwen2-VL 视觉大模型 快速 Qwen2-VL-7B-Instruct部署


b站视频: https://www.bilibili.com/video/BV12JrXYNE3j/

1 环境

再AutoDL上进行快速部署

基础镜像选择
在这里插入图片描述
在这里插入图片描述

2 项目代码与安装

https://github.com/QwenLM/Qwen2-VL

git clone https://github.com/QwenLM/Qwen2-VL
cd Qwen2-VL
pip install qwen-vl-utils[decord]
pip install transformers
pip install 'accelerate>=0.26.0'

3 模型下载

模型地址

https://www.modelscope.cn/Qwen/Qwen2-VL-7B-Instruct

pip install modelscope

采用SDK方式下载

#模型下载(这一步需要记录下载的位置)
from modelscope import snapshot_download
model_dir = snapshot_download('Qwen/Qwen2-VL-7B-Instruct')

在这里插入图片描述

移动模型

mv /root/.cache/modelscope/hub/Qwen/Qwen2-VL-7B-Instruct /root/Qwen

在这里插入图片描述

4 运行Qwen2-VL

在这里插入图片描述
执行脚本:

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

5 Web UI 例子

搭建Qwen2-VL web版本的例子
在这里插入图片描述
执行脚本:
web_demo_mm.py

有两处需要修改:
第一处:DEFAULT_CKPT_PATH = ‘/root/Qwen/Qwen2-VL-7B-Instruct’
修改模型加载的路径
在这里插入图片描述
第二处:
修改web ui 的端口号
parser.add_argument(‘–server-port’, type=int, default=6006, help=‘Demo server port.’)
在这里插入图片描述
然后直接执行:

python web_demo_mm.py

在这里插入图片描述

其他


自己的机器搭建:

环境与代码模型需求:

环境:
PyTorch  2.3.0
CUDA 12.1

PyTorch / 2.3.0 / 3.12(ubuntu22.04) / 12.1

conda create --name Qwen python=3.12

pip install torchvision==0.20.0 -f https://download.pytorch.org/whl/torch_stable.html

或者
pip install torch==2.3.0+cu121 torchvision==0.20.0+cu124 -f https://download.pytorch.org/whl/torch_stable.html


conda create --name Qwen python=3.12


代码:
https://github.com/QwenLM/Qwen2-VL

下载后安装:
cd Qwen2-VL
pip install qwen-vl-utils[decord]
pip install transformers
pip install 'accelerate>=0.26.0'

pip install modelscope


模型下载脚本(这一步需要记录下载的位置):
from modelscope import snapshot_download
model_dir = snapshot_download('Qwen/Qwen2-VL-7B-Instruct')

开始

启动环境

conda activate Qwen
### Qwen2-7B-Instruct-AWQ与vllm的使用说明 #### 下载文档 对于希望利用`Qwen2-VL-7B-Instruct-AWQ`模型并借助`vllm`工具来提升性能的研究者或开发者而言,获取必要的资源是第一步。可以从指定的项目地址下载所需材料[^1]。 #### 安装配置环境 安装过程中涉及创建适合运行该模型及其优化器`vllm`的工作环境。通常建议通过Docker容器化技术简化这一过程,确保不同操作系统上的兼容性和一致性[^2]。具体操作如下: ```bash docker pull registry.gitcode.com/hf_mirrors/ai-gitcode/qwen2-vl-7b-instruct:latest docker run -it --gpus all -p 8000:8000 qwen2-vl-7b-instruct bash ``` 上述命令用于拉取最新的镜像文件,并启动带有GPU支持的服务端口映射到本地8000端口上。 #### 升级vllm包 为了保持最佳实践状态以及获得最新特性,在实际部署前应当确认已安装版本是最新的。可以通过pip工具轻松完成更新动作[^3]: ```bash pip install --upgrade vllm ``` 此指令会自动处理依赖关系并将软件包升级至最高稳定版。 #### 示例代码展示 下面给出一段简单的Python脚本作为实例,展示了如何加载预训练好的`Qwen2-VL-7B-Instruct-AWQ`模型并通过`vllm`执行推理任务: ```python from transformers import AutoModelForCausalLM, AutoTokenizer import torch tokenizer = AutoTokenizer.from_pretrained("qwen2-vl-7b-instruct-awq") model = AutoModelForCausalLM.from_pretrained("qwen2-vl-7b-instruct-awq", device_map="auto") input_text = "描述一张美丽的风景画" inputs = tokenizer(input_text, return_tensors="pt").to('cuda') outputs = model.generate(**inputs) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` 这段程序实现了从输入提示词到生成对应描述的过程,其中包含了调用CUDA加速计算的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CSPhD-winston-杨帆

给我饭钱

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

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

打赏作者

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

抵扣说明:

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

余额充值