首页 > 其他分享 >Qwen-VL环境搭建&推理测试

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

时间:2024-05-13 12:11:29浏览次数:12  
标签:code VL Qwen model trust True 搭建

引子 这几天阿里的Qwen2.5大模型在大模型圈引起了轰动,号称地表最强中文大模型。前面几篇也写了QWen的微调等,视觉语言模型也写了一篇CogVLM,感兴趣的小伙伴可以移步https://blog.csdn.net/zzq1989_/article/details/138118608?spm=1001.2014.3001.5501。前面也写过一篇智谱AI的视觉大模型(https://blog.csdn.net/zzq1989_/article/details/138337071?spm=1001.2014.3001.5501)。Qwen-VL 是阿里云研发的大规模视觉语言模型(Large Vision Language Model, LVLM)。Qwen-VL 可以以图像、文本、检测框作为输入,并以文本和检测框作为输出。 一、模型介绍 1、强大的性能 在四大类多模态任务的标准英文测评中(Zero-shot Captioning/VQA/DocVQA/Grounding)上,均取得同等通用模型大小下最好效果; 2、多语言对话模型 天然支持英文、中文等多语言对话,端到端支持图片里中英双语的长文本识别;这里就得吐槽下CogVLM了,竟然没有支持中文 3、多图交错对话 支持多图输入和比较,指定图片问答,多图文学创作等; 4、首个支持中文开放域定位的通用模型 通过中文开放域语言表达进行检测框标注; 5、细粒度识别和理解 相比于目前其它开源LVLM使用的224分辨率,Qwen-VL是首个开源的448分辨率的LVLM模型。更高分辨率可以提升细粒度的文字识别、文档问答和检测框标注。 二、安装环境 请移步QWen1.5微调的地址,https://blog.csdn.net/zzq1989_/article/details/138118608?spm=1001.2014.3001.5501 docker run -it --rm --gpus=all -v /mnt/code/LLM_Service/:/workspace qwen:v1.0 bash 三、推理测试 1、QWen-VL-chat cd /workspace/qwen-vl python qwen-vl-chat.py

from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation import GenerationConfig
import torch
torch.manual_seed(1234)

# 请注意:分词器默认行为已更改为默认关闭特殊token攻击防护。
tokenizer = AutoTokenizer.from_pretrained("/workspace/model/Qwen-VL-Chat", trust_remote_code=True)

# 打开bf16精度,A100、H100、RTX3060、RTX3070等显卡建议启用以节省显存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-VL-Chat", device_map="auto", trust_remote_code=True, bf16=True).eval()
# 打开fp16精度,V100、P100、T4等显卡建议启用以节省显存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-VL-Chat", device_map="auto", trust_remote_code=True, fp16=True).eval()
# 使用CPU进行推理,需要约32GB内存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-VL-Chat", device_map="cpu", trust_remote_code=True).eval()
# 默认gpu进行推理,需要约24GB显存
model = AutoModelForCausalLM.from_pretrained("/workspace/model/Qwen-VL-Chat", device_map="cuda", trust_remote_code=True, fp16=True).eval()

# 可指定不同的生成长度、top_p等相关超参(transformers 4.32.0及以上无需执行此操作)
# model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-VL-Chat", trust_remote_code=True)

# 第一轮对话
query = tokenizer.from_list_format([
    {'image': 'https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg'}, # Either a local path or an url
    {'text': '这是什么?'},
])
response, history = model.chat(tokenizer, query=query, history=None)
print(response)
# 图中是一名女子在沙滩上和狗玩耍,旁边是一只拉布拉多犬,它们处于沙滩上。

# 第二轮对话
response, history = model.chat(tokenizer, '框出图中击掌的位置', history=history)
print(response)
# <ref>击掌</ref><box>(536,509),(588,602)</box>
image = tokenizer.draw_bbox_on_latest_picture(response, history)
if image:
  image.save('1.jpg')
else:
  print("no box")

2、QWen-VL python qwen-vl.py
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation import GenerationConfig
import torch
torch.manual_seed(1234)

tokenizer = AutoTokenizer.from_pretrained("/workspace/model/Qwen-VL-Chat", trust_remote_code=True)

# 打开bf16精度,A100、H100、RTX3060、RTX3070等显卡建议启用以节省显存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-VL", device_map="auto", trust_remote_code=True, bf16=True).eval()
# 打开fp16精度,V100、P100、T4等显卡建议启用以节省显存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-VL", device_map="auto", trust_remote_code=True, fp16=True).eval()
# 使用CPU进行推理,需要约32GB内存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-VL", device_map="cpu", trust_remote_code=True).eval()
# 默认gpu进行推理,需要约24GB显存
model = AutoModelForCausalLM.from_pretrained("/workspace/model/Qwen-VL-Chat", device_map="cuda", trust_remote_code=True, fp16=True).eval()

# 可指定不同的生成长度、top_p等相关超参(transformers 4.32.0及以上无需执行此操作)
# model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-VL", trust_remote_code=True)

query = tokenizer.from_list_format([
    {'image': 'https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg'}, # Either a local path or an url
    {'text': 'Generate the caption in English with grounding:'},
])
inputs = tokenizer(query, return_tensors='pt')
inputs = inputs.to(model.device)
pred = model.generate(**inputs)
response = tokenizer.decode(pred.cpu()[0], skip_special_tokens=False)
print(response)
# <img>https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg</img>Generate the caption in English with grounding:<ref> Woman</ref><box>(451,379),(731,806)</box> and<ref> her dog</ref><box>(219,424),(576,896)</box> playing on the beach<|endoftext|>
image = tokenizer.draw_bbox_on_latest_picture(response)
if image:
  image.save('2.jpg')
else:
  print("no box")

 

标签:code,VL,Qwen,model,trust,True,搭建
From: https://www.cnblogs.com/nick-algorithmer/p/18188932

相关文章

  • 域搭建
    1.什么是域?一种用于管理计算机组的网络结构,一个域的组成大致分为域控机(DC)和用户机。2.搭建过程我使用的虚拟机镜像为Windowsserver2016和window10EducationEdition。搭建的域环境包括一个域树和一个单域,域树中包含两个子域,网络拓补图如下。2.1.父域控制器搭建需要......
  • yum仓库搭建
    yum仓库搭建目录yum仓库搭建一、yum仓库简介二、准备安装源1、软件仓库的提供方式2、RPM软件包的来源3、构建centos7软件仓库4、在软件仓库中加入非官方RPM包组三、搭建本地yum仓库1、实例四、ftp搭建yum仓库4.1、服务端4.2、客户端4.3、实例:4.3.1服务端4.3.2客户端五、http......
  • 一个低级问题导致vLLM加载大模型时ray卡死
    这两天一直被一个问题困扰,用vLLM加载千问时不能开并行(tensor_parallel_size>1),一开就会卡在ray阶段,最初是提示StartedalocalRayinstance,后来手工启动ray集群,就提示connectedtoRaycluster。无论怎样调都无法跑下去,根本不会加载模型,换了各种版本的vllm、transformer、ray......
  • Berkeley vLLM:算力减半、吞吐增十倍
    BerkeleyvLLM:算力减半、吞吐增十倍来源 https://zhuanlan.zhihu.com/p/697142422 随着大语言模型(LLM)的不断发展,这些模型在很大程度上改变了人类使用AI的方式。然而,实际上为这些模型提供服务仍然存在挑战,即使在昂贵的硬件上也可能慢得惊人。现在这种限制正在被打破。最近,......
  • [附源码]新天龙八部3永恒经典之江山策仿官方_联网+单机搭建架设教程
    新天龙八部3永恒经典之江山策仿官_联网架设搭建_附赠GM工具+视频教程本教程仅限学习使用,禁止商用,一切后果与本人无关,此声明具有法律效应!!!!教程是本人亲自搭建成功的,绝对是完整可运行的,踩过的坑都给你们填上了。如果你是小白也没问题,跟着教程走也是可以搭建成功的,但是一定要有耐心......
  • wps的VLOOKUP函数只显示公式不显示结果,在公式中已经出现结果了,但在表格中不显示结果
     在公式中已经有结果了,但是表格中只显示公式1、在公式那里点击“显示公式”就可以2、选中公式列后更改格式 ......
  • FM20S用户手册-Linux开发环境搭建
     ......
  • 准实时数仓搭建指南:以仓储式会员商超为模拟场景
    在电商和新零售持续冲击传统零售商超的今天,仓储式会员店反而成功逃脱曾经的“水土不服”预测,业绩一路向好。与此同时,随着人工智能、大数据、智慧物流等技术的不断革新,零售批发的消费场景也进一步拓展,对数据分析的要求也越发迫切。本文将以巴基斯坦Metro的数仓项目为例,以操作指......
  • qgroundcontrol开发环境搭建源码编译
    qgroundcontrol是一款无人机地面站开源软件,C++/QT开发在https://github.com/mavlink/qgroundcontrol上就能找到,选择稳定版下载最新的是2.6下载https://github.com/mavlink/qgroundcontrol/archive/Stable_V2.6.zipQT的对应版本http://download.qt-project.org/official_releas......
  • CentOS上搭建SFTP
    CentOS上搭建SFTP 在CentOS上安装SFTP服务通常是通过安装OpenSSH来实现的,因为OpenSSH默认提供了SFTP功能。以下是在CentOS上安装SFTP的步骤: 一、安装OpenSSH服务器:sudoyuminstallopenssh-server启动SSH服务:sudosystemctlstartsshd确保SSH服务随系统启动而启动:sud......