Advertisement
Vageyser

Stable Diffusion Text to Image generation Powershell function

Aug 24th, 2022 (edited)
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Generate-txt2img {
  2. Param(
  3. [Parameter(Mandatory=$true,HelpMessage="Prompt to an image of",Position=0)] $Prompt,
  4. [Parameter(Mandatory=$false,HelpMessage="Image Width divisible by 32")] [Int]$W,
  5. [Parameter(Mandatory=$false,HelpMessage="Image Height divisible by 32")] [Int]$H,
  6. [Parameter(Mandatory=$false,HelpMessage="how closely the AI follows your prompt")] $Scale,
  7. [Parameter(Mandatory=$false,HelpMessage="specifies how many times to run the sampling loop")] [Int]$Iterations,
  8. [Parameter(Mandatory=$false,HelpMessage="specifies directory images will be saved to")] [String[]]$outdir,
  9. [Parameter(Mandatory=$false,HelpMessage="specifies the seed used for results. This allows for reproducible results")] [Int]$seed,
  10. [Parameter(Mandatory=$false,HelpMessage="specifies the aspect ratio. 1:1 is default. Acceptable values are 3:2, 2:3, 16:9, and 9:16. This will override Width and Height if they were defined")] [String[]]$ar,
  11. [Parameter(Mandatory=$false,HelpMessage="specifies the number of sampling steps in the Diffusion process. Increasing this number will increase computation time but may improve results.")] [Int]$steps,
  12. [Parameter(Mandatory=$false,HelpMessage="Use optional parameters not defined here. Things like --plms, --ckpt, --precision, --f, --C, etc")] $opt
  13. )
  14. if (!$W) { [int]$W = '512' }
  15. if (!$H) { [int]$H = '512' }
  16. if (!$Scale) { $Scale = '7.5' }
  17. if (!$Iterations) { [int]$Iterations = '4' }
  18. if (!$outdir) { $outdir = "E:\stable-diffusion-main\outputs\txt2img" } ## Hardcoded path - Please ensure this matches your environment
  19. if (!$seed) { $seed = Get-Random -Minimum 1 -Maximum 999999 }
  20. if (!$steps) { $steps = '50' }
  21. if ($ar -eq '3:2') { [int]$W = '768'; [int]$H = '512' }
  22. if ($ar -eq '2:3') { [int]$W = '512'; [int]$H = '768' }
  23. if ($ar -eq '16:9') { [int]$W = '768'; [int]$H = '448' }
  24. if ($ar -eq '9:16') { [int]$W = '448'; [int]$H = '768' }
  25. $dir = "E:\stable-diffusion-main" ## Root path for your Stable Diffusion instance - Please ensure this matches your environment
  26. $currentPath = get-location
  27. set-location $dir
  28. if (!((get-module).name -match "Conda")) {
  29.     write-host "Loading Miniconda PowerShell module"
  30.     if (test-path "C:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1") { & 'C:\ProgramData\Miniconda3\shell\condabin\conda-hook.ps1' }
  31.     else { & "$env:userprofile\Miniconda3\shell\condabin\conda-hook.ps1" }
  32.     conda activate ldm ## Conda instance name - Please ensure this matches your environment
  33. }
  34. else { write-host "Conda module already loaded`n`n;)`n`n" }
  35. $starttime = get-date
  36. $rundeets = "Prompt: `"$prompt`"`nHeight: $H`nWidth: $W`nSeed: $seed`nIterations $Iterations`nSteps: $steps`nScale: $Scale"
  37. write-output "Processing text. Images will be saved somewhere here: `"$outdir`"`n$rundeets"
  38. ## Python script to use can be updated below. This uses the default scripts with the main repo and probably most forks.
  39. python $dir\scripts\txt2img.py --outdir "$outdir" --prompt "$prompt" --H $H --W $W --seed $seed --n_iter $Iterations --ddim_steps $steps --n_samples 1 --scale $scale --skip_grid $opt
  40. write-output "Process complete. Recap:`nImages will be saved somewhere here: `"$outdir`"`n$rundeets"
  41. $newimg = gci "$outdir\samples" | where { $_.creationTime -gt $starttime }
  42. # if (Get-Process -name EXCEL) { Get-Process -name EXCEL | Stop-Process }
  43. if (!(Test-path "$outdir\runbook_txt2img.csv")) {
  44.     $logbook = {} | Select "Filename","Prompt","Width","Height","Scale","Iterations","Seed","Steps","FullPath" | Export-Csv "$outdir\runbook_txt2img.csv" -NoTypeInformation
  45.     Get-Content "$outdir\runbook_txt2img.csv" | Where { $_.Replace(",","") -ne "" } | Out-File "$outdir\runbook_txt2img.csv"
  46.     }
  47. $tempbook = {} | Select "Filename","Prompt","Width","Height","Scale","Iterations","Seed","Steps","FullPath" | Export-Csv "$env:temp\runbook_txt2img.csv" -NoTypeInformation
  48. $runbook = Import-Csv "$env:temp\runbook_txt2img.csv"
  49. $newimg | % {
  50. $runbook.Filename = $_.name
  51. $runbook.Prompt = $Prompt
  52. $runbook.Width = $W
  53. $runbook.Height = $H
  54. $runbook.Scale = $Scale
  55. $runbook.Iterations = $Iterations
  56. $runbook.Seed = $seed
  57. $runbook.Steps = $steps
  58. $runbook.FullPath = $_.fullname
  59. $runbook | export-csv "$outdir\runbook_txt2img.csv" -Append -NoTypeInformation
  60. $seed++
  61. }
  62. set-location $currentPath
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement