EveryDream-trainer/README.md

89 lines
6.1 KiB
Markdown
Raw Normal View History

2022-09-06 01:13:09 -06:00
# Dreambooth on Stable Diffusion
This is an implementtaion of Google's [Dreambooth](https://arxiv.org/abs/2208.12242) with [Stable Diffusion](https://github.com/CompVis/stable-diffusion). The original Dreambooth is based on [Imagen](https://imagen.research.google/) text-to-image model. However, neither the model nor the pre-trained weights of Imagen is available. To enable people to fine-tune a text-to-image model with a few examples, I implemented the idea of Dreambooth on Stable diffusion.
This code repository is based on that of [Textual Inversion](https://github.com/rinongal/textual_inversion). Note that Textual Inversion only optimizes word ebedding, while dreambooth fine-tunes the whole diffusion model.
2022-09-06 01:25:05 -06:00
The implementation makes minimum changes over the official codebase of Textual Inversion. In fact, due to lazyness, some components in Textual Inversion, such as the embedding manager, are not deleted, although they will never be used here.
2022-09-06 01:13:09 -06:00
2022-09-06 01:13:19 -06:00
## Usage
2022-09-06 01:14:17 -06:00
### Preparation
2022-09-06 11:41:11 -06:00
To fine-tune a stable diffusion model, you need to obtain the pre-trained stable diffusion models following their [instructions](https://github.com/CompVis/stable-diffusion#stable-diffusion-v1). Weights can be downloaded on [HuggingFace](https://huggingface.co/CompVis). You can decide which version of checkpoint to use, but I use ```sd-v1-4-full-ema.ckpt```.
2022-09-06 01:25:05 -06:00
2022-09-06 12:35:37 -06:00
We also need to create a set of images for regularization, as the fine-tuning algorithm of Dreambooth requires that. Details of the algorithm can be found in the paper. Note that in the original paper, the regularization images seem to be generated on-the-fly. However, here I generated a set of regularization images before the training. The text prompt for generating regularization images can be ```photo of a <class>```, where ```<class>``` is a word that describes the class of your object, such as ```dog```. The command is
2022-09-06 01:28:58 -06:00
```
2022-09-06 11:00:45 -06:00
python scripts/stable_txt2img.py --ddim_eta 0.0 --n_samples 8 --n_iter 1 --scale 10.0 --ddim_steps 50 --ckpt /path/to/original/stable-diffusion/sd-v1-4-full-ema.ckpt --prompt "a photo of a <class>"
2022-09-06 01:28:58 -06:00
```
2022-09-06 12:35:37 -06:00
I generate 8 images for regularization, but more regularization images may lead to stronger regularization and better editability. After that, save the generated images (separately, one image per ```.png``` file) at ```/root/to/regularization/images```.
2022-09-06 01:25:05 -06:00
2022-09-09 12:41:56 -06:00
**Updates on 9/9**
We should definitely use more images for regularization. Please try 100 or 200, to better align with the original paper. To acomodate this, I shorten the "repeat" of reg dataset in the [config file](https://github.com/XavierXiao/Dreambooth-Stable-Diffusion/blob/main/configs/stable-diffusion/v1-finetune_unfrozen.yaml#L96).
For some cases, if the generated regularization images are highly unrealistic (happens when you want to generate "man" or "woman"), you can find a diverse set of images (of man/woman) online, and use them as regularization images.
2022-09-06 01:25:05 -06:00
### Training
2022-09-06 01:35:02 -06:00
Training can be done by running the following command
```
python main.py --base configs/stable-diffusion/v1-finetune_unfrozen.yaml
-t
--actual_resume /path/to/original/stable-diffusion/sd-v1-4-full-ema.ckpt
-n <job name>
--gpus 0,
--data_root /root/to/training/images
--reg_data_root /root/to/regularization/images
--class_word <xxx>
```
2022-09-06 01:25:05 -06:00
2022-09-06 10:40:06 -06:00
Detailed configuration can be found in ```configs/stable-diffusion/v1-finetune_unfrozen.yaml```. In particular, the default learning rate is ```1.0e-6``` as I found the ```1.0e-5``` in the Dreambooth paper leads to poor editability. The parameter ```reg_weight``` corresponds to the weight of regularization in the Dreambooth paper, and the default is set to ```1.0```.
2022-09-06 10:35:58 -06:00
2022-09-06 10:40:06 -06:00
Dreambooth requires a placeholder word ```[V]```, called identifier, as in the paper. This identifier needs to be a relatively rare tokens in the vocabulary. The original paper approaches this by using a rare word in T5-XXL tokenizer. For simplicity, here I just use a random word ```sks``` and hard coded it.. If you want to change that, simply make a change in [this file](https://github.com/XavierXiao/Dreambooth-Stable-Diffusion/blob/main/ldm/data/personalized.py#L10).
2022-09-06 10:35:58 -06:00
2022-09-06 10:47:53 -06:00
Training will be run for 800 steps, and two checkpoints will be saved at ```./logs/<job_name>/checkpoints```, one at 500 steps and one at final step. Typically the one at 500 steps works well enough. I train the model use two A6000 GPUs and it takes ~15 mins.
2022-09-06 01:25:05 -06:00
### Generation
2022-09-06 10:47:53 -06:00
After training, personalized samples can be obtained by running the command
```
python scripts/stable_txt2img.py --ddim_eta 0.0
--n_samples 8
--n_iter 1
--scale 10.0
--ddim_steps 100
--ckpt /path/to/saved/checkpoint/from/training
--prompt "photo of a sks <class>"
```
In particular, ```sks``` is the identifier, which should be replaced by your choice if you happen to change the identifier, and ```<class>``` is the class word ```--class_word``` for training.
2022-09-06 10:52:08 -06:00
## Results
2022-09-06 10:55:35 -06:00
Here I show some qualitative results. The training images are obtained from the [issue](https://github.com/rinongal/textual_inversion/issues/8) in the Textual Inversion repository, and they are 3 images of a large trash container. Regularization images are generated by prompt ```photo of a container```. Regularization images are shown here:
2022-09-06 10:58:08 -06:00
![](assets/a-container-0038.jpg)
After training, generated images with prompt ```photo of a sks container```:
![](assets/photo-of-a-sks-container-0018.jpg)
Generated images with prompt ```photo of a sks container on the beach```:
![](assets/photo-of-a-sks-container-on-the-beach-0017.jpg)
Generated images with prompt ```photo of a sks container on the moon```:
![](assets/photo-of-a-sks-container-on-the-moon-0016.jpg)
2022-09-06 11:54:41 -06:00
Some not-so-perfect but still interesting results:
Generated images with prompt ```photo of a red sks container```:
![](assets/a-red-sks-container-0021.jpg)
Generated images with prompt ```a dog on top of sks container```:
![](assets/a-dog-on-top-of-sks-container-0023.jpg)
2022-09-06 10:55:35 -06:00