【书生大模型实战营】进阶岛 第4关 InternVL 多模态模型部署微调实践

news2024/9/20 12:39:36

文章目录

  • 【书生大模型实战营】进阶岛 第4关 InternVL 多模态模型部署微调实践
  • 学习任务
  • 什么是InternVL
  • InternVL 模型总览
  • Dynamic High Resolution
  • Pixel Shuffle
  • InternVL 部署微调实践
  • 准备InternVL模型
  • 准备环境
  • 激活虚拟环境(注意:后续的所有操作都需要在这个虚拟环境中进行)
  • 安装一些必要的库
  • 安装其他依赖
  • 创建一个目录,用来存放源代码
  • 准备微调数据集
    • 首先让我们安装一下需要的包
    • 让我们把数据集挪出来
  • InternVL 微调攻略
  • 合并权重&&模型转换
  • transfer weights

【书生大模型实战营】进阶岛 第4关 InternVL 多模态模型部署微调实践

学习任务

follow 教学文档和视频使用QLoRA进行微调模型,复现微调效果,并能成功讲出梗图.
尝试使用LoRA,或调整xtuner的config,如LoRA rank,学习率。

什么是InternVL

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

InternVL 模型总览

在这里插入图片描述

Dynamic High Resolution

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

在这里插入图片描述

Pixel Shuffle

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

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 pytorch2.1.2 torchvision0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia -y

安装其他依赖

apt install libaio-dev
pip install transformers4.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/
让我们打开数据集的一张图看看,我们选择jsonl里的第一条数据对应的图片。首先我们先把这张图片挪动到InternLM文件夹下面。

cp InternLM/datasets/CLoT_cn_2000/ex_images/007aPnLRgy1hb39z0im50j30ci0el0wm.jpg InternLM/

在这里插入图片描述

InternVL 推理部署攻略
我们用LMDeploy来推理这张图片~看看它能不能成功解释出梗图呢?

使用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”: “\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

需要修改的部分
#Copyright © OpenMMLab. All rights reserved.
from mmengine.hooks import (CheckpointHook, DistSamplerSeedHook, IterTimerHook,
LoggerHook, ParamSchedulerHook)
from mmengine.optim import AmpOptimWrapper, CosineAnnealingLR, LinearLR
from peft import LoraConfig
from torch.optim import AdamW
from transformers import AutoTokenizer

from xtuner.dataset import InternVL_V1_5_Dataset
from xtuner.dataset.collate_fns import default_collate_fn
from xtuner.dataset.samplers import LengthGroupedSampler
from xtuner.engine.hooks import DatasetInfoHook
from xtuner.engine.runner import TrainLoop
from xtuner.model import InternVL_V1_5
from xtuner.utils import PROMPT_TEMPLATE

#######################################################################
#PART 1 Settings #
#######################################################################
#Model
path = ‘/root/model/InternVL2-2B’

#Data
data_root = ‘/root/InternLM/datasets/CLoT_cn_2000/’
data_path = data_root + ‘ex_cn.json’
image_folder = data_root
prompt_template = PROMPT_TEMPLATE.internlm2_chat
max_length = 6656

#Scheduler & Optimizer
batch_size = 4 # per_device
accumulative_counts = 4
dataloader_num_workers = 4
max_epochs = 6
optim_type = AdamW
#official 1024 -> 4e-5
lr = 2e-5
betas = (0.9, 0.999)
weight_decay = 0.05
max_norm = 1 # grad clip
warmup_ratio = 0.03

#Save
save_steps = 1000
save_total_limit = 1 # Maximum checkpoints to keep (-1 means unlimited)

#######################################################################
#PART 2 Model & Tokenizer & Image Processor #
#######################################################################
model = dict(
type=InternVL_V1_5,
model_path=path,
freeze_llm=True,
freeze_visual_encoder=True,
quantization_llm=True, # or False
quantization_vit=False, # or True and uncomment visual_encoder_lora
# comment the following lines if you don’t want to use Lora in llm
llm_lora=dict(
type=LoraConfig,
r=128,
lora_alpha=256,
lora_dropout=0.05,
target_modules=None,
task_type=‘CAUSAL_LM’),
# uncomment the following lines if you don’t want to use Lora in visual encoder # noqa
# visual_encoder_lora=dict(
# type=LoraConfig, r=64, lora_alpha=16, lora_dropout=0.05,
# target_modules=[‘attn.qkv’, ‘attn.proj’, ‘mlp.fc1’, ‘mlp.fc2’])
)

#######################################################################
#PART 3 Dataset & Dataloader #
#######################################################################
llava_dataset = dict(
type=InternVL_V1_5_Dataset,
model_path=path,
data_paths=data_path,
image_folders=image_folder,
template=prompt_template,
max_length=max_length)

train_dataloader = dict(
batch_size=batch_size,
num_workers=dataloader_num_workers,
dataset=llava_dataset,
sampler=dict(
type=LengthGroupedSampler,
length_property=‘modality_length’,
per_device_batch_size=batch_size * accumulative_counts),
collate_fn=dict(type=default_collate_fn))

#######################################################################
#PART 4 Scheduler & Optimizer #
#######################################################################
#optimizer
optim_wrapper = dict(
type=AmpOptimWrapper,
optimizer=dict(
type=optim_type, lr=lr, betas=betas, weight_decay=weight_decay),
clip_grad=dict(max_norm=max_norm, error_if_nonfinite=False),
accumulative_counts=accumulative_counts,
loss_scale=‘dynamic’,
dtype=‘float16’)

#learning policy
#More information: https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/param_scheduler.md # noqa: E501
param_scheduler = [
dict(
type=LinearLR,
start_factor=1e-5,
by_epoch=True,
begin=0,
end=warmup_ratio * max_epochs,
convert_to_iter_based=True),
dict(
type=CosineAnnealingLR,
eta_min=0.0,
by_epoch=True,
begin=warmup_ratio * max_epochs,
end=max_epochs,
convert_to_iter_based=True)
]

#train, val, test setting
train_cfg = dict(type=TrainLoop, max_epochs=max_epochs)

#######################################################################
#PART 5 Runtime #
#######################################################################
#Log the dialogue periodically during the training process, optional
tokenizer = dict(
type=AutoTokenizer.from_pretrained,
pretrained_model_name_or_path=path,
trust_remote_code=True)

custom_hooks = [
dict(type=DatasetInfoHook, tokenizer=tokenizer),
]

#configure default hooks
default_hooks = dict(
# record the time of every iteration.
timer=dict(type=IterTimerHook),
# print log every 10 iterations.
logger=dict(type=LoggerHook, log_metric_by_epoch=False, interval=10),
# enable the parameter scheduler.
param_scheduler=dict(type=ParamSchedulerHook),
# save checkpoint per save_steps.
checkpoint=dict(
type=CheckpointHook,
save_optimizer=False,
by_epoch=False,
interval=save_steps,
max_keep_ckpts=save_total_limit),
# set sampler seed in distributed evrionment.
sampler_seed=dict(type=DistSamplerSeedHook),
)

#configure environment
env_cfg = dict(
# whether to enable cudnn benchmark
cudnn_benchmark=False,
# set multi process parameters
mp_cfg=dict(mp_start_method=‘fork’, opencv_num_threads=0),
# set distributed parameters
dist_cfg=dict(backend=‘nccl’),
)

#set visualizer
visualizer = None

#set log level
log_level = ‘INFO’

#load from which checkpoint
load_from = None

#whether to resume training from the loaded checkpoint
resume = False

#Defaults to use random seed and disable deterministic
randomness = dict(seed=None, deterministic=False)

#set log processor
log_processor = dict(by_epoch=False)

在这里插入图片描述

在这里插入图片描述

合并权重&&模型转换

用官方脚本进行权重合并

如果这里你执行的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/
最后我们的模型在:/root/InternLM/convert_model/,文件格式:

.
|-- added_tokens.json
|-- config.json
|-- configuration_intern_vit.py
|-- configuration_internlm2.py
|-- configuration_internvl_chat.py
|-- conversation.py
|-- generation_config.json
|-- model.safetensors
|-- modeling_intern_vit.py
|-- modeling_internlm2.py
|-- modeling_internvl_chat.py
|-- special_tokens_map.json
|-- tokenization_internlm2.py
|-- tokenizer.model
`-- tokenizer_config.json
微调后效果对比
现在我们微调好啦,让我们再来试试

我们把下面的代码替换进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
在这里插入图片描述

在这里插入图片描述

可以多试一试,也可以换一换图片试试。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2096928.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数字化转型下的高效设备管理:构建智能化、精细化管理体系

企业应对日益复杂的设备管理挑战,通过数字化、智能化手段提升设备管理效率与设备可用性,具体体现在以下几个方面: 全面信息化与结构化:系统首先解决了设备信息散乱无序的问题,通过在线建档与台账管理,将设备…

记一次学习--webshell绕过(利用清洗函数)

目录 样本 样本修改 样本 <?php $a array("t", "system"); shuffle($a); $a[0]($_POST[1]); 通过 shuffle 函数打乱数组,然后通过$a[0]取出第一个元素&#xff0c;打乱后第一个元素可能是t也可能是system。然后再进行POST传参进行命令执行。 这里抓…

Ubuntu 安装个人热点

1. 安装必要的软件 首先&#xff0c;我们需要确保有一些工具已经装好&#xff0c;这些工具会帮助我们创建 Wi-Fi 热点。打开终端&#xff0c;输入以下命令来安装这些工具&#xff1a; sudo apt-get install git hostapd iptables dnsmasq 2. 下载并安装 create_ap 我们接下来…

RabbitMQ 02 操作,配置信息

01.介绍启动&#xff0c;关闭 02.环境 2.1 MQ是用Erlang语言写的 2.2 一个RabbitMQ 节点 一个 Erlang节点一个Erlang 程序 &#xff08;RabbitMQ程序&#xff09; 2.3 Erlang节点&#xff1a; 这个是Erlang节点集群状态下&#xff1a; 2.4 启动节点 2.5 查看日志信息 …

【KingbaseES 人大金仓】| Docker 部署 | 详细步骤

文章目录 版本说明准备工作第 1 步:导入 Docker 镜像第 2 步:创建容器相关说明运行容器第 3 步:查看运行状态第 4 步:使用 ksql 访问数据库第 5 步:修改 license第 6 步:修改配置文件【可选】✅ 本文目标:了解 KingbaseES 数据库、了解 KingbaseES 数据库版本、Docker 部…

CNN中的注意力机制综合指南:从理论到Pytorch代码实现

注意力机制已经成为深度学习模型&#xff0c;尤其是卷积神经网络&#xff08;CNN&#xff09;中不可或缺的组成部分。通过使模型能够选择性地关注输入数据中最相关的部分&#xff0c;注意力机制显著提升了CNN在图像分类、目标检测和语义分割等复杂任务中的性能。本文将全面介绍…

C#开发基础之多线程编程的常见错误实践和最佳实践

前言 在多线程编程中&#xff0c;由于存在共享资源和竞争条件等问题&#xff0c;容易出现各种错误。以下是一些常见的多线程编程错误及如何避免它们&#xff1a; 1. 竞态条件&#xff08;Race Condition&#xff09; 在多个线程同时访问共享资源时&#xff0c;可能会发生数据…

计算机网络 第1章

计算机网络概念 计算机网络&#xff08;Computer networking&#xff09;是一个分散的、自治的计算机系统&#xff0c;通过通信设备与线路连接起来&#xff0c;由功能完善的软件实现资源共享和信息传递的系统。 计算机网络&#xff08;简称网络&#xff09;&#xff1a;由若干结…

CAD | 背景改为白色

Op空格 打开选项面板 /或者直接右键选项 然后 显示-颜色-二位模型空间-统一背景-白-应用并关闭-确定

Red Hat 9 — Red Hat 9.4Linux系统 虚拟机安装【保姆级教程】

Mac分享吧 文章目录 效果一、下载软件二、安装软件与配置1、安装2、配置 三、查看基本信息安装完成&#xff01;&#xff01;&#xff01; 效果 一、下载软件 下载软件 地址&#xff1a;www.macfxb.cn 二、安装软件与配置 1、安装 2、配置 三、查看基本信息 安装完成&#xf…

Python批量提取pdf标题-作者信息

程序示例精选 Python批量提取pdf标题-作者信息 如需安装运行环境或远程调试&#xff0c;见文章底部个人QQ名片&#xff0c;由专业技术人员远程协助&#xff01; 前言 这篇博客针对《Python批量提取pdf标题-作者信息》编写代码&#xff0c;代码整洁&#xff0c;规则&#xff0…

编译FFmpeg动态库

编译FFmpeg动态库 环境 macOS High SierraFFmpeg 4.3android-ndk-r21b 编译so库 下载FFmpeg4.3源代码&#xff0c;进入源码目录创建build_android.sh脚本&#xff0c;ffmpeg从4.0起新增了target-osandroid&#xff0c;所以不用再修改configure文件。 注意&#xff1a; ndk…

WPF 手撸插件 七 日志记录

1、环境日志这里使用的是log4net. 2、WPF全局捕获异常&#xff0c;代码如下。 using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Ta…

系统架构设计师——系统性能

性能指标 计算机性能指标 操作系统性能指标 网络的性能指标 数据库的性能指标 数据库管理系统的性能指标 应用系统的性能指标 Web服务器的性能指标 性能计算 定义法 计算方法主要包括定义法、公式法、程序检测法和仪器检测法。这些方法分别通过直接获取理想数据、应用衍生出的…

【docker】docker 镜像仓库的管理

Docker 仓库&#xff08; Docker Registry &#xff09; 是用于存储和分发 Docker 镜像的集中式存储库。 它就像是一个大型的镜像仓库&#xff0c;开发者可以将自己创建的 Docker 镜像推送到仓库中&#xff0c;也可以从仓库中拉取所需的镜像。 Docker 仓库可以分为公共仓…

Jsoncpp的安装与使用

目录 安装Jsoncpp Jsoncpp的使用 Value类 构造函数 检测保存的数据类型 提取数据 对json数组的操作 对Json对象的操作 FastWriter类 Reader类 JsonCpp 是一个C库&#xff0c;用于解析和生成JSON数据。它支持解析JSON文件或字符串到C对象&#xff0c;以及将C对象序列…

MySQL的安装—>Mariadb的安装(day21)

该网盘链接有效期为7天&#xff0c;有需要评论区扣我&#xff1a; 通过网盘分享的文件&#xff1a;mariadb-10.3.7-winx64.msi 链接: https://pan.baidu.com/s/1-r_w3NuP8amhIEedmTkWsQ?pwd2ua7 提取码: 2ua7 1 双击打开安装软件 本次安装的是mariaDB&#xff0c;双击打开mar…

SprinBoot+Vue学生选课微信小程序的设计与实现

目录 1 项目介绍2 项目截图3 核心代码3.1 Controller3.2 Service3.3 Dao3.4 application.yml3.5 SpringbootApplication3.5 Vue3.6 uniapp代码 4 数据库表设计5 文档参考6 计算机毕设选题推荐7 源码获取 1 项目介绍 博主个人介绍&#xff1a;CSDN认证博客专家&#xff0c;CSDN平…

python进阶篇-day03-学生管理系统与深浅拷贝

day03-学生管理系统-面向对象 魔术方法: __ dict __将对象的属性和属性值封装为字典 用字典的值实例化对象: 对象名(**字典) > 拆包 student.py """ 该文件记录的是: 学生类的信息. ​ 学生的属性如下:姓名, 性别, 年龄, 联系方式, 描述信息 ""&…

单片机-STM32 ADC应用(五)

1.ADC模数转换 模拟数字转换器即A/D转换器&#xff0c;或简称ADC&#xff0c;通常是指一个将模拟信号转变为数字信号的电子元件。通常的模数转换器是将一个输入电压信号转换为一个输出的数字信号。由于数字信号本身不具有实际意义&#xff0c;仅仅表示一个相对大小。故任何一个…