Qwen-VL本地化部署及微调实践
创建虚拟环境
conda create -name vl python=3.10.8
模型部署
下载模型文件
https://huggingface.co/Qwen/Qwen-VL-Chat/tree/main
下载项目代码
https://github.com/QwenLM/Qwen-VL
安装python依赖环境
pip3 install -r requirements.txt
pip3 install -r requirements_openai_api.txt
pip3 install -r requirements_web_demo.txt
修改web_demo_mm.py及openai_api.py的部分代码
启动测试
web页面:
open_ai接口:
模型微调
环境部署
pip3 install deepspeed
pip3 install peft
pip3 install optimum
pip3 install auto-gptq
数据准备
创建一个json文件:
[
{
"id": "identity_0",
"conversations": [
{
"from": "user",
"value": "你好"
},
{
"from": "assistant",
"value": "我是Qwen-VL,一个支持视觉输入的大模型。"
}
]
},
{
"id": "identity_1",
"conversations": [
{
"from": "user",
"value": "Picture 1: <img>https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg</img>\n图中的狗是什么品种?"
},
{
"from": "assistant",
"value": "图中是一只拉布拉多犬。"
},
{
"from": "user",
"value": "框出图中的格子衬衫"
},
{
"from": "assistant",
"value": "<ref>格子衬衫</ref><box>(588,499),(725,789)</box>"
}
]
},
{
"id": "identity_2",
"conversations": [
{
"from": "user",
"value": "Picture 1: <img>assets/mm_tutorial/Chongqing.jpeg</img>\nPicture 2: <img>assets/mm_tutorial/Beijing.jpeg</img>\n图中都是哪"
},
{
"from": "assistant",
"value": "第一张图片是重庆的城市天际线,第二张图片是北京的天际线。"
}
]
},
{
"id": "identity_3",
"conversations": [
{
"from": "user",
"value": "Picture 1: <img>assets/mm_tutorial/gongren.png</img>\n解析图片中存在的问题,并返回相关的技术监督细则?"
},
{
"from": "assistant",
"value": "图片中存在以下问题:工人没有佩戴安全帽。地面上有大量的扬尘,可能对环境和工人的健康造成威胁。电线杆之间的距离过近,可能存在安全隐患。工人站在未铺设完成的路上,可能存在安全风险。相关的技术监督细则包括:《建筑施工安全检查标准》规定,工人进入施工现场时必须佩戴安全帽。《中华人民共和国大气污染防治法》规定,企业应当采取措施防治扬尘污染。《电力设施保护条例》规定,架空电力线路的杆塔、拉线不得跨越道路。《建筑施工安全操作规程》规定,工人应当在已经铺设完成的路上工作,避免站在未完成的路面上。"
}
]
}
]
为针对多样的VL任务,提供特殊tokens: <img> </img> <ref> </ref> <box> </box>
对于带图像输入的内容可表示为 Picture id: <img>img_path</img>\n{your prompt}
,其中id表示对话中的第几张图片。"img_path"可以是本地的图片或网络地址。
对话中的检测框可以表示为<box>(x1,y1),(x2,y2)</box>
,其中 (x1, y1) 和(x2, y2)分别对应左上角和右下角的坐标,并且被归一化到[0, 1000)的范围内. 检测框对应的文本描述也可以通过<ref>text_caption</ref>
表示。
微调
我这里因为资源有限,选择的QLora形式。修改finetune/finetune_qlora_single_gpu.sh中的一些设置,其中主要修改标红部分,其他参数自己设当调整。
运行sh finetune/finetune_qlora_single_gpu.sh
开始微调:
(vl) [root@iZf8zjfeutx2vqfwk4rqc2Z Qwen-VL-master]# sh finetune/finetune_qlora_single_gpu.sh
[2024-02-23 10:51:48,457] [INFO] [real_accelerator.py:191:get_accelerator] Setting ds_accelerator to cuda (auto detect)
/root/miniconda3/envs/vl/lib/python3.10/site-packages/transformers/utils/generic.py:260: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.
torch.utils._pytree._register_pytree_node(
/root/miniconda3/envs/vl/lib/python