去皮卡多 发表于 2024-8-23 21:46:21

stable diffusion中dreambooth模子训练

简介

一、参考质料

1、Github项目网址
2、huggingface网址
3、wandb网址
二、运行备注

1、背景先容
由于之前没有运行Github项目的经验,这个项目是我在Github尝试举行的第一个项目,训练过程中碰到了许多报错,耗费了大量时间熟悉服务器、wsl的用法、vs code的连接方式等。在初期我盼望可以或许找到这个项目环境配置的相关讲授资料,发现许多质料都不是很熟悉,对于我这样的新手小白来说跟进非常困难,因此在本篇文章中将尽量详细地举行项目配置先容,盼望可以或许资助到以后使用此项目上手的同学。
2、模子训练信息
(1)模子在服务器(Linux系统)上训练。由于我使用的是windows系统,使用wsl假造机连接服务器
注:须要提前配置wsl假造环境(如安装Ubuntu等)
(2)编译器为vs code,详细连接方式可见博客文章《vs code通过ssh连接服务器报错》
模子训练步骤

注:在举行模子训练之前牢记先阅读项目目次下的readme文件
一、连接服务器

1、方式一:如果下载了Ubuntu,直接在开机处搜索栏搜索Ubuntu打开终端输入以下代码,按照提示输入登录暗码即可连接到远程服务器。
ssh user_name@server_IP
2、方式二:使用powershell连接远程服务器。按下Win + R打开“运行”对话框,输入Powershell打开Powershell界面,输入wsl,加载完毕后再按照1中步骤即可连接远程服务器。
二、假造环境配置

1、阅读stable diffusion首页readme文件可知,推荐用PyPI或conda在假造环境中安装diffusers。阅读dreambooth中readme文件可知,重要步骤为创建假造环境→安装diffusers→安装依赖→加速器配置→模子训练。
注:在此步骤之前,确保服务器已经安装conda或pip。在以下先容中涉及假造环境创建或激活的指令均以conda为例。
2、导航到服务器中目的文件夹位置,创建假造环境
cd dest_location # dest_location替换为目标文件夹地址
conda create -n env_name python==3.10 # 此处填写所需python版本即可
3、导航到目的文件夹位置(最好与创建的假造环境在同一文件夹下),安装diffusers,输入以下指令安装diffusers
注:不要开代理或梯子,制止出现报错
conda install -c conda-forge diffusers # 安装diffusers
4、安装环境依赖,导航到diffusers母文件夹,输入以下指令。
cd diffusers
pip install -e .
5、进入diffusers/example/dreambooth文件夹,执行以下指令。
注:如果出现有包未安装的警告,则按照提示进入相应网址下载安装相应安装包
pip install -r requirements_sdxl.txt
accelerate config
输入accelerate config后出现若干选择题,一般环境下选择默认即可(默认使用1张GPU卡)。若盼望同时使用多张GPU卡举行模子训练,则可按照以下文章举行显卡配置(多卡训练模子accelerate配置)
6、在diffusers/examples/dreambooth路径下新建一个download.py文件,内容如下:
from huggingface_hub import snapshot_download

local_dir = "./dog"
snapshot_download(
    "diffusers/dog-example",
    local_dir=local_dir, repo_type="dataset",
    ignore_patterns=".gitattributes",
)
在同一起径下新建一个model_test.sh文件(训练模子指令),内容如下:
export MODEL_NAME="stabilityai/stable-diffusion-xl-base-1.0"
export INSTANCE_DIR="dog" # 存放学习图片文件夹
export OUTPUT_DIR="lora-trained-xl" # 存放输出结果图片文件夹
export VAE_PATH="madebyollin/sdxl-vae-fp16-fix"

accelerate launch train_dreambooth_lora_sdxl.py \
--pretrained_model_name_or_path=$MODEL_NAME\
--instance_data_dir=$INSTANCE_DIR \
--pretrained_vae_model_name_or_path=$VAE_PATH \
--output_dir=$OUTPUT_DIR \
--mixed_precision="fp16" \
--instance_prompt="a photo of sks dog" \ # 输出图片指令
--resolution=1024 \
--train_batch_size=1 \
--gradient_accumulation_steps=4 \
--learning_rate=1e-4 \
--report_to="wandb" \
--lr_scheduler="constant" \
--lr_warmup_steps=0 \
--max_train_steps=500 \
--validation_prompt="A photo of sks dog in a bucket" \
--validation_epochs=25 \
--seed="0" \
--push_to_hub
由model_test.sh文件可知,须要在model_test.sh统一母文件夹下新建一个存放学习图片的文件夹(dog)以及一个存放输出结果的文件夹(lora-trained-xl)。
7、(已连接服务器的环境下)打开创建的假造环境,指令如下所示:
source env_name/bin/activate # 注意此时需要先导航到虚拟环境所在母文件夹下
连接到创建的假造环境后,终端表现如下所示(我创建的环境名称为dreambooth_env):
https://i-blog.csdnimg.cn/direct/d2823b7fcfa443658d47f6571e81bccc.png
8、在假造环境中运行download.py文件,下载所需的学习文件。指令为:
python download.py
注:在此过程中可能须要获取huggingface官网的API,按照提示在hf官网举行注册即可。
9、在假造环境中举行模子训练,指令如下
chmod +x model_test.sh
./model_test.sh
在运行训练文件之前确保假造环境中已经下载了wandb文件,指令如下:
pip install wandb
在此过程中可能须要获取wandb的API,在wandb官网举行注册即可。
10、在服务器及GPU有剩余内存的环境下,应该是可以或许正常举行模子训练的。如果未能乐成运行,出现以下报错可以按照以下方式举行解决。


[*]GPU内存不足(CUDA out of memory. Tried to allocate xxx MiB. GPU)

[*]解决方式:在服务器中查看GPU内存使用环境,指令为:nvidia-smi
输出表头包含每栏内容主题,如GPU名称、风扇转速、GPU运行状态、内存使用环境等,详细内容可见查看服务器显卡状态文章。
如果有GPU剩余较多内存,则说明默认使用的芯片内存不足,但系统并未能调用别的芯片举行模子训练。此时可以对训练模子使用到的GPU举行指定。指定方式为在model_test.sh文件中修改(accelerate launch train_dreambooth_lora_sdxl.py \)语句为以下内容:accelerate launch CUDA_VISIBLE_DEVICES=xx train_dramvooth_lora_sdxl.py \# 此中xx替代为所需指定的GPU卡号,通过nvidia-smi
可以查看GPU卡号 ```

[*]找不到学习图片报错,详细如下所示:
Traceback (most recent call last):
File ".../diffusers/examples/dreambooth/train_dreambooth_lora_sdxl.py", line 1984, in <module>
    main(args)
File ".../diffusers/examples/dreambooth/train_dreambooth_lora_sdxl.py", line 1415, in main
    train_dataset = DreamBoothDataset(
File ".../diffusers/examples/dreambooth/train_dreambooth_lora_sdxl.py", line 780, in __init__
    instance_images =
File ".../diffusers/examples/dreambooth/train_dreambooth_lora_sdxl.py", line 780, in <listcomp>
    instance_images =
File ".../dreambooth_env/lib/python3.10/site-packages/PIL/Image.py", line 3431, in open
    fp = builtins.open(filename, "rb")
IsADirectoryError: Is a directory: '.../diffusers/examples/dreambooth/dog/.huggingface'
上述报错说明尝试打开图片文件时给出的路径下为文件夹而非图片文件,详细解决方式为修改train_dreambooth_lora_sdxl.py文件第780行左右的代码,修改方式如下:
# 原代码
instance_images =

# 过滤掉目录,只保留文件
instance_images =

# 或者只处理指定格式的图像文件
instance_images = ]


[*]以下报错可以忽略
subprocess.CalledProcessError: Command '['./dreambooth_env/bin/python', 'train_dreambooth_lora_sdxl.py', '--pretrained_moth=stabilityai/stable-diffusion-xl-base-1.0', '--instance_data_dir=dog', '--pretrained_vae_model_name_or_path=madebyollin/sdxl-vae-fp16ut_dir=lora-trained-xl', '--mixed_precision=fp16', '--instance_prompt=a photo of sks dog', '--resolution=1024', '--train_batch_size=1',ccumulation_steps=4', '--learning_rate=1e-4', '--report_to=wandb', '--lr_scheduler=constant', '--lr_warmup_steps=0', '--max_train_stepsdation_prompt=A photo of sks dog in a bucket', '--validation_epochs=25', '--seed=0', '--push_to_hub']' returned non-zero exit status 1.
注:上述文件注意存放在同一文件夹下。如果仍有题目可以在Github项目中的issue栏目举行搜索 or 提问 / 询问ChatGPT / 欣赏器搜索,Github上issue位置如下所示。
https://i-blog.csdnimg.cn/direct/f51fff74b5e84ed9aba1ecce63325aef.png#pic_center

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: stable diffusion中dreambooth模子训练