Buckets:
Control over Diffusion Models
Dreambooth
Although diffusion models and GANs can generate many unique images, they can't always generate what you need exactly. Hence, you have to fine-tune a model, which usually requires a lot of data and computation. However, some techniques can be used to personalize a model with just a few examples.
One example is Dreambooth by Google Research, a training technique that updates the entire diffusion model by training on just a few images of a subject or style. It works by associating a special word in the prompt with the example images. The details on Dreambooth can be found in the paper and the Hugging Face Dreambooth training documentation.
Below, you can see the results of Dreambooth being used to train on 4 images of a dog and some inference examples.
You can recreate the results following the Hugging Face documentation given above.
From this example, it's clear that the model has learned about that specific dog and can generate new images of that dog in diverse poses and backgrounds. Although computing, data, and time are improvements, others have found more efficient ways to customize a model.
This is where the current most popular method for this comes in, which is Low Rank Adaptation (LoRA). This method was initially developed to efficiently fine-tune Large Language Models by Microsoft in this paper. The main idea is to factorise the weight update matrix into 2 low-rank matrices, which are optimized during training whilst the rest of the model is frozen. Hugging face documentation has a good conceptual guide on how LoRA works.
Now, if we put those ideas together we can use LoRA to efficiently fine-tune a diffusion model on a few examples using Dreambooth. A tutorial Google Colab notebook on how to do this can be found here.
Due to the quality and efficiency of this method, many people have created their own LoRA parameters which many can be found on a website called Civitai and Hugging Face. For Civitai you can download the LoRA weights which usually are in the range of 50-500MB and in the case of Hugging Face version you can just load the model directly from the model hub. Below is an example of how to load the LoRA weights in both cases and then fuse them with the model.
We can start with installing diffusers library.
pip install diffusers
We will initialize the StableDiffusionXLPipeline and load LoRA adapter weights.
from diffusers import StableDiffusionXLPipeline
import torch
model = "stabilityai/stable-diffusion-xl-base-1.0"
pipe = StableDiffusionXLPipeline.from_pretrained(model, torch_dtype=torch.float16)
pipe.load_lora_weights(
"lora_weights.safetensors"
) # if you want to install from a weight file
pipe.load_lora_weights(
"ostris/crayon_style_lora_sdxl"
) # if you wish to install a lora from a repository directly
pipe.fuse_lora(lora_scale=0.8)
This makes it quick to load a customised diffusion model and use it for inference, especially since there are a lot of models to choose from. Then, if we want to remove the LoRA weights, we can call pipe.unfuse_lora() which will return the model to its original state. As for the lora_scale parameter, this is a hyperparameter that controls how much the LoRA weights are used during inference. A value of 1.0 means the LoRA weights are fully used and a value of 0.0 means the LoRA weights are not used at all. The best value is often between 0.7 and 1.0 but it's worth experimenting with different values to see what works best for your use case.
You can try some of the Hugging Face LoRA models in this Gradio demo:
Xet Storage Details
- Size:
- 5.9 kB
- Xet hash:
- cf151f0c54ecc2465dcc7e937633603ec06990e9f165d155a70e1441a25a6e24
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.