Text-to-image
Last updated
Last updated
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 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 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 π 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.
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 .
You can reduce your memory footprint even more by enabling memory efficient attention with xFormers. Make sure you have and pass the --enable_xformers_memory_efficient_attention
flag to the training script.
xFormers is not available for Flax.
Store your model on the Hub by adding the following argument to the training script:
Copied
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
PytorchHide Pytorch content
Copied
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
Copied
JAXHide JAX content
Before running the script, make sure you have the requirements installed:
Copied
Copied
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 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.
PytorchHide Pytorch contentCopied
JAXHide JAX contentCopied
Launch the for a fine-tuning run on the 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 argument.
To finetune on your own dataset, prepare the dataset according to the format required by π . You can , or you can .
accelerate
allows for seamless multi-GPU training. Follow the instructions for running distributed training with accelerate
. Here is an example command:
With Flax, itβs possible to train a Stable Diffusion model faster on TPUs and GPUs thanks to . 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.
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 argument.
Now you can launch the like this:
To finetune on your own dataset, prepare the dataset according to the format required by π . You can , or you can .
We support training with the Min-SNR weighting strategy proposed in 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 that compares the loss surfaces of the following setups:
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 guide.
Now you can load the fine-tuned model for inference by passing the model path or model name on the Hub to the :
We support fine-tuning the UNet shipped in via the train_text_to_image_sdxl.py
script. Please refer to the docs .
We also support fine-tuning of the UNet and Text Encoder shipped in with LoRA via the train_text_to_image_lora_sdxl.py
script. Please refer to the docs .