Text-to-image
Text-to-image
The text-to-image fine-tuning script is experimental. It’s easy to overfit and run into issues like catastrophic forgetting. We recommend you explore different hyperparameters to get the best results on your dataset.
Text-to-image models like Stable Diffusion generate an image from a text prompt. This guide will show you how to finetune the CompVis/stable-diffusion-v1-4
model on your own dataset with PyTorch and Flax. All the training scripts for text-to-image finetuning used in this guide can be found in this repository if you’re interested in taking a closer look.
Before running the scripts, make sure to install the library’s training dependencies:
Copied
And initialize an 🌍 Accelerate environment with:
Copied
If you have already cloned the repo, then you won’t need to go through these steps. Instead, you can pass the path to your local checkout to the training script and it will be loaded from there.
Hardware requirements
Using gradient_checkpointing
and mixed_precision
, it should be possible to finetune the model on a single 24GB GPU. For higher batch_size
’s and faster training, it’s better to use GPUs with more than 30GB of GPU memory. You can also use JAX/Flax for fine-tuning on TPUs or GPUs, which will be covered below.
You can reduce your memory footprint even more by enabling memory efficient attention with xFormers. Make sure you have xFormers installed and pass the --enable_xformers_memory_efficient_attention
flag to the training script.
xFormers is not available for Flax.
Upload model to Hub
Store your model on the Hub by adding the following argument to the training script:
Copied
Save and load checkpoints
It is a good idea to regularly save checkpoints in case anything happens during training. To save a checkpoint, pass the following argument to the training script:
Copied
Every 500 steps, the full training state is saved in a subfolder in the output_dir
. The checkpoint has the format checkpoint-
followed by the number of steps trained so far. For example, checkpoint-1500
is a checkpoint saved after 1500 training steps.
To load a checkpoint to resume training, pass the argument --resume_from_checkpoint
to the training script and specify the checkpoint you want to resume from. For example, the following argument resumes training from the checkpoint saved after 1500 training steps:
Copied
Fine-tuning
PytorchHide Pytorch content
Launch the PyTorch training script for a fine-tuning run on the Pokémon BLIP captions dataset like this.
Specify the MODEL_NAME
environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the pretrained_model_name_or_path
argument.
Copied
To finetune on your own dataset, prepare the dataset according to the format required by 🌍 Datasets. You can upload your dataset to the Hub, or you can prepare a local folder with your files.
Modify the script if you want to use custom loading logic. We left pointers in the code in the appropriate places to help you. 🌍 The example script below shows how to finetune on a local dataset in TRAIN_DIR
and where to save the model to in OUTPUT_DIR
:
Copied
Training with multiple GPUs
accelerate
allows for seamless multi-GPU training. Follow the instructions here for running distributed training with accelerate
. Here is an example command:
Copied
JAXHide JAX content
With Flax, it’s possible to train a Stable Diffusion model faster on TPUs and GPUs thanks to @duongna211. This is very efficient on TPU hardware but works great on GPUs too. The Flax training script doesn’t support features like gradient checkpointing or gradient accumulation yet, so you’ll need a GPU with at least 30GB of memory or a TPU v3.
Before running the script, make sure you have the requirements installed:
Copied
Specify the MODEL_NAME
environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the pretrained_model_name_or_path
argument.
Now you can launch the Flax training script like this:
Copied
To finetune on your own dataset, prepare the dataset according to the format required by 🌍 Datasets. You can upload your dataset to the Hub, or you can prepare a local folder with your files.
Modify the script if you want to use custom loading logic. We left pointers in the code in the appropriate places to help you. 🌍 The example script below shows how to finetune on a local dataset in TRAIN_DIR
:
Copied
Training with Min-SNR weighting
We support training with the Min-SNR weighting strategy proposed in Efficient Diffusion Training via Min-SNR Weighting Strategy which helps to achieve faster convergence by rebalancing the loss. In order to use it, one needs to set the --snr_gamma
argument. The recommended value when using it is 5.0.
You can find this project on Weights and Biases that compares the loss surfaces of the following setups:
Training without the Min-SNR weighting strategy
Training with the Min-SNR weighting strategy (
snr_gamma
set to 5.0)Training with the Min-SNR weighting strategy (
snr_gamma
set to 1.0)
For our small Pokemons dataset, the effects of Min-SNR weighting strategy might not appear to be pronounced, but for larger datasets, we believe the effects will be more pronounced.
Also, note that in this example, we either predict epsilon
(i.e., the noise) or the v_prediction
. For both of these cases, the formulation of the Min-SNR weighting strategy that we have used holds.
Training with Min-SNR weighting strategy is only supported in PyTorch.
LoRA
You can also use Low-Rank Adaptation of Large Language Models (LoRA), a fine-tuning technique for accelerating training large models, for fine-tuning text-to-image models. For more details, take a look at the LoRA training guide.
Inference
Now you can load the fine-tuned model for inference by passing the model path or model name on the Hub to the StableDiffusionPipeline:
PytorchHide Pytorch contentCopied
JAXHide JAX contentCopied
Stable Diffusion XL
We support fine-tuning the UNet shipped in Stable Diffusion XL via the
train_text_to_image_sdxl.py
script. Please refer to the docs here.We also support fine-tuning of the UNet and Text Encoder shipped in Stable Diffusion XL with LoRA via the
train_text_to_image_lora_sdxl.py
script. Please refer to the docs here.
Last updated