Llama 3 图片理解能力微调(XTuner+LLaVA 版)实践笔记

Llama 3 图片理解能力微调(XTuner+LLaVA 版)实践笔记

    正在检查是否收录...

      基于 Llama3-8B-Instruct 和 XTuner 团队预训练好的 Image Projector 微调自己的多模态图文理解模型 LLaVA。

环境、模型、数据准备

conda create -n llama3 python=3.10 conda activate llama3 conda install pytorch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 pytorch-cuda=12.1 -c pytorch -c nvidia

接下来安装 XTuner

cd ~ git clone -b v0.1.18 https://github.com/InternLM/XTuner cd XTuner pip install -e .[all]

克隆Llama3仓库

cd ~ git clone https://github.com/SmartFlowAI/Llama3-Tutorial
模型准备
准备 Llama3 权重

从 OpenXLab 上下载 Meta-Llama-3-8B-Instruct 的权重

mkdir -p ~/model cd ~/model git lfs install git clone https://code.openxlab.org.cn/MrCat/Llama-3-8B-Instruct.git Meta-Llama-3-8B-Instruct
准备 Visual Encoder 权重

我们接下来准备 Llava 所需要的 openai/clip-vit-large-patch14-336,权重,即 Visual Encoder 权重

访问 https://huggingface.co/openai/clip-vit-large-patch14-336 以进行下载

准备 Image Projector 权重

然后我们准备 Llava 将要用到的 Image Projector 部分权重

相关权重可以访问:https://huggingface.co/xtuner/llava-llama-3-8b 以及 https://huggingface.co/xtuner/llava-llama-3-8b-v1_1 。(已经过微调,并非 Pretrain 阶段的 Image Projector)

数据准备

我们按照 Tutorial/xtuner/llava/xtuner_llava.md at camp2 · InternLM/Tutorial · GitHub 中的教程来准备微调数据。为了让大家可以快速上手,我们选择了使用过拟合的方式快速实现

cd ~ git clone https://github.com/InternLM/tutorial -b camp2 python ~/tutorial/xtuner/llava/llava_data/repeat.py \ -i ~/tutorial/xtuner/llava/llava_data/unique_data.json \ -o ~/tutorial/xtuner/llava/llava_data/repeated_data.json \ -n 200

微调过程

训练启动
xtuner train ~/Llama3-Tutorial/configs/llama3-llava/llava_llama3_8b_instruct_qlora_clip_vit_large_p14_336_lora_e1_finetune.py --work-dir ~/llama3_llava_pth --deepspeed deepspeed_zero2

训练过程所需显存约为44447 MiB,在单卡 A100 上训练所需时间为30分钟

在训练好之后,我们将原始 image projector 和 我们微调得到的 image projector 都转换为 HuggingFace 格式,为了下面的效果体验做准备

xtuner convert pth_to_hf ~/Llama3-Tutorial/configs/llama3-llava/llava_llama3_8b_instruct_qlora_clip_vit_large_p14_336_lora_e1_finetune.py \ ~/model/llama3-llava-iter_2181.pth \ ~/llama3_llava_pth/pretrain_iter_2181_hf xtuner convert pth_to_hf ~/Llama3-Tutorial/configs/llama3-llava/llava_llama3_8b_instruct_qlora_clip_vit_large_p14_336_lora_e1_finetune.py \ ~/llama3_llava_pth/iter_1200.pth \ ~/llama3_llava_pth/iter_1200_hf
效果体验
Pretrain 模型
export MKL_SERVICE_FORCE_INTEL=1 xtuner chat /root/model/Meta-Llama-3-8B-Instruct \ --visual-encoder /root/model/clip-vit-large-patch14-336 \ --llava /root/llama3_llava_pth/pretrain_iter_2181_hf \ --prompt-template llama3_chat \ --image /root/tutorial/xtuner/llava/llava_data/test_img/oph.jpg

此时可以看到,Pretrain 模型只会为图片打标签,并不能回答问题

Finetune 后 模型
export MKL_SERVICE_FORCE_INTEL=1 xtuner chat /root/model/Meta-Llama-3-8B-Instruct \ --visual-encoder /root/model/clip-vit-large-patch14-336 \ --llava /root/llama3_llava_pth/iter_1200_hf \ --prompt-template llama3_chat \ --image /root/tutorial/xtuner/llava/llava_data/test_img/oph.jpg

经过 Finetune 后,我们可以发现,模型已经可以根据图片回答我们的问题了

总结

### 文章总结
本文重点介绍了如何利用已预训练的**Llama3-8B-Instruct**模型和**XTuner**团队的**Image Projector**,进一步微调自己的多模态图文理解模型**LLaVA**。整个流程涵盖了从环境搭建、模型与数据准备,到具体的微调过程及效果体验,以下是详细步骤和关键点的总结:
#### 一、环境和工具准备
- **创建并激活Python环境**:使用`conda`命令安装Python 3.10及相关库(如PyTorch和CUDA)。
- **安装XTuner**:通过Git克隆XTuner仓库并安装,该工具用于模型的优化和微调。
- **克隆Llama3仓库**:准备Llama3的基础代码和配置。
#### 二、模型准备
- **下载权重**:
- 从**OpenXLab**下载**Meta-Llama-3-8B-Instruct**权重。
- 准备**Visual Encoder**(用于视觉编码)的权重,来自**CLIP**模型的**clip-vit-large-patch14-336**。
- 准备**Image Projector**的权重,用于将图像映射到潜在的语言模型中。
#### 三、数据准备
- 采用过拟合方式快速实现微调,使用**Tutorial**仓库中的脚本准备数据集。通过复制重复的数据样本来实现。
#### 四、微调过程
- **启动训练**:使用XTuner的`train`命令启动训练过程,指定配置文件、工作目录等,并选用`deepspeed_zero2`以优化显存使用。
- **训练后的处理**:将微调后的Image Projector权重转换为 Hugging Face 格式,便于后续验证和使用。
#### 五、效果体验
- **Pretrain 模型体验**:使用未经微调的模型,仅能为图片打标签,无法回答问题。
- **Finetune 后模型体验**:使用微调后的模型,模型能够根据图片内容回答相关问题,展现出多模态图文理解能力提升。
整个流程详细展示了从基础环境搭建到最终效果呈现的各个阶段,为读者提供了完整的多模态图文理解模型微调方案。 llamagitclictoclipcode多模态githubpythonpytorchclone数据准备huggingfacechatxlajsonintelopenai回答问题prompt
  • 本文作者:李琛
  • 本文链接: https://wapzz.net/post-18831.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.3W+
5
0
1
WAP站长官方

如何利用Stable Diffusion在AI绘画领域赚钱,(附详细教程)小白兼职必看!

上一篇

LLM大语言模型-ChatGPT、LLaMA、ChatGLM、文心一言、通义千问

下一篇
  • 复制图片
按住ctrl可打开默认菜单