Code Monkey home page Code Monkey logo

sdv1.5_lora's Introduction

Stable Diffusionv1.5 text-to-image fine-tuning

train_text_to_image.py script from huggingface diffusers

Running locally with PyTorch

Installing the dependencies

Before running the scripts, make sure to install the library's training dependencies:

Important

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:

git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .

Then cd in the example folder and run

pip install -r requirements.txt

And initialize an 🤗Accelerate environment with:

accelerate config

Training with LoRA

Low-Rank Adaption of Large Language Models was first introduced by Microsoft in LoRA: Low-Rank Adaptation of Large Language Models by Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen.

In a nutshell, LoRA allows adapting pretrained models by adding pairs of rank-decomposition matrices to existing weights and only training those newly added weights. This has a couple of advantages:

  • Previous pretrained weights are kept frozen so that model is not prone to catastrophic forgetting.
  • Rank-decomposition matrices have significantly fewer parameters than original model, which means that trained LoRA weights are easily portable.
  • LoRA attention layers allow to control to which extent the model is adapted toward new training images via a scale parameter.

cloneofsimo was the first to try out LoRA training for Stable Diffusion in the popular lora GitHub repository.

With LoRA, it's possible to fine-tune Stable Diffusion on a custom image-caption pair dataset on consumer GPUs like Tesla T4, Tesla V100.

Training

First, you need to set up your development environment as is explained in the installation section. Make sure to set the MODEL_NAME and DATASET_NAME environment variables. Here, we will use Stable Diffusion v1-5 and the Pokemons dataset.

Note: Change the resolution to 768 if you are using the stable-diffusion-2 768x768 model.

Note: It is quite useful to monitor the training progress by regularly generating sample images during training. Weights and Biases is a nice solution to easily see generating images during training. All you need to do is to run pip install wandb before training to automatically log images.

export MODEL_NAME="CompVis/stable-diffusion-v1-5"
export DATASET_NAME="lambdalabs/pokemon-blip-captions"

For this example we want to directly store the trained LoRA embeddings on the Hub, so we need to be logged in and add the --push_to_hub flag.

huggingface-cli login

Now we can start training!

accelerate launch --mixed_precision="bf16" --num_processes=1 --num_machines=1 --dynamo_backend=no train_text_to_image_lora.py \
  --pretrained_model_name_or_path=$MODEL_NAME \
  --dataset_name=$DATASET_NAME \
  --dataloader_num_workers=4 \
  --resolution=512 \
  --rank=4 \
  --center_crop \
  --random_flip \
  --train_batch_size=8 \
  --gradient_accumulation_steps=4 \
  --max_train_steps=5000 \
  --learning_rate=1e-04 \
  --max_grad_norm=1 \
  --lr_scheduler="cosine" \
  --lr_warmup_steps=0 \
  --output_dir=${OUTPUT_DIR} \
  --checkpoints_total_limit=5 \
  --validation_prompt="A pokemon with blue eyes." \
  --num_validation_images=4 \
  --validation_epochs=30 \
  --seed=1337 \
  --enable_xformers_memory_efficient_attention \
  --report_to="wandb" \

The above command will also run inference as fine-tuning progresses and log the results to Weights and Biases.

Note: When using LoRA we can use a much higher learning rate compared to non-LoRA fine-tuning. Here we use 1e-4 instead of the usual 1e-5. Also, by using LoRA, it's possible to run train_text_to_image_lora.py in consumer GPUs like T4 or V100.

Inference

Please refer to inference.py

Training with xFormers:

You can enable memory efficient attention by installing xFormers and passing the --enable_xformers_memory_efficient_attention argument to the script.

xFormers training is not available for Flax/JAX.

Note:

According to this issue, xFormers v0.0.16 cannot be used for training in some GPUs. If you observe that problem, please install a development version as indicated in that comment.

sdv1.5_lora's People

Contributors

anonym0u3 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.