Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2023
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. # https://github.com/cloneofsimo/lora
  2. # https://github.com/kohya-ss/sd-scripts
  3. # https://rentry.org/2chAI_LoRA_Dreambooth_guide_english
  4.  
  5. ##### Config start #####
  6.  
  7. $sd_scripts_dir = "F:\AI\sd-scripts" # Path to kohya-ss/sd-scripts repository
  8.  
  9. $ckpt = "F:\AI\GIT-Bash\stable-diffusion-webui\models\Stable-diffusion\nai.ckpt" # Path to checkpoint (ckpt / safetensors)
  10. $is_sd_v2_ckpt = 0 # '1' if loading SD 2.x ckeckpoint
  11. $is_sd_v2_768_ckpt = 0 # '1', if loading SD 2.x-768 checkpoint
  12. $image_dir = "F:\AI\Preprocess\Aquarius\img" # Path to training images folder
  13. $reg_dir = "F:\AI\Preprocess\Aquarius\reg" # Path to regularization folder (path can lead to an empty folder, but folder must exist)
  14. $output_dir = "F:\AI\GIT-Bash\stable-diffusion-webui\Lora\" # LoRA network saving path
  15. $output_name = "Aquarius_v2" # LoRA network file name (no extension)
  16.  
  17. $train_batch_size = 5 # How much images to train simultaneously. Higher number = less training steps (faster), higher VRAM usage
  18. $resolution = 768 # Training resolution
  19. $num_epochs = 10 # Number of epochs
  20. $save_every_n_epochs = 1 # Save every n epochs
  21. $save_last_n_epochs = 999 # Save only last n epochs
  22. $max_token_length = 225 # Max token length. Possible values: 75 / 150 / 225
  23. $clip_skip = 2 # https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#ignore-last-layers-of-clip-model
  24.  
  25. # Advanced settings
  26.  
  27. $learning_rate = 1e-4 # Learning rate
  28. $unet_lr = $learning_rate # U-Net learning rate
  29. $text_encoder_lr = $learning_rate # Text encoder learning rate
  30. $scheduler = "cosine_with_restarts" # linear, cosine, cosine_with_restarts, polynomial, constant (default), constant_with_warmup
  31. $network_dim = 256 # Size of network. Higher number = higher accuracy, higher output file size
  32. $save_precision = "fp16" # None, float, fp16, bf16
  33. $mixed_precision = "fp16" # no, fp16, bf16
  34. $is_random_seed = 1 # 1 = random seed, 0 = static seed
  35. $shuffle_caption = 1 # Shuffle comma-separated captions
  36. $keep_tokens = 1 # Keep heading N tokens when shuffling caption tokens
  37. $use_vae = 1
  38. $vae_path = "F:\AI\GIT-Bash\stable-diffusion-webui\models\VAE\nai.vae.pt"
  39.  
  40. # Logging
  41.  
  42. $logging_enabled = 0
  43. $logging_dir = "F:\AI\GIT-Bash\stable-diffusion-webui\Lora\"
  44. $log_prefix = $output_name
  45.  
  46. ##### Config end #####
  47.  
  48. function Is-Numeric ($value) { return $value -match "^[\d\.]+$" }
  49.  
  50. function Write-ColorOutput($ForegroundColor)
  51. {
  52. $fc = $host.UI.RawUI.ForegroundColor
  53. $host.UI.RawUI.ForegroundColor = $ForegroundColor
  54. if ($args) { Write-Output $args }
  55. else { $input | Write-Output }
  56. $host.UI.RawUI.ForegroundColor = $fc
  57. }
  58.  
  59. Write-Output "Calculating number of images in folders"
  60. $total = 0
  61. $is_structure_wrong = 0
  62. $abort_script = 0
  63. $iter = 0
  64.  
  65. Get-ChildItem -Path $image_dir -Directory | ForEach-Object {
  66. $parts = $_.Name.Split("_")
  67. if (!(Is-Numeric $parts[0]))
  68. {
  69. Write-ColorOutput red "Error in $($_):`n`t$($parts[0]) is not a number"
  70. $is_structure_wrong = 1
  71. return
  72. }
  73. if ([int]$parts[0] -le 0)
  74. {
  75. Write-ColorOutput red "Error in $($_):`nNumber of repeats in folder name must be >0"
  76. $is_structure_wrong = 1
  77. return
  78. }
  79. $repeats = [int]$parts[0]
  80. $imgs = Get-ChildItem $_.FullName -Depth 0 -File -Include *.jpg, *.png, *.webp | Measure-Object | ForEach-Object { $_.Count }
  81. if ($iter -eq 0) { Write-Output "Training images:" }
  82. $img_repeats = ($repeats * $imgs)
  83. Write-Output "`t$($parts[1]): $repeats repeats * $imgs images = $($img_repeats)"
  84. $total += $img_repeats
  85. $iter += 1
  86. }
  87.  
  88. $iter = 0
  89.  
  90. if ($is_structure_wrong -eq 0) { Get-ChildItem -Path $reg_dir -Directory | % { if ($abort_script -ne "n") { ForEach-Object {
  91. $parts = $_.Name.Split("_")
  92. if (!(Is-Numeric $parts[0]))
  93. {
  94. Write-ColorOutput red "Error in $($_):`n`t$($parts[0]) is not a number"
  95. $is_structure_wrong = 1
  96. return
  97. }
  98. if ([int]$parts[0] -le 0)
  99. {
  100. Write-ColorOutput red "Error in $($_):`nNumber of repeats in folder name must be >0"
  101. $is_structure_wrong = 1
  102. return
  103. }
  104. $repeats = [int]$parts[0]
  105. $reg_imgs = Get-ChildItem $_.FullName -Depth 0 -File -Include *.jpg, *.png, *.webp | Measure-Object | ForEach-Object { $_.Count }
  106. if ($iter -eq 0) { Write-Output "Regularization images:" }
  107. if ($reg_imgs -eq 0)
  108. {
  109. Write-ColorOutput darkyellow "Warning: regularization images folder exists, but is empty"
  110. do { $abort_script = Read-Host "Abort script? (y/N)" }
  111. until (($abort_script -eq "y") -or ($abort_script -ceq "N"))
  112. return
  113. }
  114. else
  115. {
  116. $img_repeats = ($repeats * $reg_imgs)
  117. Write-Output "`t$($parts[1]): $repeats repeats * $reg_imgs images = $($img_repeats)"
  118. $iter += 1
  119. }
  120. } } } }
  121.  
  122. if ($is_structure_wrong -eq 0 -and ($abort_script -eq "n" -or $abort_script -eq 0))
  123. {
  124. if ($reg_imgs -gt 0)
  125. {
  126. $total *= 2
  127. Write-Output "Training steps number doubled: number of regularization images >0"
  128. }
  129.  
  130. Write-Output "Image number with repeats: $total"
  131. Write-Output "Training batch size: $train_batch_size"
  132. Write-Output "Number of epochs: $num_epochs"
  133. $max_training_steps = [int]($total / $train_batch_size * $num_epochs)
  134. Write-Output "Number of steps: $total / $train_batch_size * $num_epochs = $max_training_steps"
  135.  
  136. if ($is_random_seed -le 0) { $seed = 1337 }
  137. else { $seed = Get-Random }
  138.  
  139. $image_dir = $image_dir.TrimEnd("\", "/")
  140. $reg_dir = $reg_dir.TrimEnd("\", "/")
  141. $output_dir = $output_dir.TrimEnd("\", "/")
  142. $logging_dir = $logging_dir.TrimEnd("\", "/")
  143.  
  144. $run_parameters = "--network_module=networks.lora --pretrained_model_name_or_path=`"$ckpt`" --train_data_dir=`"$image_dir`" --reg_data_dir=`"$reg_dir`" --output_dir=`"$output_dir`" --output_name=`"$output_name`" --caption_extension=`".txt`" --resolution=$resolution --prior_loss_weight=1 --enable_bucket --min_bucket_reso=256 --max_bucket_reso=1024 --train_batch_size=$train_batch_size --learning_rate=$learning_rate --unet_lr=$unet_lr --text_encoder_lr=$text_encoder_lr --max_train_steps=$max_training_steps --use_8bit_adam --xformers --save_every_n_epochs=$save_every_n_epochs --save_last_n_epochs=$save_last_n_epochs --save_model_as=safetensors --keep_tokens=$keep_tokens --clip_skip=$clip_skip --seed=$seed --network_dim=$network_dim --cache_latents --lr_scheduler=$scheduler --mixed_precision=$mixed_precision --save_precision=$save_precision"
  145.  
  146. if ($max_token_length -eq 75) { }
  147. else
  148. {
  149. if ($max_token_length -eq 150 -or $max_token_length -eq 225) { $run_parameters += " --max_token_length=$($max_token_length)" }
  150. else { Write-ColorOutput darkyellow "The max_token_length is incorrect! Use value 75" }
  151. }
  152.  
  153. if ($is_sd_v2_ckpt -le 0) { Write-Output "Stable Diffusion 1.x checkpoint" }
  154. if ($is_sd_v2_ckpt -ge 1)
  155. {
  156. if ($is_sd_v2_768_ckpt -ge 1)
  157. {
  158. $v2_resolution = "768"
  159. $run_parameters += " --v_parameterization"
  160. }
  161. else { $v2_resolution = "512" }
  162. Write-Output "Stable Diffusion 2.x ($v2_resolution) checkpoint"
  163. $run_parameters += " --v2"
  164. if ($clip_skip -eq -not 1)
  165. {
  166. Write-ColorOutput darkyellow "Warning: training results of SD 2.x checkpoint with clip_skip other than 1 might be unpredictable"
  167. do { $abort_script = Read-Host "Abort script? (y/N)" }
  168. until (($abort_script -eq "y") -or ($abort_script -ceq "N"))
  169. }
  170. }
  171.  
  172. if ($shuffle_caption -ge 1) { $run_parameters += " --shuffle_caption" }
  173.  
  174. if ($logging_enabled -ge 1) { $run_parameters += " --logging_dir=`"$logging_dir`" --log_prefix=`"$output_name`""}
  175.  
  176. if ($use_vae -ge 1) { $run_parameters += " --vae=`"$vae_path`"" }
  177.  
  178. sleep -s 1
  179.  
  180. if ($abort_script -eq "n" -or $abort_script -eq 0)
  181. {
  182. Write-ColorOutput green "Running script with parameters:"
  183. sleep -s 1
  184. Write-Output "$($run_parameters -split '--' | foreach { if ($_ -ceq '') { Write-Output '' } else { Write-Output --`"$_`n`" } } | foreach { $_ -replace '=', ' = ' })"
  185. $script_origin = (get-location).path
  186. cd $sd_scripts_dir
  187. .\venv\Scripts\activate
  188. powershell accelerate launch --num_cpu_threads_per_process 12 train_network.py $run_parameters
  189. deactivate
  190. cd $script_origin
  191. }
  192. }
  193.  
  194. # 13.01.23 by anon
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement