여러분이 사용하고 계신 브라우저는 HTML5를 지원하지 않기 때문에 몇몇 요소가 제대로 보이도록 JScript를 사용하고 있습니다. 하지만 여러분의 브라우저 설정에서 스크립트 기능이 꺼져있으므로, 현재 페이지를 제대로 확인하시려면 스크립트 기능을 켜주셔야 합니다. 이미지 생성 Ai - kohya_ss 드림부스 기반 LoRA GUI로 LoRA 학습 방법
이미지 생성 Ai – kohya_ss 드림부스 기반 LoRA GUI로 LoRA 학습 방법
10개월전 작성
10개월전 수정

준비물

kohya_ss WebUI, Stable Diffusion WebUI, Checkpoint 파일, LoRA 학습 시킬 이미지 30장~100장 정도.

kohya_ss

kohya_ss 드림부스는 기본적으로 CUI(Character User Interface) 방식이다.

Windows 95 출시 이후 CUI 방식 보다 GUI(Graphical User Interface) 방식이 많이 사용되므로 여기서 kohya_ss 드림부스 기반 LoRA GUI에 공부해볼 예정이다.

LoRA GUI로 LoRA 학습 시키는 방법을 공부해보자.

Satable Diffusion

Stable Diffusion은 이미지를 생성하는 Ai로 2022년에 출시된 딥 러닝 텍스트-이미지 모델이다.

자세한 내용은 Stable Diffusion 설치 방법 문서를 참고해보자.

CheckPoint

쉽게 생각하여 이미지를 만드는 큰 틀이라고 생각하면 된다.

LoRA 학습 시에는 본인이 자주 사용하는 CheckPoint 파일을 사용하여 학습을 시키면된다.

파일 확장자: .ckpt .safetensors / .ckpt는 요즘 보안 이슈로 거의 대부분 .safetensors 확장자를 사용한다.
저장 위치: stable-diffusion-webui\models\Stable-diffusion

LoRA

LoRA(Low-Rank Adaptation)는 다른 추가 학습 기법들보다 많이 사용되고 있다.

Microsoft 연구원이 개발한 Stable Diffusion을 위한 기존 모델에 새로운 피사체를 학습시키는 추가 학습 기법의 일종이다.

특정한 이미지의 캐릭터를 만들고 싶을 때 LoRA에서 학습을 시켜서 그 이미지의 캐릭터를 쉽게 만들어낼 수 있다.

미리보기 이미지는 png 형식만 적용되며 파일 이름과 동일해야 WebUI에서 미리보기 이미지가 표시된다.

파일 확장자: .ckpt .safetensors / .ckpt는 요즘 보안 이슈로 거의 대부분 .safetensors 확장자를 사용한다.
저장 위치: stable-diffusion-webui\models\Lora

kohya_ss 설치

1. Windows PowerShell » 관리자 권한으로 실행

 

2. 탐색기 실행 후 kohya_ss 저장할 위치(예: D:\Ai)로 이동 » 빈 공간에서 마우스 우클릭 » 터미널에서 열기

git clone https://github.com/bmaltais/kohya_ss.git
1
2
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
 
새로운 기능 및 개선 사항에 대한 최신 PowerShell을 설치 하세요! https://aka.ms/PSWindows
 
D:\Ai> git clone https://github.com/bmaltais/kohya_ss.git
Cloning into ‘kohya_ss’…
remote: Enumerating objects: 8311, done.
remote: Counting objects: 100% (2667/2667), done.
remote: Compressing objects: 100% (456/456), done.
remote: Total 8311 (delta 2311), reused 2492 (delta 2205), pack-reused 5644
Receiving objects: 100% (8311/8311), 6.00 MiB | 7.37 MiB/s, done.
Resolving deltas: 100% (5761/5761), done.
D:\Ai> cd kohya_ss
D:\Ai\kohya_ss> .\setup.bat
 
Kohya_ss GUI setup menu:
 
1. Install kohya_ss gui
2. (Optional) Install cudann files
3. (Optional) Install bitsandbytes-windows
4. (Optional) Manually configure accelerate
5. (Optional) Start Kohya_ss GUI in browser
6. Quit
 
Enter your choice: 1
 
1. Torch 1
2. Torch 2
3. Cancel
 
Enter your choice: 2
 
21:57:05-117149 INFO Version: v21.7.16
21:57:05-121150 INFO Python 3.10.6 on Windows
21:57:05-132657 INFO nVidia toolkit detected
21:57:05-134657 INFO Installing modules from requirements_windows_torch2.txt…
21:57:05-138233 INFO Installing package: torch==2.0.1+cu118 torchvision==0.15.2+cu118 –index-url
https://download.pytorch.org/whl/cu118
21:58:43-228851 INFO Installing package: xformers==0.0.20 bitsandbytes==0.35.0
21:58:56-850003 INFO Installing package: accelerate==0.19.0 tensorboard==2.12.3 tensorflow==2.12.0
22:00:16-477181 INFO Installing modules from requirements.txt…
22:00:16-480181 INFO Installing package: albumentations==1.3.0
22:00:31-231057 INFO Installing package: altair==4.2.2
22:00:43-451547 INFO Installing package: dadaptation==3.1
22:00:48-116823 INFO Installing package: diffusers[torch]==0.10.2
22:00:51-672429 INFO Installing package: easygui==0.98.3
22:00:52-805051 INFO Installing package: einops==0.6.0
22:00:53-886640 INFO Installing package: fairscale==0.4.13
22:00:55-635483 INFO Installing package: ftfy==6.1.1
22:00:56-850855 INFO Installing package: gradio==3.33.1
22:01:13-113647 INFO Installing package: huggingface-hub>=0.13.3
22:01:14-410068 INFO Installing package: lion-pytorch==0.0.6
22:01:15-562833 INFO Installing package: lycoris_lora==0.1.6
22:01:28-478828 INFO Installing package: opencv-python==4.7.0.68
22:01:30-499431 INFO Installing package: prodigyopt==1.0
22:01:31-957170 INFO Installing package: pytorch-lightning==1.9.0
22:01:36-663130 INFO Installing package: rich==13.4.1
22:01:38-714676 INFO Installing package: safetensors==0.3.1
22:01:40-138148 INFO Installing package: timm==0.6.12
22:01:42-719193 INFO Installing package: tk==0.1.0
22:01:44-178515 INFO Installing package: toml==0.10.2
22:01:45-643438 INFO Installing package: transformers==4.26.0
22:01:57-050388 INFO Installing package: voluptuous==0.13.1
22:01:58-564491 INFO Installing package: wandb==0.15.0
22:02:05-379516 INFO Installing package: -e .
22:02:10-564923 INFO Copying bitsandbytes files…
22:02:10-612511 INFO Configuring accelerate…
22:02:10-617108 INFO Copied accelerate config file to:
C:\Users\mingg\AppData\Local\huggingface\accelerate\default_config.yaml

 

3. https://github.com/bmaltais/python-library/raw/main/cudnn_windows.zip 클릭하여 cudnn_windows.zip 파일 다운로드

 

4. cudnn_windows.zip 파일을 kohya_ss 설치 폴더 안으로 이동 » cudnn_windows.zip 압축 해제

 

5. 다시 터미널에서 이어서 진행

2
3
4
This machine
No distributed training
no
no
no
all
fp16
5
Kohya_ss GUI setup menu:
 
1. Install kohya_ss gui
2. (Optional) Install cudann files
3. (Optional) Install bitsandbytes-windows
4. (Optional) Manually configure accelerate
5. (Optional) Start Kohya_ss GUI in browser
6. Quit
 
Enter your choice: 2
 
22:46:05-227924 INFO Checking for CUDNN files in D:\Ai\kohya_ss\venv\Lib\site-packages\torch\lib…
22:46:08-480450 INFO Copied CUDNN 8.6 files to destination
 
Kohya_ss GUI setup menu:
 
1. Install kohya_ss gui
2. (Optional) Install cudann files
3. (Optional) Install bitsandbytes-windows
4. (Optional) Manually configure accelerate
5. (Optional) Start Kohya_ss GUI in browser
6. Quit
 
Enter your choice: 3
 
22:47:34-592340 INFO Installing package: bitsandbytes-windows
 
Kohya_ss GUI setup menu:
 
1. Install kohya_ss gui
2. (Optional) Install cudann files
3. (Optional) Install bitsandbytes-windows
4. (Optional) Manually configure accelerate
5. (Optional) Start Kohya_ss GUI in browser
6. Quit
 
Enter your choice: 4
 
————————————————————————————————————————
In which compute environment are you running?
Please select a choice using the arrow or number keys, and selecting with enter
* This machine
AWS (Amazon SageMaker)
Please select a choice using the arrow or number keys, and selecting with enter
* No distributed training
multi-CPU
multi-GPU
TPU
Do you want to run your training on CPU only (even if a GPU / Apple Silicon device is available)? [yes/NO]:no
Do you wish to optimize your script with torch dynamo?[yes/NO]:no
Do you want to use DeepSpeed? [yes/NO]: no
What GPU(s) (by id) should be used for training on this machine as a comma-seperated list? [all]:all
Please select a choice using the arrow or number keys, and selecting with enter
no
* fp16
bf16
fp8
accelerate configuration saved at C:\Users\mingg/.cache\huggingface\accelerate\default_config.yaml
 
Kohya_ss GUI setup menu:
 
1. Install kohya_ss gui
2. (Optional) Install cudann files
3. (Optional) Install bitsandbytes-windows
4. (Optional) Manually configure accelerate
5. (Optional) Start Kohya_ss GUI in browser
6. Quit
 
Enter your choice: 5

 

6. 새로운 창이 열리면 http://127.0.0.1:7860를 Control+클릭하여 실행한다.

설정에 대한 답변을 잘못했다면 (Optional) Manually configure accelerate 선택 후 다시 설정할 수 있다.

 

7. 완료 되었다면 종료

6
Kohya_ss GUI setup menu:
 
1. Install kohya_ss gui
2. (Optional) Install cudann files
3. (Optional) Install bitsandbytes-windows
4. (Optional) Manually configure accelerate
5. (Optional) Start Kohya_ss GUI in browser
6. Quit
 
Enter your choice: 6
 
Quitting the program.
D:\Ai\kohya_ss>

 

8. LoRA 학습할 이미지들을 폴더에 모아놓자.

나는 kohya_ss 설치 폴더 경로\making 폴더 안에 폴더를 학습할 LoRA 별로 폴더를 만들어서 저장해놓았다.

kohya_ss 사용하는 방법

1. kohya_ss 설치 폴더로 진입 » images 폴더 생성 » kohya_ss\images 폴더로 들어간 후 output 폴더 생성

다른 폴더들은 자동으로 생성된다.

 

2. Stable Diffusion WEBUI 실행 » 확장기능 혹은 Extensions 탭 » 설정 » [설치] 혹은 [Install] 클릭

설정할 항목
URL로부터 확장기능 설치 / Install from URL
:
https://github.com/toriato/stable-diffusion-webui-wd14-tagger

 

3. Stable Diffusion WebUI » Tagger » Batch from directory » 설정 » [Interrogate] 클릭 » 완료되면 Stable-diffusion 종료

설정할 항목
인풋 이미지 경로 / input directory
:
이미지 넣어둔 경로 지정
Threshold
:
0.35
Use spaces instead of underscore
:
체크 표시
Escape brackets
:
체크 표시
Unload model after running
:
체크 표시

 

4. kohya_ss 설치 폴더로 진입 » gui.bat 파일 실행 » http://127.0.0.1:7860/ 에서 Control + 클릭하여 실행 » 브라우져가 실행되며 kohya_ss WebUI가 실행 된다.

 

5. Dreambooth LORA » Tools » Dreambooth/LoRA Folder preparation » 설정 » Prepare training data 클릭

설정할 항목
instance prompt
:
인스턴트 프롬프트 입력. (예: oca, mgt, omd, mdl 등 1토큰 프롬프트 입력)
Class prompt
:
어느 프롬프트를 입력해야 해당 LORA를 잘 호출할지 선택하는 부분 (예: 1girl, 1boy 등)
Training images
:
이미지 넣어둔 경로 지정
Repeats
:
100
Destination training directory
:
kohya_ss 설치 폴더 경로\images

 

6. Dreambooth LORA » Traning parameters » 설정 » [Advanced Configuration ◀] 클릭 » Clip skip 설정

설정할 항목
LoRA network weights
:
재학습 하는것 아니면 적지 않아도된다.
Train batch size
:
2 / 4. 2배수로 설정
Epoch
:
3~10 사이 결과 보고 판단하자.
Save every N epochs
:
1
Cache latents
:
체크 표시
Optimizer
:
AdamW8
Learning rate
:
0.0001
Optimizer
:
AdamW8bit
Text Encoder learning rate
:
0.00005
Unet learning rate
:
0.0001
Network Rank (Dimension)
:
8
Network Alpha
:
1
Max resolution
:
768,768. Max resolution을 키우면 완성본의 품질이 상승하지만 만들어지는 시간이 길어지므로 768,768 권장한다.
Clip skip
:
2. Clip skip은 LORA가 프롬프트 잘 반영하게 하기위해 설정한다.


 

7. Dreambooth LORA » Folders » 설정

설정할 항목
Image Folder
:
kohya_ss 설치 폴더 경로\images\img 폴더 지정. kohya_ss 설치 폴더 경로\images\img 폴더의 하위 폴더를 지정하면 에러가 발생된다.
Output folder
:
kohya_ss 설치 폴더 경로\images\output 폴더 지정
Model output name
:
WebUI에서 가져올 LoRA 이름 지정

 

8. Dreambooth LORA » Source model » 설정 » [Train Model] 클릭

설정할 항목
Pretrained model name or path
:
그림체를 결정할 Checkpoint 선택. (예: Stable-diffusion 설치된 경로\models\Stable-diffusionprismboxmix_v20.safetensors)
Model Quick Pick
:
custom
Save trained model as
:
safetensors

 

9. 다 끝나면 kohya_ss 설치 폴더 경로\images\output 폴더로 이동 » WebUI에서 가져올 LoRA 이름-00000X.safetensors 이 없는 WebUI에서 가져올 LoRA 이름.safetensors 파일 복사 » C:\stable-diffusion-webui\models\Lora 에 붙여넣기

 

10. 이제 Stable-diffusion 에서 학습한 LoRA 파일을 사용하면된다.

관련 포스트
이미지 생성 Ai - kohya_ss 드림부스 기반 LoRA GUI로 LoRA 학습 방법 - 현재글

Mingg`s Diary
밍구
공부 목적 블로그