1.InternVL简介
InternVL 是一种用于多模态任务的深度学习模型,旨在处理和理解多种类型的数据输入,如图像和文本。它结合了视觉和语言模型,能够执行复杂的跨模态任务,比如图文匹配、图像描述生成等。通过整合视觉特征和语言信息,InternVL 可以在多模态领域取得更好的表现。
对于InternVL这个模型来说,它vision模块就是一个微调过的ViT,llm模块是一个InternLM的模型。对于视觉模块来说,它的特殊之处在Dynamic High Resolution。
InternVL独特的预处理模块:动态高分辨率(Dynamic High Resolution),是为了让ViT模型能够尽可能获取到更细节的图像信息,提高视觉特征的表达能力。对于输入的图片,首先resize成448的倍数,然后按照预定义的尺寸比例从图片上crop对应的区域。细节如图所示。
Pixel Shuffle在超分任务中是一个常见的操作,PyTorch中有官方实现,即nn.PixelShuffle(upscale_factor) 该类的作用就是将一个tensor中的元素值进行重排列,假设tensor维度为[B, C, H, W], PixelShuffle操作不仅可以改变tensor的通道数,也会改变特征图的大小。
2.InternVL 部署微调实践
我们选定的任务是让InternVL-2B生成文生图提示词,这个任务需要VLM对图片有格式化的描述并输出。
让我们来一起完成一个用VLM模型进行冷笑话生成,让你的模型说出很逗的冷笑话吧。在这里,我们微调InterenVL使用xtuner。部署InternVL使用lmdeploy。
准备InternVL模型
我们使用InternVL2-2B模型。该模型已在share文件夹下挂载好,现在让我们把移动出来。
cd /root mkdir -p model # cp 模型 cp -r /root/share/new_models/OpenGVLab/InternVL2-2B /root/model/
准备环境
这里我们来手动配置下xtuner。
- 配置虚拟环境
conda create --name xtuner python=3.10 -y # 激活虚拟环境(注意:后续的所有操作都需要在这个虚拟环境中进行) conda activate xtuner # 安装一些必要的库 conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y # 安装其他依赖 apt install libaio-dev pip install transformers==4.39.3 pip install streamlit==1.36.0
- 安装xtuner
# 创建一个目录,用来存放源代码 mkdir -p /root/InternLM/code cd /root/InternLM/code git clone -b v0.1.23 https://github.com/InternLM/XTuner
进入XTuner目录
cd /root/InternLM/code/XTuner pip install -e '.[deepspeed]'
- 安装LMDeploy
pip install lmdeploy==0.5.3
- 安装验证
xtuner version ##命令 xtuner help
准备微调数据集
我们这里使用huggingface上的zhongshsh/CLoT-Oogiri-GO据集。
@misc{zhong2023clot,
title={Let's Think Outside the Box: Exploring Leap-of-Thought in Large Language Models with Creative Humor Generation},
author={Zhong, Shanshan and Huang, Zhongzhan and Gao, Shanghua and Wen, Weushao and Lin, Liang and Zitnik, Marinka and Zhou, Pan},
journal={arXiv preprint arXiv:2312.02439},
year={2023}
}
数据集我们从官网下载下来并进行去重,只保留中文数据等操作。并制作成XTuner需要的形式。并已在share里,我们一起从share里挪出数据集。
## 首先让我们安装一下需要的包 pip install datasets matplotlib Pillow timm ## 让我们把数据集挪出来 cp -r /root/share/new_models/datasets/CLoT_cn_2000 /root/InternLM/datasets/
抽出一张图片
使用pipeline进行推理
之后我们使用lmdeploy自带的pipeline工具进行开箱即用的推理流程,首先我们新建一个文件。
touch /root/InternLM/code/test_lmdeploy.py cd /root/InternLM/code/
然后把以下代码拷贝进test_lmdeploy.py中。
from lmdeploy import pipeline from lmdeploy.vl import load_image pipe = pipeline('/root/model/InternVL2-2B') image = load_image('/root/InternLM/007aPnLRgy1hb39z0im50j30ci0el0wm.jpg') response = pipe(('请你根据这张图片,讲一个脑洞大开的梗', image)) print(response.text)
运行执行推理结果。
python3 test_lmdeploy.py
推理后
发现直接使用2b模型不能很好的讲出梗,现在我们要对这个2b模型进行微调。
InternVL 微调
准备数据集
数据集格式为:
# 为了高效训练,请确保数据格式为: { "id": "000000033471", "image": ["coco/train2017/000000033471.jpg"], # 如果是纯文本,则该字段为 None 或者不存在 "conversations": [ { "from": "human", "value": "<image>\nWhat are the colors of the bus in the image?" }, { "from": "gpt", "value": "The bus in the image is white and red." } ] }
这里我们也为大家准备好了可以直接进行微调的数据集。数据集就是咱们刚才复制进InternLM/datasets的数据。
配置微调参数
让我们一起修改XTuner下 InternVL的config,文件在: /root/InternLM/code/XTuner/xtuner/configs/internvl/v2/internvl_v2_internlm2_2b_qlora_finetune.py
- 需要修改的部分
启动训练
合并权重&模型转换
用官方脚本进行权重合并
如果这里你执行的epoch不是6,是小一些的数字。你可能会发现internvl_ft_run_8_filter下没有iter_3000.pth, 那你需要把iter_3000.pth切换成你internvl_ft_run_8_filter目录下的pth即可。
cd XTuner # transfer weights python3 xtuner/configs/internvl/v1_5/convert_to_official.py xtuner/configs/internvl/v2/internvl_v2_internlm2_2b_qlora_finetune.py /root/InternLM/work_dir/internvl_ft_run_8_filter/iter_3000.pth /root/InternLM/InternVL2-2B/
效果对比
我们把下面的代码替换进test_lmdeploy.py中,然后跑一下效果。
from lmdeploy import pipeline from lmdeploy.vl import load_image pipe = pipeline('/root/InternLM/InternVL2-2B') image = load_image('/root/InternLM/007aPnLRgy1hb39z0im50j30ci0el0wm.jpg') response = pipe(('请你根据这张图片,讲一个脑洞大开的梗', image)) print(response.text)
运行
cd /root/InternLM/code python3 test_lmdeploy.py
效果还不错。
再尝试几张不在训练集中的图:
都非常有趣,符合梗图的特色。