|
| 1 | +# DreamBooth training example |
| 2 | + |
| 3 | +[DreamBooth](https://arxiv.org/abs/2208.12242) is a method to personalize text2image models like stable diffusion given just a few(3~5) images of a subject. |
| 4 | +The `train_dreambooth.py` script shows how to implement the training procedure and adapt it for stable diffusion. |
| 5 | + |
| 6 | + |
| 7 | +## Running locally with PyTorch |
| 8 | + |
| 9 | +** Create Virtual Environment** |
| 10 | + |
| 11 | +```bash |
| 12 | +conda create -n dreambooth python=3.8 -y |
| 13 | +conda activate dreambooth |
| 14 | +``` |
| 15 | + |
| 16 | +### Installing the dependencies |
| 17 | + |
| 18 | +Before running the scripts, make sure to install the library's training dependencies: |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install git+https://github.com/huggingface/diffusers |
| 22 | +pip install -U -r requirements.txt --no-deps |
| 23 | + |
| 24 | +``` |
| 25 | + |
| 26 | +### Dog toy example |
| 27 | + |
| 28 | +Now let's get our dataset. For this example we will use some dog images: https://huggingface.co/datasets/diffusers/dog-example. |
| 29 | + |
| 30 | +Let's first download it locally: |
| 31 | + |
| 32 | +```python |
| 33 | +from huggingface_hub import snapshot_download |
| 34 | + |
| 35 | +local_dir = "./dog" |
| 36 | +snapshot_download( |
| 37 | + "diffusers/dog-example", |
| 38 | + local_dir=local_dir, repo_type="dataset", |
| 39 | + ignore_patterns=".gitattributes", |
| 40 | +) |
| 41 | +``` |
| 42 | +Official repository for the dataset of the Google paper DreamBooth: https://github.com/google/dreambooth |
| 43 | + |
| 44 | +### Finetuning With CPU using IPEX |
| 45 | +The following script shows how to use CPU with BF16 |
| 46 | + |
| 47 | +**___Note: Change the `resolution` to 768 if you are using the [stable-diffusion-2](https://huggingface.co/stabilityai/stable-diffusion-2) 768x768 model.___** |
| 48 | + |
| 49 | +```bash |
| 50 | +export MODEL_NAME="CompVis/stable-diffusion-v1-4" |
| 51 | +export INSTANCE_DIR="dog" |
| 52 | +export OUTPUT_DIR="path-to-save-model" |
| 53 | + |
| 54 | +# recommend to use numactl to bind cores in Intel cpus for better performance |
| 55 | + |
| 56 | +OMP_NUM_THREADS=<physic_cores> numactl -m 0 -C 0-<physic_cores-1> python train_dreambooth_ipex.py \ |
| 57 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 58 | + --instance_data_dir=$INSTANCE_DIR \ |
| 59 | + --output_dir=$OUTPUT_DIR \ |
| 60 | + --instance_prompt="a photo of sks dog" \ |
| 61 | + --resolution=512 \ |
| 62 | + --train_batch_size=1 \ |
| 63 | + --gradient_accumulation_steps=1 \ |
| 64 | + --learning_rate=5e-6 \ |
| 65 | + --lr_scheduler="constant" \ |
| 66 | + --lr_warmup_steps=0 \ |
| 67 | + --max_train_steps=400 \ |
| 68 | + --use_bf16 \ |
| 69 | + --ipex |
| 70 | +``` |
| 71 | + |
| 72 | +### Training with prior-preservation loss |
| 73 | + |
| 74 | +Prior-preservation is used to avoid overfitting and language-drift. Refer to the paper to learn more about it. For prior-preservation we first generate images using the model with a class prompt and then use those during training along with our data. |
| 75 | +According to the paper, it's recommended to generate `num_epochs * num_samples` images for prior-preservation. 200-300 works well for most cases. The `num_class_images` flag sets the number of images to generate with the class prompt. You can place existing images in `class_data_dir`, and the training script will generate any additional images so that `num_class_images` are present in `class_data_dir` during training time. |
| 76 | + |
| 77 | +```bash |
| 78 | +export MODEL_NAME="CompVis/stable-diffusion-v1-4" |
| 79 | +export INSTANCE_DIR="dog" |
| 80 | +export CLASS_DIR="path-to-class-images" |
| 81 | +export OUTPUT_DIR="path-to-save-model" |
| 82 | + |
| 83 | +OMP_NUM_THREADS=<physic_cores> numactl -m 0 -C 0-<physic_cores-1> python train_dreambooth_ipex.py \ |
| 84 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 85 | + --instance_data_dir=$INSTANCE_DIR \ |
| 86 | + --class_data_dir=$CLASS_DIR \ |
| 87 | + --output_dir=$OUTPUT_DIR \ |
| 88 | + --with_prior_preservation --prior_loss_weight=1.0 \ |
| 89 | + --instance_prompt="a photo of sks dog" \ |
| 90 | + --class_prompt="a photo of dog" \ |
| 91 | + --resolution=512 \ |
| 92 | + --train_batch_size=1 \ |
| 93 | + --gradient_accumulation_steps=1 \ |
| 94 | + --learning_rate=5e-6 \ |
| 95 | + --lr_scheduler="constant" \ |
| 96 | + --lr_warmup_steps=0 \ |
| 97 | + --num_class_images=200 \ |
| 98 | + --max_train_steps=800 \ |
| 99 | + --use_bf16 \ |
| 100 | + --ipex |
| 101 | +``` |
| 102 | + |
| 103 | +### With GPU using accelerate |
| 104 | + |
| 105 | +And initialize an [🤗Accelerate](https://github.com/huggingface/accelerate/) environment with: |
| 106 | + |
| 107 | +```bash |
| 108 | +accelerate config |
| 109 | +``` |
| 110 | + |
| 111 | +Or for a default accelerate configuration without answering questions about your environment |
| 112 | + |
| 113 | +```bash |
| 114 | +accelerate config default |
| 115 | +``` |
| 116 | +When running `accelerate config`, if we specify torch compile mode to True there can be dramatic speedups. |
| 117 | + |
| 118 | +And launch the training using: |
| 119 | + |
| 120 | +```bash |
| 121 | +export MODEL_NAME="CompVis/stable-diffusion-v1-4" |
| 122 | +export INSTANCE_DIR="dog" |
| 123 | +export OUTPUT_DIR="path-to-save-model" |
| 124 | + |
| 125 | +accelerate launch train_dreambooth.py \ |
| 126 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 127 | + --instance_data_dir=$INSTANCE_DIR \ |
| 128 | + --output_dir=$OUTPUT_DIR \ |
| 129 | + --instance_prompt="a photo of sks dog" \ |
| 130 | + --resolution=512 \ |
| 131 | + --train_batch_size=1 \ |
| 132 | + --gradient_accumulation_steps=1 \ |
| 133 | + --learning_rate=5e-6 \ |
| 134 | + --lr_scheduler="constant" \ |
| 135 | + --lr_warmup_steps=0 \ |
| 136 | + --max_train_steps=400 \ |
| 137 | + --push_to_hub |
| 138 | +``` |
| 139 | + |
| 140 | +### Training with prior-preservation loss |
| 141 | + |
| 142 | +```bash |
| 143 | +export MODEL_NAME="CompVis/stable-diffusion-v1-4" |
| 144 | +export INSTANCE_DIR="dog" |
| 145 | +export CLASS_DIR="path-to-class-images" |
| 146 | +export OUTPUT_DIR="path-to-save-model" |
| 147 | + |
| 148 | +accelerate launch train_dreambooth.py \ |
| 149 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 150 | + --instance_data_dir=$INSTANCE_DIR \ |
| 151 | + --class_data_dir=$CLASS_DIR \ |
| 152 | + --output_dir=$OUTPUT_DIR \ |
| 153 | + --with_prior_preservation --prior_loss_weight=1.0 \ |
| 154 | + --instance_prompt="a photo of sks dog" \ |
| 155 | + --class_prompt="a photo of dog" \ |
| 156 | + --resolution=512 \ |
| 157 | + --train_batch_size=1 \ |
| 158 | + --gradient_accumulation_steps=1 \ |
| 159 | + --learning_rate=5e-6 \ |
| 160 | + --lr_scheduler="constant" \ |
| 161 | + --lr_warmup_steps=0 \ |
| 162 | + --num_class_images=200 \ |
| 163 | + --max_train_steps=800 \ |
| 164 | + --push_to_hub |
| 165 | +``` |
| 166 | + |
| 167 | +### Inference |
| 168 | + |
| 169 | +Once you have trained a model using the above command, you can run inference simply using the `StableDiffusionPipeline`. Make sure to include the `identifier` (e.g. sks in above example) in your prompt. |
| 170 | + |
| 171 | + |
| 172 | +```python |
| 173 | +from diffusers import StableDiffusionPipeline |
| 174 | +import torch |
| 175 | + |
| 176 | +model_id = "path-to-your-trained-model" |
| 177 | +# use gpu |
| 178 | +#pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda") |
| 179 | +pipe = StableDiffusionPipeline.from_pretrained(model_id) |
| 180 | +prompt = "A photo of sks dog in a bucket" |
| 181 | +image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0] |
| 182 | + |
| 183 | +image.save("dog-bucket.png") |
| 184 | +``` |
| 185 | + |
| 186 | +### Inference from a training checkpoint |
| 187 | + |
| 188 | +You can also perform inference from one of the checkpoints saved during the training process, if you used the `--checkpointing_steps` argument. Please, refer to [the documentation](https://huggingface.co/docs/diffusers/main/en/training/dreambooth#performing-inference-using-a-saved-checkpoint) to see how to do it. |
0 commit comments