Qwen-VL环境搭建&推理测试
引子
这几天阿里的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模型。更高分辨率可以提升细粒度的文字识别、文档问答和检测框标注。
二、安装环境
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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" ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了