当前位置: 代码网 > it编程>前端脚本>Python > huggingface学习|用dreambooth和lora对stable diffusion模型进行微调

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

2024年08月02日 Python 我要评论
新建一个python文件(如取名为train),保存以下代码,将对应的模型路径、prompt内容和图片名进行修改即可。新建一个python文件(如取名为train),保存以下代码,将对应的模型路径、prompt内容和图片名进行修改即可。–pretrained_model_name_or_path:Hub 上模型的名称或预训练模型的本地路径。–instance_data_dir:包含训练数据集的文件夹的路径(示例图像)–push_to_hub:是否将训练好的模型推送到Hub。运行上述python文件。


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

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

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

(二)数据集准备

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

(三)模型微调

  1. 加速器默认配置
accelerate config default
  1. 运行train_dreambooth文件
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 

(四)运行微调后的模型

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

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 train.py

最终结果为:
在这里插入图片描述

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

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

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

(二)数据集准备

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

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

(三)模型微调

  1. 加速器默认配置
accelerate config default
  1. 运行text_to_image_lora.py文件
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内容和图片名进行修改即可。

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 train.py

最终结果为:
在这里插入图片描述

参考:
huggingface dreambooth
huggingface lora

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com