PAI ArtLab Kohya

更新时间:
复制 MD 格式

This document describes how to use the Kohya tool.

Log on to the PAI ArtLab console.

Prerequisites

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

  1. Log in to PAI ArtLab. In the upper-right corner, hover over the image icon and select the China (Shanghai) region.

  2. On the Dataset page, click Create Dataset and set a name for the dataset.

  3. 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.

  4. 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

  1. On the Toolbox page, click the Kohya (Exclusive Edition) card to launch the tool.

  2. 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.

  3. 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.

  4. 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

  1. On the Model > Model Scope page, select a checkpoint model and add it to My Models.

  2. On the Kohya (Exclusive Edition) page, navigate to the LoRA > Training tab and configure the following parameters.

    1. On the Source model tab, configure the following parameters.

      Parameter

      Description

      Model quick pick

      Select custom.

      Pretrained model name or path

      1. Click the image icon on the right to refresh the model list.

      2. From the drop-down list, select /data-oss/models/Stable-diffusion.

      3. Append / to the path and select the model you added.

    2. 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.

    3. 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.

  3. 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

  1. On the Model > My Models page, click the image icon on the right of the model card to add the checkpoint model and the trained LoRA model to Stable Diffusion (Shared Edition).

  2. On the Toolbox page, click the Stable Diffusion (Shared Edition) card to launch the tool.

  3. Click the image icon next to Stable Diffusion model and select the checkpoint model.

  4. On the text-to-image tab, configure the following parameters.

    1. 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

    2. 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>.

    3. 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,

  5. 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, or stabilityai/stable-diffusion-2-1-base.

  • Method 2: Select a custom base model

    1. Upload a base model or add an existing model.

    2. 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.