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用于官方代码。