InternLM-llama3微调(进阶作业)

微调llama3 增强图片理解

复制代码
#环境配置
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
cd ~
git clone -b v0.1.18 https://github.com/InternLM/XTuner
cd XTuner
pip install -e .[all]
cd ~
git clone https://github.com/SmartFlowAI/Llama3-Tutorial
#准备模型
#1 llama3模型
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
#2准备 Llava 所需要的 openai/clip-vit-large-patch14-336,权重,即 Visual Encoder 权重
mkdir -p ~/model
cd ~/model
ln -s /root/share/new_models/openai/clip-vit-large-patch14-336 .
#3准备 Llava 将要用到的 Image Projector 部分权重
mkdir -p ~/model
cd ~/model
ln -s /root/share/new_models/xtuner/llama3-llava-iter_2181.pth .

Llama3-8B-Instruct 权重:这是核心的模型权重,负责处理文本数据和执行指令性任务。
Visual Encoder 权重(openai/clip-vit-large-patch14-336):用于图像理解,将图像转换成模型可以理解的表示。
Image Projector 权重:这个权重通常用于进一步处理从 Visual Encoder 得到的图像表示,使其更适合与文本数据进行融合和交互。

Image Projector 输入图像 图像向量 输入文本 文本Embedding模型 文本向量 L L M 输出文本

#准备数据

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

但是我出现了显卡不够的情况

把命令替换成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_offload

区别在于:后者是显存不足,内存来补。 看到训练正常了

然后比较训练前和训练后

复制代码
#训练前
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
#训练后
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

可以看到

相关推荐
Mintopia11 分钟前
OpenClaw 是什么?为什么节后热度如此之高?
人工智能
爱可生开源社区21 分钟前
DBA 的未来?八位行业先锋的年度圆桌讨论
人工智能·dba
叁两3 小时前
用opencode打造全自动公众号写作流水线,AI 代笔太香了!
前端·人工智能·agent
敏编程3 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪3 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
strayCat232553 小时前
Clawdbot 源码解读 7: 扩展机制
人工智能·开源
王鑫星3 小时前
SWE-bench 首次突破 80%:Claude Opus 4.5 发布,Anthropic 的野心不止于写代码
人工智能
lnix3 小时前
当“大龙虾”养在本地:我们离“反SaaS”的AI未来还有多远?
人工智能·aigc
泉城老铁4 小时前
Dify知识库如何实现多关键词AND检索?
人工智能