Stable LoRA
1.0.0
使用Microsoft的官方實現訓練Lora,並使用穩定的擴散模型進行培訓。這是訓練洛拉斯而沒有增加複雜性的最有效和最簡單的方法,同時在圖書館和實施之間也可以共享。
?此存儲庫仍在積極發展中。期待錯誤,直到刪除WIP標誌為止。 ?
將此存儲庫克隆到您的培訓文件夾中:
git clone https://github.com/ExponentialML/Stable-LoRA安裝此存儲庫的要求:
pip install -r requirements.txt培訓是通過使用擴散器庫來完成的。
有關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"訓練後,結果將保存在您的輸出文件夾中。
默認情況下,自動1111 WebUI變體與它們一起保存(WebUI擴展正在開發)。
只需在訓練循環之前將其添加到模型中。
# 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添加到模型後,如果需要,您可以輕鬆添加參數Wise Optimizer參數。
# 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可以使用SafetEnsor或傳統方式使用.pt文件來保存:
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 )。 cloneofsimo為Lora實施代碼。
Microsoft用於官方代碼。