This document describes how to use the Kohya tool.
Log on to the PAI ArtLab console.
Prerequisites
-
You have enabled PAI ArtLab and granted the required permissions. For more information, see Enable PAI ArtLab and grant permissions.
-
(Optional) You have claimed a free trial, a coupon, or purchased a resource plan. For more information, see PAI ArtLab billing.
Use your resources, coupon, or resource plan within their validity period. For details, see View usage and expiration.
Procedure
This tutorial shows how to use Kohya (Exclusive Edition) to train an oil painting style model and generate images from it. For this tutorial, prepare a dataset of 15 natural landscape oil paintings with a resolution of 768x768 pixels.
Step 1: Create a dataset
-
Log in to PAI ArtLab. In the upper-right corner, hover over the
icon and select the China (Shanghai) region. -
On the Dataset page, click Create Dataset and set a name for the dataset.
-
Open the dataset, click New Folder, and set a name for the folder.
The folder name must follow the format Number_CustomName, where the number specifies how many times the images are repeated during training. For example,
30_test. -
Upload your images to the created folder. The images must meet the following requirements:
-
Use at least 15 clear images.
-
Avoid excessively high resolutions. For LoRA training with the sd1.5 base model, a resolution of 512x512 or 512x768 pixels is sufficient.
-
Avoid images with watermarks, low resolution, strange lighting, complex or unrecognizable content, and odd angles.
-
Step 2: Caption images
-
On the Toolbox page, click the Kohya (Exclusive Edition) card to launch the tool.
-
Go to the Utilities > Captioning > WD14 Captioning tab and configure the following parameters.
Parameter
Description
Image folder to caption
Select the folder that you created. If the folder does not appear in the drop-down list, manually enter its path.
For example,
/data-oss/datasets/test/30_test.Undesired tags
Enter any prompts you want to exclude.
Prefix to add to WD14 caption
Enter a LoRA trigger word in the format DatasetName+Number.
For example,
test1. -
Click Caption images to start automatic bulk captioning.
The process takes 2 to 3 minutes. When captioning done appears in the log, the captioning is complete.
-
On the Datasets page, open the folder that you created and click an image to view its corresponding caption.
You can also modify the caption.
Step 3: Train the model
-
On the Model > Model Scope page, select a checkpoint model and add it to My Models.
-
On the Kohya (Exclusive Edition) page, navigate to the LoRA > Training tab and configure the following parameters.
-
On the Source model tab, configure the following parameters.
Parameter
Description
Model quick pick
Select custom.
Pretrained model name or path
-
Click the
icon on the right to refresh the model list. -
From the drop-down list, select /data-oss/models/Stable-diffusion.
-
Append
/to the path and select the model you added.
-
-
On the Folders tab, configure the following parameters.
Parameter
Description
Output folder
Select the dataset that you created.
Model output name
Enter a name for the trained LoRA model. For example,
test. -
On the Parameters tab, configure the following parameters.
Parameter
Description
Epoch
Set this parameter to 20.
Max resolution
Set this parameter to 768,768.
Enable buckets
Clear this checkbox.
Clear this checkbox if all images in the dataset have the same dimensions.
text encoder learning rate
Set this parameter to 0.00001.
Network rank (dimension)
Set this parameter to 128.
Network alpha
Set this parameter to 64.
-
-
Click Start training. When model saved appears in the log, the model training is complete.
Logs are generated during training. The loss value, a key metric, measures the difference between the model's predictions and the actual results. The ideal loss value varies by model type. Refer to the following table for typical ranges:
Model type
Loss value
Character model
0.06-0.09
Object model
0.07-0.09
Style model
0.08-0.13
Functional model
0.003-0.05
Step 4: Evaluate the model
-
On the Model > My Models page, click the
icon on the right of the model card to add the checkpoint model and the trained LoRA model to Stable Diffusion (Shared Edition). -
On the Toolbox page, click the Stable Diffusion (Shared Edition) card to launch the tool.
-
Click the
icon next to Stable Diffusion model and select the checkpoint model. -
On the text-to-image tab, configure the following parameters.
-
On the Generation tab, configure the following parameters.
Parameter
Description
Steps
Set this parameter to 30.
Script
Select X/Y/Z plot.
-
X type: Prompt S/R
-
X values: NUM,000001,000002,000003
-
Y type: Prompt S/R
-
Y values: STRENGTH,0.3,0.5,0.6,0.7,0.8,0.9,1
-
-
On the Lora tab, click Refresh, and then click the added LoRA model.
If you cannot find the LoRA model, select one of the trained LoRA models and modify the prompt.
For example, change
<lora:test-000002:1>to<lora:test-NUM:STRENGTH>. -
Enter the prompts.
Parameter
Description
Positive prompt
test1, outdoors, sky, day, cloud, water, tree, blue sky, no humans, traditional media, grass, building, nature, scenery, house, castle,Negative prompt
lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit,fewer digits, cropped, worst quality, low quality,normal quality, jpeg artifacts, signature,watermark, username, blurry,(worst quality:1.4),(low quality:1.4), (monochrome:1.1), Eagetive,
-
-
Click Generate and wait for the image to be generated.
An X/Y/Z plot image is generated. You can use the X/Y/Z plot script to test all trained models and compare their effects at different strength values.
Related operations
Select a checkpoint base model
-
Method 1: Select a preset checkpoint base model provided by the platform, such as sd1.5 xl.
In the Source model section, expand the Model quick pick drop-down list. You can select a pre-trained model as the base model, such as
stabilityai/stable-diffusion-xl-base-1.0,stabilityai/stable-diffusion-xl-refiner-1.0, orstabilityai/stable-diffusion-2-1-base. -
Method 2: Select a custom base model
-
Select custom from the Model quick pick drop-down list. For Pretrained model name or path, select /data-oss/models/Stable-diffusion, append
/, and then select the checkpoint model that you have added or uploaded to My Models.