Advertisement
Vageyser

Stable Diffusion Image to Image generation Powershell function

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