StableDiffusion的Lora模型训练

Blog1年前 (2023)更新 VoxHuang
689 0 0

前言

首先模型训练需要的条件比较严苛

  • 科学的网络
  • 强大的显卡
  • 基本的Linux知识
  • 耐心与时间

准备工作

将需要训练的图片放置在一个文件夹内

打开https://www.birme.net/?target_width=768&target_height=768调整图片尺寸

创建提示词:
打开StableDiffusion webui
打开训练
选择输入文件夹与输出文件夹
调整图片尺寸
勾选图片左右左右翻转和位深度模式
开始转换
编辑提示词

kohya_ss

安装 Python 3.10.6
安装 Git
安装 Visual Studio 2015, 2017, 2019, and 2022 redistributable

Windows11需要管理员权限运行power shell,然后输入命令

set-ExecutionPolicy RemoteSigned

在需要安装的位置git

git clone https://github.com/bmaltais/kohya_ss.git
cd kohya_ss
setup.bat

第一个选在本地运行还是在Amazon Sagemaker上运行,选本地“The Machine”;
第二个选是否有分布式训练,是否多CPU训练,多GPU训练,用TPU还是MPS,按回车
第三个选的是“是否只用CPU训练”,输入no,回车
第四个是否用torch dynamo来优化脚本,输入no,回车
DeepSpeed,我们输入no,回车
第六个选择所有GPU来做训练,输入all,回车
最后,训练精度选择no,回车

CUDNN 8.6

此步骤是可选的,但可以提高 NVIDIA 30X0/40X0 所有者的学习速度。 它允许更大的训练批量和更快的训练速度。

我强烈建议您下载它们以提高样本生成速度(在 4090 GPU 上几乎提高 50%),您可以在此处下载它们。

要安装,只需解压缩目录并将 cudnn_windows 文件夹放在kohya_ss的安装根目录中。

运行以下命令进行安装:

.\venv\Scripts\activate

python .\tools\cudann_1.8_install.py

kohya_ss文件夹说明

打开kohya_ss文件夹,upgrade.ps1管理员终端运行可以更新,gui.bat可以打开kohya_ss程序

准备训练文件夹

  • 新建一个文件夹命名为xx_lora
  • 在xx_lora里建立image,log,model三文件夹
  • 在image文件夹内新建100_xx,100表示每张图片训练100步,注意总步数需要达到1500
  • 训练图片及提示词复制到100_xx文件夹内

kohya_ss运行界面

创建预设.JSON文件

LoraBasicSettings.json

{
"pretrained_model_name_or_path": "runwayml/stable-diffusion-v1-5",
"v2": false,
"v_parameterization": false,
"logging_dir": "C:/Users/ohmni/AI/SAMPLE IMAGES/ADAMS/LORA OUTPUT/log",
"train_data_dir": "C:/Users/ohmni/AI/SAMPLE IMAGES/ADAMS/LORA OUTPUT/img",
"reg_data_dir": "",
"output_dir": "C:/Users/ohmni/AI/SAMPLE IMAGES/ADAMS/LORA OUTPUT/model",
"max_resolution": "512,512",
"learning_rate": "0.0001",
"lr_scheduler": "constant",
"lr_warmup": "0",
"train_batch_size": 2,
"epoch": "1",
"save_every_n_epochs": "1",
"mixed_precision": "bf16",
"save_precision": "bf16",
"seed": "1234",
"num_cpu_threads_per_process": 2,
"cache_latents": true,
"caption_extension": ".txt",
"enable_bucket": false,
"gradient_checkpointing": false,
"full_fp16": false,
"no_token_padding": false,
"stop_text_encoder_training": 0,
"use_8bit_adam": true,
"xformers": true,
"save_model_as": "safetensors",
"shuffle_caption": false,
"save_state": false,
"resume": "",
"prior_loss_weight": 1.0,
"text_encoder_lr": "5e-5",
"unet_lr": "0.0001",
"network_dim": 128,
"lora_network_weights": "",
"color_aug": false,
"flip_aug": false,
"clip_skip": 2,
"gradient_accumulation_steps": 1.0,
"mem_eff_attn": false,
"output_name": "Addams",
"model_list": "runwayml/stable-diffusion-v1-5",
"max_token_length": "75",
"max_train_epochs": "",
"max_data_loader_n_workers": "1",
"network_alpha": 128,
"training_comment": "",
"keep_tokens": "0",
"lr_scheduler_num_cycles": "",
"lr_scheduler_power": ""
}

LoraLowVRAMSettings.json

{
"pretrained_model_name_or_path": "runwayml/stable-diffusion-v1-5",
"v2": false,
"v_parameterization": false,
"logging_dir": "C:/Users/ohmni/AI/SAMPLE IMAGES/ADAMS/LORA OUTPUT/log",
"train_data_dir": "C:/Users/ohmni/AI/SAMPLE IMAGES/ADAMS/LORA OUTPUT/img",
"reg_data_dir": "",
"output_dir": "C:/Users/ohmni/AI/SAMPLE IMAGES/ADAMS/LORA OUTPUT/model",
"max_resolution": "512,512",
"learning_rate": "0.0001",
"lr_scheduler": "constant",
"lr_warmup": "0",
"train_batch_size": 1,
"epoch": "1",
"save_every_n_epochs": "1",
"mixed_precision": "fp16",
"save_precision": "fp16",
"seed": "1234",
"num_cpu_threads_per_process": 2,
"cache_latents": true,
"caption_extension": ".txt",
"enable_bucket": false,
"gradient_checkpointing": true,
"full_fp16": false,
"no_token_padding": false,
"stop_text_encoder_training": 0,
"use_8bit_adam": true,
"xformers": true,
"save_model_as": "safetensors",
"shuffle_caption": false,
"save_state": false,
"resume": "",
"prior_loss_weight": 1.0,
"text_encoder_lr": "5e-5",
"unet_lr": "0.0001",
"network_dim": 128,
"lora_network_weights": "",
"color_aug": false,
"flip_aug": false,
"clip_skip": 2,
"gradient_accumulation_steps": 1.0,
"mem_eff_attn": true,
"output_name": "Addams",
"model_list": "runwayml/stable-diffusion-v1-5",
"max_token_length": "75",
"max_train_epochs": "",
"max_data_loader_n_workers": "1",
"network_alpha": 128,
"training_comment": "",
"keep_tokens": "0",
"lr_scheduler_num_cycles": "",
"lr_scheduler_power": ""
}

Dreambooth Lora标签

点击configguration file导入预设.JSON文件

8G以上显存选择LoraBasicSettings.json,8G以下显存选择LoraLowVRAMSettings.json

Source model选择基础模型

Folder标签

  • image folder标签选择文件夹image
  • logging folder标签选择log文件夹
  • output folder标签选择model文件夹
  • model output name标签输入模型名称

Training parameters标签

  • max resolution修改图片分辨率

开始训练

静候佳音,同时关注电脑显卡状况

© 版权声明

相关文章

暂无评论

暂无评论...