|
| 1 | +# DreamBooth训练示例:Stable Diffusion 3 (SD3) |
| 2 | + |
| 3 | +[DreamBooth: Fine Tuning Text-to-Image Diffusion Models for Subject-Driven Generation](https://arxiv.org/abs/2208.12242) 是一种用于个性化文本到图像模型的方法,只需要主题的少量图像(3~5张)即可。 |
| 4 | + |
| 5 | +`train_dreambooth_sd3.py` 脚本展示了如何进行DreamBooth全参数微调[Stable Diffusion 3](https://huggingface.co/papers/2403.03206), `train_dreambooth_lora_sd3.py` 脚本中展示了如何进行DreamBooth LoRA微调。 |
| 6 | + |
| 7 | + |
| 8 | +> [!NOTE] |
| 9 | +> Stable Diffusion 3遵循 [Stability Community 开源协议](https://stability.ai/license)。 |
| 10 | +> Community License: Free for research, non-commercial, and commercial use for organisations or individuals with less than $1M annual revenue. You only need a paid Enterprise license if your yearly revenues exceed USD$1M and you use Stability AI models in commercial products or services. Read more: https://stability.ai/license |
| 11 | +
|
| 12 | + |
| 13 | +## DreamBooth微调 |
| 14 | + |
| 15 | +### 安装依赖 |
| 16 | + |
| 17 | +在运行脚本之前,请确保安装了库的训练依赖项: |
| 18 | + |
| 19 | +```bash |
| 20 | +pip install -r requirements_sd3.txt |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +### 示例 |
| 26 | +首先需要获取示例数据集。在这个示例中,我们将使用一些狗的图像:https://paddlenlp.bj.bcebos.com/models/community/westfish/develop-sdxl/dog.zip 。 |
| 27 | + |
| 28 | +解压数据集``unzip dog.zip``后,使用以下命令启动训练: |
| 29 | + |
| 30 | +```bash |
| 31 | +export MODEL_NAME="stabilityai/stable-diffusion-3-medium-diffusers" |
| 32 | +export INSTANCE_DIR="dog" |
| 33 | +export OUTPUT_DIR="trained-sd3" |
| 34 | +wandb offline |
| 35 | +``` |
| 36 | + |
| 37 | +```bash |
| 38 | +python train_dreambooth_sd3.py \ |
| 39 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 40 | + --instance_data_dir=$INSTANCE_DIR \ |
| 41 | + --output_dir=$OUTPUT_DIR \ |
| 42 | + --mixed_precision="fp16" \ |
| 43 | + --instance_prompt="a photo of sks dog" \ |
| 44 | + --resolution=1024 \ |
| 45 | + --train_batch_size=1 \ |
| 46 | + --gradient_accumulation_steps=4 \ |
| 47 | + --learning_rate=1e-4 \ |
| 48 | + --report_to="wandb" \ |
| 49 | + --lr_scheduler="constant" \ |
| 50 | + --lr_warmup_steps=0 \ |
| 51 | + --max_train_steps=50 \ |
| 52 | + --validation_prompt="A photo of sks dog in a bucket" \ |
| 53 | + --validation_epochs=25 \ |
| 54 | + --seed="0" \ |
| 55 | + --checkpointing_steps=250 |
| 56 | +``` |
| 57 | + |
| 58 | +fp16训练需要显存67000MiB,为了更好地跟踪我们的训练实验,我们在上面的命令中使用了以下标志: |
| 59 | + |
| 60 | +* `report_to="wandb"` 将确保在 Weights and Biases 上跟踪训练运行。要使用它,请确保安装 `wandb`,使用 `pip install wandb`。 |
| 61 | +* `validation_prompt` 和 `validation_epochs` 允许脚本进行几次验证推理运行。这可以让我们定性地检查训练是否按预期进行。 |
| 62 | + |
| 63 | + |
| 64 | +### 推理 |
| 65 | +训练完成后,我们可以通过以下python脚本执行推理: |
| 66 | +```python |
| 67 | +from ppdiffusers import StableDiffusion3Pipeline |
| 68 | +from ppdiffusers import ( |
| 69 | + AutoencoderKL, |
| 70 | + StableDiffusion3Pipeline, |
| 71 | + SD3Transformer2DModel, |
| 72 | +) |
| 73 | +import paddle |
| 74 | + |
| 75 | +transformer_path = "your-checkpoint/transformer" |
| 76 | + |
| 77 | +pipe = StableDiffusion3Pipeline.from_pretrained( |
| 78 | + "stabilityai/stable-diffusion-3-medium-diffusers", paddle_dtype=paddle.float16 |
| 79 | +) |
| 80 | +transformer = SD3Transformer2DModel.from_pretrained(transformer_path) |
| 81 | + |
| 82 | +image = pipe("A picture of a sks dog in a bucket", num_inference_steps=25).images[0] |
| 83 | +image.save("sks_dog_dreambooth_finetune.png") |
| 84 | +``` |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +## LoRA + DreamBooth |
| 89 | + |
| 90 | +[LoRA](https://huggingface.co/docs/peft/conceptual_guides/adapter#low-rank-adaptation-lora) 是一种流行的节省参数的微调技术,允许您以极少的可学习参数实现全微调的性能。 |
| 91 | + |
| 92 | +要使用 LoRA 进行 DreamBooth,运行: |
| 93 | + |
| 94 | +```bash |
| 95 | +export MODEL_NAME="stabilityai/stable-diffusion-3-medium-diffusers" |
| 96 | +export INSTANCE_DIR="dog" |
| 97 | +export OUTPUT_DIR="trained-sd3-lora" |
| 98 | +export USE_PEFT_BACKEND=True |
| 99 | +wandb offline |
| 100 | + |
| 101 | +python train_dreambooth_lora_sd3.py \ |
| 102 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 103 | + --instance_data_dir=$INSTANCE_DIR \ |
| 104 | + --output_dir=$OUTPUT_DIR \ |
| 105 | + --mixed_precision="fp16" \ |
| 106 | + --instance_prompt="a photo of sks dog" \ |
| 107 | + --resolution=512 \ |
| 108 | + --train_batch_size=1 \ |
| 109 | + --gradient_accumulation_steps=4 \ |
| 110 | + --learning_rate=5e-5 \ |
| 111 | + --report_to="wandb" \ |
| 112 | + --lr_scheduler="constant" \ |
| 113 | + --lr_warmup_steps=0 \ |
| 114 | + --max_train_steps=500 \ |
| 115 | + --validation_prompt="A photo of sks dog in a bucket" \ |
| 116 | + --validation_epochs=25 \ |
| 117 | + --seed="0" \ |
| 118 | + --checkpointing_steps=250 |
| 119 | +``` |
| 120 | + |
| 121 | +fp16训练需要显存47000MiB,。训练完成后,我们可以通过以下python脚本执行推理: |
| 122 | +```python |
| 123 | +from ppdiffusers import StableDiffusion3Pipeline |
| 124 | +from ppdiffusers import ( |
| 125 | + AutoencoderKL, |
| 126 | + StableDiffusion3Pipeline, |
| 127 | + SD3Transformer2DModel, |
| 128 | +) |
| 129 | +import paddle |
| 130 | + |
| 131 | +pipe = StableDiffusion3Pipeline.from_pretrained( |
| 132 | + "stabilityai/stable-diffusion-3-medium-diffusers", paddle_dtype=paddle.float16 |
| 133 | +) |
| 134 | +pipeline.load_lora_weights('your-lora-checkpoint') |
| 135 | + |
| 136 | +image = pipe("A picture of a sks dog in a bucket", num_inference_steps=25).images[0] |
| 137 | +image.save("sks_dog_dreambooth_lora.png") |
| 138 | +``` |
0 commit comments