InternVL 微调实践闯关任务

一、InternVL介绍

InternVL 是一种用于多模态任务的深度学习模型,旨在处理和理解多种类型的数据输入,如图像和文本。它结合了视觉和语言模型,能够执行复杂的跨模态任务,比如图文匹配、图像描述生成等。通过整合视觉特征和语言信息,InternVL 可以在多模态领域取得更好的表现。

对于InternVL这个模型来说,它vision模块就是一个微调过的ViT,llm模块是一个InternLM的模型。对于视觉模块来说,它的特殊之处在Dynamic High Resolution。

1.1 Dynamic High Resolution

动态高分辨率,为了让ViT模型能够尽可能获取到更细节的图像信息,提高视觉特征的表达能力。对于输入的图片,首先resize成448的倍数,然后按照预定义的尺寸比例从图片上crop对应的区域。细节如图所示。

1.2 Pixel Shuffle

Pixel Shuffle在超分任务中是一个常见的操作,PyTorch中有官方实现,即nn.PixelShuffle(upscale_factor) 该类的作用就是将一个tensor中的元素值进行重排列,假设tensor维度为[B, C, H, W], PixelShuffle操作不仅可以改变tensor的通道数,也会改变特征图的大小。

二、InternVL 部署微调实践

我们选定的任务是让InternVL-2B生成文生图提示词,这个任务需要VLM对图片有格式化的描述并输出。

2.1 准备InternVL模型

在InternStudio开发机上面官方准备好的模型移动出来:

cd /root
mkdir -p model

cp 模型

cp -r /root/share/new_models/OpenGVLab/InternVL2-2B /root/models/
2.2 准备环境
  • 配置虚拟环境
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
2.3 准备微调数据集

我们这里使用huggingface上的zhongshsh/CLoT-Oogiri-GO据集,特别鸣谢~。

数据集InternLM官方进行了去重,只保留中文数据等操作。并制作成XTuner需要的形式。并已在share里,我们一起从share里挪出数据集。

## 首先让我们安装一下需要的包
pip install datasets matplotlib Pillow timm

## 让我们把数据集挪出来
cp -r /root/share/new_models/datasets/CLoT_cn_2000 /root/InternLM/datasets/

数据集格式为:

我们使用lmdeploy来部署未微调前的模型,看下效果:

from lmdeploy import pipeline
from lmdeploy.vl import load_image

pipe = pipeline('/root/models/InternVL2-2B')

image = load_image('/root/InternLM/007aPnLRgy1hb39z0im50j30ci0el0wm.jpg')
response = pipe(('请你根据这张图片,讲一个脑洞大开的梗', image))
print(response.text)

未微调前,模型不能讲出梗:

2.4 开始微调

改/root/InternLM/code/XTuner/xtuner/configs/internvl/v2/internvl_v2_internlm2_2b_qlora_finetune.py文件中的模型路径和数据集位置:

使用xtuner进行微调:

NPROC_PER_NODE=1 xtuner train /root/InternLM/code/XTuner/xtuner/configs/internvl/v2/internvl_v2_internlm2_2b_qlora_finetune.py  --work-dir /root/InternLM/work_dir/internvl_ft_run_8_filter  --deepspeed deepspeed_zero1

2.5 合并权重

微调完成后使用convert_to_official.py来合并权重,转成HuggingFace格式:

cp /root/InternLM/code/XTuner/xtuner/configs/internvl/v1_5/convert_to_official.py /root/InternLM/

python convert_to_official.py /root/InternLM/code/XTuner/xtuner/configs/internvl/v2/internvl_v2_internlm2_2b_qlora_finetune.py /root/InternLM/work_dir/internvl_ft_run_8_filter/iter_500.pth /root/InternLM/convert_model

转换完成的模型放在目录/root/InternLM/convert_model

2.6 效果测试

修改模型路径为刚才转换后的微调模型,并显示加上chat_template_config

from lmdeploy import pipeline,TurbomindEngineConfig,ChatTemplateConfig,GenerationConfig
from lmdeploy.vl import load_image

chat_template_config = ChatTemplateConfig('internvl-internlm2')
backend_config = TurbomindEngineConfig(tp=1)
gen_config = GenerationConfig(top_p=0.8,
                              top_k=40,
                              temperature=0.8,
                              max_new_tokens=1024)
#pipe = pipeline('/root/models/InternVL2-2B',chat_template_config=chat_template_config)
pipe = pipeline('/root/InternLM/convert_model',chat_template_config=chat_template_config)
image = load_image('/root/InternLM/007aPnLRgy1hb39z0im50j30ci0el0wm.jpg')
response = pipe(('请你根据这张图片,讲一个脑洞大开的梗', image),gen_config=gen_config)
print(response.text)

可以看到微调后的模型能够讲梗了:

  • 24
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值