Implementation of 'SVDiff: Compact Parameter Space for Diffusion Fine-Tuning'
Project description
SVDiff-pytorch
An implementation of SVDiff: Compact Parameter Space for Diffusion Fine-Tuning by using d🧨ffusers.
My summary tweet is found here.
left: LoRA, right: SVDiff
Compared with LoRA, the number of trainable parameters is 0.6 M less parameters and the file size is only <1MB (LoRA: 3.1MB)!!
Installation
$ pip install svdiff-pytorch
Or, manually
$ git clone https://github.com/mkshing/svdiff-pytorch
$ pip install -r requirements.txt
Training
The following example script is for "Single-Subject Generation", which is a domain-tuning on a single object or concept (using 3-5 images). (See Section 4.1)
According to the paper, the learning rate for SVDiff needs to be 1000 times larger than the lr used for fine-tuning.
export MODEL_NAME="runwayml/stable-diffusion-v1-5"
export INSTANCE_DIR="path-to-instance-images"
export CLASS_DIR="path-to-class-images"
export OUTPUT_DIR="path-to-save-model"
accelerate launch train_svdiff.py \
--pretrained_model_name_or_path=$MODEL_NAME \
--instance_data_dir=$INSTANCE_DIR \
--class_data_dir=$CLASS_DIR \
--output_dir=$OUTPUT_DIR \
--with_prior_preservation --prior_loss_weight=1.0 \
--instance_prompt="photo of a sks dog" \
--class_prompt="photo of a dog" \
--resolution=512 \
--train_batch_size=1 \
--gradient_accumulation_steps=1 \
--learning_rate=5e-3 \
--lr_scheduler="constant" \
--lr_warmup_steps=0 \
--num_class_images=200 \
--max_train_steps=800
Inference
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import torch
from svdiff_pytorch import load_unet_for_svdiff
pretrained_model_name_or_path = "runwayml/stable-diffusion-v1-5"
spectral_shifts_ckpt = "spectral_shifts.safetensors-path"
unet = load_unet_for_svdiff(pretrained_model_name_or_path, spectral_shifts_ckpt=spectral_shifts_ckpt, subfolder="unet")
# load pipe
pipe = StableDiffusionPipeline.from_pretrained(
pretrained_model_name_or_path,
unet=unet,
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe.to("cuda")
image = pipe("A picture of a sks dog in a bucket", num_inference_steps=25).images[0]
You can use the following CLI too! Once it's done, you will see grid.png for the result.
python inference.py \
--pretrained_model_name_or_path="runwayml/stable-diffusion-v1-5" \
--spectral_shifts_ckpt="spectral_shifts.safetensors-path" \
--prompt="A picture of a sks dog in a bucket" \
--scheduler_type="dpm_solver++" \
--num_inference_steps=25 \
--num_images_per_prompt=2
Additional Features
Spectral Shift Scaling
You can adjust the strength of the weights by --spectral_shifts_scale
Here's a result for 0.8, 1.0, 1.2 (1.0 is the default).
Fast prior generation by using ToMe
By using ToMe for SD, the prior generation can be faster!
$ pip install tomesd
And, add --enable_tome_merging to your training arguments!
Citation
@misc{https://doi.org/10.48550/arXiv.2303.11305,
title = {SVDiff: Compact Parameter Space for Diffusion Fine-Tuning},
author = {Ligong Han and Yinxiao Li and Han Zhang and Peyman Milanfar and Dimitris Metaxas and Feng Yang},
year = {2023},
eprint = {2303.11305},
archivePrefix = {arXiv},
primaryClass = {cs.CV},
url = {https://arxiv.org/abs/2303.11305}
}
@misc{hu2021lora,
title = {LoRA: Low-Rank Adaptation of Large Language Models},
author = {Hu, Edward and Shen, Yelong and Wallis, Phil and Allen-Zhu, Zeyuan and Li, Yuanzhi and Wang, Lu and Chen, Weizhu},
year = {2021},
eprint = {2106.09685},
archivePrefix = {arXiv},
primaryClass = {cs.CL}
}
@article{bolya2023tomesd,
title = {Token Merging for Fast Stable Diffusion},
author = {Bolya, Daniel and Hoffman, Judy},
journal = {arXiv},
url = {https://arxiv.org/abs/2303.17604},
year = {2023}
}
Reference
TODO
- Training
- Inference
- Scaling spectral shifts
- Support multiple spectral shifts (Section 3.2)
- Cut-Mix-Unmix (Section 3.3)
- SVDiff + LoRA
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file svdiff-pytorch-0.1.1.tar.gz.
File metadata
- Download URL: svdiff-pytorch-0.1.1.tar.gz
- Upload date:
- Size: 43.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebc77ac17b1a2cf7a7bf2a5c12cbd8180813d13d7a056a23d1924c8c867e4a74
|
|
| MD5 |
89d5fc1971d35ee9563c913e07c82420
|
|
| BLAKE2b-256 |
7640983ccacde12c2e88e6880339d86122cb37d9cfb8b0ff59b5ab375e9c80fa
|
File details
Details for the file svdiff_pytorch-0.1.1-py3-none-any.whl.
File metadata
- Download URL: svdiff_pytorch-0.1.1-py3-none-any.whl
- Upload date:
- Size: 48.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b4f0c57371d03e9fcc9409217bb082289db6084be41e3a35ae415f96568c5e1
|
|
| MD5 |
be65a0a294d25000dd46b653b69c564b
|
|
| BLAKE2b-256 |
c06b91be3aa40702f3f97b96ef94dfed7afd60f7c2be36f81b87c86b6e6e75ca
|