|
| 1 | +## Textual Inversion fine-tuning example |
| 2 | + |
| 3 | +[Textual inversion](https://arxiv.org/abs/2208.01618) is a method to personalize text2image models like stable diffusion on your own images using just 3-5 examples. |
| 4 | +The `textual_inversion.py` script shows how to implement the training procedure and adapt it for stable diffusion. |
| 5 | + |
| 6 | +## Running on Colab |
| 7 | + |
| 8 | +Colab for training |
| 9 | +[](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_textual_inversion_training.ipynb) |
| 10 | + |
| 11 | +Colab for inference |
| 12 | +[](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/stable_conceptualizer_inference.ipynb) |
| 13 | + |
| 14 | +## Running locally with PyTorch |
| 15 | +### Installing the dependencies |
| 16 | + |
| 17 | +Before running the scripts, make sure to install the library's training dependencies: |
| 18 | + |
| 19 | +**Important** |
| 20 | + |
| 21 | +To make sure you can successfully run the latest versions of the example scripts, we highly recommend **installing from source** and keeping the install up to date as we update the example scripts frequently and install some example-specific requirements. To do this, execute the following steps in a new virtual environment: |
| 22 | +```bash |
| 23 | +git clone https://github.com/huggingface/diffusers |
| 24 | +cd diffusers |
| 25 | +pip install . |
| 26 | +``` |
| 27 | + |
| 28 | +Then cd in the example folder and run: |
| 29 | +```bash |
| 30 | +pip install -r requirements.txt |
| 31 | +``` |
| 32 | + |
| 33 | +And initialize an [🤗 Accelerate](https://github.com/huggingface/accelerate/) environment with: |
| 34 | + |
| 35 | +```bash |
| 36 | +accelerate config |
| 37 | +``` |
| 38 | + |
| 39 | +### Cat toy example |
| 40 | + |
| 41 | +First, let's login so that we can upload the checkpoint to the Hub during training: |
| 42 | + |
| 43 | +```bash |
| 44 | +huggingface-cli login |
| 45 | +``` |
| 46 | + |
| 47 | +Now let's get our dataset. For this example we will use some cat images: https://huggingface.co/datasets/diffusers/cat_toy_example . |
| 48 | + |
| 49 | +Let's first download it locally: |
| 50 | + |
| 51 | +```py |
| 52 | +from huggingface_hub import snapshot_download |
| 53 | + |
| 54 | +local_dir = "./cat" |
| 55 | +snapshot_download("diffusers/cat_toy_example", local_dir=local_dir, repo_type="dataset", ignore_patterns=".gitattributes") |
| 56 | +``` |
| 57 | + |
| 58 | +This will be our training data. |
| 59 | +Now we can launch the training using: |
| 60 | + |
| 61 | +**___Note: Change the `resolution` to 768 if you are using the [stable-diffusion-2](https://huggingface.co/stabilityai/stable-diffusion-2) 768x768 model.___** |
| 62 | + |
| 63 | +**___Note: Please follow the [README_sdxl.md](./README_sdxl.md) if you are using the [stable-diffusion-xl](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0).___** |
| 64 | + |
| 65 | +```bash |
| 66 | +export MODEL_NAME="runwayml/stable-diffusion-v1-5" |
| 67 | +export DATA_DIR="./cat" |
| 68 | + |
| 69 | +accelerate launch textual_inversion.py \ |
| 70 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 71 | + --train_data_dir=$DATA_DIR \ |
| 72 | + --learnable_property="object" \ |
| 73 | + --placeholder_token="<cat-toy>" \ |
| 74 | + --initializer_token="toy" \ |
| 75 | + --resolution=512 \ |
| 76 | + --train_batch_size=1 \ |
| 77 | + --gradient_accumulation_steps=4 \ |
| 78 | + --max_train_steps=3000 \ |
| 79 | + --learning_rate=5.0e-04 \ |
| 80 | + --scale_lr \ |
| 81 | + --lr_scheduler="constant" \ |
| 82 | + --lr_warmup_steps=0 \ |
| 83 | + --push_to_hub \ |
| 84 | + --output_dir="textual_inversion_cat" |
| 85 | +``` |
| 86 | + |
| 87 | +A full training run takes ~1 hour on one V100 GPU. |
| 88 | + |
| 89 | +**Note**: As described in [the official paper](https://arxiv.org/abs/2208.01618) |
| 90 | +only one embedding vector is used for the placeholder token, *e.g.* `"<cat-toy>"`. |
| 91 | +However, one can also add multiple embedding vectors for the placeholder token |
| 92 | +to increase the number of fine-tuneable parameters. This can help the model to learn |
| 93 | +more complex details. To use multiple embedding vectors, you should define `--num_vectors` |
| 94 | +to a number larger than one, *e.g.*: |
| 95 | +```bash |
| 96 | +--num_vectors 5 |
| 97 | +``` |
| 98 | + |
| 99 | +The saved textual inversion vectors will then be larger in size compared to the default case. |
| 100 | + |
| 101 | +### Inference |
| 102 | + |
| 103 | +Once you have trained a model using above command, the inference can be done simply using the `StableDiffusionPipeline`. Make sure to include the `placeholder_token` in your prompt. |
| 104 | + |
| 105 | +```python |
| 106 | +from diffusers import StableDiffusionPipeline |
| 107 | +import torch |
| 108 | + |
| 109 | +model_id = "path-to-your-trained-model" |
| 110 | +pipe = StableDiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.float16).to("cuda") |
| 111 | + |
| 112 | +prompt = "A <cat-toy> backpack" |
| 113 | + |
| 114 | +image = pipe(prompt, num_inference_steps=50, guidance_scale=7.5).images[0] |
| 115 | + |
| 116 | +image.save("cat-backpack.png") |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +## Training with Flax/JAX |
| 121 | + |
| 122 | +For faster training on TPUs and GPUs you can leverage the flax training example. Follow the instructions above to get the model and dataset before running the script. |
| 123 | + |
| 124 | +Before running the scripts, make sure to install the library's training dependencies: |
| 125 | + |
| 126 | +```bash |
| 127 | +pip install -U -r requirements_flax.txt |
| 128 | +``` |
| 129 | + |
| 130 | +```bash |
| 131 | +export MODEL_NAME="duongna/stable-diffusion-v1-4-flax" |
| 132 | +export DATA_DIR="path-to-dir-containing-images" |
| 133 | + |
| 134 | +python textual_inversion_flax.py \ |
| 135 | + --pretrained_model_name_or_path=$MODEL_NAME \ |
| 136 | + --train_data_dir=$DATA_DIR \ |
| 137 | + --learnable_property="object" \ |
| 138 | + --placeholder_token="<cat-toy>" \ |
| 139 | + --initializer_token="toy" \ |
| 140 | + --resolution=512 \ |
| 141 | + --train_batch_size=1 \ |
| 142 | + --max_train_steps=3000 \ |
| 143 | + --learning_rate=5.0e-04 \ |
| 144 | + --scale_lr \ |
| 145 | + --output_dir="textual_inversion_cat" |
| 146 | +``` |
| 147 | +It should be at least 70% faster than the PyTorch script with the same configuration. |
| 148 | + |
| 149 | +### Training with xformers: |
| 150 | +You can enable memory efficient attention by [installing xFormers](https://github.com/facebookresearch/xformers#installing-xformers) and padding the `--enable_xformers_memory_efficient_attention` argument to the script. This is not available with the Flax/JAX implementation. |
0 commit comments