Skip to content

Commit d8062ad

Browse files
authored
Feature/colossalai (#1793)
Support ColossalAi for Dreamblooth
1 parent be99201 commit d8062ad

File tree

4 files changed

+817
-0
lines changed

4 files changed

+817
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [DreamBooth](https://github.com/huggingface/diffusers/tree/main/examples/dreambooth) by [colossalai](https://github.com/hpcaitech/ColossalAI.git)
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_colossalai.py` script shows how to implement the training procedure and adapt it for stable diffusion.
5+
6+
By accommodating model data in CPU and GPU and moving the data to the computing device when necessary, [Gemini](https://www.colossalai.org/docs/advanced_tutorials/meet_gemini), the Heterogeneous Memory Manager of [Colossal-AI](https://github.com/hpcaitech/ColossalAI) can breakthrough the GPU memory wall by using GPU and CPU memory (composed of CPU DRAM or nvme SSD memory) together at the same time. Moreover, the model scale can be further improved by combining heterogeneous training with the other parallel approaches, such as data parallel, tensor parallel and pipeline parallel.
7+
8+
## Installing the dependencies
9+
10+
Before running the scripts, make sure to install the library's training dependencies:
11+
12+
```bash
13+
pip install -r requirements_colossalai.txt
14+
```
15+
16+
### Install [colossalai](https://github.com/hpcaitech/ColossalAI.git)
17+
18+
```bash
19+
pip install colossalai==0.2.0+torch1.12cu11.3 -f https://release.colossalai.org
20+
```
21+
22+
**From source**
23+
24+
```bash
25+
git clone https://github.com/hpcaitech/ColossalAI.git
26+
python setup.py install
27+
```
28+
29+
## Dataset for Teyvat BLIP captions
30+
Dataset used to train [Teyvat characters text to image model](https://github.com/hpcaitech/ColossalAI/tree/main/examples/images/diffusion).
31+
32+
BLIP generated captions for characters images from [genshin-impact fandom wiki](https://genshin-impact.fandom.com/wiki/Character#Playable_Characters)and [biligame wiki for genshin impact](https://wiki.biligame.com/ys/%E8%A7%92%E8%89%B2).
33+
34+
For each row the dataset contains `image` and `text` keys. `image` is a varying size PIL png, and `text` is the accompanying text caption. Only a train split is provided.
35+
36+
The `text` include the tag `Teyvat`, `Name`,`Element`, `Weapon`, `Region`, `Model type`, and `Description`, the `Description` is captioned with the [pre-trained BLIP model](https://github.com/salesforce/BLIP).
37+
38+
## Training
39+
40+
The arguement `placement` can be `cpu`, `auto`, `cuda`, with `cpu` the GPU RAM required can be minimized to 4GB but will deceleration, with `cuda` you can also reduce GPU memory by half but accelerated training, with `auto` a more balanced solution for speed and memory can be obtained。
41+
42+
**___Note: Change the `resolution` to 768 if you are using the [stable-diffusion-2](https://huggingface.co/stabilityai/stable-diffusion-2) 768x768 model.___**
43+
44+
```bash
45+
export MODEL_NAME="CompVis/stable-diffusion-v1-4"
46+
export INSTANCE_DIR="path-to-instance-images"
47+
export OUTPUT_DIR="path-to-save-model"
48+
49+
torchrun --nproc_per_node 2 train_dreambooth_colossalai.py \
50+
--pretrained_model_name_or_path=$MODEL_NAME \
51+
--instance_data_dir=$INSTANCE_DIR \
52+
--output_dir=$OUTPUT_DIR \
53+
--instance_prompt="a photo of sks dog" \
54+
--resolution=512 \
55+
--train_batch_size=1 \
56+
--learning_rate=5e-6 \
57+
--lr_scheduler="constant" \
58+
--lr_warmup_steps=0 \
59+
--max_train_steps=400 \
60+
--placement="cuda"
61+
```
62+
63+
64+
### Training with prior-preservation loss
65+
66+
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.
67+
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.
68+
69+
```bash
70+
export MODEL_NAME="CompVis/stable-diffusion-v1-4"
71+
export INSTANCE_DIR="path-to-instance-images"
72+
export CLASS_DIR="path-to-class-images"
73+
export OUTPUT_DIR="path-to-save-model"
74+
75+
torchrun --nproc_per_node 2 train_dreambooth_colossalai.py \
76+
--pretrained_model_name_or_path=$MODEL_NAME \
77+
--instance_data_dir=$INSTANCE_DIR \
78+
--class_data_dir=$CLASS_DIR \
79+
--output_dir=$OUTPUT_DIR \
80+
--with_prior_preservation --prior_loss_weight=1.0 \
81+
--instance_prompt="a photo of sks dog" \
82+
--class_prompt="a photo of dog" \
83+
--resolution=512 \
84+
--train_batch_size=1 \
85+
--learning_rate=5e-6 \
86+
--lr_scheduler="constant" \
87+
--lr_warmup_steps=0 \
88+
--max_train_steps=800 \
89+
--placement="cuda"
90+
```
91+
92+
## Inference
93+
94+
Once you have trained a model using above command, the inference can be done simply using the `StableDiffusionPipeline`. Make sure to include the `identifier`(e.g. sks in above example) in your prompt.
95+
96+
```python
97+
from diffusers import StableDiffusionPipeline
98+
import torch
99+
100+
model_id = "path-to-save-model"
101+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
102+
103+
prompt = "A photo of sks dog in a bucket"
104+
image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]
105+
106+
image.save("dog-bucket.png")
107+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import torch
2+
3+
from diffusers import StableDiffusionPipeline
4+
5+
6+
model_id = "path-to-your-trained-model"
7+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
8+
9+
prompt = "A photo of sks dog in a bucket"
10+
image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0]
11+
12+
image.save("dog-bucket.png")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
diffusers
2+
torch
3+
torchvision
4+
ftfy
5+
tensorboard
6+
modelcards
7+
transformers

0 commit comments

Comments
 (0)