Skip to main content

๐ŸŸข Training LoRa

According to user feedback, some friends are interested in training LoRa themselves. To facilitate practical operations for users of different backgrounds, we are using the integrated package from Bilibili Uploader Autumn Leaves aaaki for demonstration.

Training package download: https://pan.quark.cn/s/d81b8754a484

Developers can clone directly via Git at https://github.com/Akegarasu/lora-scripts

git clone --recurse-submodules https://github.com/Akegarasu/lora-scripts

Environment Setupโ€‹

  1. Install Python version 3.10

Make sure to check 'add python.exe to PATH'

Click this.

  1. Run powershell as administrator

Enter Set-ExecutionPolicy -ExecutionPolicy RemoteSigned and press Enter, then if the following message pops up, belong to y, press Enter

After completing, belong to python, press Enter, if as shown then step is complete.

If entering python causes an error not finding python or opens the Microsoft Store

Please reinstall python, this time we choose customize install

Click next

In this step, we check this

Once installed, it can be used normally.

Install dependencies Go to the downloaded lora-scripts path, right-click install-cn, and run with powershell

![](https://cdn.jsdelivr.net/gh/donttal/imgbed/img/9be5b149e341b95ba5bdeb8dda5b0a09.png)

After installation, the powershell window will disappear on its own. If there is an error, the window will display the error information. There were no errors in this step for me.

Prepare Training Imagesโ€‹

Start the Stable Diffusion web UI application, refer to the previous chapters, select Training->Image Preprocessing. We have placed the training images in a folder beforehand, I used images of Dilireba for the experiment. In the source directory of the webUI, paste the path of the folder containing the images.

Target directory, paste the path of some empty folder.

Check automatic facial focus cropping and generate labels using Deepbooru, then click preprocessing.

After completion, our target folder and labels are as shown below

Close the WebUI application!!!

Then in the lora-scripts directory, create a new folder, I named it train_graph

Enter the newly created folder, then create another folder

Then enter the newly created folder and create a new folder. This folder is named [number]_[name]. The author is 6_dilireba here.

Put all our previously pre-processed picture data into this folder.

Then we use the text editor (Notepad, VScode, Notepad++, etc.) under the lora-scripts path to open the file train.ps1.

The content is as follows

The first of these two places we pay attention to here is the path of the bottom model, which is the path of the stable Diffusion large model. Here, the author is here.

https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main

Download v1-5-pruned.ckpt as an experiment, and change its name to model.ckpt and put it under the path of lora-script/sd-models, which is the path corresponding to the first red box.

Then modify the data path of the second red box to the /train_graph/dilireba path created in front of us.

ๆ‰พๅˆฐ output_name่ฟ™ไธชๅœฐๆ–น๏ผŒๆจกๅž‹ๅๅญ—่‡ชๅทฑๅ‘ฝๅ

Save and modify! Save and modify! Save and modify!

Trainโ€‹

Go back to the lora-scripts path, right-click train.ps, and run with powershell.

Here, the author encountered an error as shown in the figure below.

We use the text editor to open train.ps under the lora-scripts path again. If there is no error in the same model, you can ignore this step.

Find optimizer_type, it defaults to AdamW8bit, and the author modifies it to AdamW as shown in the figure below, save the modification! Save and modify! Save and modify!

Then go back to lora-scripts, [right-click train.ps] (http://xn--train-iw8hn010b.ps/), run with powershell, and train normally as shown in the figure below.

Complete the trainingโ€‹

After the training is completed, you can see our trained lora model under the lora-scripts/output path.

Then we can use the lora model we have trained. For details, please refer to the Lora section in the previous chapter of Introduction to the Stable Diffusion Model.

Compare before and after using lora. The author only used 6 pictures for training, and there was no adjustment, so the effect was limited, but the change of style was relatively obvious.