안정적인 확산 모델을 사용하여 Microsoft의 공식 구현을 사용하여 LORA를 기차하십시오. 이것은 도서관과 구현간에 공유 할 수있는 복잡성없이 LORA를 훈련시키는 가장 효율적이고 쉬운 방법입니다.
? 이 저장소는 여전히 활발한 개발 중입니다. WIP 플래그가 제거 될 때까지 버그를 기대하십시오. ?
이 저장소를 교육 폴더로 복제하십시오.
git clone https://github.com/ExponentialML/Stable-LoRA이 저장소에서 요구 사항을 설치합니다.
pip install -r requirements.txtDiffusers 라이브러리를 사용하여 교육이 이루어집니다.
Dreambooth의 예제 스크립트는 example_train_script.sh를 참조하십시오.
실행하는 것은 쉽습니다.
accelerate launch train_lora . py
- - pretrained_model_name_or_path = "runwayml/stable-diffusion-v1-5"
- - instance_data_dir = "instance_images"
- - lora_rank = 64
- - output_dir = "./output"
- - instance_prompt = "a shld dog"
- - resolution = 512
- - mixed_precision = "fp16"
- - train_text_encoder
- - learning_rate = 1e-4
- - resize = True
- - save_steps = 200
- - preview_steps = 100
- - lr_scheduler = "constant_with_warmup"
- - lr_warmup_steps = 50
- - max_train_steps = 5000
- - save_preview
- - preview_prompt = "a shld dog as a super hero"교육 후 결과는 출력 폴더에 저장됩니다.
기본적으로 Automatic1111 WebUI 변형은 그들과 함께 저장됩니다 (WebUI Extension이 개발 중).
교육 루프 전에 모델에 추가하십시오.
# Import function to add the LoRA, as well as target modules.
from stable_lora . lora import add_lora_to , UNET_REPLACE , TEXT_ENCODER_REPLACE
from diffusers import StableDiffusionPipeline
# Load a Stable Diffusion Model
pipeline = StableDiffusionPipeline . from_pretrained ( pretrained_model_name_or_path )
# Freeze the models. Remember, only the LoRA weights get trained, not the model itself.
pipeline . unet . requires_grad_ ( False )
pipeline . text_encoder . requires_grad_ ( False )
# Add LoRA to the UNET
add_lora_to (
pipeline . unet ,
target_module = UNET_REPLACE ,
search_class = [ torch . nn . Linear , torch . nn . Conv2d ],
r = 32
)
# Add LoRA to the Text Encoder
add_lora_to ( pipeline . text_encoder , target_module = TEXT_ENCODER_REPLACE , r = 32 )
# Your optimizers and training code...모델에 LORA를 추가 한 후 필요한 경우 매개 변수 현명한 최적화 매개 변수를 쉽게 추가 할 수 있습니다.
# Example
unet_params = []
text_encoder_params = []
for n , p in unet . named_parameters ():
if 'lora' in n :
unet_params . append ({
"params" : p ,
"lr" : args . learning_rate
})
for n , p in text_encoder . named_parameters ():
if 'lora' in n :
text_encoder_params . append ({
"params" : p ,
"lr" : args . learning_rate_text
})
optimizer_params = unet_params + text_encoder_params .pt 파일을 사용하여 SAFETENSORS 또는 기존 방식을 사용하여 저장을 수행 할 수 있습니다.
from stable_lora . lora import save_lora
save_lora ( unet , path = 'save_file_path.safetensors' )
save_lora ( text_encoder = text_encoder , use_safetensors = False , path = 'save_file_path.pt' )또한 모드를 평가 또는 열차 모드로 설정할 수 있습니다.
from stable_lora . lora import set_mode
# Train mode
set_mode ( unet , is_train = True )
# Evaluation mode
set_mode ( unet , is_train = False )이것은 안정적인 확산 1.5 기반 모델에서 테스트되었습니다.
5e-6 , 3e-5 및 1e-4 학습 속도를 시도하는 것이 좋습니다.
기본 순위는 32 이지만 r 매개 변수를 통해 전달하여 설정할 수 있습니다. 더 낮은 순위를 사용하면 높은 순위가 더 많이 소비되면서 메모리가 줄어 듭니다. 이것은 또한 파일 저장 크기에 기여합니다.
이러한 요소는 귀하의 작업에만 의존하지만 좋은 출발점입니다.
편견도 훈련 할 수 있습니다.
데이터 및 학습 속도에 따라 교육에서 추가 성능을 짜낼 수 있습니다.
# Example | Applicable to Unet or Text Encoder
lora_bias = 'lora_only'
add_lora_to ( pipeline . text_encoder , target_module = TEXT_ENCODER_REPLACE , r = 32 , lora_bias = lora_bias )
# Must be set here as well when saving.
save_lora ( text_encoder = text_encoder , use_safetensors = True , path = 'save_file_path.pt' , lora_bias = lora_bias )PT & safetensors )를 저장 및로드하는 것을 구현하십시오. LORA 구현 코드를위한 CloneofSimo.
공식 코드의 Microsoft.