安定した拡散モデルを使用したMicrosoftの公式実装を使用してLoraを訓練します。これは、複雑さを追加せずにLORAをトレーニングする最も効率的で最も簡単な方法でありながら、ライブラリと実装の間で共有可能です。
?このレポはまだアクティブな開発中です。 WIPフラグが削除されるまでバグを期待してください。 ?
このリポジトリをトレーニングフォルダーにクローンします。
git clone https://github.com/ExponentialML/Stable-LoRAこのリポジトリから要件をインストールします。
pip install -r requirements.txtトレーニングは、Diffusersライブラリを使用して行われます。
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 Paramsを簡単に追加できます。
# 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ファイルを使用して、セーフテンサーまたは従来の方法を使用して行うことができます。
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。
公式コードのマイクロソフト。