huggingface学习|用dreambooth和lora对stable diffusion模型进行微调

目录


用dreambooth对stable-diffusion-v1-5模型进行微调

(一)模型下载和环境配置

  1. 准备好需要微调的模型如stable-diffusion-v1-5模型
  2. 下载diffusers模型并进入diffusers文件夹下载相关包
python 复制代码
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .
  1. 进入dreambooth文件夹下载相关包
python 复制代码
cd examples/dreambooth
pip install -r requirements.txt

(二)数据集准备

官方样例提供的数据为五张小狗照片,下载完毕后放入任意一个文件夹即可
如果是自己的数据集,准备好图片后放入一个指定文件夹即可

(三)模型微调

  1. 加速器默认配置
python 复制代码
accelerate config default
  1. 运行train_dreambooth文件
python 复制代码
accelerate launch train_dreambooth.py \
  --pretrained_model_name_or_path="./stable-diffusion-v1-5"  \
  --instance_data_dir="./image_data" \
  --output_dir="./outputs" \
  --instance_prompt="a photo of a sks dog" \
  --resolution=512 \
  --train_batch_size=1 \
  --gradient_accumulation_steps=1 \
  --learning_rate=5e-6 \
  --lr_scheduler="constant" \
  --lr_warmup_steps=0 \
  --max_train_steps=400 

--pretrained_model_name_or_path:Hub 上模型的名称或预训练模型的本地路径

--instance_data_dir:包含训练数据集的文件夹的路径(示例图像)

--instance_prompt:包含示例图像的特殊单词的文本提示

--train_text_encoder:是否也训练文本编码器

--output_dir:训练好的模型保存在哪里

--push_to_hub:是否将训练好的模型推送到Hub

--checkpointing_steps:模型训练时保存检查点的频率;如果由于某种原因训练被中断,这很有用,您可以通过添加--resume_from_checkpoint到训练命令来从该检查点继续训练

(四)运行微调后的模型

新建一个python文件(如取名为train),保存以下代码,将对应的模型路径、prompt内容和图片名进行修改即可。

python 复制代码
from diffusers import DiffusionPipeline
import torch

pipeline = DiffusionPipeline.from_pretrained("path_to_saved_model", torch_dtype=torch.float16, use_safetensors=True).to("cuda")
image = pipeline("A photo of sks dog in a bucket", num_inference_steps=50, guidance_scale=7.5).images[0]
image.save("dog-bucket.png")

运行上述python文件

python 复制代码
python train.py

最终结果为:

用lora对stable-diffusion-v1-5模型进行微调

(一)模型下载和环境配置

  1. 准备好需要微调的模型如stable-diffusion-v1-5模型
  2. 下载diffusers模型并进入diffusers文件夹下载相关包
python 复制代码
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .
  1. 进入text_to_image文件夹下载相关包
python 复制代码
cd examples/text_to_image
pip install -r requirements.txt

(二)数据集准备

官方样例提供的数据为pokemon-blip-captions图,下载完毕后放入任意一个文件夹即可

如果是自己的数据集,则需要在数据文件夹下放入相关图片和一个名为metadata.jsonl的文件(可以通过txt文件输入相关内容后修改后缀名即可),其中metadata.jsonl文件中的内容为图像名和对应的提示文本:

(三)模型微调

  1. 加速器默认配置
python 复制代码
accelerate config default
  1. 运行text_to_image_lora.py文件
python 复制代码
accelerate launch train_text_to_image_lora.py \
  --pretrained_model_name_or_path="../dreambooth/stable-diffusion-v1-5" \
  --dataset_name="./pokemon-blip-captions" \
  --dataloader_num_workers=8 \
  --resolution=512 \
  --center_crop \
  --random_flip \
  --train_batch_size=1 \
  --gradient_accumulation_steps=4 \
  --max_train_steps=15000 \
  --learning_rate=1e-04 \
  --max_grad_norm=1 \
  --lr_scheduler="cosine" \
  --lr_warmup_steps=0 \
  --output_dir="./output" \
  --hub_model_id="pokemon-lora" \
  --checkpointing_steps=500 \
  --validation_prompt="A pokemon with blue eyes." \
  --seed=1337

(四)运行微调后的模型

新建一个python文件(如取名为train),保存以下代码,将对应的模型路径、prompt内容和图片名进行修改即可。

python 复制代码
from diffusers import AutoPipelineForText2Image
import torch

pipeline = AutoPipelineForText2Image.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")
pipeline.load_lora_weights("path/to/lora/model", weight_name="pytorch_lora_weights.safetensors")
image = pipeline("A pokemon with blue eyes").images[0]
image.save("pokemon.png")

运行上述python文件

python 复制代码
python train.py

最终结果为:

参考:
huggingface dreambooth
huggingface lora

相关推荐
程途拾光15810 分钟前
中文界面跨职能泳道图制作教程 PC
大数据·论文阅读·人工智能·信息可视化·流程图
洛生&11 分钟前
Elevator Rides
算法
长颈鹿仙女12 分钟前
深度学习详解拟合,过拟合,欠拟合
人工智能·深度学习
CORNERSTONE36515 分钟前
智能制造为什么要实现EMS和MES的集成
大数据·人工智能·制造
weixin_6688986419 分钟前
Ascend LlamaFactory微调书生模型
人工智能
2501_9335130420 分钟前
关于一种计数的讨论、ARC212C Solution
算法
全栈技术负责人21 分钟前
AI驱动开发 (AI-DLC) 实战经验分享:重构人机协作的上下文工程
人工智能·重构
Wu_Dylan21 分钟前
智能体系列(二):规划(Planning):从 CoT、ToT 到动态采样与搜索
人工智能·算法
一招定胜负22 分钟前
OpenCV轮廓检测完全指南:从原理到实战
人工智能·opencv·计算机视觉
散峰而望27 分钟前
【算法竞赛】栈和 stack
开发语言·数据结构·c++·算法·leetcode·github·推荐算法